// Event Handlers
Core.event.addToCartBegin = function (button, productId) {
    var className = $(button).attr('class');
    $(button).removeAttr('onclick').removeClass(className).addClass(className.replace("-over", "") + '-wait');
}; 

Core.event.addToCartEnd = function (button,id,html,incartButtonText, movetoCart, addedText) {
	$('.cart-div').replaceWith(html);
	$('.header-cart').addClass('cart');
	$('.header-cart.auth').addClass('auth-cart').removeClass('auth');
	$('.header-cart.auth-preorder').addClass('auth-cart-preorder').removeClass('auth-preorder');
	var className = $(button).attr('class');
    $(button).removeClass(className).addClass(className.replace("-over", "").replace("-wait", "") + '-incart');
	if (incartButtonText && incartButtonText != ''){
		if ($('.text',button).text()) {
			$('.text',button).text(incartButtonText);
		}
		else {
			$(button).text(incartButtonText);
		}
	}
	if (addedText && addedText != ''){
		if ((jQuery.trim(className) ==  'button buy-wait') || (jQuery.trim(className) ==  'button buy-wait-over')) {
	    	addedText = '<div style="height: 17px;">&nbsp;</div>' + addedText;
	    }
		$('#added_prod-' + id).html(addedText);
	}
	if (movetoCart){
		$(button).unbind("click").removeAttr("onclick"); // Unbind click to prevent repeated call AddToCart().
		$(button).click( function(){
			window.location = typeof(language) == 'undefined' ? '/cart' : language + '/cart';
		});
	}
}; 

Core.event.deleteFromCartEnd = function (sender, productId,totalValues) {
	//alert('dfc=' + totalValues.totalquantity);
	$(".product-row-"+productId, ".cart-table").remove();
	_updateTotalValues(totalValues);
};

Core.event.refreshCartItemsBegin = function (sender, data) {
	$("#refresh-all").addClass("update");
};
Core.event.refreshCartItemsEnd = function (sender, data) {
	_updateCartItems(data.products);
	_updateTotalValues(data.total, data.NoCheckout);
	
};

// End Event Handlers

function _updateCartItems(products) {
	for(i=0; i<products.length; i++) {
        if (products[i].inStock == 0) {
            selector = "error";
            remove = "";
            $("#product-status-" + products[i].productId).html("<span class=\"error\">" + products[i].NoLabel + "</span>");
        }
        if(products[i].inStock < products[i].quantity) {
            selector = "error";
            remove = "";
            $("#product-status-" + products[i].productId).html("<span class=\"error\">" + products[i].IsLabel + "</span>");
        } else {
            selector = "";
            remove = "error";
            $("#product-status-" + products[i].productId).html(products[i].YesLabel);
        }
        $(".quantity-" + products[i].productId).addClass(selector).removeClass(remove);
        
		if(products[i].discountAvailable){
			$(".price-without-discount-" + products[i].productId, ".cart-table").addClass("cart-product-price-default");
			$(".amount-without-discount-" + products[i].productId, ".cart-table").addClass("cart-product-amount-default");
			css = '{"width" : "100%", "display" : "block"}';
		}else{
			$(".price-without-discount-" + products[i].productId, ".cart-table").removeClass("cart-product-price-default");
			$(".amount-without-discount-" + products[i].productId, ".cart-table").removeClass("cart-product-amount-default");
			css = '{"width" : "0%", "display" : "none"}';
		}
			
		$(".price-without-discount-" + products[i].productId, ".cart-table").text(products[i].priceWithoutDiscount);
		$(".price-with-discount-" + products[i].productId, ".cart-table").css("display", products[i].discountAvailable ? "block" : "none").css("width", products[i].discountAvailable ? "100%" : "0%");
		$(".price-with-discount-" + products[i].productId, ".cart-table").text(products[i].priceWithDiscount);
		$(".amount-without-discount-" + products[i].productId, ".cart-table").text(products[i].amountWithoutDiscount);
		$(".amount-with-discount-" + products[i].productId, ".cart-table").css("display", products[i].discountAvailable ? "block" : "none").css("width", products[i].discountAvailable ? "100%" : "0%");
		$(".amount-with-discount-" + products[i].productId, ".cart-table").text(products[i].amountWithDiscount);
		$("#weight-" + products[i].productId, ".cart-table").text(products[i].weight);
	}
};


function _updateTotalValues(jsonTotalValues, jsonNoCheckout) {
	if(jsonTotalValues.totalquantity == 0){
		window.location = typeof(language) == 'undefined' ? '/' : language + '/';
	}
	if ($('p.cartTopMsg').length) $('p.cartTopMsg').remove(); //remove out of stock message on the top.
	$("#total-amount-default, #discount-all").css("display", jsonTotalValues.totalAmountWithDiscount != jsonTotalValues.totalAmountWithoutDiscount ? "block" : "none");
	$(".total-amount-with-discount", ".cart-table").text(jsonTotalValues.totalAmountWithDiscount);
	$(".total-amount-without-discount", ".cart-table").text(jsonTotalValues.totalAmountWithoutDiscount);
	
	$(".total-quantity", ".cart-table").text(jsonTotalValues.totalquantity);
	$(".total-discount", ".cart-table").text(jsonTotalValues.totalDiscount);
	$(".total-weight", ".cart-table").text(jsonTotalValues.totalWeight);
	$('.cart-div').replaceWith(jsonTotalValues.cartBlock);
	if($("#cart-promotion").length){
		$('#cart-promotion').html(jsonTotalValues.promotion);
	}
	$("#step-gifts, #step-gifts-img").css("display", jsonTotalValues.countgifts ? "block" : "none");
	$("#refresh-all").removeClass("update");
	$('#to_checkout, #to_gifts').replaceWith(jsonTotalValues.CheckoutGiftsButton);
	
}

$(document).ready(function(){
    $(".cart-info-button").mousemove(function(e){
        tip = "#cart-tips-" + $(this).attr("code");
        $(tip).css({"display" : "block", "top" : e.pageY - $(this).offset().top, "left" : e.pageX - $(this).offset().left + 20});
      });
    
    $(".cart-info-button").mouseout(function(){
        $(tip).css("display", "none");
    });
    if(($.browser.msie) && (jQuery.browser.version == "6.0")){
    	$('#frame').css('overflow-y','auto'); // ugly fix for ie6 (main content height)
    }
});
