/* 
	Document Information
	------------------------------------------------------
		Filename:		menu.js
		Purpose:		Javascript for menu system. Handles
						drawing, hiding, positioning, etc.
		Tested On:		IE 5.5/6, Firefox 2.0/Gecko
		Author:			LD Smith, for GX Communications
		Contact:		eldee.smith@gmail.com
		
	-------------------------------------------------------
*/

// Absolute navbar bullet y-positions. Used for snapping the navbox to the menu via the bullet image,
// not the top-left of the div. This gives us more flexibility with tweaking the result of the menu
// positioning and allows us to add/remove items from the submenu without having to re-code absolute
// top-left values. From top to bottom: 159,192,225,257,288,320,352,384,416
navpos 		= new Array(3);
navpos[0] 	= 159;
navpos[1] 	= 159;
navpos[3] 	= 192;
// index 2 was removed per request, so all proceeding indexes moved down one.
// index 3 was removed per request, so all proceeding indexes moved down one more.
//navpos[2] 	= 225;
//navpos[3] 	= 257;
navpos[4] 	= 225;
navpos[5] 	= 257;
navpos[6] 	= 288;
navpos[7] 	= 320;
navpos[8] 	= 352;

// there are some circumstances where we will need to alter the global x offset
var global_x_offset = 0;

// define the browser type for per-browser manual adjustments
var browserType;

if (document.layers) 											{	browserType = "nn4"	}
if (document.all) 												{	browserType = "ie"	}
if (window.navigator.userAgent.toLowerCase().match("gecko")) 	{	browserType= "gecko"}


function getObject(objName)
{
	if (browserType == "gecko" )
		document.poppedLayer = eval('document.getElementById(\''+objName+'\')');
	else if (browserType == "ie")
		document.poppedLayer = eval('document.all[\''+objName+'\']');
	else
	 	document.poppedLayer = eval('document.layers[\'`'+objName+'\']');
	
	return document.poppedLayer;
}

function hide(objName) {
	if(getObject(objName))
		getObject(objName).style.visibility = "hidden";
}

function show(objName) {
	if(getObject(objName))
		getObject(objName).style.visibility = "visible";
}
	
function hide_navbox(index)
{
	// menu indices #2 and #3 were removed per request. Allowing them to be 'hidden' would cause an error.
	if(index == 2 || index == 3) {
		return;
	}
	
	if(!index)
	{
		// hide all navboxes. there are 9 total
		for (var x = 1; x <= 9; x++) {
			hide("navbox"+x);			
			var objLink = getObject("nav"+x);			
			objLink.style.color = "#5380aa";
		}		
	} else {
		hide("navbox"+index);
		
		var objLink = getObject("nav"+index);			
		objLink.style.color = "#5380aa";			
	}
}

function show_navbox(index)
{
	hide_navbox(); // hide all navboxes
	var objLink = getObject("nav"+index);	
	if(!objLink) {
		return;	
	}
	objLink.style.color = "#edac41";
	  
	var obj = getObject("navbox"+index);
	
	obj.style.top = "0";	
	var snap_bullet = GetBulletPos(index); // this will be a somewhat ambiguous value, we will only use it to calculate the distance from the navbox origin.
	var diff_x = 153; // the width of the navbox
	
	/* Kludge alert: IE lags behind gecko 10 pixels on the X axis at all times, so all IE x-values will be minus 10. */
	if(browserType == "ie")
		diff_x += 10;
	
	obj.style.top = navpos[eval(index-1)] - snap_bullet[1] + "px";
	redraw_navbox(index);
		
	show("navbox"+index);
}

function redraw_navbox(index)
{
	// Note: the redraw could cause flicker on some browsers, however- since the operator will need
	// to mouse-out of the navbox area to resize the window, the elements that would normally flicker
	// would be hidden, thus eliminating the problem.
									
	var navbox_x		= 466;
	var width 			= getViewportSize()[0];			// the width of our rendered space in a browser (the viewport is not the width of the screen).
	var layer 		 	= getObject("navbox" + index);	// div reference
	if(!layer)
		return;
	var xoffset			= ((width - 800)/2);			// (width - 800)/2 gets us our left-most margin. this value + 800 gives us our right margin.
														// the x offset will be used to make sure our navbox appears in the right place.	
	/* 
		our design width is 800px wide, so if the viewport has
		decreased to that amount or lower, we will use normal
		absolute positioning with no variable offset.
	*/
	
	if(width <= 800) {											// If we're at the minimum width, don't adjust the absolute position of the menu.
		layer.style.left 	= navbox_x + global_x_offset + "px";
	} else if (browserType == "gecko" && width < 1440) { 		// This design was calibrated to 1440px. On gecko browsers, the bullet will shift 10 pixels
		layer.style.left 	= (xoffset + navbox_x + global_x_offset - 10) + "px";	// to the left if the position is between the calibrated value and the minimum width (800 pixels).
	}
	else {
		layer.style.left 	= (xoffset + navbox_x + global_x_offset) + "px";		// Adjust for screen width if we're larger than the minimum width (800 pixels).
	}
	
}

function getViewportSize()
{
	var size = [0, 0];
	
	if (typeof window.innerWidth != 'undefined') {
		size = [
			window.innerWidth,
			window.innerHeight
		];
	} else if (typeof document.documentElement != 'undefined'
				&& typeof document.documentElement.clientWidth !=
				'undefined' && document.documentElement.clientWidth != 0) {
		size = [
			document.documentElement.clientWidth,
			document.documentElement.clientHeight
		];
	} else {
		size = [
			getObject('body')[0].clientWidth,
			getObject('body')[0].clientHeight
		];
	}
 
	// Render the design at 100% height in the event that it is smaller than the viewport's height.
	var oTable	= getObject("wrapper");
	var before = oTable.style.height;
	oTable.style.height = size[1] + "px";
	
	return size;
}

/*
	Function:		findPos(object)
	Description:	Returns the absolute x and y pixel coordinates of any object that has been loaded via the DOM
*/
function findPos(objName)
{
	var oElement 	= getObject(objName);
	var pos_x 		= 0;
	while( oElement != null ) {
		pos_x 		+= oElement.offsetLeft;
		oElement 	= oElement.offsetParent;
	}
	
	oElement 		= getObject(objName);
	var pos_y 		= 0;
	while( oElement != null ) {
		pos_y 		+= oElement.offsetTop;
		oElement 	= oElement.offsetParent;
	}
	
	var position = [
       pos_x,
       pos_y
    ];
	return position;
}

function GetBulletPos(index)
{	
	var bullety = findPos("bullet"+index)[1] + 3;
	var bulletx = findPos("bullet"+index)[0] + 37; // the width of this element is actually 40 pixels, but we're trying to find the center of the bullet
	return [bulletx, bullety];	
}

function init(pagetype) {	
	getViewportSize();	// resize the height to match 100% of the browser viewport's height.
	if(pagetype == "interior") {
		global_x_offset = -10;	
	}
}
