output = "";
activeDiv = "";
vOffset = 5;
hOffset = 5;
divs = new Array();
styles = new Array();
styles[0] = "width:100%;";
styles[1] = "background-color: transparent; border:0 ;z-index:10; font-size: 15px; padding: 10px; margin: 0 0 15px 0;";
styles[2] = "background-color: #4275b5; border:1px white solid;z-index:20;visibility:hidden; font-size: 15px;";
styles[3] = "background-color: #4275b5; border:1px white solid;z-index:30;visibility:hidden; font-size: 15px;";
styles[4] = "background-color: #4275b5; border:1px white solid; z-index:40;visibility:hidden; font-size: 15px;";
styles[5] = "background-color: #4275b5; border:1px white solid;z-index:50;visibility:hidden; font-size: 15px;";
linkstyles = new Array();
linkstyles[0] = "color: black; text-decoration: none; width: 100%; display: block;";
linkstyles[1] = "color: white; font-weight: bold; text-decoration: none; width: 100%; display: block;";
linkstyles[2] = "color: white; text-decoration: none; width: 100%; display: block;";
linkstyles[3] = "color: white; text-decoration: none; width: 100%; display: block;";
linkstyles[4] = "color: white; text-decoration: none; width: 100%; display: block;";
linkstyles[5] = "color: white; text-decoration: none; width: 100%; display: block;";
function Menu(_orientation){
	this.type = "root";
	this.level = 0;
	this.children = new Array();
	this.orientation = _orientation;
	this.addChild = addChild;
}

function MenuItem(_text, _link, _width, _height){	
	this.type = "menuitem";
	this.children = new Array();
	this.addChild = addChild;
	this.text = _text;
	this.link = _link ? _link: "javascript:void(0)";
	this.parent = null;
	this.index = null;
	this.level = null;
	this.width = _width ? _width : 120;
	this.height = _height ? _height : 20;
	this.x = null;
	this.y = null;
	this.id = null;
}

function addChild(_menuItem){
	var currentIndex = this.children.length
	this.children[currentIndex] = _menuItem;
	var thisItem = this.children[currentIndex];
	thisItem.parent = this;
	thisItem.index = currentIndex;
	thisItem.level = thisItem.parent.level + 1;
	var x, y, thisId;
	x = 0;
	y = 0;
	if (thisItem.level == 1) 
		{
			if (thisItem.parent.orientation == "h"){
				for (var xCount = 0; xCount < (thisItem.parent.children.length - 1); xCount++){
					x += thisItem.parent.children[xCount].width;
				}
				y = 0;
			} else {
				x = 0;
				for (var yCount = 0; yCount < (thisItem.parent.children.length - 1); yCount++){
					y += thisItem.parent.children[yCount].height + 10;
					y++; // + 1 is to make up for the 1 px border
				}
			}
		}	
	else if (thisItem.level == 2  && thisItem.parent.parent.orientation == "h") 
		{
			x = thisItem.parent.x;
			y = thisItem.parent.height + 1; // + 1 is to make up for the 1px border
			for (var yCount = 0; yCount < (thisItem.parent.children.length - 1); yCount++){
					y += thisItem.parent.children[yCount].height;
			} 
		}
	else
		{
			if (thisItem.level == 4 && thisItem.parent.index == 1){
				vOffset = -150; // This is necessary because the menu was going off the bottom
			} else {
				vOffset = 5;
			}
			x = thisItem.parent.x + thisItem.parent.width - hOffset;
			y = thisItem.parent.y  + vOffset;
			for (var yCount = 0; yCount < (thisItem.parent.children.length - 1); yCount++){
				y += thisItem.parent.children[yCount].height;
			}
		}
	
		thisItem.x = x;
		thisItem.y = y;
		
		var thisId ="div";
		for (mainCounter = thisItem.level; mainCounter >= 1; mainCounter--){
			var thisIdText = "thisItem."
			for (subCounter = (mainCounter - 1); subCounter >= 1; subCounter--) { 
				thisIdText += "parent.";
			}
			thisIdText += "index";
			thisId += "_" + eval(thisIdText);
		}		
		thisItem.id = thisId;
		divs[divs.length] = thisId;
}


function render(item,_x,_y){
	if (item.type == "root") { 
			output += "<div style='" + styles[0] + "position:absolute;left:" + _x + "px; top: " + _y + "px'>\n\n";
	}
	for (var counter = 0; counter < item.children.length; counter++){
		var thisItem = item.children[counter];	
		var image = "";
		if (thisItem.children.length > 0 && item.level > 0) {
			image = "background-image:url(arrow.gif);";
			image += "background-repeat:no-repeat;";
			image += "background-position:95% 50%;";
		}
		output += "<div ";
		output += "style='position:absolute; left: " + thisItem.x + "px; top: " + thisItem.y + "px; ";
		output += "width: " + thisItem.width + "px; height: " + thisItem.height + "px; ";
		output += image ;
		output += styles[thisItem.level] + "' ";
		output += "id='" + thisItem.id + "' ";
		output += "onmouseover=\"" + generateMouseOver(thisItem,thisItem.id) + "\" ";
		output += "onmouseout=\"" + generateMouseOut(thisItem) + "\" ";
		output += ">";
		output += "<a href='" + thisItem.link + "'";
		output += " onmouseover=\"" + generateMouseOver2() + "\" ";
		output += " onmouseout=\"" + generateMouseOut2() + "\" ";
		output += " style='" + linkstyles[thisItem.level] + "'";
		output += ">";
		output += thisItem.text;
		output += "</a>";
		output += "</div>\n\n";		
		render(item.children[counter]);
	} 
	if (item.type == "root") output += "</div>";
}

function generateMouseOver(_item,_id){
	var code = "";
	if (_item.level > 1){
		code += "this.style.backgroundColor='#bdc3de';";
	}
	code += "activeDiv=this.id;";
	return code;
}
function generateMouseOut(_item){
	var code = "";
	if (_item.level > 1){
		code += "this.style.backgroundColor='#4275b5';";
	} else {
		code += "this.style.backgroundColor='';";
	}
	code += "activeDiv='';";
	return code;
}
function generateMouseOver2(_item,_id){
	code = " this.style.color='#DF0032';";
	return code;
}
function generateMouseOut2(){
	code = "this.style.color='white';";
	return code;
}

function hideDivs(){
	var activeDivLevel = 0;
	for (var activeDivLevelCounter = 0; activeDivLevelCounter < activeDiv.length; activeDivLevelCounter++){
		if (activeDiv.charAt(activeDivLevelCounter) == "_") activeDivLevel++;
	}

	for (var d = 0; d < divs.length; d++){
		document.getElementById(divs[d]).style.visibility="hidden";
		var whatLevel = 0;
		for (var levelCounter = 0; levelCounter < divs[d].length; levelCounter++){
			if (divs[d].charAt(levelCounter) == "_") whatLevel++;
		}
		
		if (whatLevel == 1){ //main items always display
			document.getElementById(divs[d]).style.visibility="visible";
			if (divs[d].substr(0,5) == activeDiv.substr(0,5)) {
				document.getElementById(divs[d]).firstChild.style.backgroundColor="#BFC2DF";
			} else {
				document.getElementById(divs[d]).firstChild.style.backgroundColor="";
			}
		} else if (activeDiv == "") { //if mouse is not over a div, all non-main items do not display
			document.getElementById(divs[d]).style.visibility="hidden";
		} else if (whatLevel > (activeDivLevel + 1)) { //if an item is more than one level deeper than the active item, do not display
			document.getElementById(divs[d]).style.visibility="hidden";
		} else {
			if (whatLevel > activeDivLevel){ //if an item is one level deeper than the active item
				if (divs[d].substr(0,activeDiv.length) == activeDiv){//if the active item's text equals the beginning of this item
					document.getElementById(divs[d]).style.visibility="visible";
				}
			} else if (whatLevel == activeDivLevel) {//if an item is at the same level as the active item
				if (divs[d].substring(0,divs[d].lastIndexOf("_")) == activeDiv.substring(0,activeDiv.lastIndexOf("_")) ){ // if an item has the same parent
					document.getElementById(divs[d]).style.visibility="visible";
				}
			} else {
				RE = /^(.+)_\d+$/;
				var match = RE.exec(divs[d])[1];
				if (divs[d].substr(0, match.length) == activeDiv.substr(0,match.length)){
					document.getElementById(divs[d]).style.visibility="visible";
				}
			}
		} 
	}	
}


function init(){
	setInterval('hideDivs()',250);
}

