var JQ= jQuery.noConflict();
JQ(document).ready(function($){ 

$(window).bind("resize", function() {
  		var	windowWidth = $(window).width();
		var	windowHeight = $(window).height();
 		
		$(".options-del").css("top",  windowHeight/2-$(".options-del").height()/2);
		$(".options-del").css("left", windowWidth/2-$(".options-del").width()/2);
		//$("#contentWrap").css({"left": 50 +"%"});
		//$("#contentWrap").css({"margin-left": -((windowWidth /2 ) - ($("#contentWrap").width()/2)) +"px"});
	});

	var popupStatus = 0;
	var id;	
	
	function LOEpopUp(id){
		if(popupStatus==0){	
			$("#update"+id).hide();
			$("#remove-options"+id).show();
			$(".message").html("Enter number of qty/quantities you want to remove: ");
			$("#options_"+id).fadeIn("slow");
			popupStatus = 1;
		}
	}
	
	function popUp(id){
		if(popupStatus==0){	
			$("#remove-options"+id).hide();
			$("#update"+id).show();
			$(".message").html("Enter your desire number of qty/quantities of this item: ");
			$("#options_"+id).fadeIn("slow");
			popupStatus = 1;
		}
	}
	
	function popAd(id){
		if(popupStatus==0){	
			$(".message").html("Enter your desire number of qty/quantities of this item: ");
			$("#options_"+id).fadeIn("slow");
			popupStatus = 1;
		}
	}
		
	function centerPopup(id){
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = 80;
		var popupWidth = 300;
		//centering
	$("#options_"+id).css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
		});	
	}
	
	cart4 = {
		
			popremoveOptions: function(id){
				$("#qty"+id).keypress(function(e){
					if(e.which == 13) return false;
				});
					 
				$("#overlay").css({
					"opacity": "0.7",
				});
				
				$("#qty"+id).val($("#quantity"+id).val());
				
				$("#overlay").fadeIn("fast", function(){
					LOEpopUp(id);
					centerPopup(id);
				});				
			},
			
			popAdd: function(id){
				$("#qty"+id).keypress(function(e){
					if(e.which == 13) return false;
				});
 
				$("#overlay").css({
					"opacity": "0.7",
				});
				
				$("#qty"+id).val($("#quantity"+id).val());
				
				$("#overlay").fadeIn("fast", function(){
					popAd(id);
					centerPopup(id);
				});				
			},
			
			popupdate: function(id){
				$("#qty"+id).keypress(function(e){
					if(e.which == 13) return false;
				});
					 
				$("#overlay").css({
					"opacity": "0.7",
				});
				
				$("#qty"+id).val($("#quantity"+id).val());
				
				$("#overlay").fadeIn("fast", function(){
					popUp(id);
					centerPopup(id);
				});				
			},
			
			close: function(){
				$("#overlay").fadeOut("slow");
				$(".options-del").css("display", "none");
				popupStatus = 0;
				id = "";
			},
			
			beforeSuccess: function(){
				$(".btn-cart").attr("disabled", "disabled").addClass("disable");
				$(".sidebar-button-remove").attr("disabled", "disabled").addClass("disable");
				$(".sideupdate").removeAttr("onclick").addClass("disable");
				$(".a-remove-all").removeAttr("onclick").addClass("disable");

			},
			
			Asuccess: function(){
				$(".btn-cart").removeAttr("disabled").removeClass("disable");
				$(".sidebar-button-remove").removeAttr("disabled").removeClass("disable");
			},
			
			removeall: function(url){
				if(confirm("Are you sure you would like to remove all items from the shopping cart?")){
					cart4.close();
					$.ajax({
						url:url,
						cache: false,
						async:true,
						beforeSend: function(){
							$("div.actions").replaceWith($(".sideLoader").css({"display":"block","color":"#D01B16"}));
							cart4.beforeSuccess();
						},
						success: function(data){	
							var newparam = window.location.href.slice(window.location.href.indexOf('?') + 1);
							var oldlocation = window.location.pathname;
							var newlocation = oldlocation+"?"+newparam;
							var location = newparam.length>0?newlocation:oldlocation;
							$(".header .links").load(location+" .header .links>*");
							$(".col-main").load(location+" .col-main>*",function(){
								cart4.beforeSuccess();
								$(".block-cart").load(location+"  .block-cart>*",function(){cart4.Asuccess();});
							});
						}
					});
				}
			},
			
			remove: function(url, id){
				if(confirm("Are you sure you would like to remove this item from the shopping cart?")){
					$.ajax({
						url: url,
						cache: false, 
						async:true,
						beforeSend: function(){
							$("#forajax_"+id).replaceWith($("#ajaxloader_"+id).css({"display":"block","color":"#D01B16"}));
							cart4.beforeSuccess();
						},
						success: function(data){
							var newparam = window.location.href.slice(window.location.href.indexOf('?') + 1);
							var oldlocation = window.location.pathname;
							var newlocation = oldlocation+"?"+newparam;
							var location = newparam.length>0?newlocation:oldlocation;
							$(".header .links").load(location+" .header .links>*");
							$(".col-main").load(location+" .col-main>*",function(){
								cart4.beforeSuccess();
								$(".block-cart").load(location+"  .block-cart>*",function(){cart4.Asuccess();});
							});
						}
					});
				}
			},
			
			addBulk: function(prodId){
				var Qty = $("#qty"+prodId).val();
				var data = "product="+prodId +"&qty="+Qty;
				var url = $("#addcarturl").val();
				var qsplit = Qty.split(".");
				var decimal = qsplit[1];
					if(Qty == 0 || decimal > 0){
					alert("Invalid Quantity Value!");
					return false;
					}
				if(confirm("Are you sure you would like "+Qty+" qty/quantities in this item?")){
					cart4.close();
					$.ajax({
						url: url,
						cache: false, 
						data: data,
						type: 'post',
						async:true,
						beforeSend: function(data){
							$("#ajax_loader_"+prodId).css({"display":"block"});
							cart4.beforeSuccess();
						},
						success: function(data){	
							var newparam = window.location.href.slice(window.location.href.indexOf('?') + 1);
							var oldlocation = window.location.pathname;
							var newlocation = oldlocation+"?"+newparam;
							var location = newparam.length>0?newlocation:oldlocation;
							$(".header .links").load(location+" .header .links>*");
							$(".col-main").load(location+" .col-main>*",function(){
								cart4.beforeSuccess();
								$("#ajax_loader_"+prodId).css({"display":"block"});
								$(".block-cart").load(location+"  .block-cart>*",function(){
									cart4.Asuccess();
									$("#ajax_loader_"+prodId).css({"display":"none"});
								});
							});
							
							
						}
					});	
				}
			
			},
			
			removeOptions: function(itemid){
				var preQty = $("#pqty"+itemid).val();
				var curQty = $("#qty"+itemid).val();						 
				var leftQty = preQty - curQty;
				$("#qty"+itemid).val(leftQty);
				var data = $("#product_update_form_"+itemid).serialize();
				var url = $("#product_update_form_"+itemid).attr('action');
				$("#qty"+itemid).val(curQty);
				var qsplit = curQty.split(".");
				var decimal = qsplit[1];
					if(leftQty < 0 || curQty == 0 || decimal > 0){
					alert("Invalid Quantity Value!");
					return false;
					}
				if(confirm("Are you sure you would like to remove "+curQty+" qty/quantities in this item?")){
					cart4.close();
					$.ajax({
						url: url,
						cache: false, 
						data: data,
						type: 'post',
						async:true,
						beforeSend: function(){
							$("#forajax_"+itemid).replaceWith($("#ajax_loader_"+itemid).css({"display":"block","color":"#D01B16"}));
							cart4.beforeSuccess();	
						},
						success: function(data){	
							var newparam = window.location.href.slice(window.location.href.indexOf('?') + 1);
							var oldlocation = window.location.pathname;
							var newlocation = oldlocation+"?"+newparam;
							var location = newparam.length>0?newlocation:oldlocation;
							$(".header .links").load(location+" .header .links>*");
							$(".col-main").load(location+" .col-main>*",function(){
								cart4.beforeSuccess();
								$(".block-cart").load(location+"  .block-cart>*",function(){cart4.Asuccess();});
							});
						}
					});	
				}
			},
			
			update: function(itemid){
				var preQty = $("#pqty"+itemid).val();
				var curQty = $("#qty"+itemid).val();						 
				var data = $("#product_update_form_"+itemid).serialize();
				var url = $("#product_update_form_"+itemid).attr('action');
				$("#qty"+itemid).val(curQty);
				var qsplit = curQty.split(".");
				var decimal = qsplit[1];
					if(decimal > 0){
					alert("Invalid Quantity Value!");
					return false;
					}
				if(confirm("Are you sure you would like to update your cart?")){
					cart4.close();
					$.ajax({
						url: url,
						data: data,
						cache: false, 
						type: 'post',
						async:true,
						beforeSend: function(){
							$("#forajax_"+itemid).replaceWith($("#ajax_loader_"+itemid).css({"display":"block","color":"#D01B16"}));
							cart4.beforeSuccess();
						},
						success: function(data){
							var newparam = window.location.href.slice(window.location.href.indexOf('?') + 1);
							var oldlocation = window.location.pathname;
							var newlocation = oldlocation+"?"+newparam;
							var location = newparam.length>0?newlocation:oldlocation;
							$(".header .links").load(location+" .header .links>*");
							$(".col-main").load(location+" .col-main>*",function(){
								cart4.beforeSuccess();
								$(".block-cart").load(location+"  .block-cart>*",function(){cart4.Asuccess();});
							});
						}
					});	
				}
			}
	};

});
