var subMenus = new Array;
function getElementsByClassName( strClassName, obj ) {
    var ar = arguments[2] || new Array();
    var re = new RegExp("\\b" + strClassName + "\\b", "g");

    if ( re.test(obj.className) ) {
        ar.push( obj );
    }
    for ( var i = 0; i < obj.childNodes.length; i++ )
        getElementsByClassName( strClassName, obj.childNodes[i], ar );
    
    return ar;
}

function retrieveCookie( cookieName ) {
	/* retrieved in the format
	cookieName4=value; cookieName3=value; cookieName2=value; cookieName1=value
	only cookies for this domain and path will be retrieved */
	var cookieJar = document.cookie.split( "; " );
	for( var x = 0; x < cookieJar.length; x++ ) {
		var oneCookie = cookieJar[x].split( "=" );
		if( oneCookie[0] == escape( cookieName ) ) { return unescape( oneCookie[1] ); }
	}
	return null;
}

function setCookie( cookieName, cookieValue, lifeTime, path, domain, isSecure ) {
	if( !cookieName ) { return false; }
	if( lifeTime == "delete" ) { lifeTime = -10; } //this is in the past. Expires immediately.
	/* This next line sets the cookie but does not overwrite other cookies.
	syntax: cookieName=cookieValue[;expires=dataAsString[;path=pathAsString[;domain=domainAsString[;secure]]]]
	Because of the way that document.cookie behaves, writing this here is equivalent to writing
	document.cookie = whatIAmWritingNow + "; " + document.cookie; */
	document.cookie = escape( cookieName ) + "=" + escape( cookieValue ) +
		( lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() : "" ) +
		( path ? ";path=" + path : "") + ( domain ? ";domain=" + domain : "") + 
		( isSecure ? ";secure" : "");
	//check if the cookie has been set/deleted as required
	if( lifeTime < 0 ) { if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return false; } return true; }
	if( typeof( retrieveCookie( cookieName ) ) == "string" ) { return true; } return false;
}

window.onload = function() {
	var menus = getElementsByClassName('clkMenu', document.body);
	var currentHost = window.location.host;
	var arrHost = currentHost.split("\.");
	var toPosition = arrHost.length-3;
	var topHost = new Array;
	for(var i=arrHost.length-1; i > toPosition; --i) {
		topHost.unshift(arrHost[i]);
	}
	var finalHost = topHost.join(".");
	
	for( var a = 0; a < menus.length; a++ ) {
		var menu = menus[a];
		var items = menu.childNodes;
		for( var b = 0; b < items.length; b++ ) {
			var item = items[b];
			var bits = item.childNodes;
			if( bits.length > 1 ) {
				item.className = item.className+" hasSubMenu"
				for( var c = 0; c < bits.length; c++ ) {
					var me = bits[c];
					subMenus.push(me);
					var nn   = me.tagName;
					if( nn == "undefined") {	} else // ignore
					if( nn == "A"  &&  me.id != "void") {
						var bikkit = retrieveCookie( 'clkmenuopen'+b );
						if( bikkit == b ) {
							me.className = me.className+" subMenuLinkOpen";
						} else {
							me.className = me.className+" subMenuLinkClosed";
						}
						me.href = "javascript:void(0);";
						me.name = b;
						me.onclick = function() {
							for(var o=0; o<subMenus.length; ++o) {
								var bit = subMenus[o];
								var n=bit.name;
								setCookie('clkmenuopen'+bit.name, '', -3600, '/', finalHost);
							}
							
							var parentkids = this.parentNode.childNodes;
							for( var n = 0; n < parentkids.length; n++ ) {
								var pk = parentkids[n];
								var nn = pk.tagName;
								if( nn != null && nn != "A" ) {
									var submen = pk;
								}
							}
							var submenu = submen.style.display;
							if( submenu == "none" ) {
								this.className = this.className.replace(/subMenuLinkClosed/, "subMenuLinkOpen");
								submen.style.display = "block";
								setCookie('clkmenuopen'+this.name, this.name, null, '/', finalHost);
							} else {
								this.className = this.className.replace(/subMenuLinkOpen/, "subMenuLinkClosed");
								submen.style.display = "none";
								setCookie('clkmenuopen'+this.name, '', null, '/', finalHost);
							}
						}
					} else if( nn != null ) {
						me.className = me.className+" subMenu";
						var bikkit = retrieveCookie( 'clkmenuopen'+b );
						if( bikkit == b ) {
							me.style.display = "block";
						} else {
							me.style.display = "none";					
						}
					}
				}
			}
		}
	}
}

