var scrollOffset;
var scrollHeight;

var qoCartOffset;
var scrollTop;
var cartScrolling;

//Dom Ready
$(function(){
	qoAjaxLoadIMG= new Image(); 
  qoAjaxLoadIMG.src="/images/buttons/ajax-loader-red.gif"; 
	qoAjaxLoadBG= new Image(); 
  qoAjaxLoadBG.src="/images/buttons/button-red-140w.jpg"; 
	
	//Set up initial dimensions when the page loads
	scrollOffset = $('#qoitemsscroller').offset();
	scrollHeight = $('#qoitemsscroller').height();
	
	//determine wether the QO cart has a fixed height and scrolling
	cartItemScroll();
	
	//Set up numbers for positioning of  QO Cart
	scrollTop = parseInt($(window).scrollTop());
	qoCartOffset = $('#qocart').offset();
	qoCartOffset = qoCartOffset.top;
	qoCartOffset = parseInt(qoCartOffset)
	qoCartOffset = qoCartOffset - 40;

	if(scrollTop > qoCartOffset) { 
		$('#qocart').css('position','fixed');
		$('#qocart').css('top','40px');
		cartScrolling = true;	
	} else { 
		cartScrolling = false;
	}
	
	//When scrolling set the position of the QO Cart
	$(window).scroll(function(){	
		setCartPos(); 
	});
	
	//When resizing window set height and scrollbars on the QO cart
	$(window).resize(function(){	
		cartItemScroll(); 
	});
	
});


//Set up when to scroll the QO cart
var cartItemScroll = function() { 
	
	winHeight = $(window).height();
	scrollCurHeight = $('#qoitemsscroller').height();
	scrollNewHeight = winHeight - scrollOffset.top - 30;
	
	//Min height of the scroller
	if(scrollNewHeight < 100) { 
		scrollNewHeight = 100;
	}
	
	//alert('scrollHeight is ' + scrollHeight + ' //// scrollNewHeight is ' + scrollNewHeight);
	if(scrollHeight > scrollNewHeight) {
		//alert('Scroll Height is greater than the new height');
		$('#qoitemsscroller').height(scrollNewHeight);
	} 
	else {
		if(scrollCurHeight > scrollNewHeight) {
			//alert('Scroll Height is less than the new height');
			$('#qoitemsscroller').height(scrollNewHeight);
		}
		else {
	 		$('#qoitemsscroller').height('auto');
		}
	}
}

var setCartPos = function(){
	scrollTop = parseInt($(window).scrollTop());

	if(scrollTop > qoCartOffset) { 
		if (cartScrolling == false) { 
			$('#qocart').css('position','fixed');
			$('#qocart').css('top','40px');
			cartScrolling = true;
		}
	} else { 
		$('#qocart').css('position','relative');
		$('#qocart').css('top','auto');
		cartScrolling = false;
	}
}

var qoAddedMsg = '<div class="addedmsg">The item has been added to your cart</div>';

var qoAddtoCart = function(prodRefId) {
	$('.addedmsg').remove();
  var valid = addtocart(prodRefId);

//  alert('qoAddtoCart(): valid = '+ valid);

  if (!valid) {
    return false;
  }

  var skuid = $('#skuid' + prodRefId).val();
  var quantity = $('#qoquantity' + prodRefId).val();

  if (quantity == null || quantity == '' || quantity < 1) {
    alert("Please choose a quantity greater than 1");
    return false;
  }

//  alert('qoAddtoCart(): skuid = '+ skuid);
//  alert('qoAddtoCart(): quantity = '+ quantity);

  if (skuid == null || skuid == '') {
    alert('Something went wrong when adding to cart.');
    return false;
  }

  var parms = "&zoi_save=1&zoi_sku-1=" + skuid + "&zoi_quantity-1=" + quantity;
	
	hideButton('addtocart'+prodRefId);
	
  $('#qosidebar').load('/za/HNT/includes/mini-cart.jsp',parms,function(){
																										 	
    var qoCartItemCount = $('#qocartitemcount').html();
    var qoCartItemTotal = $('#qocartitemtotal').html();
    var ess = '';
	
    if (qoCartItemCount > 1) {
      ess = 's';
    }
			$('#headercartitems').html(qoCartItemCount + ' Item'+ess+', ' + qoCartItemTotal);
			var qoPromo = $('#qocartpromo').html();
			$('#headercartpromo').html(qoPromo);
			if (cartScrolling == true) { 
				$('#qocart').css('position','fixed');
				$('#qocart').css('top','40px');
			} 
			else { 
				$('#qocart').css('position','relative');
				$('#qocart').css('top','auto');
			}
			showButton('addtocart'+prodRefId);
			itemAddedMsg(prodRefId);
			cartItemScroll();
			setCartPos();
			//if we are using ie6 scroll them to the top
			if (jQuery.browser.msie) {
				if (parseInt(jQuery.browser.version) < 7) {
					scrollMe('qocontent');
				}
			}
  });

  return false;
  
}

var qoeditcart = function(remove,updateqty,itemid,qtyid) {

//  alert('remove = ' + remove + "\nupdateqty=" + updateqty + "\nitemid=" + itemid + "\nqtyid=" + qtyid);

  if (updateqty) {
    var quantity = $('#cartqtyinputminicart'+qtyid).val();
//    alert('quantity = ' + quantity);
    if (quantity == null || quantity == '' || quantity < 1) {
      alert("Please choose a quantity greater than 1");
      return false;
    }
  }


  var parms = "zoi_save=1&zoi_id-1=" + itemid;

  if (remove) {
    parms += "&zoi_delete-1=TRUE";
  } else if (updateqty) {
    parms += "&zoi_quantity-1=" + quantity;
  }

//	hideButton('addtocart'+prodRefId);

  $('#qosidebar').load('/za/HNT/includes/mini-cart-edit.jsp',parms,function(){
    var qoCartItemCount = $('#qocartitemcount').html();
    var qoCartItemTotal = $('#qocartitemtotal').html();
    var ess = '';

    if (qoCartItemCount > 1) {
      ess = 's';
    }
    $('#headercartitems').html(qoCartItemCount + ' Item'+ess+', ' + qoCartItemTotal);
			if (cartScrolling == true) {
				$('#qocart').css('position','fixed');
				$('#qocart').css('top','40px');
			}
			else {
				$('#qocart').css('position','relative');
				$('#qocart').css('top','auto');
			}
//			showButton('addtocart'+prodRefId);
//			itemAddedMsg(prodRefId);
			cartItemScroll();
			setCartPos();
  });

  return false;

}


var hideCartButtonHTML = '<div id="addtocartloading" class="loadingbox"><img src="/images/product/detail/load-add-to-cart.gif" alt="Loading" title="Loading" /></div>';

var hideCartButton = function(id) { 
	$('#'+id).hide();
	$('#'+id).after(hideCartButtonHTML);
}

var showCartButton = function(id) { 
	$('#'+id).show();
	$('#'+id).next('div.loadingbox').remove();
}

var itemAddedMsg = function(prodRefId){
	$('#addtocart'+prodRefId).after(qoAddedMsg);
	$('.addedmsg').fadeIn('fast',function(){
		var delay=setTimeout(function(){
			$('.addedmsg').fadeOut('fast',function(){
				$('.addedmsg').remove();
			});
		},5000);
	});
}
