$(document).ready(function(){
	$("div#errorBox").hide();
	doPreview();	// Invoking the function
	$("#addItem").click(function (){
		var proCount = $("#productCount").val();
		if(proCount == 0 || proCount == ''){
			$("#errorBox").html(" Please enter the number of items to be added. ");
			$("div#errorBox").show();
			return false;									   
			} else {
				$("form#product-details").submit();				
		}
	});
	// Updation of the cart
	$("a#updateCart").click(function (){
		var dataStr = $("form#myCart").serialize();
		$.ajax({
			type: "POST",
	   		url: "updateCart.php",
			data: dataStr,
			success: function(data){
				$("#cart_list").load("cart_items.php");
				removeCartItem();
				return false;
			}
		
		});		
		return false;
	});
	return false;
});	
// Restricting the user to enter only the numbers
function isNumberKey(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
     //alert("numbers only");
	return false;
}


// Remove a item from the cart

function removeCartItem(id){ 
			$.post("my-cart.php", { id: id, mm_action: "doDelete" },
				function(data){ 
					if (jQuery.trim(data) == 'success')
						{
							window.location = "my-cart.php";
						}
					}
		  );
		
}	

// Delete all the items in the cart for a particular user
function clearCartItem(id){
	$.post("my-cart.php", { id: id, mm_action: "doClear" },
		function(data){
			if (jQuery.trim(data) == 'success')
				{
				window.location = "my-cart.php";
				}
			}
	);		
}	
// Add to the cart directly while clicking on buy now
function productBuyNow(id){
	$.post("my-cart.php", { product_id: id, MM_action: "addToCart" },
		function(data){
				window.location = "my-cart.php";				
			}
	);		
}

// Image Privew
this.doPreview = function(){ 
	/* CONFIG */
	xOffset = 10;
	yOffset = 30;
	// these 2 variable determine popup's distance from the cursor
	// you might want to adjust to get the right result
	/* END CONFIG */
	$("img.preview").hover(function(e){
	
		var source = this.src;
		this.t = this.title;
		this.title = ""; 
		
		// Display position calculation
		var screenWidth = screen.width;
		var screenHeight = screen.height;
		var totalWidth = e.pageX + 280;
		var totalHeight = e.pageY + 365;
		//alert(screenWidth +" width \n"+totalWidth +" total width \n"+e.pageX +" e.page x \n");
		/*
		if(totalWidth > screenWidth){
			yOffset = -230;			
			} else {
			xOffset = 10;
			yOffset = 30;
		}	
		if(totalHeight > screenHeight){
			xOffset = -30;		
			} else {
			xOffset = 10;
			yOffset = 30;
		}	
		*/
		//the path is 
		//photos/imgName
		//alert(source);
		//return false;
		var image_name = source.replace("ico", "tn");
		//alert(image_name);
		//return false;
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ image_name +"' alt='Image preview' />"+ c +"</p>"); 
		
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("slow");      
	},
	function(){
		this.title = this.t; 
		$("#preview").remove();
	}); 
	$("img.preview").mousemove(function(e){
		// Display position calculation
		var screenWidth = screen.width;
		var screenHeight = screen.height;
		var totalWidth = e.pageX + 280;
		var totalHeight = e.pageY + 365;
		//alert(screenWidth +" width \n"+totalWidth +" total width \n"+e.pageX +" e.page x \n");
		/*
		if(totalWidth > screenWidth){
			xOffset = -30;
			yOffset = -230;			
			} else {
			xOffset = 10;
			yOffset = 30;
		}	
		*/
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});   
};