

$(document).ready(function(){
	var countDateStamp = new Date();
	$.ajax({
		dataType: 'text', 
		url:"" + "/checkout/data/item_count.jsp?time=" + countDateStamp.getTime(),
		success: function(data) {
			$("#headerItemCount").html(data);
		}
	});
});

var persistentCartCommands = new Array(11);
persistentCartCommands[0] = '/checkout/universal_cart.jsp';
persistentCartCommands[1] = '/checkout/add_item_pc.cmd';
persistentCartCommands[2] = '/checkout/add_items_pc.cmd';
persistentCartCommands[3] = '/checkout/delete_item_in_cart.cmd';
persistentCartCommands[4] = '/checkout/add_catalog_order_item_pc.cmd';
persistentCartCommands[5] = '/user/add_wishlist_item_to_basket_pc.cmd';
persistentCartCommands[6] = '/user/add_all_wishlist_items_to_basket_pc.cmd';
persistentCartCommands[7] = '/user/instore_pickup_zip_json_pc.jsp';
persistentCartCommands[8] = '/user/add_item_from_edit_to_wishlist.cmd';
persistentCartCommands[9] = '/user/copy_all_wishlist_items_to_basket_pc.cmd';
persistentCartCommands[10] = '/user/copy_wishlist_item_to_basket_pc.cmd';

var persistentCartContainerId = "#widget-ucart";
var persistentCartCloseButClass = ".widget-ucart-close-but";
var hideTimeOuts= new Array();

var ucartLoadingHTML = 	'<div id="widget-ucart">' +
				 		'  <div id="glo-ucart-top" class="widget-ie6png"><!--  --></div>' +
				  		'  <div id="glo-ucart-body" class="widget-ie6png">' +
				  		'    <div id="glo-ucart-content">'+
				  		'	   <div class="widget-ima-loader"><img src="/assets/images/common/loading.gif" alt="Loading..." /></div>' +
				  		'    </div>' +
				  		'  </div>' +
  				  		'</div>';

var ucartSimpleHTML = 	'<div id="widget-ucart"></div>';

var persistentCartIsShowing = false;
  				  		
/* Function(s) to Show the Basket Layer */
function showBasket(action,params,refreshPage,refreshDelayTime) {
	if( (action == "show") || (action == "showFromQuickview") )
	{ showloading(ucartLoadingHTML); requestURL = persistentCartCommands[0]; }
	else if(action == "addProduct")
	{ showloading(ucartSimpleHTML); requestURL = persistentCartCommands[1];}
	else if(action == "addEnsemble")
	{ showloading(ucartSimpleHTML); requestURL = persistentCartCommands[2];}
	else if(action == "remove")
	{ showloading(ucartLoadingHTML); requestURL = persistentCartCommands[3];}
	else if(action == "addCatalogItems")
	{ showloading(ucartSimpleHTML); requestURL = persistentCartCommands[4];}
	else if(action == "addProductWishlist")
	{ showloading(ucartSimpleHTML); requestURL = persistentCartCommands[5];}
	else if(action == "addAllProductsWishlist")
	{ showloading(ucartSimpleHTML); requestURL = persistentCartCommands[6];}
	else if(action == "copyAllProductsWishlist")
	{ showloading(ucartSimpleHTML); requestURL = persistentCartCommands[9];}
	else if(action == "copyProductWishlist")
	{ showloading(ucartSimpleHTML); requestURL = persistentCartCommands[10];}
	else
	{ alert("missing action"); }

	requestURL = requestURL;
	params = "ts=" + timestamp() + "&action=" + action + "&" + params;

	$.ajax({
		type: "POST",
		url: requestURL,
		data: params,
		dataType: "html",
      	timeout: 15000,
		success: function(data) {
	
			hideloading();
			$(persistentCartContainerId).append(data);
			$(persistentCartContainerId).show();
			if (refreshPage != undefined && refreshPage) {
            	setTimeout( function() { location.reload(true); }, refreshDelayTime != undefined? refreshDelayTime : 0);
         }
         persistentCartIsShowing = true;
			return true;
		},
		error: function() {
		   
			hideloading();
			return false;
		}
	});
};
function showWishList(action,params,itemGuid,productId,productVariantId,quantity,refreshPage,refreshDelayTime){
  if(action == "addToWishList")
	{ showloading(ucartSimpleHTML); requestURL = persistentCartCommands[8];}
	else
	{ alert("missing action"); }
	requestURL = requestURL;
	params = "ts=" + timestamp() + "&action=" + action + "&" + params;
	if(itemGuid==undefined)
	                itemGuid="";
	$.ajax({
		type: "POST",
		url: requestURL,
		data: params,
		dataType:"html",
		success: function(data) {
		hideloading();
	    $(persistentCartContainerId).append(data);
	    $(persistentCartContainerId).show();
	    var nextPage=$(data).find("input[name=nextPage]").val();         
	       if(nextPage=="loginPage")  {
	                 if(itemGuid==""){
	                        
                         location.href="/user/login.jsp?dest=user/add_item_to_wishlist.cmd?productId="+productId+"$--$productVariantId="+productVariantId+"$--$quantity="+quantity;
	                 }
	                      
	                   else
	                      location.href="/user/login.jsp?dest=/user/add_item_to_wishlist_ajax.cmd?productId="+productId+"$--$itemUUID="+itemGuid+"$--$productVariantId="+productVariantId+"$--$quantity="+quantity;
	          }
	           else
	                  if(nextPage=="wishList")
	                           location.href="/user/wishlist.jsp";
	          if (refreshPage != undefined && refreshPage) {
            	setTimeout( function() { location.reload(true); }, refreshDelayTime != undefined? refreshDelayTime : 0);
         }           
		      persistentCartIsShowing = true;
		return true;
		},
		error: function() {
			hideloading();
			return false;
		}
	});
};
function showloading(htmlToShow) {
	$(persistentCartContainerId).remove();

	//load, position, show new cart
	$("body").append(htmlToShow);
	positionpersistentCart();
	$(persistentCartContainerId).show();

	// add an event for close layer.
	//$(persistentCartCloseButClass).click(function() { hideBasket(); });
};

function hideloading() {
	$(persistentCartContainerId + " *").remove();
	$(persistentCartContainerId).html("");
};


//edit this function to position cart.

function positionpersistentCart() {
	
	//newLeft = 10 + ($("body").width() / 2) + ( $(".page_wrap").width() / 2 ) - $(persistentCartContainerId).width(); - old fry starter code
	
    var $widget_ucart = $( "#widget-ucart" );
	$widget_ucart.appendTo($('div.common-header-wrapper'))
   /* var newLeft = ($( document ).width() / 2) - ($widget_ucart.outerWidth() / 2);
	//var newTop = ($( window ).height() / 2) - ($widget_ucart.outerHeight() / 2); In IE and normal browsers we have different results
	var newTop = 80;
    
	$(persistentCartContainerId).css("left", newLeft + "px");
	$(persistentCartContainerId).css("top", newTop + "px");*/
};
  				 
 //edit this function to update the setup
function setupPersistentCartButtons() {
	// draw focus near this
	window.location = "#";

	$(persistentCartCloseButClass).unbind("click").click(function() {            
		hideBasket();
	});
	$(persistentCartCloseButClass).attr("href","javascript:void(0)");
	clearAllTimeouts();
};

//Edit this function if need to do something special on basket close.
function hideBasket() {
	$(persistentCartContainerId).hide();
	$(persistentCartContainerId).remove();

	shoppingBagBut = $("#widget-header-active-link").eq(0);
	$(shoppingBagBut).attr("id","");
	$(shoppingBagBut).mouseout();
	
	persistentCartIsShowing = false;
};

function isShowingBasket() {
	return persistentCartIsShowing;
}

function updateHeader(amt) {
	if(amt == 1)
	{ $("#widget-ucart-item-count").text(amt); }
	else
	{ $("#widget-ucart-item-count").text(amt);}
};


function addToCart(prefix, container) {
  
	var params = getCartUpdateParams(prefix, container);

	persistentCartIsShowing = false;			
  	showBasket('addProduct',params);
};

function addToCartFromQuickView(prefix, container){

   var scope = $(prefix);
	if (container)
		scope = $(container).parents(prefix);
   

   //see if this is an update.
  	if( $("input[name=itemGUID]", scope).size() > 0 )
  	{
  	  EventTracker.track("Product Quick View Update Cart",$("input[name=productName]", scope).val());
  	}
  	else
  	     EventTracker.track("Product Quick View Add to Cart",$("input[name=productName]", scope).val());

   addToCart(prefix, container);

};

          /*Add to cart */
   function addToCartProductDetail(prefix,container){
   
         var scope = $(prefix);
	    if (container)
		scope = $(container).parents(prefix);
     
         addToCart(prefix, container);
    
         /*track the event in the google analytic*/
         EventTracker.track("Product Details Add to Cart",$("input[name=productName]", scope).val());
    
        };

function updateProductInTheCart(prefix, container) {

	var params = getCartUpdateParams(prefix, container);
	
	showloading(ucartSimpleHTML); 
	requestURL = '/checkout/update_item_in_order_ajax.cmd';
	params = "ts=" + timestamp() + "&action=addProduct&" + params;
	
	
	$.ajax({
		url: '/checkout/update_item_in_order_ajax.cmd',
		data: params,
		type: "POST",
		dataType: "text",
		success:function(data){

			if (data.charAt(0) == "{") {
				var editParent = $("#panelEditItem");
				navigate.showLoading(editParent);
			
				hideQuickView();
				navigate.clearLoading(editParent);
				
				services.setBaseVariantImageUrl('http:\/\/images.intimissimi.com\/get\/w\/98\/h\/98\/{0}.image?notfound=notavailable');
				services.updateOrderSummary(data);
				
				updateHeader($.parseJSON(data).itemCount);
			} else {
				$(persistentCartContainerId).append(data);
				$(persistentCartContainerId).show();
			}
			
			return true;
		}
	});
	
};

function getCartUpdateParams(prefix, container) {
	var scope = $(prefix);
	if (container)
		scope = $(container).parents(prefix);

	var productVariantId = $("input[name=productVariantId]", scope).val();
	if (productVariantId == null || productVariantId == undefined)
		productVariantId = $("input[name=productVariantId2]", scope).val();

	var colorSelectedValue = $("input[name=colorSelectedValue]", scope).val();
	if (colorSelectedValue == null || colorSelectedValue == undefined) {
		colorSelectedValue = "";
	}

	var sizeSelectedValue = $("input[name=sizeSelectedValue]", scope).val();
	if (sizeSelectedValue == null || sizeSelectedValue == undefined) {
		sizeSelectedValue = "";
	}

	params =  "productName=" + $("input[name=productName]", scope).val() +
			  "&productId=" + $("input[name=productId]", scope).val() +
   	 		  "&categoryId=" + $("input[name=categoryId]", scope).val() +
   	 		  "&parentCategoryId=" + $("input[name=parentCategoryId]", scope).val() +
   	 		  "&subCategoryId=" + $("input[name=subCategoryId]", scope).val() +
   	 		  "&quantity=" + $("input[name=quantity]", scope).val() +
   	 		  "&isFromCheckout=" + $("input[name=isFromCheckout]", scope).val() +
   	 		  "&productVariantId=" + productVariantId +
   			  "&colorSelectedValue=" + colorSelectedValue +
   			  "&sizeSelectedValue=" + sizeSelectedValue;
   	 		  
	//see if this is an update.
  	if( $("input[name=itemGUID]", scope).size() > 0 )
  	{
  	  params = params + "&itemGUID=" + $("input[name=itemGUID]", scope).val() + "&isUpdate=1"; 
  	 
  	}
  

	if( $("input[name=onBasketPage]", scope).size() > 0 )
  		{ params = params + "&onBasketPage=" + $("input[name=onBasketPage]", scope).val(); }
  	
	if (prefix != undefined)
		params = params + "&prefix=" + prefix;
		
	return params; 
};

function addToWishList(prefix, container) {

	var scope = $(prefix);
	if (container)
		scope = $(container).parents(prefix);

 
      
    
	var productVariantId = $("input[name=productVariantId]", scope).val();
	if (productVariantId == null || productVariantId == undefined)
		productVariantId = $("input[name=productVariantId2]", scope).val();

	var colorSelectedValue = $("input[name=colorSelectedValue]", scope).val();
	if (colorSelectedValue == null || colorSelectedValue == undefined) {
		colorSelectedValue = "";
	}

	var sizeSelectedValue = $("input[name=sizeSelectedValue]", scope).val();
	if (sizeSelectedValue == null || sizeSelectedValue == undefined) {
		sizeSelectedValue = "";
	}

	params =  "productName=" + $("input[name=productName]", scope).val() +
			  "&productId=" + $("input[name=productId]", scope).val() +
   	 		  "&categoryId=" + $("input[name=categoryId]", scope).val() +
   	 		  "&parentCategoryId=" + $("input[name=parentCategoryId]", scope).val() +
   	 		  "&subCategoryId=" + $("input[name=subCategoryId]", scope).val() +
   	 		  "&quantity=" + $("input[name=quantity]", scope).val() +
   	 		  "&productVariantId=" + productVariantId +
   			  "&colorSelectedValue=" + colorSelectedValue +
   			  "&sizeSelectedValue=" + sizeSelectedValue;
   	 		  
	//see if this is an update.
  	if( $("input[name=itemGUID]", scope).size() > 0 )
  	{ 
  	   params = params + "&itemGUID=" + $("input[name=itemGUID]", scope).val() + "&isUpdate=1"; 
  	   
  	}

  	         

    var itemGuid=$("input[name=itemGUID]", scope).val();
    
    var productId=$("input[name=productId]", scope).val();
    
    var productVariantId=productVariantId;
    
    var quantity=quantity=$("input[name=quantity]", scope).val();
    
    
	
	if( $("input[name=onBasketPage]", scope).size() > 0 )
  		{ params = params + "&onBasketPage=" + $("input[name=onBasketPage]", scope).val(); }
  	
	if (prefix != undefined)
		params = params + "&prefix=" + prefix;
 	persistentCartIsShowing = false;
    EventTracker.track("Product Quick View Add to Wish List",$("input[name=productName]", scope).val());			
  	showWishList('addToWishList',params,itemGuid,productId,productVariantId,quantity);
};

function wishListAddToCart(params,refreshPage,refreshDelayTime) {
    
    
    EventTracker.track("Wish List Add to Cart",params.substring(params.indexOf("=")+1,params.indexOf("&")));
	showBasket('addProductWishlist',params,refreshPage,refreshDelayTime);
};

function wishListAddAllToCart(params,refreshPage,refreshDelayTime) {
   /*Track move all to cart*/
     EventTracker.track("Wish List Move All to Cart","Wish List Move All into cart");
	showBasket('addAllProductsWishlist',params,refreshPage,refreshDelayTime);
};

function wishListCopyToCart(params,refreshPage,refreshDelayTime) {
	showBasket('copyProductWishlist',params,refreshPage,refreshDelayTime);
};

function wishListCopyAllToCart(params,refreshPage,refreshDelayTime) {
	showBasket('copyAllProductsWishlist',params,refreshPage,refreshDelayTime);
};

function addCatalogOrderItemsToCart() {
    params = "productId=" + $("input[name=productId]").val() +
  		     "&itemNumber=" + $("input[name=itemNumber]").val() +
  			 "&productName=" + $("input[name=productName]").val() +
  			 "&productVariantId=" + $("input[name=productVariantId]").val() +
  			 "&quantity=" + $("input[name=quantity]").val() +
  			 "&colorSelectedValue=" + $("input[name=colorSelectedValue]").val() +
  			 "&sizeSelectedValue=" + $("input[name=sizeSelectedValue]").val();
    showBasket('addCatalogItems',params);
};

function addEnsembleToCart(type) {
	params =  "productName=" + $("input[name=productName]").val() +
			  "&ensembleId=" + $("input[name=ensembleId]").val() +
			  "&categoryId=" + $("input[name=categoryId]").val() +
   	 		  "&parentCategoryId=" + $("input[name=parentCategoryId]").val();

	// iterate through products in the ensemble for variant id
	$(".the-variant-ids").each(function() {
		params = params + "&" + $(this).attr("name") + "=" + $(this).val();
	});

	$(".the-variant-colors").each(function() {
		params = params + "&" + $(this).attr("name") + "=" + $(this).val();
	});

	$(".the-variant-sizes").each(function() {
		params = params + "&" + $(this).attr("name") + "=" + $(this).val();
	});

	// iterate through products for qty
	$(".the-variant-qtys").each(function() {
		if( type == 'all' )
		{
		  $(this).val("1");
		  params = params + "&" + $(this).attr("name") + "=1";
		}
		else
		{ params = params + "&" + $(this).attr("name") + "=" + $(this).val(); }
	});

	// iterate through products variant count
	$(".the-variant-count").each(function() {		
		params = params + "&" + $(this).attr("name") + "=" + $(this).val();
	});
	
	params = params + "&productCount=" + $(".the-variant-ids").length;
	
	persistentCartIsShowing = false;
	showBasket('addEnsemble',params);
};

function setUserZipCodePC(refresh)  {
    var params = "ts=" + timestamp() + "&action=updateUserZipCode" +
                 "&storesListZipCode=" +  $("input[name=storesListZipCodePC]").val() +
                 "&storesListLatitude=" +  $("input[name=storesListLatitudePC]").val() +
                 "&storesListLongitude=" +  $("input[name=storesListLongitudePC]").val();
    var requestURL = persistentCartCommands[7];

    $.ajax({
		type: "GET",
		url: requestURL,
		data: params,
		dataType: "json",
      	timeout: 15000,
		success: function(data) {    
			if(data.validZip){
    			if (refresh) {
					showBasket('show', '');
				}
				return true;
			}else{
				alert(data.result);
				return false;
			}
		},
		error: function() {
			alert('There was an error trying to save your zip code.');
			return false;
		}
	});

}

function setWishListVisibility(val, containerId, successMessage, errorMessage) {
	setWishListPreference('wishlistvisibility', val, containerId, successMessage, errorMessage);
}

function setWishListSizeVisibility(val, containerId, successMessage, errorMessage) {
	setWishListPreference('wishlistsizevisibility', val, containerId, successMessage, errorMessage);
}

function setWishListPreference(action, val, containerId, successMessage, errorMessage)  {
    var params = "action=" + action + "&value=" + val;
    var containerId = '#' + containerId;
    var contentId = containerId + 'message';

	// Reset container properties
	$(containerId).attr('class', 'formFieldContainer');	
    $(containerId).css('opacity', '0');

    $.ajax({
		type: "GET",
		url: "/user/wishlist_visibility_json.jsp",
		data: params,
		dataType: "json",
      	timeout: 15000,
		success: function(data) {
			
			if(data.result == 'OK') {
				$(contentId).text(successMessage);
				$(containerId).addClass('valid');
			} else {
				$(contentId).text(errorMessage);
				$(containerId).addClass('invalid');
			}
			
			$(containerId).animate({opacity:1});
		},
		error: function() {

			$(contentId).text(errorMessage);
			$(containerId).addClass('invalid');
			$(containerId).animate({opacity:1});
		}
	});
}

// Edit this per site to adjust location
function adjustDivLocation(divToAdjust) {
	var bWindowOffsets = getScrollXY();
	var bWindowViewport = getViewportSize();
	var qvTop = ((bWindowViewport[1] / 2) - ($(divToAdjust).height() / 2)) + bWindowOffsets[1];
	qvTop = (qvTop < 0) ? 100 : qvTop;
	$(divToAdjust).css("top",qvTop+"px");
	$(divToAdjust).css("left","50%");
	$(divToAdjust).css("margin-left",-($(divToAdjust).width()/2));
};

// Helper Function(s)
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getViewportSize() {
  var vpW = 0, vpH = 0;
  if (typeof window.innerWidth != 'undefined')
  {
    vpW = window.innerWidth,
    vpH = window.innerHeight;
  }
  else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
  {
    vpW = document.documentElement.clientWidth,
    vpH = document.documentElement.clientHeight;
  }
  else
  {
    vpW = document.getElementsByTagName('body')[0].clientWidth,
    vpH = document.getElementsByTagName('body')[0].clientHeight;
  }
  return [  vpW, vpH ];
}

function errorAppend(area,msg) {
	$(area).html(msg.replace(/&amp;/g, "&").replace(/&lt;/g,
        "<").replace(/&gt;/g, ">").replace(/&#39;/g, "'"));
	$(area).show();
	initPageHeight();
};

function resetErrorFields() {
	$(".glo-tex-error").hide();
	$(".glo-tex-error").html("");
   initPageHeight();
};

function messageAppend(area,msg) {
	$(area).html(msg);
	$(area).show();
	initPageHeight();
};

function resetMessageFields() {
	$(".glo-tex-info").hide();
	initPageHeight();
};

function clearAllTimeouts() {
	for(x = 0; x < hideTimeOuts.length; x++)
	{ clearTimeout(hideTimeOuts[x]); }
};

function timestamp() { 
	return new Date().getTime(); 
}
	
function loadQuickView(overlayURL)
{
	if (typeof OverlayWidget != "undefined") 
 		OverlayWidget.show("#headerOverlay", null, { sourceURL : overlayURL }); 
}	

function hideQuickView()
{
	if (typeof OverlayWidget != "undefined") { OverlayWidget.hideAll(); }
}

function loadOverlay(overlay, overlayURL)
{
	if (typeof OverlayWidget != "undefined") 
 		OverlayWidget.show(overlay, null, { sourceURL : overlayURL }); 
}
