/**
 * This is the shopping cart that holds the selections of the user
 */
var ShoppingCart = 
{
	STATUS_ADD : "added",
	STATUS_DELETED : "deleted",
	CART_COOKIE : "shopping_cart_items",
	
	cartDiv : null,
	itemList : null,
	/**
	 * Initializes the shopping cart
	 */
	init : function()
	{
		this.itemList = {};
		if (this.loadCart() == false)
		{
			alert("Please enable cookies in your browser to allow access to Shopping Cart");
		}
	},
	
	/**
	 * Adds an item to the shopping cart
	 * @param {Array} itemId
	 */
	addItems: function(items)
	{
		if (typeof(items) == "string")
		{
			items = [items];
		}
		
		// adding only 10 items at the MAX due to Amazon service
		var itemCount = 0;
		for (itemName in this.itemList)
		{
			itemCount++
		}
		if (itemCount >= 10)
		{
			alert("You can add only 10 book titles at a time");
			return; 
		}
		
		for (var i = 0; i < items.length; i++)
		{
			this.itemList[items[i]] = ShoppingCart.STATUS_ADD;
			this.showAsAdded(items[i]);
		}
		this.persistCart();
	},

	/**
	 * Changes the add to cart button to show added 
	 * @param {Object} item
	 */	
	showAsAdded : function(item)
	{
		var button = document.getElementById("cartButton" + item);
		if (button)
		{
			button.href = "#";
			button.style.cursor = "default";
			button.innerHTML = '<span class="button">Added</span>';
		}
	},
	
	/**
	 * Deletes a list of items from the 
	 * shopping cart
	 * @param {Array} itemList
	 */
	deleteItems : function(items)
	{
		if (typeof(items) == "string")
		{
			items = [items];
		}

		for (var i = 0; i < items.length; i++)
		{
			this.itemList[items[i]] = ShoppingCart.STATUS_DELETED;
		}
	},
	
	/**
	 * Persists the cart in cookies
	 */
	persistCart : function()
	{
		YAHOO.log("Persisting the shopping cart to the cookies");
		var cartString = ShoppingCart.CART_COOKIE + "=";
		for (itemId in this.itemList)
		{
			if (this.itemList[itemId] == ShoppingCart.STATUS_ADD)
			{
				cartString += itemId + ",";
			}
		}
		document.cookie = cartString;
	},

	/**
	 * Redirect to the shopping cart page
	 */
	showCart : function()
	{
		this.loadCart();
		var isEmpty = true;
		var shopUrl = "/pustak/books/order?";
		for (x in this.itemList)
		{
			if (this.itemList[x] == ShoppingCart.STATUS_ADD)
			{
				isEmpty = false;
				shopUrl += "&bookId=" + x;
			}
		}
		
		if (isEmpty == true)
		{
			if (document.getElementById("cartDiv"))
			{ 
				document.getElementById("cartDiv").innerHTML = "No Items in Shopping Cart";
			}
			else
			{
				alert("No Items in Shopping Cart");
			}
		}
		else
		{
			document.location = shopUrl;
		}	
	},

	/**
	 * Tells us if an item is in a cart of Not
	 * @param {Object} item
	 */
	isInCart :function(item)
	{
		if (this.itemList[item] == ShoppingCart.STATUS_ADD)
		{
			return true;
		}
		else
		{
			return false;	
		}
	},
	
	/**
	 * Loads the cart from the page
	 */
	loadCart : function()
	{
		if (typeof(document.cookie) == "undefined")
		{
			return false;
		}
		var cookieList = document.cookie.split(";");
		YAHOO.log("Loading items from the cart");
		for (var i = 0; i < cookieList.length; i++)
		{
			var key = cookieList[i].substr(0,cookieList[i].indexOf("="));
			if (key.indexOf(ShoppingCart.CART_COOKIE) != -1)
			{
				key = cookieList[i].substr(cookieList[i].indexOf("=") + 1).split(",");
				for (var i = 0; i < key.length; i++)
				{
					if (key[i] != "")
					{
						this.itemList[key[i]] = ShoppingCart.STATUS_ADD;
					}
				}
			}
		}
	}
}

ShoppingCart.init();