var pxBelow="18";
var pxLeft="35";
var pxMiniCartWidth="285";

function loadMiniCart(){
	if(document.getElementById('cartlinkspandiv').style.display!="block"){
		objCartLink.Populate();				
		objMiniCart.DisplayWait();
		// populate the mini cart
		objMiniCart.GetResponse("0");
		//make sure the timer is not paused, then start the timer
		objMiniCart.Resume();
		objTimer.Secs = allPagesMiniCartSecs;
		objTimer.ResetSecs = afterPauseMiniCartSecs;
		objTimer.Pause = objMiniCart.IsPause;
	  objTimer.Finish = unloadMiniCart;
		objTimer.Start();
	}
}
function closeMiniCart(){
  objMiniCart.Pause();
  $jq("#cartlinkspandivgutter").slideUp(1000, function () {initializeMiniCart();});
  objMiniCart.Resume();
	objTimer.Stop();
}
function initializeMiniCart(){
	document.getElementById('cartlinkspandiv').style.display="none";
	var miniCartDiv = document.getElementById('cartlinkspandiv');
	miniCartDiv.innerHTML="";
	miniCartDiv.style.display="none";
	document.getElementById('miniCartUnderlayId').style.height = '0px';
	document.getElementById('miniCartUnderlayId').style.width = '0px';  
}
function unloadMiniCart(){
  objMiniCart.Pause();
  $jq("#cartlinkspandivgutter").slideUp(1000,function () {initializeMiniCart();});
  objMiniCart.Resume();
}
function minicartOmniture(){
	var minicartS=s_gi(s_account);
	minicartS.pageName = "EasyCart";
	minicartS.channel = "Shopping Cart";
	minicartS.eVar4 = "Cart";
	minicartS.products = document.getElementById("omniProductList").value;
	minicartS.events = "scView" + document.getElementById("omniMinicartEvents").value;
	var s_code=minicartS.t();if(s_code)document.write(s_code);
}
/* This is the MiniCart object*/
var MiniCart = function(){
	this.clsdgTimerStatus = false;
	this.Pause = function(){
		objMiniCart.clsdgTimerStatus = true;
	}
	this.Resume = function(){
		objMiniCart.clsdgTimerStatus = false;
	}
	this.IsPause = function(){
		return objMiniCart.clsdgTimerStatus;
	}
	this.DisplayWait = function(){
		var waitDiv = document.getElementById('cartlinkspandivwait');
		objMiniCart.Reposition(waitDiv);
		waitDiv.style.display="block";
		var miniCartIframe = document.getElementById('miniCartUnderlayId');
		objMiniCart.Reposition(miniCartIframe);
		miniCartIframe.style.height = document.body.clientHeight+'px';
		miniCartIframe.style.width = document.body.clientWidth+'px';
		miniCartIframe.style.top = '0px';
		miniCartIframe.style.left = '0px';
	}
	this.HideWait = function(){
		document.getElementById('cartlinkspandivwait').style.display="none";
		var miniCartIframe = document.getElementById('miniCartUnderlayId');
		miniCartIframe.style.height = '0px';
		miniCartIframe.style.width = '0px';
	}
	this.DisplayMiniCart = function(theResponse){
		// hide wait
		objMiniCart.HideWait();
		// display mini cart
		var miniCartDiv = document.getElementById('cartlinkspandiv');
		// make sure javascript works
		NMAjax.setInnerHtml(miniCartDiv, theResponse.responseText);
		miniCartDiv.style.display="block";
		// reposition minicart
		var miniCartDivGutter = document.getElementById('cartlinkspandivgutter');
		objMiniCart.Reposition(miniCartDivGutter);
		miniCartDivGutter.style.display="block";
		//update top items of items div
		objMiniCart.ItemsOfItems();
		// resize product section of mini cart
		objMiniCart.ResizeScroll();
		// resize underlay iframe
		objMiniCart.ResizeIframe(miniCartDiv);
		//send omniture info
		minicartOmniture();
	}
	this.HideMiniCart = function(){
		document.getElementById('cartlinkspandiv').style.display="none";
		document.getElementById('cartlinkspandivgutter').style.display="none";
	}
	this.RepositionAll = function(){
		objMiniCart.Reposition(document.getElementById('cartlinkspandivwait'));
		objMiniCart.Reposition(document.getElementById('cartlinkspandiv'));
		objMiniCart.Reposition(document.getElementById('cartlinkspandivgutter'));
		objMiniCart.Reposition(document.getElementById('miniCartUnderlayId'));
	}
	this.Reposition = function(theElement){		
		//find the location on page			
		var cartlinkspan = document.getElementById('cartlinkspan');
		
       if (cartlinkspan == null)
        {
        	cartlinkspan = document.getElementById('cartPlaceHolderImg');	
        }   
    
    var cartlinkspanwidth = parseInt(cartlinkspan.offsetWidth); 		
    

    var coordLocatorDiv = document.getElementById('cartPlaceHolderImg');
			var agt=navigator.userAgent.toLowerCase();
//handle ie diff chrome and safari calc offsetparent differently
				if (agt.indexOf("msie") != -1){
        var coordLocatorDiv = document.getElementById('cartlinkspan');
           if (coordLocatorDiv == null)
            {
            	coordLocatorDiv = document.getElementById('cartPlaceHolderImg');	
            }        
				}
    var xCoord=0;
    var yCoord=0; 
      while(coordLocatorDiv.offsetParent) {
        xCoord += coordLocatorDiv.offsetLeft;
        coordLocatorDiv=coordLocatorDiv.offsetParent;
      }
      coordLocatorDiv = document.getElementById('cartlinkspan');
       if (coordLocatorDiv == null)
        {
        	coordLocatorDiv = document.getElementById('cartPlaceHolderImg');	
        }        
      while(coordLocatorDiv.offsetParent) {
        yCoord += coordLocatorDiv.offsetTop;
        coordLocatorDiv=coordLocatorDiv.offsetParent;
      }
		var endLocation =  xCoord;
				if (agt.indexOf("msie") != -1){
        endLocation = cartlinkspanwidth + xCoord;
				}		
		//now decrement length of div to determine where to start
		var leftStart = endLocation - pxMiniCartWidth;			
		var topStart = yCoord;				
		var posTop = parseInt(topStart)+parseInt(pxBelow);		
		theElement.style.top = posTop+'px';
		theElement.style.left = leftStart+'px';						
	}
	this.ResizeScroll = function(){	  
		// get full height of mini cart
		var miniCartDiv = document.getElementById('miniCart');
		var defaultMiniCartHeight = miniCartDiv.offsetHeight;						
		// get scrollable section of mini cart
		var miniCartDisplayDiv = document.getElementById('miniCartDisplayDiv');
		var miniCartScrollHeight = miniCartDisplayDiv.offsetHeight;
		// get non-scrollable section of mini cart
		var miniCartHeaderFooterHeight = parseInt(defaultMiniCartHeight) - parseInt(miniCartScrollHeight);		
		// get displayable client height
		var acctlink = document.getElementById('acctlink');
		var topHeight = parseInt(acctlink.offsetTop) + parseInt(pxBelow);
		var theClientHeight = document.documentElement.clientHeight;
		if(theClientHeight == 0){
			theClientHeight = document.body.clientHeight;
		}
		var clientHeight = parseInt(theClientHeight) - parseInt(topHeight);
		// get max scrollabe section based on window height
		var maxMiniCartScrollHeight = parseInt(clientHeight) - parseInt(miniCartHeaderFooterHeight);
		// resize scrollable section of mini cart

		var ieMiniCartScrollHeight = parseInt(miniCartScrollHeight) + 2;
		miniCartDisplayDiv.style.height=ieMiniCartScrollHeight+"px";		

		if(maxMiniCartScrollHeight > miniCartInCartMaxHeight && miniCartInCartMaxHeight < miniCartScrollHeight){
			miniCartDisplayDiv.style.height=miniCartInCartMaxHeight+"px";
		} else if(miniCartScrollHeight > maxMiniCartScrollHeight){
			if(maxMiniCartScrollHeight < miniCartInCartMinHeight){
				miniCartDisplayDiv.style.height=miniCartInCartMinHeight+"px";
			} else {
				miniCartDisplayDiv.style.height=maxMiniCartScrollHeight+"px";
			}
		}
	}
	this.ResizeIframe = function(theDiv){
		var miniCartIframe = document.getElementById('miniCartUnderlayId');
		objMiniCart.Reposition(miniCartIframe);
		miniCartIframe.style.height = theDiv.offsetHeight+'px';
		miniCartIframe.style.width = theDiv.offsetWidth+'px';			
		miniCartIframe.style.top = theDiv.offsetTop+'px';
		miniCartIframe.style.left = theDiv.offsetLeft+'px';
		objMiniCart.Reposition(miniCartIframe);
	}
	this.ItemsOfItems = function(){
		var itemItems2 = document.getElementById('itemItems2');
		if(itemItems2 != null){
			var itemItems1 = document.getElementById('itemItems1');
			itemItems1.innerHTML = itemItems2.innerHTML;
		}
	}
	this.GetResponse = function(itemsAddedCount){
		var objCartLinkSpanDiv = new AjaxRequest();
		objCartLinkSpanDiv.id = "cartlinkspandiv";
		objCartLinkSpanDiv.url = "/store/catalog/miniCart.jhtml";		
		if(itemsAddedCount != null){
			var addToCartItemCount = "addToCartItemCount="+itemsAddedCount			
			objCartLinkSpanDiv.params = addToCartItemCount;
		}
		objCartLinkSpanDiv.Complete = objMiniCart.DisplayMiniCart;
		objCartLinkSpanDiv.Update();
	}
}
var objMiniCart = new MiniCart();
var objTimer = new Timer();
window.onresize = function () {
	if ((disableMiniCart == 'false' && abtestMiniCart != 'off') || abtestMiniCart == 'on'){
		objMiniCart.RepositionAll();
	}
}

/* This is the CartLink section  */
var CartLink = function(){
	this.cartLinkId = "cartlinkspan";
	this.url = "/common/store/nav/cartlinkSA.jhtml";
    // Member variable: Hold instance of this class
    var thisObject;
	this.Populate = function(){
		thisObject = this;
		var objCartLink = new IdUpdater();
		objCartLink.id = thisObject.cartLinkId;
		objCartLink.url = thisObject.url;
		objCartLink.params = "disableMiniCart=false";
		objCartLink.Update();
	}
}
var objCartLink = new CartLink();
