/* Script ONELEVELMENUFULL created by: Vladimir CALDERON --- 11-2001
 * Files: 
 * mymenu.js     : Commands and functions
 * mymenu_var.js : Variables and customization
 * readme.txt    : Instructions for installation and use.
 */
 
/* To know which navigator we are using */
	var AgntUsr=navigator.userAgent.toLowerCase();
	var DomYes=(document.getElementById)?1:0;				// DOM
	var NavYes=(AgntUsr.indexOf('mozilla')!=-1&&AgntUsr.indexOf('compatible')==-1)?1:0;	// Netscape
	var ExpYes=(AgntUsr.indexOf('msie')!=-1)?1:0;				// IExplorer
	var Opr5=(AgntUsr.indexOf('opera 5')!=-1||AgntUsr.indexOf('opera/5')!=-1)?1:0;	// Opera 5
	var DomNav=(DomYes&&NavYes)?1:0;					// Netscape 6 up
 	var DomExp=(DomYes&&ExpYes)?1:0;					// IExplorer 5 up
	var Nav4=(NavYes&&!DomYes&&document.layers)?1:0;			// Netscape 4
	var Exp4=(ExpYes&&!DomYes&&document.all)?1:0;				// IExplorer 4
	var PosStrt=((NavYes||ExpYes)&&!Opr5)?1:0;				// Can start

/* First of all we load menus in page */
	var FirstLocation,SecLocation,DocLocation;
	var SecWindowWidth,FirstWindowWidth,FirstWindowHeight;
	var FirstColPos,SecColPos,DocColPos;
	// Variables to know which level in recursion are we
	var menu_level = 0;
	var ShowFlag=0,FirstCreate=1,Loaded=0,Created=0,AcrossFrames=1;
	var MouseOnButton = 0;
	var InitFlag = 0;
	// The MenuContainers
	var MenuCntnr = new Array();
	var CurrentOver=null;
	var CenterText,TxtClose,ImgStr;
	var Ztop=100;
	
	var verticalCorrection = (NavYes)?0:8;

/* these variables cheange their value depending on the browser used */
	var LeftExtra=(DomNav)?LeftPaddng:0;
	var TopExtra=(DomNav)?TopPaddng:0;
	var M_Hide=(Nav4)?'hide':'hidden';
	var M_Show=(Nav4)?'show':'visible';
	var Par=(parent.frames[0])?parent:window;
	var Doc=Par.document;
	var Bod=Doc.body;
	//************ Next line bugfix 4 July 2001 ***************//
	/* If (parent frames exist AND FirstLineFrame == SecLineFrame)  ---> 
		AND netscape? ---> THEN trigger = parent.frames[FirstLineFrame]
			  - otherwise -> trigger = parent.frames[FirstLineFrame].document.body
		- otherwise -> If Netscape? ---> THEN trigger= Par  -- otherwise-> trigger = Bod */
	var Trigger=(parent.frames[0]&&FirstLineFrame==SecLineFrame)?(NavYes)?parent.frames[FirstLineFrame]:parent.frames[FirstLineFrame].document.body:(NavYes)?Par:Bod;
	WebMasterAlerts=["No such frame: ","Item not defined: ","Item needs height: ","Item needs width: ","Warning-Item doesn't need height: ","Warning-Item doesn't need width: ","Item Oke ","Menu tree oke"];
	
	/* Calls Go to menus to be written to page */
	if(DomNav&&!Opr5)Trigger.addEventListener('load',Go,false);
	else Trigger.onload=Go;
	/* If we resize window, redo the whole thing */
	Trigger.onresize=(Nav4)?ReDoWhole:RePos;

/* To repositionate the menus */
function RePos(){
	FirstWindowWidth=(ExpYes)?FirstLocation.document.body.clientWidth:FirstLocation.innerWidth;
	FirstWindowHeight=(ExpYes)?FirstLocation.document.body.clientHeight:FirstLocation.innerHeight;
	SecWindowWidth=(ExpYes)?SecLocation.document.body.clientWidth:SecLocation.innerWidth;
	SecWindowHeight=(ExpYes)?SecLocation.document.body.clientHeight:SecLocation.innerHeight;

	for(i=1; i<=VV_NbMenus; i++)
		PositionMenuStruct(MenuCntnr[i-1],
				   eval('VV_Menu'+eval(i)+'_Top'),
				   eval('VV_Menu'+eval(i)+'_Left'));
}

/* To reload the whole thing */
function ReDoWhole(){
	Doc.location.reload()}
	
/* This function writes the menus in the page, and then we just play with visibility */
function Go() {
	// If it's already loaded, do nothing
	if(Loaded||!PosStrt)return;
	
	// We mark it as loaded and we create it
	Created=0; Loaded=1;
	status='Building menu';
	if(FirstCreate){
		// 'Dummy' tests to see if there is a frame
		if(FirstLineFrame =="" || !parent.frames[FirstLineFrame]){ 
			FirstLineFrame=SecLineFrame}
		if(FirstLineFrame =="" || !parent.frames[FirstLineFrame]){
			FirstLineFrame=SecLineFrame=DocTargetFrame}
		if(FirstLineFrame =="" || !parent.frames[FirstLineFrame]){
			 FirstLineFrame=SecLineFrame=DocTargetFrame=(parent.frames[0])?parent.frames[0].name:''}
		if(SecLineFrame =="" || !parent.frames[SecLineFrame])
			SecLineFrame=DocTargetFrame;
		if(SecLineFrame =="" || !parent.frames[SecLineFrame])
			SecLineFrame=DocTargetFrame=FirstLineFrame;
		if(DocTargetFrame =="" || !parent.frames[DocTargetFrame])
			DocTargetFrame=SecLineFrame;
		
		FirstLocation=(FirstLineFrame)?parent.frames[FirstLineFrame]:window;
		SecLocation=(SecLineFrame)?parent.frames[SecLineFrame]:window;
		DocLocation=(DocTargetFrame)?parent.frames[DocTargetFrame]:window;
		if (FirstLocation==SecLocation) AcrossFrames=0;
		if (AcrossFrames)FirstLineHorizontal=(MenuFramesVertical)?0:1;
		FirstWindowWidth=(ExpYes)?FirstLocation.document.body.clientWidth:FirstLocation.innerWidth;
		FirstWindowHeight=(ExpYes)?FirstLocation.document.body.clientHeight:FirstLocation.innerHeight;
		SecWindowWidth=(ExpYes)?SecLocation.document.body.clientWidth:SecLocation.innerWidth;
		SecWindowHeight=(ExpYes)?SecLocation.document.body.clientHeight:SecLocation.innerHeight;

		// Set Beginning of text if menu centered
		if(!DomYes) {
			CenterText=(MenuTextCentered)?(NavYes)?"<div align='center'>":"align='center'":"";
			if(NavYes)
				TxtClose="</font>"+(MenuTextCentered)?"</div>":"";
		}
	}
	FirstColPos=(Nav4)?FirstLocation.document:FirstLocation.document.body;
	SecColPos=(Nav4)?SecLocation.document:SecLocation.document.body;
	DocColPos=(Nav4)?DocLocation.document:SecLocation.document.body;
			
	// We write each menu
	var PreviousMember=null;
	if(FirstCreate) {
		for(i=1; i<=VV_NbMenus; i++) {
			// If first time, we create the menu structure, with number of sub_menus
			MenuCntnr[i-1] = CreateMenuStructure('VV_Menu'+eval(i), i);
		}
		FirstCreate = 0;
	}
	else {
		for(i=1; i<=VV_NbMenus; i++)
			CreateMenuStructureAgain('VV_Menu'+eval(i));
	}

	for(i=1; i<=VV_NbMenus; i++)
		PositionMenuStruct(MenuCntnr[i-1],
						   eval('VV_Menu'+eval(i)+'_Top'),
						   eval('VV_Menu'+eval(i)+'_Left'));
	Created = 1;
	
	// We've got to positionate the menus, each menu has an absolute position
	status = 'Menu ready to use';
}

/* This function will position the menu to the x,y coordinates given when constructing menu */
// CntnrPntr Container
// Tp Space in top
// Left Space at the left
function PositionMenuStruct(CntnrPntr,Tp,Lt){
	var Topi,Lefti,Hori;
	var Cntnr=CntnrPntr;
	var Mmbr=Cntnr.FirstMember;
	var CntnrStyle=(!Nav4)?Cntnr.style:Cntnr;
	var MmbrStyle=(!Nav4)?Mmbr.style:Mmbr;
	
	var PadLeft=(Mmbr.value.indexOf('<')==-1)?LeftExtra:0;
	var PadTop=(Mmbr.value.indexOf('<')==-1)?TopExtra:0;
	var MmbrWidth=(!Nav4)?parseInt(MmbrStyle.width)+PadLeft:MmbrStyle.clip.width;
	var MmbrHeight=(!Nav4)?parseInt(MmbrStyle.height)+PadTop:MmbrStyle.clip.height;
	var CntnrWidth=(!Nav4)?parseInt(CntnrStyle.width):CntnrStyle.clip.width;
	var CntnrHeight=(!Nav4)?parseInt(CntnrStyle.height):CntnrStyle.clip.height;
	
	// Calibrating Top and Left
	//Lt=FirstWindowWidth-CntnrWidth;
	Tp+=VerCorrect;
	Tp+=verticalCorrection;
	Tp+=banner_height;
	Lt+=HorCorrect;
	CntnrStyle.top=Cntnr.OrgTop=Tp;
	CntnrStyle.left=Cntnr.OrgLeft=Lt;
	
	// Changing else to test border: ight-2*BorderWidth} --- > ight}
	Hori=Lefti=0; 
	Topi=CntnrHeight-MmbrHeight-2*BorderWidth;
	while(Mmbr!=null){
		PadLeft=(Mmbr.value.indexOf('<')==-1)?LeftExtra:0;
		PadTop=(Mmbr.value.indexOf('<')==-1)?TopExtra:0;
		MmbrStyle=(!Nav4)?Mmbr.style:Mmbr;
		MmbrWidth=(!Nav4)?parseInt(MmbrStyle.width)+PadLeft:MmbrStyle.clip.width;
		MmbrHeight=(!Nav4)?parseInt(MmbrStyle.height)+PadTop:MmbrStyle.clip.height;
		MmbrStyle=(!Nav4)?Mmbr.style:Mmbr;
		MmbrStyle.left=Lefti+BorderWidth;
		MmbrStyle.top=Topi+BorderWidth;
		if(Nav4)Mmbr.CmdLyr.moveTo(Lefti+BorderWidth,Topi+BorderWidth);
		(Hori)?Lefti-=(BorderBtwnElmnts)?(MmbrWidth+BorderWidth):(MmbrWidth):Topi-=(BorderBtwnElmnts)?(MmbrHeight+BorderWidth):(MmbrHeight);
		Mmbr=Mmbr.PrevMember;
	}
}


/* This function will write actually ONE and only ONE menu in the page */
function CreateMenuStructure(MenuName, nbOfMenu) {
	var i,Member;
	var PreviousMember=null;
	// Width is the width of first sub_menu
	var MenuWidth = eval(MenuName+'_1[3]');
	// LOOPING to know height
	var MenuHeight = 0;
	// Number of items from menu information
	var NoOffSubs = eval(MenuName + '_NbItems');
	for(i=1; i<= NoOffSubs; i++) {
		MenuHeight = MenuHeight+ eval(MenuName + '_'+i+'[2]')	
	} 
	// What's this???????????????????????????????????????
	var Location=FirstLocation;
	
	// If DOM
	if(DomYes){
		// Creates new member container
		var MemberContainer=Location.document.createElement("div");
		MemberContainer.style.position='absolute';
		MemberContainer.style.visibility='hidden';
		Location.document.body.appendChild(MemberContainer)}
	else	
		// If Netscape 4
		if(Nav4) var MemberContainer=new Layer(MenuWidth,Location)
		// Other
		else{
			Location.document.body.insertAdjacentHTML("AfterBegin","<div id='"+MenuName+"' style='visibility:hidden; position:absolute;'><\/div>"); 
			var MemberContainer=Location.document.all[MenuName]}
	// Sets some container variables and size considering borders
	MemberContainer.SetUp=ContainerSetUp;
	MemberContainer.SetUp(MenuWidth,MenuHeight,NoOffSubs);
	MemberContainer.idMenu = nbOfMenu;
	MemberContainer.openedMenu = 0;

	// Setting member containers for Explorer (i.e.each submenu)
	if(Exp4){
		MemberContainer.InnerString='';
		for(i=1;i<=NoOffSubs;i++){
			WhichMenu=MenuName+'_'+eval(i);
			MemberContainer.InnerString+="<div id='"+WhichMenu+"' style='position:absolute;'><\/div>";
		}
		MemberContainer.innerHTML=MemberContainer.InnerString;
	}
	
	// For each submenu (i.e. for a Member)
	for(i=1;i<=NoOffSubs;i++){
		WhichMenu=MenuName+'_'+eval(i);
		if(DomYes){
			Member=Location.document.createElement("div");
			Member.style.position='absolute';
			Member.style.visibility='inherit';
			MemberContainer.appendChild(Member);
			Member.SetUp=MemberSetUp;
		}
		else
			if(Nav4){
				Member=new Layer(MenuWidth,MemberContainer);
				Member.SetUp=NavMemberSetUp}
			else{	
				Member=Location.document.all[WhichMenu];
				Member.SetUp=MemberSetUp}
				
		// Setting up a member layer
		var SbMenuHeight = eval(WhichMenu+'[2]');
		Member.SetUp(MemberContainer,PreviousMember,WhichMenu,MenuWidth,SbMenuHeight);
		// Sets previous member for next loop
		PreviousMember=Member;
	}
	
	// Actually this is the last member
	MemberContainer.FirstMember=Member;
	return(MemberContainer);
}

/* When menus are already created, but are called by browser */
function CreateMenuStructureAgain(MenuName){
	var i,WhichMenu,NoOffSubs;
	var PreviousMember,Member=FirstContainer.FirstMember;
	var NumberOf = eval(MenuName+'_NbItems');
	for(i=NumberOf;i>0;i--){
		WhichMenu=MenuName+'_'+eval(i);
		PreviousMember=Member;
		Member=Member.PrevMember
	}
}

/* This will set the variables for a container of sub-menus */
// Hght contains the size of all container
function ContainerSetUp(Wdth,Hght,NoOff){
	this.FirstMember=null;
	this.OrgLeft=0;
	this.OrgTop=0;

	Hght=(BorderBtwnElmnts)?NoOff*BorderWidth+Hght+BorderWidth:Hght+2*BorderWidth;
	Wdth+=2*BorderWidth;
	
	if(Nav4){	this.visibility='hide';
		this.bgColor=BorderColor;
		this.resizeTo(Wdth,Hght)}
	else{
		// All containers have level 1
		this.Level= 1;
		this.style.backgroundColor=BorderColor;
		this.style.width=Wdth;
		this.style.height=Hght;
		this.style.fontFamily=FontFamily;
		this.style.fontWeight=(FontBold)?'bold':'normal';
		this.style.fontStyle=(FontItalic)?'italic':'normal';
		this.style.fontSize=FontSize+'pt';
		this.style.zIndex=CreateRecursLevel+Ztop;
	}
	this.closeTimer = null;
}

/* Sets up a member, i.e. a submenu entry */
// Wdth: we pass the width of menu
// MmbrCntnr: The container (menu) to whom this member is owned
// PrMmbr: The previous member, calculated in loop in CreateMenuStructure
// WhatMenu: The name of the member
function MemberSetUp(MmbrCntnr,PrMmbr,WhatMenu,Wdth,Hght){
	// What's this???????????????????????????????????????
	var Location=SecLocation;
	// This is the text actually displayed
	var MemVal=eval(WhatMenu+'[0]');
	var t,T,L,W,H,S;
	// Own parameters of member
	this.value=MemVal;
	this.ChildCntnr=null;
	this.PrevMember=PrMmbr;
	this.Level=2;
	this.LinkTxt=eval(WhatMenu+'[1]');
	this.Container=MmbrCntnr;
	this.style.cursor='default';
	
	if(MemVal.indexOf('<')==-1){
		this.style.width=Wdth-LeftExtra; 
		this.style.height=Hght-TopExtra; 
		this.style.paddingLeft=LeftPaddng; 
		this.style.paddingTop=TopPaddng;
	}
	else{
		this.style.width=Wdth; 
		this.style.height=Hght;
	}
	
	this.style.overflow='hidden'
	this.LowBack=LowBgColor;
	this.LowFontColor=FontLowColor;
	this.HighBack=(MemVal.indexOf('<img')==-1)?HighBgColor:LowBgColor;
	this.HighFontColor=(MemVal.indexOf('<img')==-1)?FontHighColor:FontLowColor; 
	this.style.color=this.LowFontColor;
	this.style.backgroundColor=this.LowBack;
	if(MenuTextCentered)this.style.textAlign='center';
	// We add text to be appearing
	if(MemVal.indexOf('<')==-1&&DomYes){
		t=Location.document.createTextNode(MemVal);
		this.appendChild(t);
	}
	else 
		this.innerHTML=MemVal;
		
	// Les actions à suivre quand on met en place un membre du menu
	if(ExpYes){
		this.onmouseover= OverMenu; 
		this.onmouseout=OutMenu;
		this.onclick=GoTo}
	else{
		this.addEventListener('mouseover',OverMenu,false);
		this.addEventListener('mouseout',OutMenu,false);
		this.addEventListener('click',GoTo,false);
	}
}


/* Sets up a member, i.e. a submenu entry, for Navigator */
// Wdth: we pass the width of menu
// MmbrCntnr: The container (menu) to whom this member is owned
// PrMmbr: The previous member, calculated in loop in CreateMenuStructure
// WhatMenu: The name of the member
function NavMemberSetUp(MmbrCntnr,PrMmbr,WhatMenu,Wdth,Hght){
	// Text inside this object
	this.value=eval(WhatMenu+'[0]');
	// if not img and not centered we give a white space at the beginning
	if(LeftPaddng&&this.value.indexOf('<')==-1&&!MenuTextCentered)
		this.value='&nbsp\;'+this.value;
	if(FontBold)this.value=this.value.bold();
	if(FontItalic)this.value=this.value.italics();
	this.Overvalue=this.value;
	this.value=this.value.fontcolor(FontLowColor);
	this.Overvalue=this.Overvalue.fontcolor(FontHighColor);
	this.value=CenterText+"<font face='"+FontFamily+"' point-size='"+FontSize+"' color='"+FontLowColor+"'>"+this.value+TxtClose;
	this.Overvalue=CenterText+"<font face='"+FontFamily+"' point-size='"+FontSize+"' color='"+FontHighColor+"'>"+this.Overvalue+TxtClose;
	this.LowBack=LowBgColor;
	this.HighBack=HighBgColor;
	
	this.visibility='inherit';
	this.PrevMember=PrMmbr;
	this.bgColor=LowBgColor;
	this.resizeTo(Wdth,Hght);
	this.document.write(this.value);
	this.document.close();
	this.CmdLyr=new Layer(Wdth,MmbrCntnr);
	this.CmdLyr.visibility='inherit';
	this.CmdLyr.Level=2;
	this.CmdLyr.LinkTxt=eval(WhatMenu+'[1]');
	this.CmdLyr.onmouseover=OverMenu;
	this.CmdLyr.onmouseout=OutMenu;
	this.CmdLyr.captureEvents(Event.MOUSEUP);
	this.CmdLyr.onmouseup=GoTo;
	this.CmdLyr.LowLyr=this;
	this.CmdLyr.Container=MmbrCntnr;
	this.CmdLyr.resizeTo(Wdth,Hght);
}

/* This functino restores the old values for background and foreground */
function OutMenu() {
	if(!Loaded||!Created) return;
	
	if(Nav4){
		if(this.LowLyr.value.indexOf('<img')==-1){
			this.LowLyr.bgColor=this.LowLyr.LowBack;
			this.LowLyr.document.write(this.LowLyr.value);
			this.LowLyr.document.close(); }
	}
	else{	
		this.style.backgroundColor=this.LowBack;
		this.style.color=this.LowFontColor;
	}
	status='';
	if(this==CurrentOver){
		var aCntnr = this.Container;
//		if (MouseOnButton != aCntnr.idMenu) {
			if (aCntnr.closeTimer) clearTimeout(aCntnr.closeTimer);
			aCntnr.closeTimer=setTimeout('hideMenu('+aCntnr.idMenu+');',DissapearDelay);
//		}
	}
	CurrentOver = null;
}

/* This function changes the background color for this layer */
function OverMenu() {
	if(!Loaded||!Created) return;

	var ThisHeight=(Nav4)?this.clip.height:parseInt(this.style.height);
	var ThisWidth=(Nav4)?this.clip.width:parseInt(this.style.width);
	
	// We mark that we are currently over
	CurrentOver=this;
	
	if(Nav4){	if(this.LowLyr.value.indexOf('<img')==-1){
			this.LowLyr.bgColor=this.LowLyr.HighBack;
			this.LowLyr.document.write(this.LowLyr.Overvalue);
			this.LowLyr.document.close()}}
	else{	
		this.style.backgroundColor=this.HighBack;
		this.style.color=this.HighFontColor;
	}
	status=this.LinkTxt;
}

/* Just for the link to be there actually */
function GoTo() {
		if(this.LinkTxt){
		status=''; 
		if(Nav4){	if(this.LowLyr.value.indexOf('<img')==-1){
				this.LowLyr.bgColor=this.LowLyr.LowBack;
				this.LowLyr.document.write(this.LowLyr.value);
				this.LowLyr.document.close()}}
		else{	this.style.backgroundColor=this.LowBack;
			this.style.color=this.LowFontColor}
		(this.LinkTxt.indexOf('javascript:')!=-1)?eval(this.LinkTxt):DocLocation.location.href=this.LinkTxt}}

/* Shows the menu given in parameter, this is the main function to put in htm page */
// NbOfMenu is the number of the menu in the array, first menu = 1
function showMenu(nb) {
	VV_mouseonbutton(nb);
	var NoOfMenu = nb;
	ClearAllMenus();
	// If not exist menus, retour
	if(!Loaded||!Created) return;

	var MemberContainer=MenuCntnr[NoOfMenu-1];
	var MCStyle=(Nav4)?MemberContainer:MemberContainer.style;
	// If first level then show, if not, hide
	MCStyle.visibility = M_Show;
	MemberContainer.openedMenu = 1;
	// This flag will tell us a menu is already and we have to hide it
	InitFlag = 1;
}

/* Makes the menu to disappear */
// NbOfMenu is the number of the menu in the array, first menu = 1
function hideMenu(nb) {
	//	MM_popupMsg('Going to close'+nb);
	var NoOfMenu = nb;
	// If not exist menus, retour
	if(!Loaded||!Created) return;
	if (CurrentOver != null) return;
	
	/* Hides Shown menu */
	var MemberContainer=MenuCntnr[NoOfMenu-1];
	// If it's hidden we return
//	if (MemberContainer.openedMenu == 0) return;
	var MCStyle=(Nav4)?MemberContainer:MemberContainer.style;
	// If first level then show, if not, hide
	MCStyle.visibility = M_Hide;
	MemberContainer.openedMenu = 0;
}

function hideWithTimer(nb) {
	VV_mouseoutbutton();
	var NoOfMenu = nb;
	if(!Loaded||!Created) return;

	status ='';
	var MemberContainer = MenuCntnr[NoOfMenu - 1];
	if (MemberContainer.closeTimer) clearTimeout(MemberContainer.closeTimer);
	MemberContainer.closeTimer=setTimeout('hideMenu('+NoOfMenu+');',DissapearDelay);
}

function ClearAllMenus() {
	var i;
	for(i=1; i<=VV_NbMenus; i++)
		hideMenu(i);
	InitFlag = 0;
}

function VV_mouseonbutton(nb) {
	if (MenuCntnr[nb - 1] == null)	return;
	var MemberContainer = MenuCntnr[nb - 1];
	if (MemberContainer.closeTimer) clearTimeout(MemberContainer.closeTimer);
	MouseOnButton = nb;
}
function VV_mouseoutbutton() {
	MouseOnButton = 0;
}