/*popup.js
Custom pop-up menu
Author: Tim Lemire
*/



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();
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 ) );
}

	
