var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1));
var is_ie   = (agt.indexOf("msie") != -1);
var is_nav5up = (is_nav && (is_major >= 5));

function createCSS(){
	var h=0, w=0, counter =0, layerid, top = 1, left = 1, color, topAbsolute, leftAbsolute;
	document.write("<style>");
	document.write("#background {position: absolute; top: 90px; left: 0px; width: 100%; height: 400px; clip: rect(0px 1000px 500px 0px);}");
	h = calHeight(root);
	w = rootW + 2;
	writeCSS("rootB", menuTop, menuLeft, w, h, menuBodercolor, 0);

	w = w -2;
	for (x=0; x<root.length; x+=7){
		eval("layerid = 'rootI" + counter + "';");
		
		if (root[x+2]==0)
			h = menuHeight;
		else
			h = root[x+2];
		if (root[x+3]==0)
			color = menuBGcolor;
		else
			color = root[x+3];
		writeCSS(layerid, top, left, w, h, color, 0);
		if (root[x+5] == 1){
			topAbsolute = menuTop + top - 1;
			leftAbsolute = menuLeft + rootW + menuIndent;
			createSubCSS(eval("'roots" + counter + "'"), topAbsolute, leftAbsolute);
		}
		top += h + 1;
		counter++;
	}
	document.write(".text {font-size:11px; font-family:Verdona, Arial; text-decoration:none; color:navy; layer-background-color:transparent; background-color:transparent;}");
	document.write("</style>");
}

function createSubCSS(name, t, l){
	var h=0, w=0, counter =0, layerid, top = 1, left = 1, color,  topAbsolute, leftAbsolute, arrayLength;
	var menuName = eval(name);
	w = eval(name + "W") + 2;
	h = calHeight(menuName);
	writeCSS((name + "B"), t, l, w, h, menuBodercolor, 2);

	w = w -2;
	eval("arrayLength = " + name + ".length;");
	var x1 = 0;
	for (x1=0; x1<arrayLength; x1+=7){
		eval("layerid = '" + name + "I" + counter + "';");
		
		if (menuName[x1+2]==0)
			h = menuHeight;
		else
			h = menuName[x1+2];
		if (menuName[x1+3]==0)
			color = menuBGcolor;
		else
			color = menuName[x1+3];
		writeCSS(layerid, top, left, w, h, color, 1);
		if (menuName[x1+5] == 1){
			topAbsolute = t + top -1;
			leftAbsolute = l + w + menuIndent;
			createSubCSS(eval("'" + name + "s" + counter + "';"), topAbsolute, leftAbsolute);
		}
		top += h +1;
		counter++;
	}
	return;	
}

function calHeight(menuName){
	var total = 0;
	for (x2=0; x2<menuName.length; x2+=7){
		if (menuName[x2+2] == 0)
			total += menuHeight;
		else
			total += menuName[x2+2];
	}
	total += menuName.length/7 + 1;
	return total;
}

var V = new Array("visible", "inherit", "hidden");
function writeCSS(layerid, top, left, width, height, color, index){
	document.write("#" + layerid + " {position: absolute; top:"+ top+ "px; left: "+ left + "px; width:" + width + "px; height:" + height + "px; clip:rect(0px " + width + "px " + height + "px 0px); layer-background-color:" + color + "; background-color: " + color + "; visibility: " + V[index]+ ";}\n");
}

function writeDiv(){
	var layerN, link, counter=0, childrenID;

	document.write("<div id='background'>&nbsp;</div>");
	var Main = new CreateLayer("background", "");
	Main.layer.onmouseover = HideAll;
	for (x=0; x<rootDiv.length; x++){
		document.write("<div id='" + rootDiv[x] + "B'>\n");
		makeLayerID(eval("'" + rootDiv[x]+ "B'"), "", "", "", -1);
		counter = 0;
		var menuName = eval(rootDiv[x]);
		for (y=0; y<menuName.length; y+=7){
			layerN = rootDiv[x] + "I" + eval("'" + counter + "'");
			if (menuName[y+1] != "")
				link = "href='" + menuName[y+1] + "'";
			document.write("<div id='" + layerN + "'><a "+ link +"class='text'>&nbsp;&nbsp;" + menuName[y] + "</a></div>\n");
			makeLayerID(layerN, eval("'" + rootDiv[x] + "B'"), menuName, y, 0);
			counter++;
			link = "";
		}
		document.write("</div>\n\n");
	}
	createLayerInfoID();
}

var layerID;
function makeLayerID(id, rootid, menuName, index, isNotBGlayer) {   // -1: no subroot (hasSubroot)
	layerID = "L" + id;
	eval(layerID + "= new CreateLayer('" + id + "','" + rootid + "');");
	if (isNotBGlayer != -1){
		var LID = eval(layerID);
		LID.id = "L" + id;

		LID.layerInfo = menuName[index+6];
		if (menuName[index+3] != 0)
			LID.BGcolor = menuName[index+3];
		else
			LID.BGcolor = menuBGcolor;

		if (menuName[index+4] != 0)
			LID.OBGcolor = menuName[index+4];
		else
			LID.OBGcolor = menuOBGcolor;

		if (menuName[index+5] == 1)
			LID.children = "L" + rootid.split("B")[0] + "s" + eval("'"+(index/7)+"'") + "B";
		else
			LID.children = 0;
	
		var level = LID.id.split("s");
		LID.level = level.length - 1;

		// 0 => onmouseout  1 => onmouseover
		LID.layer.onmouseover = new Function("ChangeColor('" + LID.id + "', 1)");
		LID.layer.onmouseout = new  Function("ChangeColor('" + LID.id + "', 0)");
	}
}

var bgC = ".backgroundColor";
if (is_nav && !is_nav5up)
	bgC = ".document.bgColor";
function ChangeColor(id, type){
	var layerID = eval(id);
	if (type == 1){
		eval("layerID.css" + bgC + "='" + layerID.OBGcolor + "';");
		HideShowLayer(layerID, 1);
	}
	else{
		eval("layerID.css" + bgC + "='" + layerID.BGcolor + "';");
		HideShowLayer(layerID, 0);
	}
}

var track = new Array();
for (x= 0; x<menuLevel-1; x++){
	track[x] = "";
}
var layerInfo = "";
function HideShowLayer(layerID, l){
	hideCondition = false;
	if (l == 1)
		hidePower = false;
	else
		hidePower = true;		

	for (x=menuLevel-2; x>=layerID.level; x--){
		if (track[x] != "")
			eval(track[x] + ".css.visibility = 'hidden'");
	}
	if (layerID.children != 0){
		eval(layerID.children + ".css.visibility = 'visible'");
		track[layerID.level] = layerID.children;
	}
	if (layerID.layerInfo != 0 && l == 1 && layerID.layerInfo != layerInfo){
		if (layerInfo != "")
			eval("L" + layerInfo + ".css.visibility = 'hidden'");
		eval("L" + layerID.layerInfo + ".css.visibility = 'visible'");
		layerInfo = layerID.layerInfo;
	}
}

var hideCondition = false;
var hidePower = true;
var IDset = false;
function HideAll(){
	if (hidePower == true){
		if (hideCondition == true){
			for (x=menuLevel-2; x>=0; x--){
				if (track[x] != "")
					eval(track[x] + ".css.visibility = 'hidden'");
			}
			if (IDset == true){
				clearInterval(hideID);
				IDset = false;
			}
			hideCondition = false;
		}
		else{
			hideCondition = true;
			if (IDset == false){
				hideID = setInterval("HideAll()", menuHideSpeed);
				IDset = true;
			}
		}
	}
}

function CreateLayer(id, rootid) {
	if (is_nav5up) {
			this.layer = document.getElementById(id);
			this.css = this.layer.style;
	}

	if (is_nav && !is_nav5up){
		if (rootid != "")
			this.layer = document.layers[rootid].document.layers[id];
		else
			this.layer = document.layers[id];
		this.css = this.layer;
	}

	if (is_ie) {
		this.layer = document.all[id];
		this.css = document.all[id].style;
	}
}

function writeCSSLayerInfo(name){
	document.write("#" + name + "{position: absolute; top:" + LayerInfoTop + "; left:" + LayerInfoLeft + "; visibility: hidden;}");
}

function createLayerInfoID(){
	if (LayerInfoDiv[0] != ""){
		for (x=0; x<LayerInfoDiv.length; x++)
		eval("L" + LayerInfoDiv[x] + "= new CreateLayer('" + LayerInfoDiv[x] + "', '');");
	}
}
