// JavaScript Document

// menus.js - contains all the code for displaying and using the menus

var currentMenu=-1;
var currentItem=-1;
var currentLevel=-1;
var oldLevel;
var clientname="undefined";
var edit=false;
var override=false;
var firstever=true;

var showing=new Array(256);  // max number of sublevels
var isHiding=false;
var hideTimer;

function initMenus(cn){
	for(n=0;n<256;n++)
		showing[n]=-1;
	clientname=cn;
	
	if(parent.document.location.href.indexOf("edit")>=0)
		edit=true;
	if(parent.document.location.href.indexOf("override")>=0)
		override=true;
}

function showMenu(number,level,orientation,e){
		firstever=false;
		// orientation is 1 for vertical, 2 for horizontal.
		
		// kill the hidetimer
		if(isHiding)
		{
			isHiding=false;
			clearTimeout(hideTimer);
		}
		
		// if we are at a ridiculously deep level, return
		if(level>255)return;
		
		// hide all the levels above this one, and set them to -1
		t=level+1;
		while(true)
		{
			if(showing[t]>=0)
			{
				eval("document.getElementById(\"menu"+showing[t]+"\").style.visibility=\"hidden\"");
				showing[t]=-1;
			}
			else break; // we've reached the -1s!
			t++;
			if(t>255)break;		
		}
		
		// Make sure we're not redisplaying a menu
		if(showing[level]==number)
			return; // we're already showing it!
		
		f=document.getElementById("menuWidth"); // get the menu width
		menuwidth=200;
		if(f.offsetWidth)
			menuwidth=f.offsetWidth;
		else if(f.clip.width)
			menuwidth=f.clip.width;
		
		screenx=800;  // get the screen width
	
		if(window.innerWidth)
			screenx=window.innerWidth;
		else if(document.body.clientWidth)
			screenx=document.body.clientWidth;
	
		currentMenu=number;
		currentLevel=level;
		
		// hide the old level
		if(showing[level]>=0) // hide it!
			eval("document.getElementById(\"menu"+showing[level]+"\").style.visibility=\"hidden\"");
			
		showing[level]=number;
		
		// positioning of the menu is based on level and e (mouse event)
		// and also on the orientation - 1 for vertical
		
		// get the mouse position
		var xpos=0,ypos=0;
		
		if(e.x)xpos=e.x;
		if(e.pageX)xpos=e.pageX;
		if(e.y)ypos=e.y;
		if(e.pageY)ypos=e.pageY;
		
		
		// get the position and dimensions of the parent element
		if(level==0) // we're coming off a navbutton, so find the navBar height or width
		{
			f=document.getElementById("navBar");
			
			// if we're horizontal, find the height of the navbar
			if(orientation==2)
			{
				if(f.offsetHeight)
					ypos=f.offsetHeight;
				else if(f.clip.height)
					ypos=f.clip.height;
			}
			else // find the width of the navbar
			{
				if(f.offsetWidth)
					xpos=f.offsetWidth;
				else if(f.clip.width)
					xpos=f.clip.width;
			}
			
			// find the position of the top or left of the current item
			
			f=document.getElementById("navitem"+number);
			
			if(orientation==2) // get the left side of it in xpos
			{
				l=0;
				if(f.style.left)
					l=f.style.left;
				if(f.style.pixelLeft)
					l=f.style.pixelLeft;
				if(f.offsetLeft)
					l=f.offsetLeft;
				
				xpos=l; // contains the position within navtable of the cell.
			}
			else // get the top of it in ypos
			{
				l=0;
				if(f.style.top)
					l=f.style.top;
				if(f.style.pixelTop)
					l=f.style.pixelTop;
				if(f.offsetTop)
					l=f.offsetTop;
				
				ypos=l; // contains the position within navtable of the cell.
			}
			
			// now find the position of navtable itself.
			
			f=document.getElementById("navtable");
			
			if(orientation==2) // horizontal, so get the left side and add it to xpos
			{
				l=0;
				if(f.style.left)
					l=f.style.left;
				if(f.style.pixelLeft)
					l=f.style.pixelLeft;
				if(f.offsetLeft)
					l=f.offsetLeft;
					
				// add it to xpos
				xpos+=l;
			}
			else // vertical, so get the top pos and add it to ypos
			{
				l=0;
				if(f.style.top)
					l=f.style.top;
				if(f.style.pixelTop)
					l=f.style.pixelTop;
				if(f.offsetTop)
					l=f.offsetTop;
					
				// add it to xpos
				ypos+=l;
			}				
		}
		else // we're coming off a menu, so come off the previous level
		{
			eval("f=document.getElementById(\"menu"+showing[level-1]+"\")");
			// we need the left position, and nothing else.
			// we already have menuwidth, and we'll use the mouse ypos.
			
			l=0;
			if(f.style.left)
				l=f.style.left;
			if(f.style.pixelLeft)
				l=f.style.pixelLeft;
			if(f.offsetLeft)
				l=f.offsetLeft;
				
			xpos=l+menuwidth;
			ypos-=5;
		}
		
		if(xpos<0)xpos=0;
		if(xpos+menuwidth>screenx)
			xpos=screenx-menuwidth;
		if(ypos<0)ypos=0;
		
		eval("f=document.getElementById(\"menu"+number+"\").style");
		f.top=ypos;
		f.left=xpos;
		f.visibility="visible";
}	

function hideMenus(){
		if(firstever)
			return; // don't hide it if there has never been one open!
					
		currentLevel=-1;
		
		// call the timedHide() function
		if(isHiding)
		{
			clearTimeout(hideTimer);
		}
		
		isHiding=true;
		
		setTimeout("timedHide()",1500);
}

function timedHide(){
	if(firstever)return;
	if(!isHiding)
	{
		clearTimeout(hideTimer);
		return;
	}

	isHiding=false;
	
	// hide the menus
	var hl=currentLevel+1;
	
	if(hl<0)hl=0;
	
	while(true)
	{
		if(showing[hl]>=0)
		{
			eval("document.getElementById(\"menu"+showing[hl]+"\").style.visibility=\"hidden\"");
			showing[hl]=-1;
		}
		else break; // we've reached the -1s!
		hl++;
		if(hl>255)break;
	}
	firstever=true;
}

function overMenu(itm,menu,level){
	if(isHiding)
	{
		isHiding=false;
		clearTimeout(hideTimer);
	}
	currentMenu=menu;
	currentLevel=level;
	showing[level]=menu;
	
	if(itm>=0)
		currentItem=itm;
		
	// clear all the menus above this one
	// they will be ok as long as you don't roll over an item in a lower menu
	t=level+1;
	while(true)
	{
		if(showing[t]>=0)
		{
			eval("document.getElementById(\"menu"+showing[t]+"\").style.visibility=\"hidden\"");
			showing[t]=-1;
		}
		else break; // we've reached the -1s!
		t++;
		if(t>255)break;		
	}

}

function setLevel(level,menu){
	if(isHiding)
	{
		isHiding=false;
		clearTimeout(hideTimer);
	}
	currentLevel=level;
	currentMenu=menu;
	showing[level]=menu;
}

function clickMenu(){
	
	absurl=document.URL;
	
	// remove everything after the last slash
	absurl=absurl.substr(0,absurl.lastIndexOf("/"));
	
	// add the php folder
	absurl+="/php/";
		
	
	if(currentMenu<0)currentMenu=homeMenu;
	if(currentItem<0)currentItem=homeItem;
	
	var e="";
	if(edit && editable[currentMenu] [currentItem]==1)
		e+="&edit=true";
	else if(edit && override)
		e+="&edit=true";
	
	
	if(pwprotected[currentMenu] [currentItem]==1)
	{
		e+="&pwprotect=true";
	}
		
	if(newspages[currentMenu] [currentItem]>0){
		e+="&newsarea="+newsarea+"&newsareanum="+newspages[currentMenu] [currentItem];
	}

	e+="&pwbox="+pwbox;
	
	if(top>0)
		frames['topFrame'].location.href=absurl+"loadpage.php?page=top&clientname="+clientname+"&menunum="+currentMenu+"&itemnum="+currentItem+"&type="+top+"&bstyle="+TOPbstyle+"&bendstyle="+TOPbendstyle+e;
	if(bottom>0)
		frames['bottomFrame'].location.href=absurl+"loadpage.php?page=bottom&clientname="+clientname+"&menunum="+currentMenu+"&itemnum="+currentItem+"&type="+bottom+"&bstyle="+BOTTOMbstyle+"&bendstyle="+BOTTOMbendstyle+e;
	if(left>0)
		frames['leftFrame'].location.href=absurl+"loadpage.php?page=left&clientname="+clientname+"&menunum="+currentMenu+"&itemnum="+currentItem+"&type="+left+"&bstyle="+LEFTbstyle+"&bendstyle="+LEFTbendstyle+e;
	if(right>0)
		frames['rightFrame'].location.href=absurl+"loadpage.php?page=right&clientname="+clientname+"&menunum="+currentMenu+"&itemnum="+currentItem+"&type="+right+"&bstyle="+RIGHTbstyle+"&bendstyle="+RIGHTbendstyle+e;
	if(topleft>0)
		frames['topleftFrame'].location.href=absurl+"loadpage.php?page=topleft&clientname="+clientname+"&menunum="+currentMenu+"&itemnum="+currentItem+"&type="+topleft+"&bstyle="+TOPLEFTbstyle+"&bendstyle="+TOPLEFTbendstyle+e;
	if(topright>0)
		frames['toprightFrame'].location.href=absurl+"loadpage.php?page=topright&clientname="+clientname+"&menunum="+currentMenu+"&itemnum="+currentItem+"&type="+topright+"&bstyle="+TOPRIGHTbstyle+"&bendstyle="+TOPRIGHTbendstyle+e;
	if(bottomright>0)
		frames['bottomrightFrame'].location.href=absurl+"loadpage.php?page=bottomright&clientname="+clientname+"&menunum="+currentMenu+"&itemnum="+currentItem+"&type="+bottomright+"&bstyle="+BOTTOMRIGHTbstyle+"&bendstyle="+BOTTOMRIGHTbendstyle+e;
	if(bottomleft>0)
		frames['bottomleftFrame'].location.href=absurl+"loadpage.php?page=bottomleft&clientname="+clientname+"&menunum="+currentMenu+"&itemnum="+currentItem+"&type="+bottomleft+"&bstyle="+BOTTOMLEFTbstyle+"&bendstyle="+BOTTOMLEFTbendstyle+e;
	if(wholearea>0)
		frames['wholeareaFrame'].location.href=absurl+"loadpage.php?page=wholearea&clientname="+clientname+"&menunum="+currentMenu+"&itemnum="+currentItem+"&type="+wholearea+"&bstyle="+WHOLEAREAbstyle+"&bendstyle="+WHOLEAREAbendstyle+e;
	if(bottomLayer)
		frames['bottombarFrame'].location.href=absurl+"loadpage.php?page=bottombar&clientname="+clientname+"&menunum="+currentMenu+"&itemnum="+currentItem+"&type=1"+"&bstyle="+BOTTOMBARbstyle+"&bendstyle="+BOTTOMBARbendstyle+e;

	hideNow();
}


function hideNow(){
	if(firstever)return;
	
	if(isHiding)
	{
		isHiding=false;
		clearTimeout(hideTimer);
	}

	var hl=0;
	while(true)
	{
		if(showing[hl]>=0)
		{
			eval("document.getElementById(\"menu"+showing[hl]+"\").style.visibility=\"hidden\"");
			showing[hl]=-1;
		}
		else break; // we've reached the -1s!
		hl++;
		if(hl>255)break;	
	}
	currentLevel=currentMenu=currentItem=-1;
}

//function openNews(mnum,inum,pnum,area){
	//alert("Opening news for page "+pnum+"\nMenu "+mnum+", Item "+inum+", area is "+area);
//}
function openNews(mnum,inum,pnum,area,bstyle,bendstyle){
	
	absurl=document.URL;
	
	// remove everything after the last slash
	absurl=absurl.substr(0,absurl.lastIndexOf("/"));
	
	// add the php folder
	absurl+="/php/";	
	
	e="";
	
	if(pwprotected[mnum] [inum]==1)
	{
		e+="&pwprotect=true";
	}
		
	if(newspages[mnum] [inum]>0){
		e+="&newsarea="+newsarea+"&newsareanum="+newspages[mnum] [inum];
	}

	e+="&pwbox="+pwbox;
	e+="&newspagenum="+pnum;
	
	frames[area+'Frame'].location.href=absurl+"loadpage.php?page="+area+"&clientname="+clientname+"&menunum="+mnum+"&itemnum="+inum+"&type=1&bstyle="+bstyle+"&bendstyle="+bendstyle+e;
}

function showNews(mnum,inum,pnum,area,bstyle,bendstyle,id){
	absurl=document.URL;

	// remove everything after the last slash
	absurl=absurl.substr(0,absurl.lastIndexOf("/"));
	
	// add the php folder
	absurl+="/php/";	
	
	e="";
	
	if(pwprotected[mnum] [inum]==1)
	{
		e+="&pwprotect=true";
	}
		
	if(newspages[mnum] [inum]>0){
		e+="&newsarea="+newsarea+"&newsareanum="+newspages[mnum] [inum];
	}

	e+="&pwbox="+pwbox;
	e+="&newspagenum="+pnum;
	e+="&id="+id;
	
	frames[area+'Frame'].location.href=absurl+"shownews.php?page="+area+"&clientname="+clientname+"&menunum="+mnum+"&itemnum="+inum+"&type=1&bstyle="+bstyle+"&bendstyle="+bendstyle+e;
}

function rollovermenubutton(s,n){
	//eval("document."+n+".src='"+s+"'");
	
	eval("document."+n+".src="+s+".src;");
}

function rolloutmenubutton(s,n){
	rollovermenubutton(s,n);
}