function euro(number)
{
	var numberStr = parseFloat(number).toFixed(2).toString();
	var numFormatDec = numberStr.slice(-2); /*decimal 00*/
	numberStr = numberStr.substring(0, numberStr.length-3); /*cut last 3 strings*/
	var numFormat = new Array;
	while (numberStr.length > 3) {
		numFormat.unshift(numberStr.slice(-3));
		numberStr = numberStr.substring(0, numberStr.length-3);
	}
	numFormat.unshift(numberStr);
	return '&euro;' + numFormat.join('.')+','+numFormatDec; /*format €000.000.000,00 */
}

function add2cart()
{

	var newHtml = '<li rel="'+$('#this_id').val()+'">';
	   newHtml += '<input type="hidden" class="price_unformatted" value="'+$('#this_price').val()+'" />';
	   newHtml += '<img class="remove" src="./'+themedir+'/images/remove.png" />';
	   newHtml += '<img class="product_list_img" style="height: 30px; width:30px; margin-top: 10px; float: left;" src="'+$('#product_image').attr('src')+'"/>';
	   newHtml += '<div class="cart_title" ><span>'+$('#this_title').val()+'<span></div>';
	   newHtml += '<div class="amount" >';
	   newHtml += 	'<span>'+lang_amount+':</span>';
	   newHtml += 	'<input type="text" rel="'+$('#this_id').val()+'" maxlength="1" class="amountcal" value="1" />';
	   newHtml += '</div>';
	   newHtml += '<span class="lbl_price" >'+lang_price+': </span>';
	   newHtml += '<span class="price" ></span>';
	   newHtml += '</li>';
	//Add an li to #cart
		if($('#cart li:first-child').length > 0)
		{
			$(newHtml).insertBefore('#cart li:first-child');
		}
		else
		{
			$('#cart').html(newHtml);
		}
		calc();

	//And slide the add div away
	$('#add2cart').css('visibility', 'hidden')
	//Now add is to the user session via ajax
	var id = $('#this_id').val();
	$.ajax({
			type: 'POST',
			url: 'ajax.php?action=add2session',
			data: 
			{
					id: id
			},
			success: function(r)
			{
				if(r != '') alert(r);
			}
		});
	//Bind functions:
	bindFunction();
	//Set BG:
	setBackground();
}
function calc()
{
	//Calculates the total price
	var total = 0;
	$('#cart li').each(function(){
		var rowtotal = ($(this).children('.price_unformatted').val()*$(this).children('div').children('input').val());
		total += rowtotal;
		$(this).children('.price').html(euro(rowtotal/100));
		//Align the title of the products in the cart, because css2 SUCKS!!
		//Get span heigt
		var span = $(this).children('.cart_title').children('span').height();
		//Calc margin
		var margin = (51-span)/2;
		//Set it
		$(this).children('.cart_title').css('margin-top', margin+'px');
		
	});
	total = total/100;
	//Now format it to a price
	$('#total').html(lang_total +': '+ euro(total));
}
function setBackground()
{
	//First count the items.
	var c = $("#cart li").length;
	$("#cart li").removeAttr('style'); //Remove all styles
	if(c == 1)
	{
	
		$("#cart li").css('background', "#EDEDF3 url('./templates/default/images/only_item_bg.png') no-repeat 0px 0px");
		//Check if the item has a buy-button
		if ($("#cart li:first-child #checkout").length == 0){
			//It doesn't, so add the total price + buy button
			$("#cart li:first-child").append('<span id="total" ></span><img src="./templates/default/images/checkout_'+language+'.png" onclick="window.location = \'http://www.utrechtstringquartet.com/webshop/checkout\'" id="checkout">');
			calc();
		}
	}
	else
	{
		//more then 1
		$("#cart li").css('background', "#EDEDF3");
		$("#cart li:last-child").css('background', "#EDEDF3 url('./templates/default/images/arrow_right.png') no-repeat 850px top !important");
		$("#cart li:first-child").css('background', "#EDEDF3 url('./templates/default/images/cart_background.png') no-repeat 0px 0px !important");
		if ($("#cart li:last-child #checkout").length == 0){
			//It doesn't, so add the total price + buy button
			$("#cart li:last-child").append('<span id="total" ></span><img src="./templates/default/images/checkout_'+language+'.png" onclick="window.location = \'http://www.utrechtstringquartet.com/webshop/checkout\'" id="checkout">');
			calc();
		}
	}

}
function remove(id)
{
	//var sure = confirm(lang_confirm_del);
	var sure = true;
		if(sure)
		{
			//Now parse to ajax.php
			$.ajax({
				type: 'POST',
				url: 'ajax.php?action=rm2session',
				data: 
				{
						id: id
				},
				success: function(r)
				{
					if(r != '')
					{ 
						alert(r);
					}
					else
					{
						$('li[rel="'+id+'"]').remove();
						//Execute function
						setBackground();
					}
					
					
				}
			});
		}
}
function update(id, value)
{
	$.ajax({
		type: 'POST',
		url: 'ajax.php?action=amount2session',
		data: 
		{
				id: id,
				amount: value
		},
		success: function(r)
		{
			if(r != '')
			{ 
				alert(r);
			}
		}
	});
	

}
function bindFunction()
{
	$('.amountcal').change(function(){
		var id = $(this).attr('rel')
		if($(this).val() == 0)
		{
			remove(id);
		}
		else
		{
			
			update(id, $(this).val());
		}
	
		calc();
	
	});
	
	$('.remove').click(function(){

		var id = $(this).parent('li').attr('rel');
		remove(id);		
	});
	
	$('.amountcal').numeric({ decimal: false, negative: false }, function() { alert("Positive integers only"); this.value = ""; this.focus(); });
	
	calc();
}
$(document).ready(function(){
	
	//Add the checkout button to the last row
	$('#cart li:last-child').append('<span id="total"></span>');
	$('#cart li:last-child').append('<img src="'+themedir +'images/checkout_'+language+'.png" id="checkout" onclick="window.location = \'http://www.utrechtstringquartet.com/webshop/checkout\'" />');
	bindFunction();
	if($('left').height() > $('desc').height())
	{
		
	}
});
