// Event Handlers
Core.event.addToCartBegin = function (button, productId) {
}; 

Core.event.addToCartEnd = function (button,id,html,incartButtonText, movetoCart, addedText) {
	$('.cart-div').replaceWith(html);
	$('.cart-link-'+id).addClass('to-cart');
	if (translate['please_register']) {
		$($(".cart-link-"+id)).text(incartButtonText)
			.unbind("click").removeAttr("onclick");
		$(".cart-link-"+id+'.to-cart').click(function() {
			
			updatePopupCart();
		});
	} else {
		$($(".cart-link-"+id)).text(incartButtonText)
		.unbind("click").removeAttr("onclick")
		.attr('href',typeof(language) == 'undefined' ? '/cart' : language + '/cart');
	}
}; 

Core.event.deleteFromCartEnd = function (sender, productId,totalValues) {
	$(".product-row-"+productId, ".cart-table").fadeOut("normal", function () {
		_updateTotalValues(totalValues);
	}).remove();
	
	$.ajax({
		url     : typeof(language)  == 'undefined' ? "/default/cart/deleteproduct" : language + "/default/cart/deleteproduct",
		type    : 'POST',
		dataType: 'json',
		data    : { productId: productId },
		success : function(data) {
			$("a.cart-link-"+productId).replaceWith(data);
		}
	});
};

Core.event.refreshCartItemsBegin = function (sender, data) {
	$("#refresh-all").addClass("update");
	//$("#addingsteps").html('');
};
Core.event.refreshCartItemsEnd = function (sender, data) {
	_updateCartItems(data.products);
	_updateTotalValues(data.total, data.NoCheckout);
	url = typeof(language)  == 'undefined' ? "/default/cart/addlinks" : language + "/default/cart/addlinks";

	$.post(url, function(data){
		$('#addingsteps').html(data);
	});
};

// End Event Handlers

function _updateCartItems(products) {
	for(i=0; i<products.length; i++) {

		if (products[i].quantity == 0 ) {
			$(".quantity-" + products[i].productId).val(1);
		}
		
		if (products[i].inStock == 0) {
			selector = "error";
			remove = "";
			var statusForPopupBlock = status = "<span class=\"error\">" + products[i].NoLabel + "</span>";
		}
		if(products[i].inStock < products[i].quantity) {
			selector = "error";
			remove = "";
			var statusForPopupBlock = status = "<span class=\"error\">" + products[i].IsLabel + "</span>";
		}
		else {
			statusForPopupBlock = selector = "";
			remove = "error";
			var status = products[i].YesLabel;
		}
		if (selector == "error") {
			$('#checkout').removeAttr("onclick").unbind('click');
		} else {
			$("#checkout").click(function(){
				$(Core.events.checkout).triggerHandler('click');
			});
		}
			
			$(".quantity-" + products[i].productId).addClass(selector).removeClass(remove);
			$("#korzyna #product-status-" + products[i].productId).html(status);
			$(".cart-product-quantity-input #product-status-" + products[i].productId).html(statusForPopupBlock);

				//$(".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" : "100%", "display" : "block"}';

			
		$(".price-without-discount-" + products[i].productId, ".cart-table").text(products[i].priceWithoutDiscount);
		$(".amount-without-discount-" + products[i].productId, ".cart-table").text(products[i].amountWithoutDiscount);
		$("#weight-" + products[i].productId, ".cart-table").text(products[i].weight);
	}
};


function _updateTotalValues(jsonTotalValues, jsonNoCheckout) {
	
	if(jsonTotalValues.totalquantity == 0){
		window.location = typeof(language) == 'undefined' ? '/' : language + '/';
	}

	$("#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 > 0){
		$('#cart-promotion').html(jsonTotalValues.promotion);
	}	
	$("#refresh-all").removeClass("update");
	checker(jsonNoCheckout);
}

function checker(jsonNoCheckout){
	$(".fail").remove();
	if($(".error").length > 0){
		$("#to_checkout").removeAttr("onclick").unbind('click') ;//unbind popup
		$("#to_checkout").bind("click", function(){
			$(".fail").remove();
			$(".bottom-buttons").after('<p class="fail"><b>' + jsonNoCheckout + '</b></p>');
		});
		$("#to_checkout").addClass("disabled");
		
	}else{
		$("#to_checkout").removeClass("disabled");
		path = typeof(language)  == 'undefined' ? "/delivery" : language + "/delivery";
		$("#to_checkout").removeAttr("onclick").unbind('click');
		if ($("#to_checkout").hasClass('logged')){
			$("#to_checkout").bind("click", function(){ window.location=path; });
		}else{
			$("#to_checkout").bind("click", function(){updatePopupCart();});
		}
	}
}

$(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");
    });
});

$(document).ready(function(){
	$(".delivery-by-my-self-text, .delivery-I-need-delivery-text").bind('click', function() {
		$(".delivery-by-my-self-body, .delivery-I-need-delivery-body").children('.collapsed-delivery').removeClass('selected');
		$(this).parent().children('.collapsed-delivery').addClass('selected');
	});
});
/*$(document).ready(function(){
	$("#deliveries .collapsible").bind('click', function() {
		updateTransport($("input",this).attr('checked', true).val());
		$("#to_checkout.button.logged.orange").addClass('active');
	});
});
/*$(document).ready(function(){
	$('#to_checkout.active-over').bind('click', function() {
		$(this).attr({'onclick' : '/ru/cart/payment'});
	});
});*/


