var height = 25; // height of the menu headers
var iheight = 24; // height of the menu_items

var topbgc = "#FFFFFF"; // background color of top level
var toptc = "#103184"; // text color of top level

var over_topbgc = "#103184";
var over_toptc = "#FFFFFF";

var bgc = "#FFFFFF"; // background color of the item
var tc = "#666666"; // text color of the item

var over_bgc = "#103184";
var over_tc = "#FFFFFF";

var speed = 1;
var timerID = "";
var N = (document.all) ? 0 : 1;
var width = 150;

var self_menu = new Array();

function write_menu() {
	smc = 0; // count the position of the self_menu
	document.write("<div style='position:absolute'>");
	mn = 0;
	mni = 1;
	start = -1;

	for(i=0;i<Link.length;i++) {
		la = Link[i].split("|");
		if (la[0] == "0" || la[0] == "2") {
			if(start == 0) {
				document.write("</div>");
				h =  csmc * iheight;
				tmn = mn; //-h
				self_menu[smc] = new Array(tmn,h,0,-2);
				smc++;
				mn--;
			}
			csmc = 0;
			document.write("<div class='menu' style='top:"+mn+";height:"+height+"' id='down"+smc+"' onclick='pull_down("+smc+","+mni+",&quot;"+la[2]+"&quot;)'><table border=0 width=150 height='100%' cellpadding=0 cellspacing=0><tr><TR><TD width=10 id='mark"+mni+"' bgcolor='"+over_topbgc+"'>&nbsp;</TD><td class='topmenu' id='heading"+mni+"' bgcolor='"+topbgc+"' onmouseover='bgcol("+mni+")' onmouseout='unbgcol("+mni+")' style='color:"+toptc+"'><b>&nbsp;"+ la[1] + "</b></td></tr></table></div>");
			if (la[0] == "0") {
				self_menu[smc] = new Array(mn,height,0,mni);
			} else {
				self_menu[smc] = new Array(2,2,2,la[3]);
			}
			smc++;
			mni++;
			mn+=height;
			start = 1;
		} else {
			if(start == 1) {
				if(N) mn+=2;
				document.write("<div class='item_panel' id='down"+smc+"' style='top:"+mn+"'>");
				start = 0;
			}
			if (la[3] == "form") {
				document.write("<a href='#' onclick='javascript: window.open");
				document.write('("'+la[2]+'", "form", "width=450, scrollbars=yes, resizable=yes");');
				document.write("'");
			} else if (la[3] == "compshare") {
				document.write("<a href='#' onclick='javascript: window.open");
				document.write('("'+la[2]+'", "form", "width=555, scrollbars=yes, resizable=yes");');
				document.write("'");
			} else if (la[3] != "") {
				document.write("<a href='#' onclick='javascript: window.open");
				document.write('("'+la[2]+'", "'+la[3]+'", "scrollbars=yes, resizable=yes");');
				document.write("'");
			} else {
				document.write("<a href='#' onclick='javascript: document.location=");
				document.write('"'+la[2]+'"');
				document.write("'");
			}
			document.write("><div id='d"+i+"' style='height:"+iheight+";text-decoration:none");
			if (N) document.write(";width:150");
			document.write("' onmouseover='color(this.id, "+i+")' onmouseout='uncolor(this.id, "+i+")'><TABLE width='100%' cellspacing=0><TR><TD width=10>&nbsp;</TD><TD class='item' id='subitem"+i+"' bgcolor='"+bgc+"' style='color:"+tc+"'><b>"+ la[1] + "</b></TD></TR></TABLE></div></a>");
			csmc++;
		}
	}
	if (start == 0) {
		document.write("</div>");
		h =  csmc * iheight;
		tmn = mn + 5; //-h
		self_menu[smc] = new Array(tmn,h,0);
		name = "down" + (self_menu.length-1);
		obj = document.getElementById(name);
		obj.style.borderBottomColor = 0;
		obj.style.borderBottomWidth = 0;
		obj.style.borderBottomStyle = "none";
		smc++;
	}
	document.write("<div class='menu' id='down"+smc+"' style='top:"+mn+";height:3;'></div>");
	document.write("</div>");
	document.getElementById("down0").style.borderTopStyle = "none";
}
  
function bgcol(obj) {
	document.getElementById("heading"+obj).bgColor = over_topbgc;
	document.getElementById("mark"+obj).bgColor = topbgc;
	document.getElementById("heading"+obj).style.color = over_toptc;
}

function unbgcol(obj) {
	document.getElementById("heading"+obj).bgColor = topbgc;
	document.getElementById("mark"+obj).bgColor = over_topbgc;
	document.getElementById("heading"+obj).style.color = toptc;
}

function color(obj, iobj) {
	document.getElementById("subitem"+iobj).style.color = over_tc;
	document.getElementById("subitem"+iobj).bgColor = over_bgc;
	document.getElementById(obj).style.cursor = "hand";
}

function uncolor(obj, iobj) {
	document.getElementById("subitem"+iobj).style.color = tc;
	document.getElementById("subitem"+iobj).bgColor = bgc;
}

function pull_down(nr,c,urllink) {
	if (!self_menu[nr+1] || self_menu[nr][0] == "2") gotourl(urllink,self_menu[nr][3]);
	else {
		if (timerID == "") {
			to = self_menu[nr+1][1];
			begin = nr + 2;
			if (timerID != "") clearTimeout(timerID);
			if (self_menu[nr+1][2] == 0) {
				if (nr == self_menu.length-2) {to++;}
				checkifdown(begin);
				epull_down(begin,to,0);
				self_menu[nr+1][2] = 1;
			} else {
				to = 0;
				self_menu[nr+1][2] = 0;
				name = "down"+(nr+2);
				open_item = 0;
				for(i=0;i<nr;i++) {
					if(self_menu[i][2] == 1) {
						open_item += self_menu[i][1];
					}
				}
				if (N == false) {
					open_item -= (c*1);
				}
				if (nr == self_menu.length-2) {
					val = self_menu[self_menu.length-1][1];
					to=-1;
				} else {
					val = parseInt(document.getElementById(name).style.top) -(open_item)-(c*height);
				}
				// epull_up(begin,to,val);
				gotourl(urllink,"");
			}
		}
	}
}

function gotourl(urllink,urltarget) {
	if (urltarget == "blank") {
		reWin=window.open(urllink,'_blank');
	} else {
		window.location = urllink;
	}
}

function checkifdown(nr) {
	yy=-2;
	for(zz=0;zz<Link.length;zz++) {
		la = Link[zz].split("|");
		if (la[0] == "0" || la[0] == "2") {
			yy = yy + 2;
			if (self_menu[yy+1]) {
				if (self_menu[yy+1][2] == 1) {
					lbegin = yy + 2;
					lc = lbegin/2;
					lto = 0;
					lname = "down"+(yy+2);
					lopen_item = 0;
					for(i=0;i<yy;i++) {
						if (self_menu[i][2] == 1) {
							lopen_item += self_menu[i][1];
						}
					}
					if (N == false) {
						lopen_item-= (lc*1);
					}
					if (yy== self_menu.length-2) {
						lval = self_menu[self_menu.length-1][1];
						lto=-1;
					} else {
						lval = parseInt(document.getElementById(lname).style.top) -(lopen_item)-(lc*height);
					}
					epull_up(lbegin,lto,lval);
					self_menu[yy+1][2] = 0;
				}
			}
		}
	}
}

function epull_down(nr,to,nowv) {
	name = "down" + (nr-1);
	obj = document.getElementById(name).style.clip = "rect(0,"+width+","+(nowv+1)+",0)";
	for (i=nr;i<self_menu.length+1;i++) {
		name = "down" + i;
		obj = document.getElementById(name);
		obj.style.top = parseInt(obj.style.top)+8;
	}
	nowv = nowv + 8;
	if(nowv <= to) timerID = setTimeout("epull_down("+nr+","+to+","+nowv+")",speed);
	else timerID = "";
	name = "down" + (nr-2);
	document.getElementById(name).style.cursor = "hand";
}

function epull_up(nr,to,nowv) {
	name = "down" + (nr-1);
	obj = document.getElementById(name).style.clip = "rect(0,"+width+","+nowv+",0)";
	for (i=nr;i<self_menu.length+1;i++) {
		name = "down" + i;
		obj = document.getElementById(name);
		obj.style.top = parseInt(obj.style.top)-8;
	}
	nowv = nowv - 8;
	if(nowv > to) timerID = setTimeout("epull_up("+nr+","+to+","+nowv+")",speed);
	else timerID = "";
	name = "down" + (nr-2);
	document.getElementById(name).style.cursor = "default";
}

function startup(nr) {
	write_menu();
	if (nr != 0) {
		tdid="heading"+nr;
		document.getElementById(tdid).bgColor = over_topbgc;
		document.getElementById(tdid).style.color = over_toptc;
		document.getElementById(tdid).onmouseover = "";
		document.getElementById(tdid).onmouseout = "";
		for(i=0;i<self_menu.length;i++) {
			if(self_menu[i][3] == nr) {
				pull_down(i,nr,"dummy");
				i==self_menu.length;
			}
		}
	}
}
