var Mtimer;

MLoaded=0;
//Browser Detect
ns4=(document.layers)?true:false
ns6=(navigator.userAgent.indexOf("Gecko")!=-1)?true:false
mac=(navigator.appVersion.indexOf("Mac")!=-1)?true:false
mac45=(navigator.appVersion.indexOf("MSIE 4.5")!=-1)?true:false
if(ns6||ns4)mac=false
opera=(navigator.userAgent.indexOf("Opera")!=-1)?true:false
ns61=(parseInt(navigator.productSub)>=20010726)?true:false
ie4=(!document.getElementById&&document.all)?true:false;
konq=(navigator.userAgent.indexOf("Konqueror")!=-1)?true:false

//mouse pointer
mp=(ns6)?"pointer":"hand";

//outside window width and height
oww=-1;
owh=-1;

frs=0;
fre=0;
nsmatch=0;
var a,strActiveMenu,oa,im,om,flta;
hlarr=new Array();
hlcnt=0;
hll=0;
Mname=new Array();
parr=new Array();
imageObject=0;
el=0;
nshl=0;
df=1;
im=0;

//ac = number of array elements before menu item data
ac=8;
m=1;
sy=1;
var itemClass;
var hoverItemClass;

//variable holding 1 or 0 to represent whether the menu is shown
ShM=0;

//window height, and window width
ww=0;
wh=0;

//image array
var imageArray=new Array();

//menuReader - determines the number of menus
menuReader =",";
while(eval("window.menu"+m)){
	menuReader +="menu"+m+",";
	tmenu=eval("menu"+m);
	Mname[m]=tmenu[0].toLowerCase();
	m++
}
menuReader =menuReader +" ";
menuReader =menuReader.split("\,");
menus=menuReader.length-1;

function fFindObject(mtxt){
//Similar to Macromedias MM_FindObject
	if(document.getElementById){
		m=document.getElementById(mtxt)
	}else if(document.all){
		m=document.all[mtxt]
	}else if(document.layers){
		m=document.layers[mtxt]
	}return m;
}

function fGetStyle(strItem){
//returns the specified objects style
	obj=fFindObject(strItem);
	if(!ns4){
		if(obj)obj=obj.style;
	}
	return obj;
}

function setTopLevelMenuPositions(){
	//set the position of the menu relative to the menu images	
	for(a=1;a<menus;a++)
	{
		menu=eval("menu"+a);
		
		if (!isNaN(parseInt(menu[1]))){
			mn=fFindObject("menu"+a);

			if (mn) {
				menuImageObject = fFindObject("img"+a);

				if (menuImageObject){
					//to make sure the menu items appear below the menu images
					if(opera){
						mnh=menuImageObject.style.pixelHeight + "px";
					}else{
						mnh=parseInt(menuImageObject.height) + "px";
					}

					mn.style.top = (parseInt(GetElementTop(menuImageObject)) + parseInt(mnh)) + "px";
					mn.style.left = (parseInt(GetElementLeft(menuImageObject))) + "px";				
				}	
			}
		}		
	}
}

function fSetPosition(strItem,txtTop,txtLeft,txtHeight,txtWidth){
	//Sets the Potiton of the item using the style
	px="px";
	if(opera){
		strItemStyle=strItem.style;
		if(txtWidth!=null)strItemStyle.pixelWidth=txtWidth;
		if(txtHeight!=null)strItemStyle.pixelHeight=txtHeight
	}else{
		strItemStyle=strItem.style;
		if(txtWidth!=null)strItemStyle.width=txtWidth+px;
		if(txtHeight!=null)strItemStyle.height=txtHeight+px;
	}
	if(txtTop!=null)strItemStyle.top=txtTop+px;
	if(txtLeft!=null)strItemStyle.left=txtLeft+px
}	

function fGetPosition(strItem){
//Returns Array containing items top,left,height, and width
	if(opera){
		txtTop=strItem.style.pixelTop;
		txtLeft=strItem.style.pixelLeft;
		txtHeight=strItem.style.pixelHeight;
		txtWidth=strItem.style.pixelWidth;
	}else if(ns6){
		txtTop=strItem.offsetTop;
		txtLeft=strItem.offsetLeft;
		txtHeight=strItem.offsetHeight;
		txtWidth=strItem.offsetWidth
	}else{
		if(mac){
			topM=strItem.offsetParent.offsetTop;
			if(!topM)topM=0;
			leftM=strItem.offsetParent.leftMargin;
			if(!leftM)leftM=0;
			menuImageObject = fFindObject("img1");
			txtLeft=parseInt(leftM)+strItem.offsetLeft;
			txtLeft +=((parseInt(document.body.clientWidth)-760)/2)-10
			
			txtTop=strItem.offsetTop;
		}else{
			txtTop=strItem.offsetTop;
			txtLeft=strItem.offsetLeft;
		}
		txtHeight=strItem.offsetHeight;
		txtWidth=strItem.offsetWidth;
	}
	if(konq){
		txtWidth=parseInt(strItem.style.width);
		txtHeight=parseInt(strItem.style.height);
	}
	var arrayPos=new Array();
	arrayPos[0]=txtTop;
	arrayPos[1]=txtLeft;
	arrayPos[2]=txtHeight;
	arrayPos[3]=txtWidth;
	return(arrayPos)
}

function fGetMouseScreenPos(e){
//Returns the Mouse Screen Pos to MouseX and MouseY Varibles
	if(ns6){
		MouseX=e.pageX;
		MouseY=e.pageY;
	}else{
		MouseX=event.clientX;
		MouseY=event.clientY
	}
	if(!opera&&document.all){
		MouseX=MouseX+document.body.scrollLeft;
		MouseY=MouseY+document.body.scrollTop;
		if(document.compatMode=="CSS1Compat")MouseY=MouseY+sy
	}
	return true
}

function parseLink(txt,what){
	txt=txt.replace(/&quot;/gi,"'");
	txt=txt+";";
	lt="";
	if(what=="link"){
		sp=txt.indexOf(" ");
		lt=txt.substr(0,sp);
	}else{
		sp=txt.indexOf(what)+what.length+1;
		if(sp>what.length+1){
			lt=txt.substr(sp,999);
			lt=lt.substr(0,lt.indexOf(";"))
		}
	}
	return lt.replace("/ /",lt);
}

function cHexColour(theColour){
//checks that the colour provided has the #, if not adds it
//and the hex code is valid length
	if(theColour.length==6){
		theColour=theColour.toUpperCase();
		x=0;
		while(x<6){
			hsh="#";
			if(theColour.charCodeAt(x)>70){
				hsh="";
				x=6
			}
			x++
		}
		theColour=hsh+theColour
	}
	return theColour
}

function fSetStatusBarText(txtStatus){
	window.status=txtStatus;
	return true;
}

function SDiv(nm,sh){
	tD=fGetStyle(nm);
	if(tD)if(sh)tD.visibility="visible";else tD.visibility="hidden"
}

function close_el(){
	for(xa=hlarr.length-1;xa>=0;xa--){
		ti=hlarr[xa].split("_");
		tmenu=eval(ti[0]);

		if(ti[0]==hll){
			return
		}
	}
	hlcnt=0;
	hlarr=new Array()
}

function fHideAllMenus(){
//function hides all menus
	gmi=41;
	ShM=0;
	om="";
	hll="";
	close_el();

	for(a=1;a<menus;a++){
		menu=eval("menu"+a);
		if (menu[4]!=1){
			SDiv("menu"+a,0)
		}else{
			strActiveMenu=""
		}
	}
}

function fSwapImg(){
//swaps active image for its hover over
	if(imageObject){
		imageObject.src=o_img
	}
}

function fHideMenu(){
//hides the menu shown, but not lower down levels unless rolled off menu
	paramArray=fHideMenu.arguments;
	if(paramArray[0]){
		if(paramArray[3].substr(0,5)!="show-"){
			
		}else{
			hlarr[hlcnt]=paramArray[2]+"_"+paramArray[1]+"_"+''+"_"+'';
			hlcnt++;
		}
		menu=eval(paramArray[2]);
		if(!menu[5]){
			Mtimer=setTimeout("fHideAllMenus()",mousedelay);
		}
		}else{
			Mtimer=setTimeout("fHideAllMenus()",mousedelay);
		}
}	

function dc(){
	if(nshl.indexOf("show-menu=")>-1)return;
	tr=ltparamArray.split("=");
	if(tr[1])parent.frames[tr[1]].location.href=nshl;
	else location.href=nshl;
}

function fgetMenuByName(mnu){
//returns a menu object for a given name
	mnu=mnu.toLowerCase();
	for(a=1;a<menus;a++){
		if(mnu==Mname[a])mnu=a
	}
	return mnu;
}

function getMenuItem(mnu,item){
//returns a specified menu item
	for(a=1;a<mnu;a++){
		menu=eval("menu"+a);
		item=item+((menu.length-ac)/5)
	}
	item--;
	return item
}

function sm(v1){
	menu=eval(v1);
	ap=fFindObject(v1);

	if(flta)if(ap.filters[0])ap.filters[0].Apply();
	gmi++;
	aps=ap.style;
	aps.zIndex=gmi;
	aps.visibility='visible';
	if(flta)if(ap.filters[0])ap.filters[0].Play();
}

function fShowMenu(txtMnu,mpos)
{
	if(isNaN(txtMnu))
	{
		for(a=1;a<menus;a++)
		{
			if(txtMnu==Mname[a])txtMnu=a
		}
	}

	clearTimeout(Mtimer);
	fHideAllMenus();
	mtxt='menu'+txtMnu;
	mi=fGetStyle(mtxt);			
	
	if(mpos)
	{
		
		document.onmousemove=fGetMouseScreenPos;
		txtMnu=fFindObject(mtxt);
		fSetPosition(mn,MouseY+2,MouseX+2,null,null);
	}
	flta=0;
	if((document.all&&document.getElementById)&&!mac)
	{
		ap=fFindObject(mtxt);
		getflta(ap);
	}
	sm(mtxt);
	strActiveMenu=txtMnu
}

function popi(v1,lyr,el)
{
	subfound=0;
	nv1=v1
	for(a=1;a<menus;a++)
	{
		if(v1.substring(10,99)==Mname[a])
		{
			v1="show-menu"+a;
			subfound=1;
			a=999
		}
	}
	sel="smenu="+el+"#";
	if(!om)om=lyr;
	clearTimeout(Mtimer);
	
	imageObject=fFindObject("im"+el)
	
	o_img="";
	
	if (imageObject)
	{
		o_img=imageObject.src;
		ima=o_img.split(".");
		enD=ima.length;
		if(ima[enD-2].indexOf("_over")==-1)ima[enD-2]=ima[enD-2]+"_over";
		nsrc="";
		for(eD=0;eD<enD;eD++){nsrc=nsrc+ima[eD]+"."}
		nsrc=nsrc.substring(0,nsrc.length-1);
		ta=om.split(",");
		NI1=new Image();
		NI1.src=nsrc;
		if(mac45){
		}else if(document.all||ns6){
			imageObject.src=nsrc
		}else{
			if(NI1.width>0)imageObject.src=nsrc
		}
	}

	if(subfound)
	{
		v1=v1.substring(5,99);
		
		menu=eval(v1);
			
		mi=fFindObject(v1);
		gp=fGetPosition(fFindObject(lyr));
		h3="";
		
		tel=fGetPosition(fFindObject(h3+"el"+el))
		np=fGetPosition(mi);
		

			if(!ns61&&ns6)tel[0]=tel[0]-gp[0];
			nt=tel[0]+gp[0]+suboffset_top;

			nl=gp[1]+tel[3]-suboffset_left;
			
			if((gp[1]+gp[3]+np[3])>=ww){
				nl=gp[1]-np[3]+suboffset_left
			}
			
			if(nt+np[2]>wh+sy){nt=wh-np[2]+sy;}
			if(menu[1]>=0)nt=menu[1]
			if(menu[2]>=0)nl=menu[2]
			fSetPosition(mi,nt,nl,null,null);
		
		om+=","+v1;
		if(ShM==1)
		{
			sm(v1);
			mi.style.visibility="visible";
		}
	}
	else
	{
		frt=v1.indexOf(" ")
		if(frt>0&&v1.indexOf("show-menu=")>-1)
		{
			lt=parseLink(v1, "target");
			if(lt)
			{
				v1=v1.substring(10,frt);
				pf=parent[lt];
				pf.mn=pf.fFindObject("menu"+pf.fgetMenuByName(v1));
				menu=eval(lyr);
				gp=fGetPosition(fFindObject(lyr));
				h3="";

				mp=fGetPosition(fFindObject(h3+"el"+el))

					fTop=pf.sy+gp[0]+mp[0];
					fLeft=100
				
				pf.fSetPosition(pf.mn,fTop,fLeft-100,null,null);
				pf.fShowMenu(v1);
				fHideAllMenus();
			}
		}
		else if(frt>0)
		{
			lt=parseLink(v1, "target");
			if(lt)
			{
				v1=v1.substring(10,frt);
				close_el(v1)
				pf=parent[lt];
				if(pf)pf.fHideAllMenus();
			}
		}
		
	}
	ta=om.split(",")
	strt=0;
	for(a=0;a<ta.length;a++)
	{
		if (ta[a]==v1)strt=0;
		if (strt)
		{
			if(ta[a])
			{
				hll=lyr;
				hel=el;
				close_el(el);
				tmnu=eval(ta[a])
				if(tmnu[17])SDiv(tmnu[17],1)
				SDiv(ta[a],0)
			}
		ta[a]=""
		}
		if(ta[a]==lyr)strt=1;
	}
	mmenu=eval(lyr)
	if(mmenu[4]&&lyr!=ta[0])
	{
		ogmi=gmi;
		fHideAllMenus();
		gmi=ogmi;
		if(v1==ta[ta.length-1])
		{
			
			tmnu=eval(v1)
			if(tmnu[17])SDiv(tmnu[17],0)
			SDiv(v1,1)
		}
		if(nv1.indexOf("target=")>0)
		{
		}
		else
		{
			popi(nv1,lyr,el);
			return
		}
	}
	om=""
	mdisp=0
	for(a=0;a<ta.length;a++)
	{
		if(ta[a]==lyr)frs=a
		if(ta[a]==v1)fre=a
		if (mdisp||(a>frs&&a<fre))
		{
			if (ta[a]&&ta[a]!=v1)
			{
				SDiv(ta[a],0)
				close_el(el);
				
			}
		}
		else
		{
			if(ta[a])om+=ta[a]+",";
		}
		if (v1==ta[a])mdisp=1
	}	
}


function hl()
{		
	var popUp=0;
	paramArray=hl.arguments;
	strURL=paramArray[1];
	
	strActiveMenu=" "+a;strActiveMenu=strActiveMenu.length-1;subimg="";paramArray[0]+=""
	if(ns6){dwd=cellpadding*2}else{dwd=0}

	if(strURL.substring(0,5)=="show-"){
		paramArray[1]=paramArray[1].toLowerCase();
		
		if(paramArray[6]==strURL) {
			if(paramArray[5])paramArray[6]=paramArray[5];
			
		}else {
			paramArray[6]="";
			strURL=strURL;
			if (paramArray[5]+" "!="undefined ") {
				strURL=paramArray[5];
				while (strURL.indexOf('"') > -1) {
					strURL = strURL.replace('"','&quot;');
				}
			}
		}
	}
	
	pI="popi('"+paramArray[1]+"','"+paramArray[4]+"',"+el+");"

	if(paramArray[1].substring(0,5)=="show-"){
		im++;
		ims="im"+im+"_"+a+"_"+el+"_"+paramArray[4];
	
		subimg="<img id="+ims+" style=\"position:absolute;\" onmouseover=\"fFindObject('el"+el+"').onmouseover();\" onmouseout=\"fFindObject('el"+el+"').onmouseout();\" border=0>"
	}
	omp=mp;

	ltarg=" ";
	lOnfunc="";
	lOffunc="";
	ltype="";
	
	if(strURL.indexOf(" ")>0){
		ltparamArray=parseLink(strURL,"target");
		if(ltarg)popUp=1;

		lOnfunc=parseLink(strURL,"onfunction")+";";
		lOffunc=parseLink(strURL,"offfunction")+";";
		ltype=parseLink(strURL,"type");
		strURL=parseLink(strURL,"link");
	}
	
	if(paramArray[6]+""=="undefined"){paramArray[6]=strURL}
	if(ltype=="form"){mp="default"}
	smO="ShM=1;";

	if(ltype=="form"||ltype=="header"){
		mp="default";
		strURL=" "
	}

	pd="fHideMenu(this," + el + ", '" + paramArray[4] + "', '"+paramArray[1]+"');"
	
	if(paramArray[0].indexOf("<img")>=0)
	{
		ipos=paramArray[0].indexOf("<img");
		paramArray[0]=paramArray[0].substr(0,ipos+4)+" border=0 onclick=\"fFindObject('el"+el+"').click()\" id=im"+el+" "+ paramArray[0].substr(ipos+5,900);
	}
	
	mt="";
			
	dw=(menu[3]-(dwd))
	if(dw+" "=="NaN "){dw=""}else{dw="width:"+dw+"px"}
	if(!menu[3]&&ie4)dw="width:1px";
	
	if (subimg.length > 0){
		hoverItemClass='menuSubItemHover';
		itemClass='menuSubItem'
	}else{
		hoverItemClass='menuItemHover';
		itemClass='menuItem'
	}

	mt+="<div id=el"+el+" class='" + itemClass + "'"
	mt+=" onMouseOver=\"fSwapClass(this,'" + hoverItemClass + "');"+smO+"; "+pI+";"+lOnfunc+";return fSetStatusBarText('"+strURL+"');\""
	mt+=" onClick=\"GotoUrl('"+strURL+"','"+popUp+"');\""	
	mt+=" onMouseOut=\"fSwapClass(this,'" + itemClass + "');fSwapImg(); "+pd+";"+lOffunc+"; return fSetStatusBarText('');\">"	
	
	mt+=paramArray[0]
	mt+="</div>";
		
	mp=omp
	document.write(mt);el++;
}


function fDrawMenu(mnu){
	menu=eval(mnu);

	menu[7]=new Array()
	if(menu[4]){parr[parr.length]=mnu.substr(4,3);}

	eq=":"
	
	if(el)ns6c=3;else ns6c=2;ns6w=0;
	if (menu[3]<1)menu[3]="undefined"
	
		iedf="";
		if(!opera&&!mac&&document.all&&menutransition) {
			iedf=" filter:";flt=menutransition.split("\;");
			
			for(fx=0;fx<flt.length;fx++){
				iedf+=" progid:DXImageTransform.Microsoft."+flt[fx]
			}
		}
		mt="<div class=\"menuBorder\" id="+mnu+" style=\""+iedf+";z-index:40;visibility:hidden;position:absolute;width:"+(menu[3]+ns6w)+"px;left:"+menu[2]+"px;top:139px;\">"
		
	document.write(mt)

	x=0;
	dlft=0
	
	for(b=ac;b<menu.length;b++)
	{
		b=b+4

		if(menu[b-3]==""){menu[b-3]="#"}
		menu[b-3]=menu[b-3].replace(/\"/gi, "&quot;")
		if(!menu[b])menu[b]=0
		hl(menu[b-4], menu[b-3],100,100,mnu,menu[b-2],menu[b-1],menu[b-5])
		txt="";
			
			mbw=menu[b];bl="";oc="";
			if(b+1==menu.length)mbw=0;
			
			if(mbw>0 && menuDivider==1)
			{
				
				dwid=(menu[3])
					
				txt="<div style=\"position:relative;\">";
				txt+="<div class=\"menuDivider\" onmouseout=\"fHideMenu();"+oc+"\" onmouseover=\"clearTimeout(Mtimer)\" style=\"width:"+dwid+"px;position:absolute;"
				txt+="clip:rect(0 100% "+mbw+"px 0);height:"+mbw+"px;"+bl+"\">"
				txt+="</div>"
				txt+="</div>"
			}

		document.write(txt);
		
		mt=""
	}
	

	mt+="</div>"
	document.write(mt)
	menu[7][1]=el
}

function fSetMenuPosition(mn)
{
	//sets the position of the menu
	if(ns6)pos="relative";else pos="absolute";
	mnt="<div id=\""+mn+"DIV\" style=\"width:1;height:1;position:"+pos+"\"></div>";
	menu=eval("menu"+fgetMenuByName(mn));
	menu[7][0]=mn+"DIV";
	document.write(mnt)
}

function fMenuScan(){
//function continuously runs and checks for window resize to reposition menu items
//ww= window width, wh= window height

	sy=self.pageYOffset;
	if(opera){
		ww=window.innerWidth;
		wh=window.innerHeight;
	}else if(document.all){
		sy=document.body.scrollTop;
		if(sy==0&&!ie4&&!mac)sy=document.documentElement.scrollTop;
		if(document.compatMode=="CSS1Compat"){
			ww=document.documentElement.clientWidth;
			wh= document.documentElement.clientHeight
		}else{
			ww=document.body.clientWidth;
			wh=document.body.clientHeight;	
		}
	}else{
		sy=pageYOffset;
		
		if (ns6){
			//for mozilla and ns6 use document Element
			ww=document.documentElement.clientWidth;
		}else{
			ww=document.body.offsetWidth;	
		}
		
		if(!ww)ww=self.innerWidth-15;
		ww--;
		wh=self.innerHeight;
		
	}
	if(MLoaded>1&&oww>0&&(ns4||opera)&&(owh!=wh||oww!=ww)){
		if(nsmatch)location.reload()
	}
	if(MLoaded==1&&owh==wh&&oww==ww)nsmatch=1;
	if(opera)nsmatch=1;
	
	if(MLoaded==1||(owh!=wh||oww!=ww)){
		setTopLevelMenuPositions();
		
		MLoaded=2;
	}
	if(MLoaded){
		oww=ww;
		owh=wh;
	}
	mspu=setTimeout('fMenuScan()',250)
}

function fMenuInit()
{
	el=0;df=-1;
	//writes out additional filter script
		
	for(x=0;x<parr.length;x++){
		fShowMenu(parr[x]);
	}

	if(!opera)fMenuScan();
	document.onmousemove=fGetMouseScreenPos;
}

function getflta(ap){
	try {
		if(ap.filters){
			flta=1
		}
	}catch(e){
	
	}
}

function GetElementLeft(eElement)
{
    if (!eElement && this)                       // if argument is invalid
    {                                            // (not specified, is null or is 0)
        eElement = this;                         // and function is a method
    }                                            // identify the element as the method owner
    
    var nLeftPos = parseInt(eElement.offsetLeft);          // initialize var to store calculations

    var eParElement = eElement.offsetParent;     // identify first offset parent element  
    while (eParElement != null)
    {                                            // move up through element hierarchy
        nLeftPos += parseInt(eParElement.offsetLeft);      // appending left offset of each parent
        eParElement = eParElement.offsetParent;  // until no more offset parents exist
    }
    
    return nLeftPos;                             // return the number calculated
}

function GetElementTop(eElement)
{
    if (!eElement && this)
    {
        eElement = this;
    }

    var nTopPos = eElement.offsetTop;
    var eParElement = eElement.offsetParent;
    while (eParElement != null)
    {
        nTopPos += parseInt(eParElement.offsetTop);
        eParElement = eParElement.offsetParent;
    }
    if (mac &&  typeof document.body.topMargin != "undefined") {
        nTopPos += parseInt(document.body.topMargin);
    }

    return nTopPos;
}

	for(a=1;a<menus;a++){
		fDrawMenu("menu"+a) 
	}

function menuOnload() {
	var menuImageObject;

	setTopLevelMenuPositions();

	if(!ns61&&ns6){
		fMenuInit()
	}
	
	MLoaded=1;
	fMenuScan()
}

function fSwapClass(object,classname){
	
	if (object.className!="menuItemDelete"){
		object.className = classname;
	}
}

function GotoUrl(url,popUp){
	while (url.indexOf('&nbsp;')>-1){
		url=url.replace('&nbsp;',' ');
	}

	if (popUp=='1'){
		window.open(url);
	}else{
		window.location = url;
	}
}

function checkPos(){
	if(!mac){
		menuOnload();
	}
}
