window.addEvent('load', function() 
{
	var drop = $('cart');
	if(drop)
		var dropFx = drop.effect('background-color', {wait: false}); // wait is needed so that to toggle the effect,
	//if(drop)
	$$('.item').each(function(item){
	 
		item.addEvent('mousedown', function(e) {
			e = new Event(e).stop();
	 
			var clone = this.clone()
				.setStyles(this.getCoordinates()) // this returns an object with left/top/bottom/right, so its perfect
				.setStyles({'opacity': 0.7, 'position': 'absolute'})
				.addEvent('emptydrop', function() {
					this.remove();
					drop.removeEvents();
				}).inject(document.body);
	 
			drop.addEvents({
				'drop': function() {
					drop.removeEvents();
					clone.remove();
					//alert();
					add_to_cart(item.getProperty('id').split('-')[1],item.getProperty('id').split('-')[2]);
					item.clone().inject(drop);
					dropFx.start('7389AE').chain(dropFx.start.pass('ffffff', dropFx));
					//alert(item.id);
				},
				'over': function() {
					dropFx.start('98B5C1');
				},
				'leave': function() {
					dropFx.start('ffffff');
				}
			});
	 
			var drag = clone.makeDraggable({
				droppables: [drop]
			}); // this returns the dragged element
	 
			drag.start(e); // start the event manual
		});
	 
	});
});
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
//
function add_to_cart(id,value)
{	var total = parseFloat($('total').getText()) + parseFloat(value);
	
	$('total').setText(CurrencyFormatted(total));
	new Ajax('actions.php',{	method: 'post',
								data:'action=add_to_cart&id='+id
							}).request();
}
function remove_from_cart(id,value)
{	var total = parseFloat($('total').getText()) - parseFloat(value);
	$('total').setText(CurrencyFormatted(total));
	new Ajax('actions.php',{	method: 'post',
								data:'action=remove_from_cart&id='+id
							}).request();
	
	return false;
}
