/*Popup.js
Author: Tim Lemire

Custom cross-browser Popup menus.
En Taro Adun.

Copyright 2004-2007 Texas State University Biology Dept.

Quick guide:


Add These two lines to the HEAD of your document:
	<script language="javascript" src="popup.dept.js" ><!--Adun smiles upon the righteous--></script>
	<link href="popup.menu.css" rel="stylesheet" type="text/css" media="all">

Now to set the mouseover points which will cause the menus to appear, you need to add an anchor some
where on the page with the id "rootx" where x is an integer.
example:

<a href="www.lol.com" id="root1">Paladin DPS</a>

this will show all menu 1 items when mouseover'd.

to add items to a menu, name them as such (must be "paragraph" tags, sorry)

<p id="menu1a">This is a menu 1 Item. </p>
<p id="menu1b">This is a munu 1 item as well!</p>
<p id="menu2a">This is a menu 2 item!</p>

the ID is "menu" followed by an integer which corresponds to which menu it will appear in, 
followed by anything - this is just so you can give them unique names, and its probably
a good idea so the browser doesn't get confused.

anyway, some last notes: make sure you have a "menubuttons[x] = new Array(); (see below)"
declared for every menu list 
you want to use. if you have a menu paragraph reference a menubuttons[] index which is not
declared, the menus will fail and you'll just have some bland links. also, the menu list 
number is the integer in the name - 1, because computers begin counting at 0.

so 
<a href="" id="root1">Blah</a>
will correspond to
menubuttons[0] = new Array();

the whole "randpic" thing is for the first website this was used on. if you don't want
to use it, comment it out from initmenu() because itll probably give you errors.

*/



var PicNum = 9;
var menuShowing;
var menuTimer;
var menuButtons = new Array();
menuButtons[0] = new Array();
menuButtons[1] = new Array();
menuButtons[2] = new Array();
menuButtons[3] = new Array();
menuButtons[4] = new Array();
menuButtons[5] = new Array();
window.onload=initmenu;
function initmenu()
{	
	//randpic();
	var menupattern;
	var x;
	if (document.layers)
	{
		for(var i = 0; i < document.layers.length; i++)
		{
			if (document.layers[i].id.match(/menu\d/))
			{
				x = document.layers[i].id.replace(/menu(\d).*/, "$1") - 1;
				menuButtons[x][menuButtons[x].length] = document.layers[i].id;
				document.layers[i].onmouseover=overMenu;
				document.layers[i].onmouseout=outMenu;
			}
			else if (document.layers[i].id.match(/root\d/))
			{
				document.layers[i].onmouseover=show;
				document.layers[i].onmouseout=hide;
			}
		}
	}
	else if (document.all)
	{
		for(var i = 0; i < document.all.length; i++)
		{
			if( typeof document.all[i].id == 'string')
			{
				if (document.all[i].id.match(/menu\d/))
				{
					x = document.all[i].id.replace(/menu(\d).*/, "$1") - 1;
					menuButtons[x][menuButtons[x].length] = document.all[i].id;
					document.all[i].onmouseover=overMenu;
					document.all[i].onmouseout=outMenu;
				}
				else if (document.all[i].id.match(/root\d/))
				{
					document.all[i].onmouseover=show;
					document.all[i].onmouseout=hide;
				}
			}
		}
	}
	else if (document.getElementById)
	{
		for(var i=0; i < document.getElementsByTagName('p').length; i++)
		{
			if (document.getElementsByTagName('p')[i].id.match(/menu\d/))
			{ 
				x = document.getElementsByTagName('p')[i].id.replace(/menu(\d).*/, "$1") - 1;
				menuButtons[x][menuButtons[x].length] = document.getElementsByTagName('p')[i].id;
				document.getElementsByTagName('p')[i].onmouseover=overMenu;
				document.getElementsByTagName('p')[i].onmouseout=outMenu;
			}
		}
		for(var i=0; i < document.getElementsByTagName('a').length; i++)
		{
			if (document.getElementsByTagName('a')[i].id.match(/root\d/))
			{ 
				document.getElementsByTagName('a')[i].onmouseover=show;
				document.getElementsByTagName('a')[i].onmouseout=hide;
			}
		}
	}
}

function hide()
{	
    if (menuTimer) clearTimeout(menuTimer);
    menuTimer = setTimeout("close("+menuShowing+");",500);
}
function close(index)
{
	menuShowing = "-1";
	var i;
	for(i = 0; i < menuButtons[index].length; i++)
	{	
		PID = menuButtons[index][i];
		if (document.layers)
		{
			document.layers[PID].visibility = 'hide';
			//document.layers[PID].top = '400px';
			//document.layers[PID].left = '0px';
		}
		else if (document.all)
		{
			document.all[PID].style.visibility = 'hidden';
			//document.all[PID].style.top = '400px';
			//document.all[PID].style.left = '0px';
		}
		else if (document.getElementById)
		{
			document.getElementById(PID).style.visibility = 'hidden';
			//document.getElementById(PID).style.top='400px';
			//document.getElementById(PID).style.left='0px';
		}
	}	
}

function show(e) 	//e for EVENT, d for DEMOCRACY
{ 
	var PID;
	var posx = 0;
	var posy = 0;
	var i;
	var x;
	var index;

	index=this.id.replace(/root(\d)/, "$1") - 1;

	if((menuShowing >= 0) && (menuShowing != index)) close(menuShowing);
						//Close any other menus being shown
	if (menuTimer) clearTimeout(menuTimer);
	if(menuShowing == index) 
	{
		return false; //menu is already shown, do nothing
	}
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
	}
	// posx contains the mouse position relative to the document
	x = getScreenWidth();
	if((x - posx) <= 180)
	{
		posx = x - 181;
	}
	
	for(i = 0; i < menuButtons[index].length; i++)
	{	
		PID = menuButtons[index][i];
		posy = i * 18;
		if (document.layers)
		{	
			document.layers[PID].visibility = 'visible';
			document.layers[PID].top = posy + 82;
			document.layers[PID].left = posx - 20;
		}
		else if (document.all)
		{
			document.all[PID].style.visibility = 'visible';
			document.all[PID].style.top = posy + 82;
			document.all[PID].style.left = posx - 20;
		}
		else if (document.getElementById)
		{	
			document.getElementById(PID).style.visibility = 'visible';
			document.getElementById(PID).style.top = posy + 82;
			document.getElementById(PID).style.left = posx - 20;
		}
	}
	menuShowing = index;
}

function overMenu()
{
	if (menuTimer) clearTimeout(menuTimer);
	if(this.style)
	{
		this.style.width = '180px';
		this.style.background = '#734b2e';
		this.style.fontWeight = 'bold';
	}
	else
	{
		this.width = '180px';
		this.background = '#734b2e';
		this.fontWeight = 'bold';
	}
}


function outMenu()
{
	hide(menuShowing);
	if(this.style)
	{
		this.style.width = '160px';
		this.style.background = '#3d663d';
		this.style.fontWeight = 'normal';
	}
	else
	{
		this.width = '160px';
		this.background = '#3d663d';
		this.fontWeight = 'normal';
	}
}

function getScreenWidth() 
{
	var x, z;
	if (self.innerWidth) // all except Explorer
	{
		x = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
	}
	if (self.pageYOffset) // all except Explorer
	{
		z = self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		z = document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		z = document.body.scrollLeft;
	}
	return x + z;
}

function randpic()
{
	document.images['frontcollage'].src='collage' + (rand(PicNum)) + '.jpg';
}

function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

	
