﻿
   
 /**** Cart Pop Up functions *****/
    
    var popCartTimeout; 
    var adjustQuantity = true;
    var showCartFlyout = true; 
     var isPopCartVisable = false; 
     var showWarranty = false;
     var warrantyFamilyID = "";
     
     var CartPromos =new Array();
     var currentPromoIndex = 0; 
     var lastPromoIndex = 0;  
     
     Event.observe(window, 'unload', function() {
        if(showWarranty)
            AccecptProductWarranty(warrantyFamilyID, false);
     });
     
     function AccecptProductWarranty(familyID, skuId, isAccepted) {
        var url = "/product/?familyid=" +familyID +"&selectedsku=" + skuId +"&action=" +(isAccepted ? "SSPAccept" : "SSPDecline");

        new Ajax.Request(url, {   
            onComplete: function(transport) {     
                if(transport.status == 200) {
                    showWarranty = false; // allow user to leave page
                    popCartTimeout = setTimeout("HidePopCart()", 2000); // 2 seconds
                    //HidePopCart();
                }
            } 
        }); 
     }
     
    function CartCallBack() 
    {
        if(req.readyState == 4) 
        {
            if(req.status == 200) 
            {  
                var addToCartResponse; 
               
               //if the popup if visible that means they pushed add to cart or quickbuy again so we should start over
               if(quickView.isVisable)
                   {
                        CartPromos =new Array();
                        currentPromoIndex = 0; 
                        lastPromoIndex = 0;  
                        $('flyoutItems').innerHTML
                     }
               
                eval("addToCartResponse = " + req.responseText); 

                if(adjustQuantity) //if we added an item to the cart
                {
                    QuickViewUpdateBasketCount(addToCartResponse._itemsInCart);
                    
                    $('flyoutItems').innerHTML += addToCartResponse._itemHTML;
                    if(addToCartResponse._itemsInCart != '0')
                    {
                        $('flyoutTotal').innerHTML = addToCartResponse._subTotal;
                        $('flyoutCount').innerHTML = addToCartResponse._itemsInCart;
                        $('flyoutWarranty').innerHTML = addToCartResponse._warrantyOffer;
                    }
                    // fixes an IE issue where there would be a empty space created due to the default display setting of 'block'
                    if(addToCartResponse._warrantyOffer.length == 0)
                        $('flyoutWarranty').style.display = 'none';
               
                    //showWarranty = addToCartResponse._showWarranty;
                    eval('showWarranty = ' +addToCartResponse._showWarranty);
                    warrantyFamilyID = addToCartResponse._warrantyFamilyID;
                  }
                  
                  
                //add the promos into the promo array
                if(lastPromoIndex == 0 )
                {
                    for(var i =0; i<addToCartResponse._promos.length;i++)
                    {
                        CartPromos[lastPromoIndex] = addToCartResponse._promos[i]; 
                        lastPromoIndex++; 
                    }
                }

                //if we have more promos in the promo array display that promo. 
                if(currentPromoIndex < lastPromoIndex)
                {
                    var promo = CartPromos[currentPromoIndex];

                    setTimeout("quickView.LoadQuickView(\""+promo._promoFamilyID+"\", null, \""+promo._parentFamilyID+"\",\""+promo._promoID+"\",\""+promo._promoTypeID+"\", \"" +promo._promoPrice+"\", \""+promo._promoText.replace(/\\/g, '\\\\').replace(/"/g, '\\\"')+"\", " +promo._promoPriceType +")", 500);

                    currentPromoIndex++; 
                }
                else
                {
                    if(isQuickBuy)
                    {
                        var domain = window.location.toString().substring(0,window.location.toString().indexOf('/',9));
                        
                        var quickbuyLink ='';
                        
                        if(IsBasketMVC == 'true' )
                        {
                            quickbuyLink = domain.replace('http:','https:') + '/Basket/?target=Basket&action=QuickBuy'; 
                        }
                        else
                        {
                            quickbuyLink = domain.replace('http:','https:') + '/BASKET/CheckOut/quickbuy.aspx?target=Basket&action=QuickBuy'; 
                        }
                        
                        window.location = quickbuyLink; 
                    }
                    if(showCartFlyout)
                    {
                        ShowPopCart();
                        if(!showWarranty)
                            popCartTimeout = setTimeout("HidePopCart()", 10000); // 10 seconds
                    }
                }
            }
        }
    }
    
    function QuickViewUpdateBasketCount(quantity)
    {
 
       var cartQuantity = document.getElementById('CartQuantity');
      
                if(cartQuantity)
                {
		            if(quantity == 1)
		                cartQuantity.innerHTML = quantity + ' item';
		             else
		                 cartQuantity.innerHTML = quantity + ' items';
		         }
		}

		
    
   
    function ShowPopCart()
    {
        if(!isPopCartVisable)
        {
            CartPromos =new Array();
            currentPromoIndex = 0; 
            lastPromoIndex = 0;  
            if(IsIE6())
                popiframe.open();
            window.scroll(0, 0);
            setTimeout("Effect.toggle('popCartWrapper','Slide', {duration:.5});", 1);
            isPopCartVisable = true; 
        }
    }
    function HidePopCart()
    {
        clearTimeout(popCartTimeout);
        Effect.SlideUp('popCartWrapper', { duration: .5 })
        isPopCartVisable = false; 
        setTimeout("$('flyoutItems').innerHTML = ''", 500);
        if(IsIE6())
            setTimeout("popiframe.close()", 500);
    }

var popiframe; 
function setupPopCart()
{
 if(IsIE6()){
	    //fix for IE6 so that the flash and dropdowns are behind the flyout.  We use Control.Window to put the flyout in an iframe, which will be on top in IE6
	    Control.Window.baseZIndex = 110000;
		popiframe = new Control.Window($('popCartIframeWrapper'),Object.extend({
		height: 300,
		width: 208,
		border:'none'
		}));
	   popiframe.options.position = new Array();
       popiframe.options.position[0] = $('popCartPosition').positionedOffset()[0];
       popiframe.options.position[1] = $('popCartPosition').positionedOffset()[1];
      }
      else
      {
      $('popCartIframeWrapper').style.position = 'relative'; 
       $('popCartIframeWrapper').style.left = $('popCartPosition').style.left;
        $('popCartIframeWrapper').style.top = $('popCartPosition').style.top;
        $('popCartWrapper').style.position = 'absolute'
      }
}