/* ================================================================ 
This copyright notice must be untouched at all times.

The original version of this script and the associated (x)html
is available at http://www.stunicholls.com/menu/tree_frog_vertical.html
Copyright (c) 2005-2007 Stu Nicholls. All rights reserved.
This script and the associated (x)html may be modified in any 
way to fit your requirements.
=================================================================== */


clickMenu = function(menu) {
	var getEls = document.getElementById(menu).getElementsByTagName("LI");
	var getAgn = getEls;
	var text, ID;
	
	try	{
		//read last-accessed-1st-level-category-ID cookie
		ID = readCookie("ID");
		//assign as clicked (open)
		getAgn[ID].className = "sub click";
	}
	catch(err){

	}	
	
	
	for (var i=0; i<getEls.length; i++) {
			getEls[i].onclick=function() {

				for (var x=0; x<getAgn.length; x++) {
					getAgn[x].className=getAgn[x].className.replace("unclick", "");
					getAgn[x].className=getAgn[x].className.replace("click", "unclick");	
					writeCookie("ID","",-1);
			    }
			    if ((this.className.indexOf('unclick'))!=-1) {
				  this.className=this.className.replace("unclick", "");
				}
				else {
				  for (var z=0; z<getAgn.length; z++) {
					getAgn[z].className=getAgn[z].className.replace("click", "");	
				  }
				  this.className+=" click";
				}
				
				
				for (var x=0; x<getAgn.length; x++) {
					
					if(getAgn[x].className.indexOf('click') >= 0){
						//alert(x);
						writeCookie(x);

						//break;
					}
			    }
				//alert(this.className);
			}			
			
			getEls[i].onmouseover=function() {
				this.className+=" hover";
			}
			getEls[i].onmouseout=function() {
				this.className=this.className.replace("hover", "");
			}
	}	
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function writeCookie(ID) {
		var d = new Date();
		d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
		document.cookie = "ID=" + ID + "; expires=" + d.toGMTString() + "; path=/";
}
