function rightclick(event) {
	alert('Het is niet toegestaan afbeeldingen te kopieren van deze site!');
	return(false);
}

function trim(value) {
	value = value.replace(/^\s+/,'');
	value = value.replace(/\s+$/,'');
	return value;
}

$(document).ready(function(){

    // debugging aid
    $.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
      alert(textStatus);
      alert(errorThrown);
      alert(XMLHttpRequest.responseText);
    }});

	// disable contextmenu
	document.body.oncontextmenu = rightclick;

	// png fix
	$.ifixpng("/style/img/pixel.gif");
	$('img[src$=".png"]').ifixpng();

	// window.open replacement
	$("a[rel=external]").click(function() {
		var link = $(this).attr('href');
		var title = $(this).attr('title');
		window.open(link, title, "menubar=0,resizable=1,width=460&height=340,scrollbars=1");
		return false;
	});

	// ajax busy screen
	$(document).ajaxStart(function(){
		$.blockUI();
	}).ajaxComplete(function(){
		$.unblockUI();
	});

	// fix the 'submit all buttons in a form' bug in IE6
	$("button").click(function(){
		var clickedButton = this;
		$(this).closest("form").find("button").not(clickedButton).attr("disabled","disabled");
	});

	// submit on click of a link
	$("a.inputLink").click(function(){
		$(this).closest("form").submit();
	});

	// arrange cartTable
	$(".cartTable tr").each(function() {
		var i = 0;
		$(this).children().each(function() {
			if(i == 0)
				$(this).css("text-align", "left");
			else
				$(this).css("text-align", "right");
			i++;
		});
	});
	$(".cartTable tr:nth-child(odd)").addClass("odd");

	$(".editAmountInput").blur(function(){
		$(this).parent().submit();
	});
	
	$(".p_basket .expand, .p_basket .baskAdd").hover(function(){
		$(this).css("cursor","pointer");
	},function(){
		$(this).css("cursor","auto");
	});

	// enable info expanding for webshop
	$(".p_basket .expand").toggle(function() {
		$(this).parent().siblings(".p_descLong").show().prev().hide().parent().removeClass("simple").addClass("expanded").yellowFade(1000);
	}, function() {
		$(this).parent().siblings(".p_descLong").hide().prev().show().parent().removeClass("expanded").addClass("simple").yellowFade(1000);
	});

	// subproduct handling
	$(".product.multiple select").change(function() {
		var element = $(this);
		var newId = $(this).find('option:selected').val();
		$.post('/index.php', {action: 'getPrice', id: newId}, function(data) {
			data = trim(data);
			if(data != "false" && !isNaN(data))
			{
				$(element).parent().siblings('.p_price').children('span').text(data).yellowFade(3000);
			}
		});
	});

	// adding to basket in shop
	$(".p_basket .baskAdd").click(function() {
		var item = $(this).parent().siblings().eq(0).text();
		var itemId = $(this).parent().parent().siblings().eq(0).val();
		var price = $(this).parent().prev().find('option:selected');
		if( !$(price).length )
			price = $(this).parent().prev().find('input');
		var priceId = $(price).val();
		var type = $(price).text();
		$("cartButton").click(function() {
			return false;
		});
		$.prompt("Item: '"+item+"', "+type+"<br />Aantal: <input type='text' name='quantity' id='baskQuantity' />", {
			overlayspeed: 0,
			promptspeed: 0,
			callback: function(v,m) {
				var quantity = $(m).children('#baskQuantity').val();
				if(v == true && isNaN(quantity) == false && parseInt(quantity) > 0) {
					var quantity = parseInt(quantity);
					$.post('/index.php', {action: "addToBasket", amount: quantity, productId: itemId, priceId: priceId}, function(reply) {
						$("cartButton").click(function() {
							return false;
						});
					});
				}
			},
			buttons: { Ok: true, Annuleren: false },
			focus: "#baskQuantity"
		});
	});

	$(".confirm").click(function() {
		if(!confirm("Weet u het zeker?"))
			return false;
	});
	
	

});

window.onresize = function(event) {
  // fix floating product stacking issue when actions are available
	if($("#shop .product").length > 0)
	{
    var shopWidth = $("#shop").width();
    var elementWidth = $("#shop .product:first").outerWidth(true);
    var amount = Math.floor(shopWidth / elementWidth);
    $("#shop .product").css("clear", "none");
    $("#shop .product:nth-child("+amount+"n+1)").css("clear", "left");
  }
}
