function disablePasting(e) {
	if ((e.ctrlKey || e.keyCode == 17) && (e.keyCode == 67 || e.keyCode == 86 || e.keyCode == 88)) {
		return false;
	}
}

function blinkMessage() {
	$("#blink").fadeIn(800, function (e) {
		$(this).fadeOut(800, blinkMessage);
	});
}

function hideAdditionalImage(imageID) {
	$("#" + imageID).hide();
}

function UpdateProductImage(sResizeImageURL, sImageURL) {
	$("#product_image").attr("src", sResizeImageURL);
	$("#product_image_link").attr("href", sImageURL);
	$("#product_image_link_enlarge").attr("href", sImageURL);
}


function Round(dNumber, iDecimals) {
	dNumber	= (Math.round(dNumber * Math.pow(10, iDecimals)) / Math.pow(10, iDecimals)).toString().replace(".", ",");
	var iDotIndex = dNumber.indexOf(",");	
	return iDotIndex === -1 ? dNumber + ",00" : iDotIndex === dNumber.length - 2 ? dNumber + "0" : dNumber;
}


/** Cart functions */
function CheckShippingOptions(bIsPost) {
	if (bIsPost) {
		$(".upsaleProduct").show();
	} else {
		$(".upsaleProduct").hide();			
	}
	
	$("[name^=chkUpsaleId]").each(function () {
		RecalculateTotal($(this), false);
	});
}

function RecalculateTotal($el, bUpdateTotal) {
	var bIsChecked = $el.is(":checked");
	if (bIsChecked) {
		$el.closest(".tblUpsaleProduct").find(".upsaleProductPrice").show();
	} else {
		$el.closest(".tblUpsaleProduct").find(".upsaleProductPrice").hide();
	}
	
	if (bUpdateTotal) {
		$("#cart_total_price").html("&euro; " + Round( parseFloat($("#ordertotal").val()) + (parseFloat($el.next("[name^=dUpsalePrice]").val()) * (bIsChecked ? 1 : -1)), 2));
	}
}

jQuery(document).ready(function($) {
	/** Disable the context menu for images */
	$("img").live("contextmenu", function () { return false; });
	
	$("#homepagePriceSelect,#drpCategories").change(function () {
		var sSelectedValue = this.options[this.selectedIndex].value.toString();
		if (sSelectedValue.length > 0) {
			window.location = sSelectedValue;
		}
	});
	
	/** Disable pasting from password field */
	$("#passwordLogon, #password, #passwordRepeat")
		.bind("contextmenu", function(e) { return false; })
		.bind("keydown", disablePasting)
		.bind("keyup", disablePasting);

	/** Usability for newsletter signup field */
	if ($("#aanmeldVeld").size() > 0) {
		var sSubscribeEmail = "Uw e-mailadres";

		if ($("#aanmeldVeld").val().length === 0) {
			$("#aanmeldVeld").val(sSubscribeEmail).css("color", "#999999");
		} else {
			$("#aanmeldVeld").css("color", "#000000");
		}
		
		$("#aanmeldVeld").focus(function (e) {
			var sCurrentValue = this.value;
			this.value = sCurrentValue === sSubscribeEmail ? "" : sCurrentValue;
			$(this).css("color", (this.value === sSubscribeEmail ? "#999999" : "#000000"));
		}).blur(function (e) {
			var sCurrentValue = this.value;
			this.value = sCurrentValue.length === 0 ? sSubscribeEmail : sCurrentValue;
			$(this).css("color", (sCurrentValue.length === 0 ? "#999999" : "#000000"));
		});
	}

	/** Blink info message that has the right ID */
	blinkMessage();
	
	/** Left menu toggling */
	$(".categories_y ul").hide();
	$(".categories_y ul:has(.active)").show();

	$(".categories_y > li > a").each(function (index, elem) {
		$(elem).parent().removeClass("active");
	}).click(function (e) {
		// First hide every list.
		$(".categories_y > li").removeClass("active");
		$(".categories_y ul").hide();
		
		// Only show the clicked list.
		$(this).parent().addClass("active");
		$(this).parent().find("ul").show();
		return false;
	});
	
	/* Cart functions */
	$("[name^=chkUpsaleId]").click( function() {
		RecalculateTotal($(this), true);
	});
	
	CheckShippingOptions($("#shippingTypePost").is(":checked"));
	
	/*$('[rel=shippingMethod]').click( function() {
		CheckShippingOptions( $(this).val() );
	});*/
});
