var page_loaded=false


function load_sub_options(opt){		
    var parent=$("#"+opt.id+" option:selected").attr("value")
	 $.post("../shop/ajax_server.php", { action: "loadSubOptions",parentId:parent},
			  function(data){			    
			    $("#sub_options").html(data).show('slide');	
				$("#sub_sub_options").hide('clip');	
				display_price(opt);		   
    });
}

function load_sub_sub_options(opt){ 	
   var parent=$("#"+opt.id+" option:selected").attr("value")
	$.post("../shop/ajax_server.php", { action: "loadSubSubOptions",parentId:parent},
   function(data){$("#sub_sub_options").html(data).show('slide');});
	display_price(opt);
}

function display_price(opt){	
	if ($("#" + opt.id + " option:selected").attr("rel") > 0) {
		$("#price_display").html("$"+$("#" + opt.id + " option:selected").attr("rel")).show('clip');
	}
}

function hide_errors(){
	$("label.error").hide();
}



function set_page_loaded(){
	page_loaded=true
}

function check_page_loaded(){
	if(!page_loaded){alert('Please wait. Page is still loading.');return false;}
	else{return true}
}

function change_country(country) {
	if(!check_page_loaded()){return}
	$.getJSON("../shop/ajax_server.php", {
		action : "change_country",
		country : country
	}, function(json) {
		$(".flag").html(json.flag ).show('blind');	
		
		$.ajax({
			  url: "../shop/ajax_server.php?action=load_state",
			  cache: false,
			  success: function(html){$(".state_container").html(html);}
			});
	
		// $("#country_container_mini").html(json.country)
		$('.country_lable_parent .country_lable').html(country).show('highlight', 1000)
		change_freight_and_total(json)
		toggle_methord_form()
		$('.country_list').val(country)
	});
}

function change_shipping_mode() {
	if(!check_page_loaded()){return}
	$.getJSON("../shop/ajax_server.php", {
		action : "change_shipping_mode",
		mode : $("[name=mode]:checked").val()
	}, function(json) {
		change_freight_and_total(json)
	});

}

function change_qty(id) {
	if(!check_page_loaded()){return}
	$.getJSON("../shop/ajax_server.php", {
		action : "change_qty",
		product : id,
		qty : $("#" + id).val()
	}, function(json) {
		$("#cart_details_html").html(json.products);
		change_freight_and_total(json)
	});
}

function remove_product(id) {
	if(!check_page_loaded()){return}
	$.getJSON("../shop/ajax_server.php", {
		action : "remove_product",
		id : id
	}, function(json) {
		$("#cart_details_html").html(json.products).show('clip');
		change_freight_and_total(json)
	});
	// stop the cart from jumping to the top
	return false;
}

function delete_product(id) {
	if(!check_page_loaded()){return}
	$.getJSON("../shop/ajax_server.php", {
		action : "delete_product",
		id : id
	}, function(json) {
		$("#cart_details_html").html(json.products).show('clip');
		change_freight_and_total(json)
	});
	// stop the cart from jumping to the top
	return false;
}

function undo_delete(id) {
	if(!check_page_loaded()){return}
	$.getJSON("../shop/ajax_server.php", {
		action : "undo_delete",
		id : id
	}, function(json) {
		$("#cart_details_html").html(json.products).show('clip');
		change_freight_and_total(json)
	});
	// stop the cart from jumping to the top
	return false;
}

function change_freight_and_total(json) {
	
	$("#shipping_cost").html("$" + json.freight);
	$("#insurance_cost").html("$" + json.insurance);
	$("#total_cost").html("$" + json.total);
	$("#cc_total").html("AU$" + json.total);
	$("#pcount").html(json.pcount);
	$("#error").slideUp()
	
	

}

function toggle_methord_form() {
	if ($('#country_list').val() == "AUSTRALIA") {
		$('#method_form').hide('slide', 200)
	} else {
		if (!$('#method_form').is(':visible')) {
			$('#method_form').show('slide', 200)
		}
	}

}

function toggle_cart() {
	if ($('#collapsed').is(':visible')) {
		$('#collapsed').hide('blind', 400)
		// $('#toggle_cart').html("Show")

	} else {
		$('#collapsed').show('blind', 300)
		// $('#toggle_cart').html("Hide")
	}
}
function show_cart() {
	if ($('#collapsed').is(':hidden')) {
		$('#collapsed').show('blind', 400)
	}
}
function hide_cart() {
	setTimeout ( "hide_cart_now()", 350 );

	
}
function hide_cart_now(){
	if ($('#collapsed').is(':visible')) {
		$('#collapsed').hide('blind', 300)
	}
}

function float_loading(){return
    var name = ".loading";
    var menuYloc = null;

    menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))
    $(window).scroll(function () {
        	var offset = menuYloc+$(document).scrollTop()+"px";
            $(name).animate({top:offset},{duration:500,queue:false});
    });
}

function  is_cart_empty(){
	if(!check_page_loaded()){return false}
	if(($('#pcount').html())==0){alert('Ooops! your cart is empty \nPlaease add a product first');return false;}
	return true;
	
}


function print_div(strid)
{
var prtContent = document.getElementById(strid);
var WinPrint =window.open('','','left=0,top=0,width=800,height=900,toolbar=1,scrollbars=1,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
}


$(function(){
	
	$(".loading_modal").dialog({    	
		modal: true,
		dialogClass: 'loading_modal_class'
		
	});
	$(".loading_modal").dialog('close');
	
	$(".loading").slideUp(); 
	float_loading();
	// close cart when clicked outside
	  $(document).bind('click', function(e) {
                var $clicked=$(e.target); // get the element clicked
                if( ! ( $clicked.is('.cart_m') || $clicked.is('div#catr_box') || $clicked.parents().is('.cart_m') || $clicked.parents().is('div#catr_box') ) )
                {
                	hide_cart_now();
                }

      });
	  
     $(".loading_modal").ajaxStart(function(){
     $(this).show();       
     $(".loading_modal").dialog('open')     
     });

     $(".loading_modal").ajaxStop(function(){
     $(this).slideUp(); 
     $(".loading_modal").dialog('close')
     });    
     
     $("div#catr_box").bind("click",function(){
    	 toggle_cart()
     });
      
});


