/*
 * Nijhof & Lee 2.0
 * Copyright 2009, Arjen Tempel / CSBTemel.nl 
 */

function replace_order_buttons(ean){
	//$("#ean"  + ean + " :submit").replaceWith("<a href=\"#\" class=\"orderbutton\" onClick=\"order('" + ean + "'); return(false);\"></a>");
}
function order(ean){
	// Toggle article $ean in shopping basket
	 $.ajax({
		   type: "POST",
		   url: "/basket/toggle",
		   data: "ean=" + ean,
		   success: function(msg){
		     alert( "Data Saved: " + msg );
		   }
		 });
}
function form_button_mouseovers(id){
	$("#" + id + " input:image").hover(function(){
		src = $(this).attr("src");
		len = src.length - 7;
		if (src.substr(len)=="off.png"){
			$(this).attr("src", src.substr(0,len) + "on.png");
		}
	}, function(){
		src = $(this).attr("src");
		len = src.length - 6;
		if (src.substr(len)=="on.png"){
			$(this).attr("src", src.substr(0,len) + "off.png");
		}
	});
}
function add_ajax_order_buttons(id){
	//alert($("#"  + id + " input:image").length);
	$("#"  + id + " input:image").bind('click',function(){
		$(this).blur();
		ean = $(this).attr('id');
		ean = ean.substr(6,13);
		basket_toggle(ean);
		return false;
	});
}


function add_ajax_update_buttons(){
	//alert($("#"  + id + " input:image").length);
	$("td.basket_updown input:image").bind('click',function(){
		$(this).blur();
		form = $(this).parent(); 
		ean = form.children(':input').val();
		count = form.children(':input').next().val();
		basket_update(ean, count);
		return false;
	});
}


function set_order_button(ean, image){
	//alert($("#"  + id + " input:image").length);
	src	= $("#order_"  + ean ).attr("src");
	if (src){
		if (src.substr(-7) == "_on.png"){
			$("#order_"  + ean ).attr("src", "/img/" + image + "_on.png");
		} else {
			$("#order_"  + ean ).attr("src", "/img/" + image + "_off.png");
		}
	}
}

var timeoutID = 0;

function basket_toggle(ean){
	// Toggle article $ean in shopping basket
	src	= $("#order_"  + ean ).attr("src");
	if (src.substr(5,5) == "order"){
		$("#order_"  + ean ).attr("src", "/img/ordering.gif");
	} else {
		$("#order_"  + ean ).attr("src", "/img/deleting.gif");	
	}
	$.ajax({
		   type: "POST",
		   url: "/basket/toggle/format/json",
		   data: "ean=" + ean,
		   dataType: "json",
		   success: function(data, textStatus){
		     $("#basketcontent").html( data.message );	
			 set_order_button(ean, data.image);
			 if (timeoutID){
				 clearTimeout(timeoutID);
			 }
			 timeoutID = setTimeout('basketcontents()', 2000);
		   }
		 });
}

function basket_update(ean, count){
	// Toggle article $ean in shopping basket
	 $.ajax({
			type: "POST",
			url: "/basket/update/format/json",
			data: "ean=" + ean + "&count=" + count, 
			dataType: "json",
			success: function(data, textStatus){
				$("#count_" + ean).text(count);
				$("#plus_" + ean).children().next().val(parseInt(count) + 1);
				$("#min_" + ean).children().next().val(parseInt(count) - 1);
				total = parseFloat($("#basket_total").text());
				oldsubtotal = parseFloat($("#subtotal_" + ean).text());
				subtotal = parseFloat($("#price_" + ean).text()) * count;
				newtotal = total - oldsubtotal + subtotal;
				$("#subtotal_"+ean).text(subtotal.toFixed(2));
				$("#basket_total").text(newtotal.toFixed(2));
				basketcontents();
				if (parseInt(count) == 0) {
					$("#min_" + ean ).hide();			
					$("#euro_" + ean ).hide();
					$("#subtotal_" + ean ).hide();			
					$("#tr1_" + ean ).addClass('basket_nocopies');
					$("#tr2_" + ean ).addClass('basket_nocopies');
				} else {
					$("#min_" + ean ).show();
					$("#euro_" + ean ).show();
					$("#subtotal_" + ean ).show();
					$("#tr1_" + ean ).removeClass('basket_nocopies');
					$("#tr2_" + ean ).removeClass('basket_nocopies');
				}
		  }
		 });
}
