//---------------------------------------------------------------------------------------------------------
// flexMenu V6.2
// JavaScript DHTML Hierarchical Menu Class 
// Currently supported browsers: 
// IE5+, Mozilla/5 (e.g. Netscape 6+, FireBird, FireFox), Safari, Camino, Konqueror, Opera 
// 
// (C) FlexScape, LLC, Torsten Heinze 1999-2007, All rights reserved.
//
// Copying or using this code (or parts of it) without explicit permission by FlexScape, LLC is prohibited 
// and will result in proscecution and severe penalties.
//---------------------------------------------------------------------------------------------------------
var timers=new Array();
var currentRID=null; //current selected root menu
var currentMNU=null; //current selected root menu
var usePopContainer=false;
//parent window containing this script (e.g. for IFRAME)
var pw=self;

//IE only --------------------------------------------------------------------------
if(document.all){var checkedForControls=false;document.onreadystatechange=checkForControls;}else{var checkedForControls=true;}
function checkForControls(){
	if(document.body){
		if(document.body.readyState=="complete"){
			if(checkForControls&&usePopContainer) return;

			usePopContainer=(document.body.getElementsByTagName("IFRAME").length+
							 document.body.getElementsByTagName("SELECT").length+
							 document.body.getElementsByTagName("OBJECT").length+
							 (typeof(window.flexWriteActiveX)!="undefined"))>0;

			//if(usePopContainer)alert(" POP CONTAINER "); //DEBUG
			checkedForControls=true;
		}
	}
}
//-----------------------------------------------------------------------------------

function doFalse(){
	return false;
}

function doLink(el,sLink){
	if((!sLink)||(sLink=="")){return}
	if(el.isdisabled) return;
	if(sLink.indexOf("javascript:")>-1){ 
		eval(sLink);
	}else{
		pw.location.href=sLink;
	}
}

function doMouseOverItem(el,sOverClass,subid){
	if(!checkedForControls) checkForControls();
	doSelect(el,sOverClass,subid);
	el.onselectstart=doFalse;
}

function doSelect(el,sOverClass,subid){
	if(!el){return}
	var mid=el.getAttribute("mid");
	var mnu=document.getElementById(mid);
	//window.status+=mnu;
	if(!mnu) return;
	var rid=mnu.getAttribute("rid");
	//if moved to another menu, then hide last menu
	if(currentRID) if(currentRID!=rid) unSelect(document.getElementById(currentRID));		
	currentRID=rid;
	if(el!=mnu.current){
		unSelect(mnu);
		el.classNameOrg=el.className;
		el.className=sOverClass;
		mnu.current=el;
	}else{
		doClearHideTimer(mid);
		return;
	}
	
	if(typeof(subid)!="undefined") doShowSubMenu(el,subid);
}

function unSelect(mnu){
	doHideSubMenuChildren(mnu);
	resetClass(mnu);
}

function resetClass(mnu){
	if(mnu.current){
		mnu.current.className=mnu.current.classNameOrg;
		mnu.current=null
	}
}

function doShowMenu(id,x,y){
	var mnu=document.getElementById(id);	//the menu
	if(!mnu){return}
	var mhv=mnu.getAttribute("hv");	
	if(usePopContainer) mnu=doCreateContainer(id,id,mhv,mnu)
	mnu.style.left=x;
	mnu.style.top=y;
	mnu.style.visibility="visible";
	currentMNU=id;
}

function doShowSubMenu(el,id){
	if(!id){return}
	var mnu=document.getElementById(id);	//the (sub) menu
	if(!mnu){return}
	
	var mid=el.getAttribute("mid");
	
	var parentMenu=document.getElementById(mid); 		// parent menu
	var rid = mnu.getAttribute("rid");					// root menu id
	
	var mhv=mnu.getAttribute("hv");						// direction of menu (horizontal or vertical)
	var phv=parentMenu.getAttribute("hv");				// direction of parent menu (horizontal or vertical)
	
	var w=mnu.offsetWidth;
	var h=mnu.offsetHeight;
	
	//determine positioning parent
	if(mhv=="|"){
		var pel=el;
		//make it look better if parent item width > submenu width
		if((phv=="-")&&(pel.offsetWidth>(w))){mnu.style.width=pel.offsetWidth;w=pel.offsetWidth;}
	}else{
		var pel=parentMenu;
		//horizontal, so same width as parentMenu
		mnu.style.width=parentMenu.offsetWidth;
	}
	
	//determine new position based on parent
	if(phv=="|"){
		var x=mnuGetLeft(pel)+pel.offsetWidth;
		var y=mnuGetTop(pel);
	}else{
		var x=mnuGetLeft(pel);
		var y=mnuGetTop(pel)+ pel.offsetHeight;
	}
			
	if((typeof(parentMenu)!="undefined")||(parentMenu.style.position=="absolute")){
		if(typeof(parentMenu.w)!="undefined"){
		var parw=parentMenu.w;
		var parh=parentMenu.h;
		}else{
		var parw=parentMenu.offsetWidth;
		var parh=parentMenu.offsetHeight;
		}
	}else{
		var parw=0;
		var parh=0;
	}	
	
	var w=mnu.offsetWidth;
	var h=mnu.offsetHeight;
	
	//for vertical menus, adjust scrolling and body width, so it fits always in visible window
	if(mhv=="|"){
		if((parentMenu.className.indexOf("_LEFT")>0)||((x+w)>=(document.body.clientWidth+document.body.scrollLeft))){
			if(phv!="-"){
				x-=w+parw-2;
			}else{
				x=document.body.clientWidth+document.body.scrollLeft-w;
			}
			//window.status=phv+":"+w+":"+parw+":"+x+":"+document.body.clientWidth+":"+document.body.scrollLeft;
		}
		
		if((parentMenu.className.indexOf("_TOP")>0)||((y+h)>=(document.body.clientHeight+document.body.scrollTop))){
			if(phv!="|"){
				y-=h+parh-2;
			}else{
				y=document.body.clientHeight+document.body.scrollTop-h-2;
			}
			//window.status=phv+":"+h+":"+parh+":"+y+":"+document.body.clientTop+":"+document.body.scrollTop;
		}
		
	}

	if(usePopContainer){
		//1st level sub menu is an IFRAME so apply correct position to 2nd level one
		//since el in sub menu 1 is now positioned relative to it's IFRAME document, 
		//must add parentMenu offset (only if vertical)
		if((mhv=="|")&&(parentMenu.style.position=="absolute")){
	 			x+=parentMenu.offsetLeft;
	 			y+=parentMenu.offsetTop;
	 	}

	 	mnu=doCreateContainer(id,rid,mhv,mnu)
	}
	mnu.style.left=x;
	mnu.style.top=y;
	//mnu.style.filter="BlendTrans(duration=0.3)";
	mnu.style.visibility="visible";
	parentMenu.childMenu=mnu;	//link with parent
	mnu.parentMenu=parentMenu; 
}

//IE4+ and ActiveX, Frames, Flash put everything in IFRAME now, so zIndex is correct
//tricky: move existing mnu (outer TABLE) into a new created IFRAME
function doCreateContainer(id,rid,mhv,mnu){
	//already created?
	if(mnu.tagName=="IFRAME"){
		//in case size changed
		var mnuDoc=document.frames[id];
		if(mnuDoc){
			mnuX=mnuDoc.document.getElementById(id);
			mnu.style.width=mnuX.offsetWidth;
			mnu.style.height=mnuX.offsetHeight;
		}
		return mnu;
	}

	//remember properties of mnu
	var mnu_margin=mnu.currentStyle.margin;
	mnu.style.margin="0px 0px 0px 0px";
	var sMenuHTML=mnu.outerHTML;
	var w=mnu.offsetWidth;
	var h=mnu.offsetHeight;
	//destroy mnu original, because IFRAME will become new mnu
	mnu.outerHTML="";

	// remove inline styles for A tags (added by IE, while fetching innerHTML)
	var re = new RegExp("<A style=\"([^\"]*)\"([^>]*)","ig");
	sMenuHTML = sMenuHTML.replace(re, "<A $2");

	//IFRAME container
	var sHTML='<IFRAME id="'+id+'" src="javascript:false" style="background:none;position:absolute;border:0px;padding:0px;margin:'+mnu_margin+';top:0px;left:0px;width:'+w+'px;height:'+h+'px;visibility:hidden;" frameborder=0 scrolling=no allowTransparency="true"></IFRAME>';
	document.body.insertAdjacentHTML("AfterBegin",sHTML);
	var sty="";
	//insert styles into iframe as well
	for (var i=0; i<document.styleSheets.length;i++){
		var styleSrc=document.styleSheets[i].href;
		if((styleSrc)&&(styleSrc!="")) sty+='<link REL="stylesheet" TYPE="text/css" HREF="'+styleSrc+'">\n';
	}
	sty+="<style type=\"text/css\">\n";
	sty+="#"+mnu.id+" A {display:block;cursor:pointer;border:0;padding:0;margin:0;text-decoration:expression(this.parentNode.currentStyle.textDecoration);text-decoration:inherit;color:expression(this.parentNode.currentStyle.color);color:inherit}\n";
	sty+="#"+mnu.id+" A:HOVER {display:block;cursor:pointer;border:0;padding:0;margin:0;text-decoration:expression(this.parentNode.currentStyle.textDecoration);text-decoration:inherit;color:expression(this.parentNode.currentStyle.color);color:inherit}\n";
	sty+="</style>\n";

	var scr='<script language="JavaScript">var pw=parent;onload=doShow;function doShow(){'+id+'.style.visibility="visible";'+id+'.onmousemove=null;'+id+'.onmouseout=null;};setTimeout("doShow()",10);</script>';
	var t=eval(id+".document");
	t.open();
	t.writeln(sty+scr+'\n<body style="background:none;" id=ifrmnu topmargin=0 leftmargin=0 rightmargin=0 marginheight=0 marginwidth=0 onmouseover="pw.doClearHideTimer(\''+id+'\')" onmousemove="pw.doClearHideTimer(\''+id+'\')" onmouseout="pw.doSetHideTimer(\''+id+'\')">'+sMenuHTML+'</body>');
	t.close();
	var cnt=document.getElementById(id);

	cnt.rid=rid;
	cnt.hv=mhv;
	mnu=cnt;
	return mnu;
} 
	
function doClearHideTimer(id){
	var mnu=document.getElementById(id);
	if(!mnu) return;
	var rid=mnu.getAttribute("rid");
	//window.status=mnu.id+":"+rid+" hide timer cleared";
	if(timers[rid]){clearTimeout(timers[rid]);timers[rid]=null}
}

function doSetHideTimer(id){
	var mnu=document.getElementById(id);
	if(!mnu) return;
	var rid=mnu.getAttribute("rid");
	doClearHideTimer(id);
	timers[rid]=setTimeout("doHideSubMenuRecurse('"+id+"')",450);
}
	
function doHideSubMenuChildren(mnu){
	if(!mnu) return;
	//close all children
	while(mnu.childMenu){
		resetClass(mnu.childMenu);
		pot=document.getElementById(mnu.childMenu.id);
		if(pot) pot.style.visibility="hidden";
		closedMenu=mnu;
		mnu=mnu.childMenu;
		closedMenu.childMenu=null;
	}
}
 
function doHideSubMenuRecurse(id){
	var mnu=document.getElementById(id);
	doHideSubMenuChildren(mnu);
	if(mnu.childMenu){return}
	//close menus all the way up to root-parent
	while(mnu){
		resetClass(mnu);
		mnu.childMenu=null; //unlink
		pot=document.getElementById(mnu.id);
		if(pot)if(pot.style.position=="absolute") pot.style.visibility="hidden";
		mnu=mnu.parentMenu;
	}
}
	 
/* ------------- get Element Absolute Left ------------------------------------------------------ */
function mnuGetLeft(el) {
	var x=0;
	while (el!= null){
		if(el.tagName!="TR"){
			var bWidth=0;if(el.currentStyle)if(el.className.indexOf("mnu")!=0)if(el.currentStyle.borderLeftWidth)bWidth=parseInt(el.currentStyle.borderLeftWidth);
			if(isNaN(bWidth)) bWidth=0;
			//if(bWidth>0) alert(el.tagName+":"+el.className+":"+bWidth);
			x += el.offsetLeft+bWidth;
		}
		el = el.offsetParent;
	}
	return x;
}

/* ------------- get Element Absolute Top	------------------------------------------------------ */
function mnuGetTop(el) {
	var y=0;
	var pel=el.offsetParent;
	if(pel.currentStyle)if(pel.currentStyle.borderTopWidth) y=parseInt(pel.currentStyle.borderTopWidth);
	if(isNaN(y)) y=0;
	y= -1 * y;

	while (el!= null) {
		if(el.tagName!="TR"){
			var bWidth=0;if(el.currentStyle)if(el.className.indexOf("mnu")!=0)if(el.currentStyle.borderTopWidth)bWidth=parseInt(el.currentStyle.borderTopWidth);
			if(isNaN(bWidth)) bWidth=0;
			//if(bWidth>0) alert(el.tagName+":"+el.className+":"+bWidth);
			y += el.offsetTop+bWidth;
		};
		el = el.offsetParent;
	}
	return y;
}

//only for MakeMenuVerticalList Navigation Module ---------------------------------------------
var openListItems=new Array();
function toggleListMenu(id,depth){
	if(!document.getElementById) return;
	
	if(document.getElementById(openListItems[depth])){
	 	if(openListItems[depth]!=id) document.getElementById(openListItems[depth]).style.display="none";
	}
	
	if(document.getElementById(id)){
	 	if(document.getElementById(id).style.display=="none"){
			document.getElementById(id).style.display="";
			openListItems[depth]=id;
		}else{
			document.getElementById(id).style.display="none";
		}
	}
}

function mnuGetStyle(el, prop) {
  var y;
  if (el.currentStyle) 
    y = el.currentStyle[prop];
  else if (window.getComputedStyle)
    y = window.getComputedStyle( el, '').getPropertyValue(prop);
  return y;
}

//----------------------------------------------------------------------------------------------
