//Cntr 	--> Container
//f 	--> first
//mem 	--> member
//ch 	--> child
//m 	--> main
//lvl	--> level
//cr	--> create

var userAgent=navigator.userAgent.toLowerCase();
var dom=(document.getElementById)?1:0;
var ns=(userAgent.indexOf('mozilla')!=-1&&userAgent.indexOf('compatible')==-1)?1:0;
var ie=(userAgent.indexOf('msie')!=-1)?1:0;
var op5=(userAgent.indexOf('opera 5')!=-1||userAgent.indexOf('opera/5')!=-1)?1:0;
var domns=(dom&&ns)?1:0;
var domie=(dom&&ie)?1:0;
var ns4=(ns&&!dom&&document.layers)?1:0;
var ie4=(ie&&!dom&&document.all)?1:0;
var zTop=100;
var strHide=(ns4)?'hide':'hidden';
var strShow=(ns4)?'show':'visible';
var fWinWidth,fWinHeight;
var loc_ = "window";
var loc = eval(loc_);

function ConfMenu(MName,ID) {
	var MName,HName;
	this.MName=MName;
	var ConfID
	this.ConfID=ID;
	
	var crLvl,fCntr,currCntr;
	var isShowing,disTimer,disDelay,isPosing;
	this.isPosing=0;
	this.crLvl=0;
	this.fCntr=null;
	this.currCntr=null;
	this.isShowing=0;
	this.disTimer=null;
	this.disDelay=500;

	var BgColor1,SubBgColor1,BgColor2,SubBgColor2,BorderColor,SubBorderColor,BorderWidth;
	var MainBorderWidth,SubBorderWidth,BorderBetween,MainBorderBetween,SubBorderBetween;
	var FontColor1,SubFontColor1,FontColor2,FontFamily,FontSize,FontWeight,FontStyle,MenuTextAlignment;

	this.BgColor1='E5E5E5';
	this.SubBgColor1='F0F0F0';
	this.BgColor2='white';
	this.SubBgColor2='C3C3C3';
	this.BorderColor='black';
	this.SubBorderColor='8E8E8E';
	this.MainBorderWidth=0;
	this.SubBorderWidth=1;
	this.MainBorderBetween=0;
	this.SubBorderBetween=1;
	this.FontColor1='black';
	this.SubFontColor1='black';
	this.FontColor2='black';
	this.SubFontColor2='black';
	this.FontFamily="verdana"
	this.FontSize="8pt";
	this.FontWeight="normal";
	this.FontStyle="normal";
	this.MenuTextAlignment='left';
	
	var mMenuItemCount,menuStartTop,menuStartLeft,chOverlap,mMenuHorizantal;
	this.mMenuItemCount=7
	this.menuStartTop=77
	this.menuStartLeft=307
	this.chOverlap=.0;
	this.mMenuHorizantal=1;
}

function getCurrConf(id) {
	return eval(Confs[id]);
}

//main function
function Goo(ConfID) {
	var Conf;
	Conf = getCurrConf(ConfID)
	fWinWidth=(ie)?loc.document.body.clientWidth:loc.innerWidth;
	fWinHeight=(ie)?loc.document.body.clientHeight:loc.innerHeight;
	Conf.fCntr=createMenu(Conf.MName,Conf.mMenuItemCount,ConfID);
	PosMenu(Conf.fCntr,Conf.menuStartTop,Conf.menuStartLeft,ConfID);
	Conf.isShowing=1;
	Initiate(ConfID);
	((ns)?loc:loc.document.body).onresize=(ns)?ReGoo:RePosMenu;
}

function RePosMenu() {
	var Conf,i;
	fWinWidth=(ie)?loc.document.body.clientWidth:loc.innerWidth;
	fWinHeight=(ie)?loc.document.body.clientHeight:loc.innerHeight;
	for(i=0;i<Confs.length;i++) {
		Conf=getCurrConf(i);
		PosMenu(Conf.fCntr,Conf.menuStartTop,Conf.menuStartLeft,Conf.ConfID);
	}
}

function ReGoo() {
	if(loc.innerWidth!=fWinWidth || loc.innerHeight!=fWinHeight) {
		loc.location.reload();
	}
}

//Initiate Init for all layers
function Initiate(ConfID) {
	var Conf;
	Conf = getCurrConf(ConfID)
	if(Conf.isShowing) { 
		Init(Conf.fCntr,ConfID);
		Conf.isShowing=0;
	}
}

//initilazi layers in container
function Init(CntnrPntr,ConfID) {
	var Conf;
	Conf = getCurrConf(ConfID)
	var elem=CntnrPntr.fElem;
	var elemStyle=(ns4)?CntnrPntr:CntnrPntr.style;
	Conf.crLvl++;
	elemStyle.visibility=(Conf.crLvl==1)?strShow:strHide;
	CntnrPntr.showFlg=(Conf.crLvl==1)?1:0;
	while(elem!=null) {
		if(elem.chCntr) {
			Init(elem.chCntr,ConfID);
		}
		elem=elem.prevElem;
	}
	Conf.crLvl--;
}

//position the menu layers
function PosMenu(cntrPtr,top,left,ConfID) {
	this.isPosing=1; 
	var Conf;
	Conf = getCurrConf(ConfID)
	Conf.crLvl++;
	ChangeBorders(ConfID);

	var topi,lefti,isHorizantal;
	var cntr=cntrPtr;
	var elem=cntr.fElem;
	var cntrStyle=(!ns4)?cntr.style:cntr;
	var elemStyle=(!ns4)?elem.style:elem;
	var elemWidth=(!ns4)?parseInt(elemStyle.width):elemStyle.clip.width;
	var elemHeight=(!ns4)?parseInt(elemStyle.height):elemStyle.clip.height;
	var cntrWidth=(!ns4)?parseInt(cntrStyle.width):cntrStyle.clip.width;
	var cntrHeight=(!ns4)?parseInt(cntrStyle.height):cntrStyle.clip.height;
	var subTop,subLeft;
	cntrStyle.top=(Conf.crLvl==1)?top:0;cntr.OrgTop=top;
	cntrStyle.left=(Conf.crLvl==1)?left:0;cntr.OrgLeft=left;
	if(Conf.crLvl==1 && Conf.mMenuHorizantal) {
		isHorizantal=1;
		lefti=cntrWidth-elemWidth-2*Conf.BorderWidth;
		topi=0;
	} else {
		isHorizantal=0;
		lefti=0;
		topi=cntrHeight-elemHeight-2*Conf.BorderWidth;
	}
	while(elem!=null) {
		ChangeBorders(ConfID);
		elemStyle.left=lefti+Conf.BorderWidth;
		elemStyle.top=topi+Conf.BorderWidth;
		if(ns4)elem.cLayer.moveTo(lefti+Conf.BorderWidth,topi+Conf.BorderWidth);
		if(elem.chCntr) {
			if(isHorizantal) {	
				subLeft=lefti;
				subTop=topi+elemHeight+2*Conf.BorderWidth;
			} else {
				subLeft=lefti+(1-Conf.chOverlap)*elemWidth+Conf.BorderWidth;
				subTop=topi+Conf.chOverlap*elemHeight;
			}
			PosMenu(elem.chCntr,subTop,subLeft,ConfID);
		}
		elem=elem.prevElem;
		if(elem) {	
			ChangeBorders(ConfID);
			elemStyle=(!ns4)?elem.style:elem;
			elemWidth=(!ns4)?parseInt(elemStyle.width):elemStyle.clip.width;
			elemHeight=(!ns4)?parseInt(elemStyle.height):elemStyle.clip.height;
			(isHorizantal)?lefti-=(Conf.BorderBetween)?(elemWidth+Conf.BorderWidth):(elemWidth):topi-=(Conf.BorderBetween)?(elemHeight+Conf.BorderWidth):(elemHeight);
		}
	}
	Conf.crLvl--;
	this.isPosing=0; 
}


function ChangeBorders(ConfID) {
	var Conf;
	Conf = getCurrConf(ConfID)
	if(Conf.crLvl==1) {
		Conf.BorderWidth = Conf.MainBorderWidth;
		Conf.BorderBetween = Conf.MainBorderBetween;
	} else {
		Conf.BorderWidth = Conf.SubBorderWidth;
		Conf.BorderBetween = Conf.SubBorderBetween;
	}
}


//creates layers
function createMenu(mId,numberOfSub,ConfID) {
	var Conf;
	Conf = getCurrConf(ConfID)
	Conf.crLvl++;
	ChangeBorders(ConfID);
	
	var i,w,h,elem;
	var mCntr;
	var prevElem=null;
	var currMenu=mId+'1';
	var totalWidth=0;
	var totalHeight=0;
	var _Width=eval(currMenu+'[4]');
	var _Height=eval(currMenu+'[3]')
	
	for(i=1;i<numberOfSub+1;i++) {
		currMenu=mId+i;
		totalWidth=eval(currMenu+'[4]')?totalWidth+eval(currMenu+'[4]'):totalWidth+_Width;
		totalHeight=eval(currMenu+'[3]')?totalHeight+eval(currMenu+'[3]'):totalHeight+_Height;
	}
	if(Conf.crLvl==1 && Conf.mMenuHorizantal) {
		totalWidth=(Conf.BorderBetween)?totalWidth+(numberOfSub+1)*Conf.BorderWidth:totalWidth+2*Conf.BorderWidth;
		totalHeight=_Height+2*Conf.BorderWidth;
	} else {	
		totalWidth=_Width+2*Conf.BorderWidth;
		totalHeight=(Conf.BorderBetween)?totalHeight+(numberOfSub+1)*Conf.BorderWidth:totalHeight+2*Conf.BorderWidth;
	}
	if(dom) {
		mCntr=loc.document.createElement("div");
		mCntr.style.position='absolute';
		mCntr.style.visibility='hidden';
		loc.document.body.appendChild(mCntr)
	} else if(ns4) {
			mCntr=new Layer(totalWidth,loc)
	} else {
		loc.document.body.insertAdjacentHTML("AfterBegin","<div id='cAkMenu' style='visibility:hidden; position:absolute;'><\/div>");
		mCntr=loc.document.all['cAkMenu']
	}
	mCntr.setUp=cntnrSetUp;
	mCntr.setUp(totalWidth,totalHeight,ConfID);
	if(ie4) {	
		mCntr.InnerString='';
		for(i=1;i<numberOfSub+1;i++) {
			mCntr.InnerString+="<div id='AkMenu"+i+"' style='position:absolute;'><\/div>";
		}
		mCntr.innerHTML=mCntr.InnerString;
	}
	for(i=1;i<numberOfSub+1;i++) {
		currMenu=mId+i;
		w=eval(currMenu+'[4]')?eval(currMenu+'[4]'):_Width;
		h=eval(currMenu+'[3]')?eval(currMenu+'[3]'):_Height;
		if(dom) {
			elem=loc.document.createElement("div");
			elem.style.position='absolute';
			elem.style.visibility='inherit';
			mCntr.appendChild(elem);
		} 
		else if(ns4) {
			elem=new Layer(w,mCntr);
		}
		else {
			elem=loc.document.all['AkMenu'+i];
		}
		elem.SetUp=(ns4)?memSetUp_ns4:memSetUp;
		elem.SetUp(mCntr,prevElem,currMenu,w,h,ConfID);
		if(eval(currMenu+'[2]')) {
			ChangeBorders(ConfID);
			elem.chCntr=createMenu(currMenu+'_',eval(currMenu+'[2]'),ConfID);
		}
		prevElem=elem;
	}
	mCntr.fElem=elem;
	Conf.crLvl--;
	return(mCntr);
}

//container layer setup function
function cntnrSetUp(w,h,ConfID) {
	var Conf;
	Conf = getCurrConf(ConfID)
	this.ConfID = ConfID;
	this.fElem=null;
	this.OrgTop=0;this.OrgLeft=0;
	this.showFlg=0;
	var bg=(Conf.crLvl==1)?Conf.BorderColor:Conf.SubBorderColor;
	if(bg)this.bgColor=bg;
	if(ns4) {
		this.visibility=strHide;
		this.resizeTo(w,h);
	}
	else {
		if(bg)this.style.backgroundColor=bg;
		this.style.fontFamily=Conf.FontFamily;
		this.style.fontWeight=(Conf.FontWeight)?Conf.FontWeight:'normal';
		this.style.fontStyle=(Conf.FontStyle)?Conf.FontStyle:'normal';
		this.style.fontSize=Conf.FontSize;
		this.style.width=w;
		this.style.height=h;
		this.style.zIndex=Conf.crLvl+zTop;
	}
}


//layer setup function text,font,high lights for ns4
function memSetUp_ns4(cntr,pelem,mname,w,h,ConfID) {
	var Conf;
	Conf = getCurrConf(ConfID)
	this.chCntr=null;
	this.ConfID = ConfID;
	this.value=eval(mname+'[0]');
	this.rollover=0;
	if(this.value.indexOf('img')!=-1) {
		this.rollover=1;
		this.img1=this.value.substring(this.value.indexOf(':')+1,this.value.lastIndexOf(':'));
		this.img2=this.value.substring(this.value.lastIndexOf(':')+1,this.value.length);
		this.imgName=mname+'img';
		this.value="<img src='"+this.img1+"' name='"+this.imgName+"'>"
	}
	this.prevElem=pelem;
	this.visibility='inherit';
	this.resizeTo(w,h);
	if(Conf.crLvl==1) {
		this.BgColor1=Conf.BgColor1;
		this.BgColor2=Conf.BgColor2;
		this.FontColor1=Conf.FontColor1;
		this.FontColor2=Conf.FontColor2;
	} else {
		this.BgColor1=Conf.SubBgColor1;
		this.BgColor2=Conf.SubBgColor2;
		this.FontColor1=Conf.SubFontColor1;
		this.FontColor2=Conf.SubFontColor2;
	}
	if(this.BgColor1)this.bgColor=this.BgColor1;
	if(FontWeight.toLowerCase()=="bold")this.value=this.value.bold();
	if(FontWeight.toLowerCase()=="italic")this.value=this.value.italics();
	if(this.FontColor1)this.value=this.value.fontcolor(this.FontColor1);
	this.value2=this.value;
	this.value="<font face='"+Conf.FontFamily+"' point-size='"+Conf.FontSize+"'>"+this.value+"</font>";
	this.value="<div align='"+Conf.MenuTextAlignment+"'>"+this.value+"</div>";
	if(this.FontColor2)this.value2=this.value.fontcolor(this.FontColor2);
	this.value2="<font face='"+Conf.FontFamily+"' point-size='"+Conf.FontSize+"'>"+this.value2+"</font>";
	this.value2="<div align='"+Conf.MenuTextAlignment+"'>"+this.value2+"</div>";
	this.document.write(this.value);
	this.document.close();
	this.cLayer=new Layer(w,cntr);
	this.cLayer.Level=Conf.crLvl;
	this.cLayer.Href=eval(mname+'[1]');
	this.cLayer.visibility='inherit';
	this.cLayer.onmouseover=OpenMenu;
	this.cLayer.onmouseout=CloseMenu;
	this.cLayer.captureEvents(Event.MOUSEUP);
	this.cLayer.onmouseup=GoTo;
	this.cLayer.lowLayer=this;
	this.cLayer.resizeTo(w,h);
	this.cLayer.Container=cntr;
	if(eval(WhatMenu+'[3]') && Conf.crLvl!=1) {
		this.cLayer.OkLayer=new Layer(7,this.cLayer);
		this.cLayer.OkLayer.visibility='inherit';
		this.cLayer.OkLayer.top=h/2-5;
		this.cLayer.OkLayer.left=w-7;
		this.cLayer.OkLayer.width=7;
		this.cLayer.OkLayer.height=10;
		ImgStr="<img src='images/MenuOK2.gif'>";
		this.cLayer.OkLayer.document.write(ImgStr);
		this.cLayer.OkLayer.document.close();
	}
}


//layer setup function text,font,high lights
function memSetUp(cntr,pelem,mname,w,h,ConfID) {
	var Conf;
	Conf = getCurrConf(ConfID)
	this.ConfID= ConfID;
	var mValue=eval(mname+'[0]');
	this.prevElem=pelem;
	this.Level=Conf.crLvl;
	this.Href=eval(mname+'[1]');
	this.Container=cntr;
	this.chCntr=null;
	this.style.overflow='hidden';
	this.style.cursor=(ie&&this.Href)?'hand':'default';
	this.style.width=w;
	this.style.height=h;
	this.rollover=0;
	if(mValue.indexOf('img')!=-1) {
		this.rollover=1;
		this.img1=mValue.substring(mValue.indexOf(':')+1,mValue.lastIndexOf(':'));
		this.img2=mValue.substring(mValue.lastIndexOf(':')+1,mValue.length);
		this.imgName=mname+'img';
		mValue="<img src='"+this.img1+"' name='"+this.imgName+"'>"
	}
	if(Conf.crLvl==1) {
		this.BgColor1=Conf.BgColor1;
		this.BgColor2=Conf.BgColor2;
		this.FontColor1=Conf.FontColor1;
		this.FontColor2=Conf.FontColor2;
	} else {
		this.BgColor1=Conf.SubBgColor1;
		this.BgColor2=Conf.SubBgColor2;
		this.FontColor1=Conf.SubFontColor1;
		this.FontColor2=Conf.SubFontColor2;
	}
	if(this.BgColor1)this.style.backgroundColor=this.BgColor1;
	if(this.FontColor1)this.style.color=this.FontColor1;
	this.style.textAlign=Conf.MenuTextAlignment;
	
	if(mValue.indexOf('<')==-1&&dom) {
		var t=loc.document.createTextNode(mValue);
		this.appendChild(t);
	} else {
		this.innerHTML=mValue;
	}
	if(eval(mname+'[2]') && Conf.crLvl!=1) {
		if(dom) {
			var t;
			t=loc.document.createElement('img');
			this.appendChild(t);
			t.style.position='absolute';
			t.src='images/MenuOK2.gif';
			t.style.width=5;
			t.style.height=10;
			t.style.top=h/2-5;
			t.style.left=w-7;
		}
		else {
			mValue+="<div style='position:absolute; top:"+h/2-5+"; left:"+w-7+"; width:7; height:10;visibility:inherit'><img src='images/MenuOK2.gif'></div>";
			this.innerHTML=mValue;
		}
	}
	if(ie) {
		this.onmouseover=OpenMenu;
		this.onmouseout=CloseMenu; 
		this.onclick=GoTo;
	} else {
		this.addEventListener('mouseover',OpenMenu,false);
		this.addEventListener('mouseout',CloseMenu,false); 
		this.addEventListener('click',GoTo,false);
	}
}


//change bgColor,fontcolor,over img
function mouseOver() {
	if(ns4) {
		if(this.lowLayer.rollover) {
			this.lowLayer.document.images[this.lowLayer.imgName].src=this.lowLayer.img2;
		} else {
			if(this.lowLayer.BgColor2)this.lowLayer.bgColor=this.lowLayer.BgColor2;
			this.lowLayer.document.write(this.lowLayer.value2);
			this.lowLayer.document.close();
		}
	} else {
		if(this.rollover) {
			loc.document.images[this.imgName].src=this.img2;
		} else {
			if(this.BgColor2)this.style.backgroundColor=this.BgColor2;
			if(this.FontColor2)this.style.color=this.FontColor2;
		}
	}
}

//change bgColor,fontcolor,over img
function mouseOut() {
	if(ns4) {
		if(this.lowLayer.rollover) {
			this.lowLayer.document.images[this.lowLayer.imgName].src=this.lowLayer.img1;
		} else {
			if(this.lowLayer.BgColor1)this.lowLayer.bgColor=this.lowLayer.BgColor1;
			this.lowLayer.document.write(this.lowLayer.value);
			this.lowLayer.document.close();
		}
	} else {
		if(this.rollover) {
			loc.document.images[this.imgName].src=this.img1;
		} else {
			if(this.BgColor1)this.style.backgroundColor=this.BgColor1;
			if(this.FontColor1)this.style.color=this.FontColor1;
		}
	}
}

//link
function GoTo() {
	if(this.Href){
		(this.Href.indexOf('javascript:')!=-1)?eval(this.Href):loc.location.href=this.Href;
	}
}

//auto close menu
function CloseMenu() {
	var Conf;
	Conf = getCurrConf(this.ConfID)
	this.mOut=mouseOut;
	this.mOut();
	if(this==Conf.currCntr) {
		Conf.isShowing=1;
		if(Conf.disTimer) {
			clearTimeout(Conf.disTimer);
		}
		Conf.disTimer=setTimeout('Initiate('+Conf.ConfID+')',Conf.disDelay);
	}
}

//Hides all child execpt one in container
function HideAllChilds(Pntr,ChldPntr,ConfID) {
	var Conf;
	Conf = getCurrConf(ConfID)
	if(Conf.disTimer) {
		clearTimeout(Conf.disTimer);
	}
	var CPCCStyle;
	while (Pntr){
		if(Pntr.chCntr&&Pntr.chCntr.showFlg) {
			CPCCStyle=(ns4)?Pntr.chCntr:Pntr.chCntr.style;
			if(Pntr.chCntr!=ChldPntr) {
				CPCCStyle.visibility=strHide;Pntr.chCntr.showFlg=0;
			}
			HideAllChilds(Pntr.chCntr.fElem,ChldPntr,ConfID);
		}
		Pntr=Pntr.prevElem;
	}
}

//Opens child menus
function OpenMenu() {
	if(this.isPosing==1)return;
	var chCntr=(ns4)?this.lowLayer.chCntr:this.chCntr;
	var ThisHt=(ns4)?this.clip.height:parseInt(this.style.height);
	var ThisWt=(ns4)?this.clip.width:parseInt(this.style.width);
	var ThisLft=(ns4)?this.Container.left:parseInt(this.Container.style.left);
	var ThisTp=(ns4)?this.Container.top:parseInt(this.Container.style.top);
	var Conf;
	Conf = getCurrConf(this.ConfID)
	Conf.currCntr=this;
	Conf.isShowing=0;
	this.mOver=mouseOver;
	this.mOver();
	HideAllChilds(this.Container.fElem,chCntr,Conf.ConfID);
	if(chCntr!=null){
		if(!Conf.isShowing){Conf.isShowing=1;}
		chCntr.showFlg=1;
		var chCntrStyle=(ns4)?this.lowLayer.chCntr:this.chCntr.style;
		chCntrStyle.top=chCntr.OrgTop+ThisTp;
		chCntrStyle.left=chCntr.OrgLeft+ThisLft;
		chCntrStyle.visibility=strShow;
	}
}
