// when the page loads, transform all product img into draggable items
// and allow them to be dropped on the cart
window.onload = function() {
/*	// retrieve all product img
	var draggables = $$("div.productImg");
	// make them all draggable
	draggables.each(function(currentDraggable) { 
		new Draggable(currentDraggable, { revert: true, ghosting: true });												 
	});
	
	// define the cart as a droppable
	Droppables.add("cart", {
		hoverclass: "cartOnHover",
		onDrop: function(element) {
			// each product image has an id like 'product_x', where 'x' is the database id,
			// so we extract it next
			var itemId = element.id.split("_");
			addToCart(itemId[1]);
		}
	});
*/	
	// bind a progress indicator to all Ajax calls
	startAjax();
}

function startAjax()
{
	Ajax.Responders.register({
	  // when an Ajax request is started, show the indicator
		onCreate: function() {
		if (Ajax.activeRequestCount > 0)
		  Element.show($("indicator"));
	  },
		// when an Ajax request is finished, hide the indicator
		onComplete: function() {
		if (Ajax.activeRequestCount == 0)
		  Element.hide($("indicator"));
	  }
	});
}

// add item to cart
function addToCart(id)
{
	var qty = !isNaN(document.getElementById("amount_"+id).value) ? parseInt(document.getElementById("amount_"+id).value,10) : 0;
	var size = document.getElementById("size_"+id);
	if (!size) 
		size = getRadio("frmProduct_"+id,"size_"+id);
	else
		size = size.value;
	if (qty==0 )
		return false;
	else 
		document.getElementById("amount_"+id).value="01";
		
	new Ajax.Request("includes/ajax_cart/server.php?action=addToCart", {
		parameters: "id=" + id + "&qty=" + qty + "&size=" + size,
		onSuccess: function(resp) {
			var cartUpdate = eval('(' + resp.responseText + ')');
			var isNew = cartUpdate['cartItemDetails'][0].isNew;
			// if this item is new in the cart, inject it inside, otherwise update the qty and subtotal
			if (isNew == 1)
			{
				var newItem = '<li id="cartItem_' + id + "_" + size + '" style="display:none">';
				newItem += '<div class="cell1" id="cartItemQty_' + id + "_" + size + '" valign="top">'+qty+'</div>';
				newItem += '<div class="cell2">' + cartUpdate['cartItemDetails'][0].title + '</div>';
				newItem += '<div class="cell3" id="cartItemSize_' + id + '">' + cartUpdate['cartItemDetails'][0].size + '</div>';
				newItem += '<div class="cell4" id="cartItemPrice_' + id + "_" + size + '">' + cartUpdate['cartItemDetails'][0].newPrice + '</div>';
				newItem += '<div class="cell5"><a href="includes/ajax_cart/server.php?action=removeFromCart&id=' + id + '&size=' + size + '" onclick="return removeFromCart(' + id + ',' + size + ')"><img src="includes/ajax_cart/img/remove.gif"></a>';
				newItem += '</div><div class="clear"></div></li>';
				
				if ($("cartIsEmpty"))
					Element.hide($("cartIsEmpty"));

				new Insertion.Top("cartItems", newItem);
				
				Effect.Appear("cartItem_" + id + "_" + size, { duration: 0.5 });
				Element.update($("cartTotalAmount"), "Totaal: " + cartUpdate['cartItemDetails'][0].total);
				new Effect.Pulsate("cartTotalAmount", {duration: 2.5});
			}
			// so, the item already existed in the cart, therefore update its quantity and subtotal
			else
			{
				Element.update($("cartItemQty_" + id + "_" + size), cartUpdate['cartItemDetails'][0].newQty);
				Element.update($("cartItemPrice_" + id + "_" + size), cartUpdate['cartItemDetails'][0].newPrice);
				Element.update($("cartTotalAmount"), "Total: " + cartUpdate['cartItemDetails'][0].total);
//				new Effect.Pulsate("cartTotalAmount", {duration: 2} );
//				new Effect.Highlight("cartItemQty_" + id, {duration: 1, startcolor:'#E8BEBE', endcolor:'#FF0000', restorecolor:'#fff'});
//				new Effect.Highlight("cartItem_" + id, {duration: 0.5, startcolor:'#5cbc52', endcolor:'#96D38F', restorecolor:'#fff'});
			}
		}
	});
	return false;
}

// remove an item from the cart
function removeFromCart(id,size)
{
	new Ajax.Request("includes/ajax_cart/server.php?action=removeFromCart", {
		parameters: "id=" + id + "&size=" + size,
		onSuccess: function(resp) {
			eval(resp.responseText);
			if (qty < 1 ){
/*				// Fade out the last item in the cart and remove it
				Effect.Fade("cartItem_" + id, { 
				  afterFinish: function(effect) { Element.remove(effect.element); } } 
				); 
*/
				Element.remove($("cartItem_" + id + "_" + size));
			}
			if (items == 0)
			{
				// update the cart's total amount and contents
				Element.update($("cartItems"), '<div id="cartIsEmpty" class="row">'+document.getElementById('emptyTxt').value+'</div>');
				Element.update($("cartTotalAmount"), "Total: 0.00 Euro");
			}
			else
			{
				if(qty>0) Element.update($("cartItemQty_" + id + "_" + size), qty);
				if(qty>0) Element.update($("cartItemPrice_" + id + "_" + size), subtotal);
				// update the cart's total amount
				Element.update($("cartTotalAmount"), "Total: " + total);
			}
		}
	});
	return false;
}

// empty the cart
function emptyCart()
{
	new Ajax.Request("includes/ajax_cart/server.php?action=emptyCart", {
		onSuccess: function(resp) {
			if (resp.responseText == 1)
			{
				// update the cart's total amount and contents
				Element.update($("cartItems"), '<div id="cartIsEmpty" class="row">'+document.getElementById('emptyTxt').value+'</div>');
				Element.update($("cartTotalAmount"), "Total: 0.00");
			}
		}
	});
	return false;
}
