var ie5 = (document.all && document.getElementById) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;

var cMenu = false, selectedObj = null, selectedSec = null, strPaths = false, iPath = 1;
var fromOnLoad = false, currentlyLoading = false, strUrl = false;

function getChildren(path,strOnLoad){

	if(!path) return;
	if (strOnLoad) fromOnLoad = true;
	else fromOnLoad = false;
	insertLoadingDiv(selectedObj);
	if(document.getElementById("loaderFrame").getAttribute("debug")) var debugMode = "&dbg=on";
	else var debugMode = "";
	if(strUrl){
		window.status = strUrl;
		if(document.frames) document.frames["loaderFrame"].location = strUrl
		else document.getElementById("loaderFrame").src = strUrl;
	}
	else{
		if(document.frames) document.frames["loaderFrame"].location = "./getdescendants.asp?path=" + path;
		else document.getElementById("loaderFrame").src = "./getdescendants.asp?path=" + path;	
	}
	currentlyLoading = true;
}

function insertLoadingDiv(obj){
	objLoading = document.getElementById("loadingDiv").childNodes[0];
	objLoadingClone = objLoading.cloneNode(true);
	selectedObj.style.cursor = "wait";
	//alert(objLoading.childNodes[0]);
	//window.top.status = objLoading.childNodes[0].nodeValue;
	objLoadingClone.style.display = "block";
	obj.appendChild(objLoadingClone);
}

function mouseClick(obj){
	if( cMenu ) hideCMenu(); 
	if(currentlyLoading) return;
	selectedObj = obj;
	if(obj.childNodes[2]) toggleChild(obj.childNodes[2], obj.childNodes[0]);
	else if(obj.getAttribute("child") == "true"){
		getChildren(obj.getAttribute("path"), true);
	}
	if(obj.className == "main"){
		if(selectedSec != null) unSelectSec(selectedSec);
		//obj.style.background = "#ededed";
		//obj.style.color = "#ff0000";
		selectedSec = obj;
		selectedObj = obj;
	}
	if(window.event){window.event.cancelBubble = true;}
}

function clickAndGo(obj){
	obj.childNodes(1).focus;
	mouseClick(obj);
	//window.top.mainWin.location = obj.childNodes(1).href;
}

function toggleChild(objDiv,objImg){
	if( objDiv.style.display == "block" || objDiv.style.display == "" ) {
		objDiv.style.display = "none";
		objImg.src = "./images/menuPlus.gif"
	}
	else {
		objDiv.style.display = "block";
		objImg.src = "./images/menuMinus.gif"
	}
}

function selectObj(obj){
	if(selectedObj != null){
	//	if(ie5) obj.childNodes[1].focus();
		selectedObj = obj;
	}
	if(obj.className == "main"){
		if(selectedSec != null) unSelectSec(selectedSec);
		obj.style.background = "#ededed";
		obj.style.color = "#ff0000";
		if(ie5) obj.childNodes[1].focus();
		selectedSec = obj;
		selectedObj = obj;
	}
	else{
		obj.style.color = "#ff0000";
		if(ie5) obj.childNodes[1].focus();
		selectedObj = obj;
	}
}

function unSelectSec(obj){
	obj.style.background = "transparent";
	obj.style.color = "#000000";
	if(obj.childNodes[2] && obj != selectedObj) obj.childNodes[2].style.display = "none";
	if(obj.childNodes[2] && obj != selectedObj) obj.childNodes[0].src = "./images/menuPlus.gif"
}

function getPath(cPath){
	var path = strPaths.split(",")
	var objID = "id" + cPath;

	if(document.getElementById(objID)){
		tmpObj = document.getElementById(objID);
		selectObj(tmpObj);
		if(tmpObj.getAttribute("child") != "false") {
			if(tmpObj.childNodes[2]) return;
			getChildren(cPath,true);
		}
		else if(path[1]) getMore(path[1]);
	}
}


function insertChildren(){
	currentlyLoading = false;
	if(document.frames) objDoc = document.frames["loaderFrame"].document;
	else objDoc = document.getElementById("loaderFrame").contentDocument;

	childCount = objDoc.getElementsByTagName("div");

	obj = selectedObj;
	if(!obj) return;

	if(childCount == 0){
		loadingNode = obj.lastChild;
		obj.removeChild(loadingNode);
		obj.setAttribute("child", "false");
		obj.style.cursor = "hand";
		window.parent.status = ""
		return;
	}

	obj.lastChild.innerHTML = objDoc.body.innerHTML;
	
	obj.childNodes[0].src = "./images/menuMinus.gif";
	obj.style.cursor = "hand";
	window.parent.status = "";
	if(strPaths && fromOnLoad) getMore(obj.getAttribute("path"));
}

function getMore(cPath){
	Paths = strPaths.split(",");
	if(iPath >= Paths.length){
		selectedObj.childNodes[1].focus();
		return;
	}
	var objID = "id" + Paths[iPath];
	tmpObj = document.getElementById(objID);
	selectObj(tmpObj);
	if(selectedObj.getAttribute("child") != "false")
		getChildren(Paths[iPath], true);
	iPath++;
}

document.onkeydown = setKeys;
