/*
####################################################################################################
# CART
# Version: 1.0.0 | Last Update: 26 October 2009 | Created On: 26 October 2009
####################################################################################################
*/

var cart =
{
	empty : true,
	commandURL : null,
	
	add : function (itemId, qty, optValue)
	{
		this.serverCommand ('add', [parseInt (itemId), (qty ? parseInt (qty) : 1), optValue]);
	},
	
	serverCommand : function (cmd, params)
	{
		if (!this.commandURL) return null;
		
		var URL = this.commandURL.replace (/{CMD}/, cmd);
		URL = URL.replace (/{PARAMS}/, params);
		
		this.loader.getURL (URL, null, [cmd, params]);
	},
	
	serverReturn : function (myBus)
	{
		if (myBus.returnData.text)
		{
			if (myBus.returnData.text == 'SUCCESS')
			{
				if (cart.empty)
				{
					cart.empty = false;
					cart.updateStamp ();
				}
				
				alert ('The item was successfully added to your shopping cart.');
			}
			else alert ('Error, item could not be added to shopping cart. Please try again.');
		}
	},
	
	updateStamp : function ()
	{
		//document.getElementById
		//this.empty
		var fullStamp = top.document.getElementById ('stamp_full');
		var emptyStamp = top.document.getElementById ('stamp_empty');
		
		if (this.empty)
		{
			fullStamp.style.display = 'none';
			emptyStamp.style.display = 'block';
		}
		else
		{
			fullStamp.style.display = 'block';
			emptyStamp.style.display = 'none';
		}
	},
	
	makeEmpty : function ()
	{
		this.empty = true;
		this.updateStamp ();
	},
	
	makeFull : function ()
	{
		this.empty = false;
		this.updateStamp ();
	}
}

cart.loader = new httpBus ('cart.loader', {callback : cart.serverReturn});

