//
//	Input:	x # of arrays named as follows:
//				peakMENUy = new Array(...)
//			where y = a sequential, unique menu number.
//
//	Array Parms:
//		0 - menu width (px)
//		1 - menuitem height (px)
//		2 - menuitem color
//		3 - menuitem background color
//		4 - menuitem highlighted background color
//		5 - font
//		6 - menu border image
//		7 - menu border color
//		8 - menu border size (px)
//		
//		9  - item 1 text
//		10 - item 1 href
//		11 - item 2 text
//		12 - item 2 href
//		13 - item 3 text
//		     .
//		nn - item (nn-9) href
//
//  after defining all arrays (in a <script> block), include this file with:
//		<script language="Javascript1.1" src="filename.js"></script>
//
//  surround each HTML menu/header with <A href=""></a>
//
//  add onMouseOver and onMouseOut properties to each link as follows:
//
// <A href="" onMouseOver="menu_popup(n,x,y)" onMouseOut="menu_popdown(n)">
//
//	where n = the menu number to pop up, and x,y are offset values in pixels
//

var activeMenu 		= null;
var activeMenuItem 	= null;
var peakMENUS 		= new Array(1);
var isLoaded		= 0;
var screenCenter = (document.layers)? (window.innerWidth-32)/2 : (document.body.clientWidth-20)/2;

// [ setup ] ------------------------------------------------------------------

function createMenusNS()
{
	var menuCount = 1;

	while(eval("window.peakMENU"+menuCount)) {
		menuArray = eval("peakMENU"+menuCount);
		
		menu = new Layer(menuArray[0],window);
		
		peakMENUS[menuCount] 	= menu;
		menu.menuNdx 			= menuCount;
		menu.onmouseout 		= menu_out;
		menu.show				= menu_show;
		menu.hide				= menu_hide;
		
		style 					= menu;
		style.visibility 		= 'hide';
		style.color 			= menuArray[2];
		style.backgroundImage 	= 'url('+menuArray[6]+')';
		style.bgColor			= menuArray[7];
		style.left 				= 0;
		style.top 				= 0;
		style.zIndex 			= 100;
				
		prevstyle = null;
		ndx = 9;
		while (menuArray[ndx]) {
			
			mitem = new Layer(menuArray[0],menu);
//			mitem.document.write ('<font color="'+menuArray[2]+'" face="'+menuArray[5]+'" size="'+menuArray[6]+'">&nbsp;'+menuArray[ndx]+'</font>');
			mitem.document.write ('<table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" width="9">'+
								  '<img src="'+menuArray[5]+'" width="3" height="3" hspace="3" vspace="5" border="0">'+
								  '</td><td width="'+(menuArray[0]-(menuArray[8]*2)-9)+'">'+
								  menuArray[ndx]+
								  '</td></tr></table>');
			mitem.document.close();
			mitem.captureEvents(Event.MOUSEUP);
			
			mitem.menu 			= menu;
			mitem.onmouseover 	= menuitem_over;
			mitem.onmouseup 	= menuitem_click;
			mitem.lolite 		= menuitem_lolite;
			mitem.hilite 		= menuitem_hilite;
			mitem.href 			= menuArray[ndx+1];
			mitem.newWindow 	= menuArray[ndx+3];

			menu.lastitem 		= mitem;

			style 				= mitem;

			style.visibility 	= 'inherit';
			style.zIndex		= 101;
			style.bgColor 		= menuArray[3];
			style.locolor 		= menuArray[3];
			style.hicolor 		= menuArray[4];

			style.left 			= menuArray[8];
			style.top 			= (prevstyle == null) ? menuArray[8] : prevstyle.top + prevstyle.clip.height + menuArray[8];
			style.clip.right 	= menuArray[0]-menuArray[8]*2;
			style.clip.bottom 	= menuArray[1]*menuArray[ndx+2];

			prevstyle 			= style;
									
			ndx += 4;
		}	
		
		menu.clip.right = menuArray[0];
		menu.clip.bottom = menu.lastitem.top + menu.lastitem.clip.height + menuArray[8];
		
		menuCount++;			
	}
	isLoaded = 1;
}

function createMenusIE()
{	
	// [ PHASE 1 : Create ] --------------------------------------------------------------------------------------------
	menuCount = 1;
	menuStr = "";
	while(eval("window.peakMENU"+menuCount)) {
		menuArray 	= eval("peakMENU"+menuCount);
		menuStr    += '<span id="rMENU'+menuCount+'" style="position:absolute">';
		ndx 		= 9;
		while (menuArray[ndx]) {
			menuStr += 	'<span id="rMENU'+menuCount+'ITEM'+((ndx-9)/4+1)+'" style="position:absolute">'+
//							'<font color="'+menuArray[2]+'" face="'+menuArray[5]+'" size="'+menuArray[6]+'">&nbsp;'+
							'<table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" width="9">'+
							'<img src="'+menuArray[5]+'" width="3" height="3" hspace="3" vspace="5" border="0">'+
							'</td><td width="'+(menuArray[0]-(menuArray[8]*2)-9)+'">'+
							menuArray[ndx]+
							'</td></tr></table>'+
//							'</font>'+
						'</span>';							  
			ndx += 4;
		}
		menuStr += '</span>';
		menuCount++;
	};
		
	window.document.body.insertAdjacentHTML('BeforeEnd',menuStr);
		
	// [ PHASE 2 : Setup/Connect ] -------------------------------------------------------------------------------------
	menuCount = 1;
		
	while(eval("window.peakMENU"+menuCount)) {
		menuArray = eval("peakMENU"+menuCount);
				
		menu = document.all["rMENU"+menuCount];
		
		peakMENUS[menuCount]	= menu;
		menu.menuNdx 			= menuCount;
		menu.onmouseout 		= menu_out;
		menu.show				= menu_show;
		menu.hide				= menu_hide;
		menu.timerid			= null;
		
		style 					= menu.style;
		style.visibility 		= 'hidden';
		style.color 			= menuArray[2];
		style.backgroundImage 	= 'url('+menuArray[6]+')';
		style.backgroundColor 	= menuArray[7];
		style.left 				= 10;
		style.top 				= 0;
		style.zIndex 			= 100;
		style.pixelWidth 		= menuArray[0];
		
		prevstyle = null;
		ndx = 9;
		while (menuArray[ndx]) {
			mitem = document.all["rMENU"+menuCount+"ITEM"+((ndx-9)/4+1)];

			mitem.menu 			= menu;
			mitem.onmouseover 	= menuitem_over;
			mitem.onmouseup 	= menuitem_click;
			mitem.lolite 		= menuitem_lolite;
			mitem.hilite 		= menuitem_hilite;
			mitem.href			= menuArray[ndx+1];
			
			mitem.newWindow		= menuArray[ndx+3];

			menu.lastitem 		= mitem;

			style 					= mitem.style;
			style.visibility 		= 'inherit';
			style.zIndex 			= 101;
			style.backgroundColor 	= menuArray[3];
			style.locolor 			= menuArray[3];
			style.hicolor 			= menuArray[4];
			style.cursor 			= "hand";

			style.pixelLeft 		= menuArray[8];
			style.pixelTop 			= (prevstyle == null) ? menuArray[8] : prevstyle.pixelTop + prevstyle.pixelHeight + menuArray[8];
			style.pixelWidth 		= menuArray[0]-menuArray[8]*2;
			style.pixelHeight 		= menuArray[1]*menuArray[ndx+2];

			prevstyle = style;
									
			ndx += 4;
		}
		menu.style.pixelHeight = menu.lastitem.style.pixelTop + menu.lastitem.style.pixelHeight + menuArray[8];
		
		menuCount++;			
	}
	isLoaded = 1;
}

function resize()
{
	if (document.layers) {
		if (saveInnerWidth < window.innerWidth || 
			saveInnerWidth > window.innerWidth || 
			saveInnerHeight > window.innerHeight || 
			saveInnerHeight < window.innerHeight ) 
    	window.location.reload();
	}
	else { screenCenter = (document.body.clientWidth-20)/2; };
}


function startup()
{
	if (document.layers) 	createMenusNS();
	else					createMenusIE();
	
	window.onresize=resize;
	window.saveInnerWidth = window.innerWidth;
	window.saveInnerHeight = window.innerHeight;
}

//if (document.layers) window.onload = startup;
//else startup();

window.onload = startup;

// ||||[ SERVICE ROUTINES ]||||||||||||||||||||||||||||||||||||||||||||||||||||

// [ menu ] -------------------------------------------------------------------

function clearTimer(menuNdx) {
	if (peakMENUS[menuNdx].timerid) {
		clearTimeout(peakMENUS[menuNdx].timerid);
		peakMENUS[menuNdx].timerid = null;
	}
}

function getRealLeft(el) {
	if (document.layers) return el.x;

    xPos 	= el.offsetLeft;
    tempEl 	= el.parentElement;
	
    while (tempEl != null) {
        xPos  += tempEl.offsetLeft;
        tempEl = tempEl.parentElement;
    }
    return xPos;
}

function getRealTop(el) {
	if (document.layers) return el.y;
	
    yPos 	= el.offsetTop;
    tempEl 	= el.offsetParent;
	
    while (tempEl != null) {
        yPos  += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
}

function fixLeftBounds(menuNdx,tryLeft)
{
	visibleWidth	= document.layers ? window.innerWidth : document.body.clientWidth;
	visibleLeft 	= document.layers ? window.pageXOffset : document.body.scrollLeft;
	visibleRight 	= visibleLeft + visibleWidth;
	
	menuWidth = document.layers ? peakMENUS[menuNdx].width : peakMENUS[menuNdx].style.pixelWidth;
	
	//right
	if ((tryLeft + menuWidth) > visibleRight) tryLeft = visibleRight - menuWidth;
	
	//left
	if (tryLeft < visibleLeft) tryLeft = visibleLeft;

	return tryLeft;	
}

function fixTopBounds(menuNdx,tryTop)
{
	visibleHeight	= document.layers ? window.innerHeight : document.body.clientHeight;
	visibleTop		= document.layers ? window.pageYOffset : document.body.scrollTop;
	visibleBottom   = visibleTop + visibleHeight;
	
	menuHeight = document.layers ? peakMENUS[menuNdx].height : peakMENUS[menuNdx].style.pixelHeight;
	
	//bottom
	if ((tryTop + menuHeight) > visibleBottom) tryTop = visibleBottom - menuHeight;
	
	//top
	if (tryTop < visibleTop) tryTop = visibleTop;
	
	return tryTop;
}
	

function menu_popup(menuNdx,xoff,yoff) {
	if (isLoaded == 1) {
		if (activeMenu != null) activeMenu.hide();
		if (activeMenuItem != null) activeMenuItem.lolite();
		
		clearTimer(menuNdx);
	
		peakMENUS[menuNdx].show(
			fixLeftBounds(menuNdx,xoff),
			fixTopBounds(menuNdx,yoff)
		);
	}
}

function menu_popdown(menuNdx) {
	if (isLoaded == 1) {
		clearTimer(menuNdx);
		peakMENUS[menuNdx].timerid = setTimeout("peakMENUS["+menuNdx+"].hide();",700);
	}
}

function menu_hide()
{
	if (document.layers)	this.visibility = "hide";
	else					this.style.visibility = "hidden";	
	if (this == activeMenu) activeMenu = null;
}

function menu_show(x,y)
{
	if (document.layers) {this.left = x; this.top = y;}
	else				 {this.style.left = x; this.style.top = y;}	

	if (document.layers) 	this.visibility = "show";
	else					this.style.visibility = "visible";
	activeMenu = this;
}

function menu_out() 
{
	clearTimer(this.menuNdx);
	this.timerid = setTimeout("peakMENUS["+this.menuNdx+"].hide();",100);
}

// [ menuitem ] ---------------------------------------------------------------

function menuitem_over()
{
	if (activeMenuItem != null) activeMenuItem.lolite();
	this.hilite();
	
	activeMenuItem = this;
	clearTimer(this.menu.menuNdx);
}

function menuitem_click()
{
	if (this.newWindow != 0) {
		window.open(this.href, "blank");
	} else {
		document.location.href = this.href;
	}
}

function menuitem_lolite()
{
	if (document.layers)	this.bgColor = this.locolor;
	else					this.style.backgroundColor = this.style.locolor;
}

function menuitem_hilite()
{
	if (document.layers)	this.bgColor = this.hicolor;
	else					this.style.backgroundColor = this.style.hicolor;
}


