var activeMenuItem = new Array();

function isUlInArray(inputObj,ulObj){
	while(inputObj && inputObj.id!='dhtmlgoodies_listMenu'){
		if(inputObj==ulObj)return true;
		inputObj = inputObj.parentNode;
	}
	return false;
}

function showHideSub(e,inputObj)
{

	if(!inputObj)inputObj=this;
	var parentObj = inputObj.parentNode;
	var ul = parentObj.getElementsByTagName('UL')[0];
	if(activeMenuItem.length>0){
		for(var no=0;no<activeMenuItem.length;no++){
			if(!isUlInArray(ul,activeMenuItem[0]) && !isUlInArray(activeMenuItem[0],ul)){
				activeMenuItem[no].style.display='none';
				activeMenuItem.splice(no,1);
				no--;
			}
		}
	}
	if(ul.offsetHeight == 0){
		ul.style.display='block';
		activeMenuItem.push(ul);
		//SET COOKIE
		var now = new Date();
		fixDate(now);
		now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
		now = now.toGMTString();
		setCookie('Category', inputObj.id, now, '/');
	}else{
		ul.style.display='none';
	}
}

function showHidePath(inputObj)
{
	var startTag = inputObj;
	showHideSub(false,inputObj);
	inputObj = inputObj.parentNode;
	while(inputObj){
		inputObj = inputObj.parentNode;
		if(inputObj.tagName=='LI')showHideSub(false,inputObj.getElementsByTagName('A')[0]);
		if(inputObj.id=='dhtmlgoodies_listMenu')inputObj=false;	
	}
}

function initMenu()
{
	var obj = document.getElementById('dhtmlgoodies_listMenu');
	var linkCounter=0;
	var aTags = obj.getElementsByTagName('A');
	var activeMenuItem = false;
	var activeMenuLink = false;
	var thisLocationArray = location.href.split(/\//);
	var fileNameThis = thisLocationArray[thisLocationArray.length-1];
	if(fileNameThis.indexOf('?')>0)fileNameThis = fileNameThis.substr(0,fileNameThis.indexOf('?'));
	if(fileNameThis.indexOf('#')>0)fileNameThis = fileNameThis.substr(0,fileNameThis.indexOf('#'));

	for(var no=0;no<aTags.length;no++){
		var parent = aTags[no].parentNode;
		var subs = parent.getElementsByTagName('UL');
		if(subs.length>0){
			aTags[no].onclick = showHideSub;
			linkCounter++;
			aTags[no].id = 'aLink' + linkCounter;
		}

		if(aTags[no].href.indexOf(fileNameThis)>=0 && aTags[no].href.charAt(aTags[no].href.length-1)!='#'){
			if(aTags[no].parentNode.parentNode && aTags[no].parentNode.parentNode.id!='dhtmlgoodies_listMenu'){
				var parentObj = aTags[no].parentNode.parentNode.parentNode;
				var a = parentObj.getElementsByTagName('A')[0];
				if(a.id && !activeMenuLink){
					activeMenuLink = aTags[no];
					activeMenuItem = a.id;
				}
			}
		}
	}
	if(activeMenuLink){
		activeMenuLink.className='activeMenuLink';
	}
	activeMenuItem = getCookie('Category') ? getCookie('Category') : 'aLink1';
	//alert(activeMenuItem);
	if(activeMenuItem){
		if(document.getElementById(activeMenuItem))showHidePath(document.getElementById(activeMenuItem));
	}
}

function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

function setCookie (name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
		(path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
		//alert(curCookie);
	document.cookie = curCookie;
}

function getCookie (name) {
	var prefix = name + '=';
	var c = document.cookie;
	var nullstring = '';
	var cookieStartIndex = c.indexOf(prefix);
	if (cookieStartIndex == -1) return nullstring;
	var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1) cookieEndIndex = c.length;
	return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

