$(function() {

bindinputclick();

});

// Opdater kurven vha Ajax
function ReloadCartFromAjax() {
$.ajax({
	url: '/AjaxProxy.aspx?ajax-updatecart=1&random=' + Math.random(),
	cache: false,
	success: function(html){
	$("#cart-items").fadeOut("fast");
	$("#cart-items").html(html);
	$("#cart-items").fadeIn("fast");
	}
 });
}


// Foej varer til kurven fra produktliste
function AddToCartProductlist(itemid,amount) {
$.ajax({
	url: '/AjaxProxy.aspx?ajax-addtocart=1&productid=' + itemid + '&quantity=' + amount + '&random=' + Math.random(),
	cache: false,
	success: function(html){
		 
	//alert(html);
	$("#ajaxmsg_" + itemid).fadeIn("fast");
	$("#q" + itemid).val('1');	
	$("#ajaxmsg_" + itemid).html("<strong>" +amount+ "</strong>&nbsp;tilf&oslash;jet");
	ReloadCartFromAjax();
	return false;		
	}
});
}


// Steal regular click event from inputs
function bindinputclick(){
// tael opad ved click
$(".price input.qty").bind("click", function(){
	var itemid = this.id.replace("q", "");
	this.value = Number(this.value) + 1;
	
	if($("#ajaxmsg_" + itemid).is(":visible")){
			$("#ajaxmsg_" + itemid).fadeOut("fast");
	}
});
// add-to-cart click
$("td.price .add-to-cart").bind("click", function(){
	var itemid = encodeURI($(this).attr("id"));
	var amount = $("#q" + itemid).val();
	if (amount == 0){
		amount = 1;
	}
	AddToCartProductlist(itemid, amount);
	return false;
});
}