$.bontonMiniCart = {};
$.bontonMiniCart.clicked = false;
$(function(){
	$('.miniCartCollapsed').hover(
		// mouseover
		function(){ $(this).addClass('miniCartExpanded'); 
		$('.itemsInBag').attr('style','text-decoration: underline');
		},
		
		// mouseout
		function(){ 
			$('.addedToCart').attr('style', 'display:none');
			$(this).removeClass('miniCartExpanded'); 
			$('.itemsInBag').attr('style','text-decoration: none');
			}
	);
	$('.miniCartUnderline').hover(
		// mouseover
		function(){$('.itemsInBag').attr('style','text-decoration: underline');},
		// mouseout
		function(){$('.itemsInBag').attr('style','text-decoration: none');}
	);
});

	
//ajax refresh and display of mini cart

function refreshMiniCart() {
   composeMiniCart();
   //var miniCartUrl = $("#miniCartUrl").val();
	//$("#miniCart").load(miniCartUrl, function(){
		//$('.closeMiniCart').click(function(event){
		//	event.preventDefault();
		//	$('.addedToCart').attr('style', 'display:none');
		//	$('.miniCartCollapsed').removeClass('miniCartExpanded');
		//});
	//});
	$('.miniCartCollapsed').addClass('miniCartExpanded');
	//$('.addedToCart').show();
}



$(function(){
	$('.closeMiniCart').click(function(event){
		event.preventDefault();
		$('.addedToCart').attr('style', 'display:none');
		$('.miniCartCollapsed').removeClass('miniCartExpanded');
	});	
});

$(function(){

if($.cookie("WC_AUTHENTICATION_-1002") != null && $.cookie("WC_USERACTIVITY_-1002") != null){

  $.cookie("SHOPPINGCART10001", null,{ path: '/'});
  
 }
composeMiniCart();

});
function composeMiniCart(){

 
  var subTotal;
  var sumQantity;
  var numItems;
  var cookieValue = $.cookie("SHOPPINGCART10001") ;
  var cartPageUrl = $('#cartPageUrl').val();
  var ahtml = '';
 
 if(cookieValue != null){
   var cookieValueArray = cookieValue.split("|||");
   var subTotalList= jQuery.parseJSON(cookieValueArray[cookieValueArray.length-1]);
   var subtotal =  subTotalList.subtotal;
   var totalQty =  parseInt(subTotalList.totalQty);
   var numItem =  parseInt(subTotalList.numItem);
   
   ahtml = ahtml + '<div class="miniCartHdr">'
                 + '<a  href="'+ cartPageUrl + '" >'
                 + '		<span class="itemsInBag">'
			   if( totalQty <= 1 ){
	ahtml = ahtml + 'Shopping Bag: ' + totalQty + ' Item ';
				}else{
	ahtml = ahtml + 'Shopping Bag: ' + totalQty + ' Items ';
				}
	ahtml = ahtml + '	</span>'
		          + '  </a>'
	              + '</div>';
	              
	}
   if(cookieValue == null){
      ahtml = ahtml + '<div class="miniCartHdr">'
                 + '<a  href="'+ cartPageUrl + '" >'
                 + '		<span class="itemsInBag">'
                 + 'Shopping Bag: 0 Item '
	             + '	</span>'
		         + '  </a>'
	             + '</div>'
                 + ' <div class="miniCartBody miniCartEmpty">Your shopping bag is empty. </div>';
     
   }else{
        ahtml = ahtml + '	<div class="miniCartBody"> '
				  // + '    <div class="addedToCart" id="addedToCart">'
				  // + '      <p>Added to cart</p>'
				  // + '        <a class="closeMiniCart" href="#">Close</a>'
				  // + '     </div>'
				   + '     <table class="cart">'
				   + '       <thead>'
				   + '	       <tr>'
				   + '		    <th class="qty">Qty</th>'
				   + '		    <th class="desc">Item</th>'
				   + '		    <th class="total">Total</th>'
				   + '	      </tr>'
				   + '       </thead>'
				   + '       <tbody>';
				//   alert(ahtml);
	var qtyDisplayed = 0;
	//alert("cookieValueArray length : " + cookieValueArray.length);
	for(var i=0; i<cookieValueArray.length-1; i++){
	//alert("cookieValueArray :" + cookieValueArray[i]);
     var iCookieValue = jQuery.parseJSON(cookieValueArray[i]);
     var rowClass;
     if( (i+1)%2 == 1){
         rowClass = "odd";

        }else{
         rowClass = "even";
      }
      if(i==0){
       rowClass = rowClass + " first";
      }
      if(i==cookieValueArray.length-2){
       rowClass = rowClass + " last";
      }
       var quanity;
       var desc;
       var productUrl;
       var originPrice;
       var promoList;
       var itemTotal;
       var spoBadge;
       
    // for(var j=0; j<iCookieValueArray.length; j++){
      // var items = iCookieValueArray[j].split("::");
      //  if(items[0] == 'quantity'){
          quantity = parseInt(iCookieValue.quantity);
       // }
     //   if(items[0] == 'desc'){
          desc = iCookieValue.desc;
      //  }
      //  if(items[0] == 'productUrl'){
          productUrl = iCookieValue.productUrl;
          if($.cookie("registry") != null && $.cookie("registry") == 'true'){
           productUrl = productUrl + '&registry=true';
          }
        //}
      //  if(items[0] == 'promoList'){
      //  alert("iCookieValuePromolist " + iCookieValue.promoList);
          promoList = iCookieValue.promoList;
          
          spoBadge = iCookieValue.spoBadge;
       // }
      //  if(items[0] == 'originPrice'){
          originPrice = iCookieValue.originPrice;
      //  }
       // if(items[0] == 'itemTotal'){
          itemTotal = iCookieValue.itemTotal;
       // }
     //  }
       qtyDisplayed = qtyDisplayed + quantity;
       ahtml = ahtml + '<tr class="' +rowClass +'">'
               + '<td class="qty">' + quantity
			   + '</td>'
			   + '<td class="item">';
					if(productUrl != null){
			ahtml = ahtml+'<a   href="' +productUrl+'"><span>' +desc+'</span></a>';
					}else{
			ahtml = ahtml+'<span>'+desc+'</span>';
					}
				   if(promoList != null && promoList != '' || (spoBadge!=null && spoBadge!='')){
			ahtml = ahtml+'<ul class="promotions">';
			         var promoArray = promoList.split("||");
			          for(var m=0;m<promoArray.length && promoArray[0]!='';m++){
			           var promoDesc;
			           var promoDisc;
			           var iPromo = promoArray[m].split(",");
			           for (var j=0; j<iPromo.length; j++){
			              var unit = iPromo[j].split(":");
			              if(unit[0] == "promoDesc"){
			                promoDesc = unit[1];
			              }
			              if(unit[0] == "promoDisc"){
			                 promoDisc = unit[1];
			             }

			          }
			ahtml = ahtml+ '<li>' + 	 promoDesc  + ':<span class="discount">'+ promoDisc + '</span> </li>';
				}
				if (spoBadge!=null && spoBadge!='') {
					ahtml = ahtml + '<li class="spo-badge">' + spoBadge + '</li>';
				}
			ahtml = ahtml + '</ul>';
					}
			ahtml =  ahtml + ' </td> ';
			
				 if(originPrice != null && originPrice !='' ){
			ahtml = ahtml + ' <td class="amount"> ' 
			              + '<del>' + originPrice +' </del>'
				          + '<ins>' + itemTotal + '</ins>'
					     + '</td>';
				 }else{
			ahtml = ahtml +	' <td class="amount">' +itemTotal+ '</td>'
				 }
			ahtml = ahtml + '</tr>'			
   }
     ahtml = ahtml + ' </tbody>'
                + '</table>';
        if(numItem > 3){
           var moreItems = totalQty - qtyDisplayed;
       ahtml = ahtml + '<div class="more-items-view">'
				+ '	<span>' + moreItems +' more items in your Shipping Bag '
				+ ' <a href="' + cartPageUrl + '" >View all </a>'	
				+ '	</span>'
				+ '</div>';
         }
  	ahtml = ahtml + '<div class="miniTotals">'
				  + '<div class="clear"></div>'
				  + '<div class="miniTotalInfo">'
				  + '	 <span class="numItem">	' ;		
						  if( totalQty < 1 ){
		ahtml = ahtml + totalQty + ' Item';			       
				           }else{
		ahtml = ahtml + totalQty + ' Items';			        
				          }
		ahtml = ahtml + '</span>'
					  + '<span class="label">Subtotal:</span>'
					  + '	<span class="amount">' + subtotal + '</span>'
					  + '	<div class="clear"></div>'
					  + '</div>'
				      + '<div class="clear"></div>'
				      + '<div><a class="button plainBtn" href="' + cartPageUrl+'" id="viewCartLink"><span>View/Edit Items</span></a>'
				      + '<a  id="miniCartCheckoutButton" class="miniCartLink" href="' + cartPageUrl+'" ><img src="/BonTon/images/bonton/minicartcheckout.gif" alt="Checkout"/></a>'
				      + '</div>'
			          + '  </div>'
				      +'<div class="discountMessage">Shipping and additional discounts will be shown during checkout.</div>'
				      +'</div></div>';
   }
  // alert(ahtml);
  $('#miniCart').html(ahtml);
  // prevent double click
  	$('a.miniCartLink').click(function (){
	  if( $.bontonMiniCart.clicked){
	    $('a.miniCartLink').removeAttr('href');
  	    return false;
	  } else {
	    $.bontonMiniCart.clicked = true;
	    return true;
	  }
	});
	$('#viewCartLink').click(function (){
	  if( $.bontonMiniCart.clicked){
		$('#viewCartLink').removeAttr('href');
  	    return false;
	  } else {
	    $.bontonMiniCart.clicked = true;   
	    return true;
	  }
	});
  }
