$(function(){
	//add click event handler on the shopping cart link
	$(".bag").click(function(s_event){
		s_event.preventDefault();
		ac_showBag();
		return false;
	});

	//add submit event handler on the product form
	$("form[name=cart_quantity]").submit(function(s_event){
		if($("#mysizewish").val()!="fgfdg") 
		{
			return(true);
		}
		s_event.preventDefault();
		ac_add_product($(this).serialize());	
		return(false);
		
	});

	$("#submitbtn").click(function(){
		$("form[name=cart_quantity]").submit();
	});

});

//send delete product request
function acDeleteProduct(id) {
	s_url="product_info.php?action=ac_rem_product&product_id="+id;
	params="";
		$.ajax({
		   type: "POST",
		   url: s_url,
		   data: params,
		   success: ac_addProductResult
		 });
}


//show bag when link clicked
function ac_showBag() {
	$("#ajaxcart").load("product_info.php?action=ac_show_popup").slideDown("fast");
}

//send add product request
function ac_add_product(params) {
		s_url="product_info.php?action=ac_addproduct";
		
		$.ajax({
		   type: "POST",
		   url: s_url,
		   data: params,
		   success: ac_addProductResult
		 });
}

//hide the bag when 'hide' link is clicked
function ac_hide() {
	$("#ajaxcart").slideUp("fast");
}

//ajax return function
function ac_addProductResult(data){	
	try {
		result=$.evalJSON(data);
	} catch(e) {
		alert("an error occured, please reload the page " + data);
	}
	//check if in iframe
	if (top === self) {
		//we are in the main window
		$("#ajaxcart").html(result['ajcart']);
		$(".cart").html(result['cart']);
		//show the cart
		$("#ajaxcart").show();
	} else {
		//redirect the output to the parent page
		top.ac_addProductResult(data);
	}
	if(result['deleted']=='1') {
		//check if we are in the sopping cart
		var url = window.location.toString();
		var d=url.indexOf('shopping_cart.php');
		if(d>=0) {
			//we are in the shopping cart, reload
			window.location.reload();
		}
	} else {
		//show the prodduct added notification
		$("#ac_product_added").show().delay(2000).fadeOut('slow');
	}
}
