$(document).ready(function(){
	//MENU
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}

	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 10, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 200, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);


	//jQuery toppanel
	$("div.panel_button").click(function(){
		$("div#panel").animate({ height: "188px" })
		.animate({ height: "178px" }, "fast");
		$("div.panel_button").toggle();
	});	
	$("div.hide_button").click(function(){
		$("div#panel").animate({ height: "0px" }, "fast");
	});	

});


function updateCart(cartItemId) {
	var itemPrice = $(".price_"+cartItemId).html();
	var qty = $(".qty_"+cartItemId).val();
	var newItemSubTotal = (itemPrice * qty).toFixed(2);
	var totalItems = 0;
	
	$(".itemSubTotal_"+cartItemId).html(newItemSubTotal);
	
	var grandTotal = 0;
	$(".itemSubTotal").each(function() {
		itemSubTotal = $(this).html();
		//add only if the value is number
		if(!isNaN(itemSubTotal) && itemSubTotal.length!=0) {
			grandTotal += parseFloat(itemSubTotal);
		}
		if(grandTotal>0){
			totalItems++;
		}
	});

	var delivery = 12;
	//var delivery = 0; // Free in June
	grandTotal += delivery;
	var gst = grandTotal/11;
	
	$(".delivery").html(delivery.toFixed(2));
	$(".gst").html(gst.toFixed(2));
	$(".grandTotal").html(grandTotal.toFixed(2));
	
	$("#cart").load("/ajax/cart.php?x=updateCart&delivery="+delivery+"&gst="+gst+"&grandTotal="+grandTotal+"&totalItems="+totalItems+"&singleItemId="+cartItemId+"&qty="+qty);
}

function clearCart(){
	$(".qty").val(0);
	$(".gst").html("0.00");
	$(".grandTotal").html("0.00");
	// UPDATE cart_items SET qty=0 WHERE cartId=cartId
}

function addToCart(cartId, itemId, price){
	//add cart itemId via load ajax to #cart
	var qty = $(".qty_"+itemId).val();
	$("#cart").load("/ajax/cart.php?x=addItem&cartId=" + cartId + "&itemId=" + itemId + "&price=" + price + "&qty=" + qty);
}

function checkMirror(){
	var same = document.orderForm.same.checked;
	
	if(same){
		document.orderForm.ship_firstname.value = document.orderForm.bill_firstname.value;
		document.orderForm.ship_lastname.value = document.orderForm.bill_lastname.value;
		document.orderForm.ship_address1.value = document.orderForm.bill_address1.value;
		document.orderForm.ship_address2.value = document.orderForm.bill_address2.value;
		document.orderForm.ship_suburb.value = document.orderForm.bill_suburb.value;
		document.orderForm.ship_postcode.value = document.orderForm.bill_postcode.value;
		document.orderForm.ship_state.value = document.orderForm.bill_state.value;
	}
}


function validateOrderForm(){
	var errormsg ='';
	var billerrormsg = '';
	var shiperrormsg = '';

	if(!$("#bill_firstname").val()){	billerrormsg += "- First name\n"; }
	if(!$("#bill_lastname").val()){		billerrormsg += "- Last name\n"; }
	if(!$("#bill_address1").val()){		billerrormsg += "- Address\n"; }
	if(!$("#bill_suburb").val()){		billerrormsg += "- Suburb\n"; }
	if(!$("#bill_postcode").val()){		billerrormsg += "- Postcode\n"; }
	if(!$("#bill_state").val()){		billerrormsg += "- State\n"; }
	if(billerrormsg!=''){ errormsg += "Missing BILLING DETAILS:\n"; errormsg += billerrormsg; }

	if(!$("#ship_firstname").val()){	shiperrormsg += "- First name\n"; }
	if(!$("#ship_lastname").val()){		shiperrormsg += "- Last name\n"; }
	if(!$("#ship_address1").val()){		shiperrormsg += "- Address\n"; }
	if(!$("#ship_suburb").val()){		shiperrormsg += "- Suburb\n"; }
	if(!$("#ship_postcode").val()){		shiperrormsg += "- Postcode\n"; }
	if(!$("#ship_state").val()){		shiperrormsg += "- State\n"; }
	if(billerrormsg!=''){				errormsg += "\n"; }
	if(shiperrormsg!=''){				errormsg += "Missing DELIVERY DETAILS:\n"; errormsg += shiperrormsg; }

	if(billerrormsg!='' || shiperrormsg!=''){ 	errormsg += "\n"; }
	if($("#buyer_email").val()==""){	errormsg += "Please enter a valid e-mail address.\n"; }
	if($("#bill_phone").val()==""){		errormsg += "Please provide a phone or mobile number\n"; }
	if($("#referrer").val()==""){		errormsg += "Please let us know how you heard about us\n"; }
	
	if(!errormsg || errormsg==''){
		return true;
	} else {
		alert(errormsg);
		return false;
	}
}
