// JavaScript Document

$(document).ready(function() {
	fadeTitles();
	
	$("#lightbox_bg").hide();
	
	$(".left_header, .left_header_off").click(function(){
		$("#left_menu_cat2").hide();
		$("#left_menu_cat1").show();
		$(".left_header_off").removeClass('left_header_off').addClass('left_header');
		$(".left_header2").removeClass('left_header2').addClass('left_header2_off');
	});

	$(".left_header2, .left_header2_off").click(function(){
		$("#left_menu_cat1").hide();
		$("#left_menu_cat2").show();
		$(".left_header2_off").removeClass('left_header2_off').addClass('left_header2');
		$(".left_header").removeClass('left_header').addClass('left_header_off');
	});

	$("#left_menu_cat1 li").click(function(){
		cat_id1 = $(this).attr("id");
		cat_id2 = cat_id1.split("_");
		cat_id = cat_id2[1];
		window.location = "index.php?cat1=" + cat_id;
	});

	$("#left_menu_cat2 li").click(function(){
		cat_id1 = $(this).attr("id");
		cat_id2 = cat_id1.split("_");
		cat_id = cat_id2[1];
		window.location = "index.php?cat2=" + cat_id;
	});
	
	$("#left_menu_static_links li").click(function(){
		page_id1 = $(this).attr("id");
		page_id2 = page_id1.split("_");
		page_id = page_id2[1];
		window.location = "index.php?p=" + page_id;
	});
	
	$("a.product_info_box").click(function(){
	$("#lightbox_bg").hide();

	prod_id1 = $(this).attr("id");
	prod_id2 = prod_id1.split("_");
	prod_id = prod_id2[1];

 var ScreenWidth;
 var ScreenHeight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 if (typeof window.innerWidth != 'undefined')
 {
      ScreenWidth = window.innerWidth,
      ScreenHeight = window.innerHeight
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       ScreenWidth = document.documentElement.clientWidth,
       ScreenHeight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 else {
       ScreenWidth = document.getElementsByTagName('body')[0].clientWidth,
       ScreenHeight = document.getElementsByTagName('body')[0].clientHeight
 }

	$("#product_detail_image").html("");

     var plx = new PHPLiveX();  
     return plx.ExternalRequest({  
         "url": "php_product_details.php",   
         "target": "lightbox_container",
		 "method": "get",
		 "params": {"id": prod_id},
		 "onFinish": function(){
					get_image_sizes(prod_id, {"onFinish": function(result){
								sizes = result.split(",");
								img_width = parseInt(sizes[0]);
								img_height = parseInt(sizes[1]);
								
								lightbox_height = img_height + 55;
								lightbox_top = parseInt((ScreenHeight - lightbox_height) / 2);
								
								lightbox_width = 710;
								lightbox_left = parseInt((ScreenWidth - lightbox_width) / 2);
								$("#lightbox_container").css("margin-left", lightbox_left);
								//$("#lightbox_container").css("right", lightbox_left);
								$("#lightbox_container").css("margin-top", lightbox_top);
								text = ScreenHeight + " " + ScreenWidth;
								//$("#").html = ;
								//$("#product_detail_price").html(text);
								$("#lightbox_bg").show();
								}
					});
			 }
       });
	});
	
	$("#lightbox_off").click(function(){
		$("#lightbox_bg").hide();
	});

	$("#slider").easySlider({
		auto: true,
		continuous: true,
		controlsShow: false,
		speed: 	1000,
		pause:  5000
	});

	$("#btn_siparis_ver").live("click", function() {
		$("#lightbox_bg").hide();
		var prod_id = $("#txt_prod_id").val();
		window.location = "index.php?order=" + prod_id;
	});


	$("#order_2").hide();
	$("#order_3").hide();
			
	$("input[name='rad_destination']").click(function() {
		destination = $(this);
		prod_id = $("#txt_prod_id").val();
		
		if(destination.val() == 1) {
			$("#order_1").show();
			$("#order_2").hide();
			$("#order_3").hide();
			dest = 1;
			ajax_update_order_total(prod_id, dest, {"target":"order_total"});
		}
		else if(destination.val() == 2) {
			$("#order_1").hide();
			$("#order_2").show();
			$("#order_3").hide();
			dest = 2;
			ajax_update_order_total(prod_id, dest, {"target":"order_total"});
		}
		else if(destination.val() == 3) {
			$("#order_1").hide();
			$("#order_2").hide();
			$("#order_3").show();
			dest = 3;
			ajax_update_order_total(prod_id, dest, {"target":"order_total"});
		}
	});
	
$('#cicek2').hide();

function fadeTitles() {
$('#cicek1').fadeIn(1000);
$('#cicek1').delay(5000).fadeOut(500, function () {
$('#cicek2').fadeIn(1000);
$('#cicek2').delay(5000).fadeOut(500, fadeTitles);
});

} 

	$("#btn_lightbox_kapat").live("click", function() {
		$("#lightbox_bg").hide();
	});

	$("#ekin_search").bind({
		click: function() {
			$("#ekin_search").val("");
		},
		keypress: function(e) {
			if (e.which == 13){
				query = $("#ekin_search").val();
				window.location = "index.php?q=" + query + "&opt=1";
			}
		},
		focusout: function(){
			$("#ekin_search").val("Anahtar Kelimelerle Arama");
		}
	});

	$("#search_for_price").change(function(){
		for_price = $("#search_for_price").val();
		window.location = "index.php?q=" + for_price + "&opt=2";
	});
	
	$("#search_for_destination").change(function(){
		for_destination = $("#search_for_destination").val();
		window.location = "index.php?q=" + for_destination + "&opt=3";
	});
	
$.validator.setDefaults({
	submitHandler: function() {
		$("#btn_contact_us").attr("disabled", "disabled");
		txt_name = $("#txt_name").val();
		txt_email = $("#txt_email").val();
		txt_subject = $("#txt_subject").val();
		txt_message = $("#txt_message").val();
			
		datastring = 'txt_name=' + txt_name + '&txt_email=' + txt_email + '&txt_subject=' + txt_subject + '&txt_message=' + txt_message;
		$.ajax({
		type: "POST",
		url: "sendmail.php",
		data: datastring,
		success: function(response){
					$("#dv_mail_sent").html(response);
					$("#dv_contact_form").hide();
				}
		});

	}
});

$("#frm_contact_form").validate({
	rules : {
		txt_name: "required",
		txt_email: "required",
		txt_subject: "required",
		txt_message: "required"
	},
	messages: {
		txt_name: "Form alanı gerekli",
		txt_email: "Form alanı gerekli",
		txt_subject: "Form alanı gerekli",
		txt_message: "Form alanı gerekli"
	}
});

});

/* ==== AJAX Functions here ==== */

function js_sendorder(form){
	contact_send_btn = document.getElementById("btn_order_done");
	contact_send_btn.disabled = "disabled";
    var plx = new PHPLiveX();  
    return plx.SubmitForm(form, {
		"target": "order_complated",
        "onFinish": function(response){  
           	order_form = document.getElementById("dv_order_form");
			order_form.style.display = "none";
			dv_order_complated = document.getElementById("order_complated");
			dv_order_complated.style.visibility = "visible";
        }  
    });
}

/* ==== AJAX Functions here ==== */