// Event Handlers
Core.event.addToPreorderBegin = function (button, id) {
    var className = $(button).attr('class');
	if (className) {
		$(button).removeAttr('onclick').removeClass(className).addClass(className.replace("-over", "") + '-wait');
    }
}; 

Core.event.addToPreorderEnd = function (button, id, html, inpreorderButtonText, movetoPreorder, addedText) {
	$('.preorder-div').replaceWith(html);
	$('.header-cart.cart').addClass('auth-cart-preorder').removeClass('cart'); 
	$('.header-cart').addClass('auth-preorder');
	$('.header-cart.auth-cart').addClass('auth-cart-preorder').removeClass('auth-cart'); 
	$('.header-cart.auth').addClass('auth-preorder').removeClass('auth');
	modify_button_preorder (id, button, inpreorderButtonText, movetoPreorder, addedText);
}; 

Core.event.deleteFromPreorderEnd = function (sender, productId, totalValues) {
	if (totalValues.totalquantity == 0){
		_poupdateTotalValues(totalValues);
		$("#preorder-form, #preorder-valuable-buttons").remove();
		return;
	}
	$(".product-row-"+productId, ".cart-table").fadeOut("normal", function () {
		_poupdateTotalValues(totalValues);
	}).remove();
};

Core.event.refreshPreorderItemsBegin = function (sender, data) {
	$("#refresh-all").addClass("update");
};

Core.event.refreshPreorderItemsEnd = function (sender, obj) {
	_populateCartItems(obj.products);
	_poupdateTotalValues(obj.total);
};

// End Event Handlers


function _populateCartItems(products){
	var selector; var remove = ""; var status; var css; var expire;
	for(i=0; i<products.length; i++) {
		if (products[i].timeDiff < 0){
			expire = "error";
			status = "intime";
		}else{
			expire = "intime";
			status = "error";
		}
		if (products[i].inStock == 0) {
			selector = "error";
			remove = "";
		}
		if(products[i].inStock < products[i].quantity) {
			selector = "error";
			remove = "";
		}
		else {
			selector = "";
			remove = "error";
		}
			
			$("div.quantity-" + products[i].productId + ".preorder-instore").addClass(selector).removeClass(remove);
			$(".time-left-" + products[i].productId).addClass(expire).removeClass(status);
		
			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" : "inline"}';
			}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 ? "inline" : "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 ? "inline" : "none").css("width", products[i].discountAvailable ? "100%" : "0%");
		$(".amount-with-discount-" + products[i].productId, ".cart-table").text(products[i].amountWithDiscount);
		$(".expire-time-" + products[i].productId, ".cart-table").val(products[i].expire);
		$(".time-left-" + products[i].productId, ".cart-table").text(products[i].timeLeft);
	}
};


function _poupdateTotalValues(jsonTotalValues) {
	$("#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);
	if ( jsonTotalValues.totalquantity == 0 ){	// Change pre-order icon if empty 
		$('.header-cart.auth-preorder').removeClass('auth-preorder').addClass('auth');
		$('.header-cart.auth-preorder').removeClass('auth-cart-preorder').addClass('auth-cart');
	}	
	$('.preorder-div').replaceWith(jsonTotalValues.preorderBlock);
	$("#refresh-all").removeClass("update");
}


function modify_button_preorder(id, button, inpreorderButtonText, movetoPreorder, addedText) {
	className = $(button).attr('class');
	if (className) {
    	$(button).removeClass(className).addClass(className.replace("-over", "").replace("-wait", "") + '-inpreorder');
    }
	if (inpreorderButtonText && inpreorderButtonText != ''){
		if ($('.text',button).text()) {
			$('.text',button).text(inpreorderButtonText);
		}
		else {
			$(button).text(inpreorderButtonText);
		}
	}
	if (addedText && addedText != ''){
		if ((jQuery.trim(className) ==  'button preorder-wait') || (jQuery.trim(className) ==  'button preorder-wait-over')) {
	    	addedText = '<div style="height: 17px;">&nbsp;</div>' + addedText;
	    }
		$('#added_prod-' + id).html(addedText);
	}
	if (movetoPreorder){
		$(button).unbind("click").removeAttr("onclick"); // Unbind click to prevent repeated call AddToPreorder().
		$(button).click( function(){
			window.location = typeof(language) == 'undefined' ? '/advanceorder' : language + '/advanceorder';
		});
	}
}

$(document).ready(function(){
	if($(".datepick").length > 0){
		$(".datepick").datepick({dateFormat : 'dd.mm.yy'});
	}
});

