var mnu_idx = 0
var curr_hide_timer = null
var curr_submenumenu = null

function appendHost(path){
	
	var out
	if(path.replace == null){
		return path;
	}
	
	// HACK: Links with a value of ??? were causing problems  
	if (path.substring(0,1) == "?"){
		return ""
	}
	
	// HACK: Some links came with a javascript: appended.  Superfluous.
	path = path.replace("javascript:","")
	
	if (/goTo\(\"/.test(path)){
		path = path.replace("goTo(\"","");	//sometimes this is being passed in. post-launch fix
		path = path.replace("\")","");		//sometimes this is being passed in. post-launch fix/
		path = path.replace(";","");		//sometimes this is being passed in. post-launch fix
 	}

/*
	Change /-rooted URLs to goTo() with server host prefixed to ensure 3rd party partner sites work properly
	strSite is defined in Browser.asp
*/

	if (path.substring(0,1) == "/"){
		out = 'goTo("'+strSite + path+'");'
	} else if (path.substring(0,4) == "http"){
		out = 'goTo("'+path+'");';
	} else{
  	  // Pass it on through (assume a JS function call or something)
		out = path
	}

	return out
}

function swapSubmenu(name){
	clearSubmenu()
	showE(name)
	curr_submenumenu = name
	window.clearTimeout(curr_hide_timer)
}

function scheduleClearSubmenu(){
	if (curr_hide_timer){
		window.clearTimeout(curr_hide_timer)
	}
	curr_hide_timer = window.setTimeout('clearSubmenu()', 1000)
}

function clearSubmenu(){
	if (curr_submenumenu) hideE(curr_submenumenu);
	
	if (curr_hide_timer){
		window.clearTimeout(curr_hide_timer)
		curr_hide_timer = null
	}
}

function NavOn(obj, strStyle){
	obj.className = "navitemroll_" + strStyle
	window.clearTimeout(curr_hide_timer)
}

function NavOff(obj, strStyle){
	obj.className = "navitem_" + strStyle
	scheduleClearSubmenu()
}

/* Classes */
function OHorizMenu(sCSSStyle, oSubMenu, sSite){
	/*Constructor Assignments */
	this.menustyle = sCSSStyle
	this.submenu = oSubMenu /* or is that aSubMenu */
	this.sSite = sSite
	
	/* Pointer to PUBLIC methods */
	this.render = render

	function render(){
		loadcss(this.menustyle)
	
		if (this.submenu != null){
			
			// Container Div
			document.write('<div class="container_' + this.menustyle + '" id="container_'+(mnu_idx)+'" name="mainnav_root'+(mnu_idx)+'"  >')
		
			// Loop through submenus and render those 
			for (var i=0; i<this.submenu.length; i++){
				if (!this.submenu[i]){
					continue
				}
				this.submenu[i].render()
			}

			// Close Container Div
			document.write('</div>')

			var sDivider = '<tr><td class="subMenuItemDivider_' + this.menustyle +'" colspan=3><img src="' + strSite + '/assets/images/sp.gif" width=1 height=1/></td></tr>';
			var str = ''
		 
			for (var i=0; i<this.submenu.length; i++){
				var child = this.submenu[i]
				
				if (!this.submenu[i]){
					continue
				}
				
				if (child.submenu != null){
					
					str += '<div class="rootMenu_' + this.menustyle + '_'+i + '" id="mainnav_'+child.id+'" >'
					str += '<table border="0" cellspacing="0" cellpadding="0">'
					str += sDivider;

									
					for (var j=0; j<child.submenu.length; j++){
						// submenu should be a MenuItem object 
						var submenu_child = child.submenu[j]
						
						
						
						if (!submenu_child){
							continue
						}
						
						// MenuItem
						str += "<tr><td class='subMenuItemDivider_" + this.menustyle +"'><img src='" + strSite + "/assets/images/sp.gif' height=19 width=1 /></td>"

						if (submenu_child.link !=''){
							sMetrics = "";
							if(submenu_child.metrics !=null){
								sMetrics = submenu_child.metrics
							}
							str += "<td class='navitem_" + this.menustyle + "' onmouseover='NavOn(this, \"" + this.menustyle + "\");' onclick='" + sMetrics + appendHost(submenu_child.link) + "' onmouseout='NavOff(this, \"" + this.menustyle + "\");' >"
						} else {
							str += "<td class='navitemheader_" + this.menustyle + "'>"
						}

						str +=  "&nbsp;&nbsp;" + submenu_child.label + "&nbsp;&nbsp;"
						str += "</td><td class='subMenuItemDivider_" + this.menustyle +"'><img src='" + strSite + "/assets/images/sp.gif' width=1 height=1/></td></tr>"
						str += sDivider;
						// End MenuItem
					}
					str += '</table>'
					str += '</div>'
				}
			}
			document.write( str  )
		}
	}
	
	/* Private Functions */
	function loadcss(sStyle){

		var headID = document.getElementsByTagName("head")[0];
		var cssNode = document.createElement('link');
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.href = strSite + '/scripts/css/menu_' + sStyle+ '.css';
		cssNode.media = 'screen';
		headID.appendChild(cssNode);
	}
}




function OMenuItem(metrics, label, img_off, img_on, link, dividerimage, submenu){
	this.metrics = "";
	 if(metrics != null){
		this.metrics = metrics.replace("javascript:","") 
	}
	
	this.id = "mnu_"+(mnu_idx++)
	this.label = label /* DOES NOT APPEAR USED ! => IS USED IN PARENT CALL UGH!!*/
	this.img_on = getImagePath(img_on)
	this.img_off = getImagePath(img_off)
	this.divider = getImagePath(dividerimage)
	this.link = appendHost(link)
	this.submenu = submenu

	function getImagePath(sImage){
		sReturn = null
		if (sImage != ""){
			sReturn = strSite + "/assets/images/nav/global/" + sImage
		}
		return sReturn 
	}

	/* Pointers to expose public function */
	this.render = render;
	
	 function render(){
		 var str = ''
		 var show_submenu = ''
		 var hide_submenu = ''
	
		/* Is there a submenu ? */
		 if (this.submenu != null){
			show_submenu = "swapSubmenu('mainnav_" + this.id + "'); "
			hide_submenu = ""
		 }
		
		/* Is there an "off image" */
		if (this.img_off != null){
			var roll_on  = ""
			var roll_off = ""
		
			/* Is there an "on image" */
			 if (this.img_on != null){
				roll_on  = "MM_swapImage('"+this.id+"','','" +this.img_on +"',1);"
				roll_off = "MM_swapImgRestore();"
			 }

			 str += '<a href=\'javascript:' + this.metrics + this.link+'\' target="_top"'
			 str += 'onmouseover="NavOn(this);'+roll_on+show_submenu+'"'
			 str += 'onmouseout="NavOff(this);'+roll_off+hide_submenu+'">'
			 str += '<img id="'+this.id+'" name="'+this.id+'" src="' +this.img_off + '" border="0" />'

			if(this.divider !=null){
				str += '<img src="'+ this.divider + '" border=0>'
			}
			str += '</a>'
		}
		document.write(str)
	}
	
}

