var LeftMenu = {
	_cookieOptions : {name:"leftMenu"},

	_openCategories : new Array(),		//Array of open category id's

	/*
	 *_load function.
	 *This function is called on the window.onload event and grabs the open category
	 *list from the leftMenu cookie.
	 */
	_load : function(){
		this._openCategories = this._getFromCookie().split(/xx/g);

		/*
		 *NOTE: PHP will be checking this cookie so that when we write the left menu it will already
		 *be displayed appropriately.  Therefore, we need to load the cookie to see what is already open,
		 *but we don't need to open anything.
		 */
	},

	/*
	 *openCategory function.
	 *This function opens a category, aka displays the sub menu and appropiate image.
	 *
	 *@param	string	category	ID of category to open
	 */
	openCategory : function(category){
		//Get subcategory menu and image nodes
		var subCategories = _ge("category_level_0_" + category).getElementsByTagName("ul")[0];
		var images = _ge("category_level_0_" + category).getElementsByTagName("img");

		if(images[0].getAttribute("name") == "downArrow"){
			var downArrow = images[0];
			var rightArrow = images[1];
		}else{
			var downArrow = images[1];
			var rightArrow = images[0];
		}

		//Toggle elements
		Element.show(subCategories);
		Element.hide(rightArrow);
		Element.show(downArrow);

		this._openCategories.push(category);
	},

	/*
	 *closeCategory function.
	 *This function close a category, aka hides the sub menu and appropiate image.
	 *
	 *@param	string	category	ID of category to close
	 */
	closeCategory : function(category){
		//Get subcategory menu and image nodes
		var subCategories = _ge("category_level_0_" + category).getElementsByTagName("ul")[0];
		var images = _ge("category_level_0_" + category).getElementsByTagName("img");

		if(images[0].getAttribute("name") == "downArrow"){
			var downArrow = images[0];
			var rightArrow = images[1];
		}else{
			var downArrow = images[1];
			var rightArrow = images[0];
		}

		//Toggle elements
		Element.hide(subCategories);
		Element.hide(downArrow);
		Element.show(rightArrow);

		this._openCategories = this._openCategories.without(category);
	},

	/*
	 *toggleCategory function.
	 *This function toggles a category, aka if it is open, it closes it and vice versa.
	 *It also saves the content of the open categories array to a cookie.
	 *
	 *@param	string	category	ID of category to toggle
	 */
	toggleCategory : function(category){
		if(this._openCategories.include(category)){
			this.closeCategory(category);
		}else{
			this.openCategory(category);
		}

		this._saveToCookie();
	},

	_saveToCookie : function(){
		setCookie(this._cookieOptions.name, this._openCategories.join("xx"), false, "/");
	},

	_getFromCookie : function(){
		var cookieVal = getCookie(this._cookieOptions.name);

		return cookieVal || "";
	}
}

//Setup event handler
Event.observe(window, "load", LeftMenu._load.bindAsEventListener(LeftMenu));
