// set the id of global navigation container
gnContainer = $('globalNav');

/**************************************************************************
Begin set the image path by protocol
***************************************************************************/
var gnImages = 'imgs/nav/i/globalNav/';

/**************************************************************************
End set the image path by protocol
***************************************************************************/

// gnActive refers to the active drop down menu, so we start it off as null
gnActive = null;

// check for object and embeds?
gnOE = 'true';

// the onload event, in most cases navInit should be added to whatever javascript initialization is already in place
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
addLoadEvent(navInit);

// launches the global navigation
function navInit() {
	navGetImages();			
	version=0

	if (navigator.appVersion.indexOf("MSIE")!=-1){
	document.execCommand("BackgroundImageCache",false,true)
	temp=navigator.appVersion.split("MSIE")
	version=parseFloat(temp[1])
	}
	
	if (typeof gnAlt == 'undefined') {
	gnAlt = 'false'
	}

	var h2s = getElementsByClass('globalNav',gnContainer,'H2');
	for (var i=0; i < h2s.length; i++)
	{
		if (h2s[i].id == "activeLink")
		{
			gnCategory = i;
		}
	}

	if (typeof gnCategory == 'undefined') {
	gnCategory = '7'
	}

	if (typeof gnFlash == 'undefined') {
		if (gnOE == 'true') {
			var objects = document.getElementsByTagName('OBJECT');
			var embeds = document.getElementsByTagName('EMBED');
			if ((objects.length >= 1) || (embeds.length >= 1))
			{
				gnFlash = 'true';
			}
			else
			{
				gnFlash = 'false';
			}
		}
		else
		{
			gnFlash = 'false';
		}
	}

	if ($('marqueeFlashContent'))
	{
		gnFlash = 'false';
	}

	if (((navigator.appVersion.indexOf('Mac') != -1) && (gnFlash == 'true')) || ((navigator.appVersion.indexOf('MSIE')!=-1) && (version<=5.4)) || (gnAlt == 'true'))
	{
	navAltInit();
	}
	else
	{
	navRegInit();
		if (version==5.5) {
			var uls = getElementsByClass('globalNavList',gnContainer,'ul');
			for (var i = 0; i < uls.length; i++) 
			{
				var lis = uls[i].getElementsByTagName('li');
				for (var j = 0; j < lis.length; j++)
				{
					lis[j].style.display = 'inline';
				}
			}
		}
	}
}

// prepares the alternate version of the global navigation with only top level functionality
function navAltInit() {

	var hrefs = getElementsByClass('globalNavLink',gnContainer,'A');
	var imgs = getElementsByClass('globalNavImage',gnContainer,'IMG');
	var divs = getElementsByClass('globalNavContent',gnContainer,'DIV');
	for (var i=0; i < imgs.length; i++)
	{
		if (i == gnCategory)
		{
		divs[i].style.display = "none";
		imgs[i].src=gnImages+"globalNav"+i+"Image_on.gif";
		}
		else
		{
		divs[i].style.display = "none";
		eval("imgs[i].onmouseout = function() {return (this.src='"+gnImages+"globalNav"+i+"Image_off.gif');}");
		eval("imgs[i].onmouseover = function() {return (this.src='"+gnImages+"globalNav"+i+"Image_on.gif');}");
		eval("hrefs[i].onfocus = function() {return (this.firstChild.src='"+gnImages+"globalNav"+i+"Image_on.gif');}");
		eval("hrefs[i].onblur = function() {return (this.firstChild.src='"+gnImages+"globalNav"+i+"Image_off.gif');}");
		}
	}
}

// prepares the regular version of the global navigation with full functionality
function navRegInit() {
	
	var hrefs = getElementsByClass('globalNavLink',gnContainer,'A');
	var imgs = getElementsByClass('globalNavImage',gnContainer,'IMG');
	var divs = getElementsByClass('globalNavContent',gnContainer,'DIV');
	var uls = getElementsByClass('globalNavList',gnContainer,'UL');
	
	gnTotal = hrefs.length-1;

	for (var i=0; i < hrefs.length; i++)
	{
		hrefs[i].id = "globalNav"+i;

		if (i == gnCategory)
		{
			imgs[i].src=gnImages+"globalNav"+i+"Image_on.gif";
		}
		imgs[i].id = "globalNav"+i+"Image";
		divs[i].id = "globalNav"+i+"Content";
		uls[i].id = "globalNav"+i+"List";
		
		eval("hrefs[i].onkeydown = function(event) {return primaryArrows(event,"+i+");}");
		eval("hrefs[i].onmouseout = function(event) {return collapse(event,this);}");
		eval("hrefs[i].onmouseover = function() {return dropDown("+i+");}");
		eval("hrefs[i].onfocus = function() {return dropDown("+i+");}");
		eval("divs[i].onmouseout = function(event) {return collapse(event,this);}");
		
		var flyouts = getElementsByClass('flyout',divs[i],'DIV');
		var lis = uls[i].getElementsByTagName('LI');
		for (var j = 0; j < lis.length; j++)
		{
			flyouts[j].id = "g"+i+"n"+j;
			flyouts[j].style.display = "none";
			
			eval("lis[j].onmouseover = function() {flyoutInit('g"+i+"n"+j+"',this)};");
			eval("lis[j].onmouseout =  function() {clearTimeout(gnTime);};");	
			eval("lis[j].firstChild.onfocus = function() {return (this.parentNode.style.backgroundColor='#660000');}");
			eval("lis[j].firstChild.onblur = function() {return (this.parentNode.style.backgroundColor='#000000');}");
			eval("lis[j].firstChild.onkeydown = function(event) {return secondaryArrows(event,this.parentNode,"+i+");}");
		}
		
		var lc = last_child(uls[i]);
		var lcfc = first_child(lc);
		eval("lcfc.onfocus = function() {return makeActiveDrop(this.parentNode,"+i+");}");
	}
	var fc = first_child($('lastLink'));
	var fcnb = node_before($('lastLink'));
	var fcnbfc = first_child(fcnb);
	eval("fc.onblur = function(event) {return collapseAlso();}");
	eval("fcnbfc.onfocus = function(event) {return makeActiveDrop(this.parentNode,"+gnTotal+");}");
	eval("$('globalNav0').onblur = function(event) {return collapseAlso();}");
	eval("$('firstLink').firstChild.onfocus = function(event) {return makeActiveDrop(this.parentNode,'0');}");
}

// last link shows rollover color and drops menu on focus
function makeActiveDrop(el,active) {
	dropDown(active);
	el.style.backgroundColor='#660000';
}

// handles the arrow key functionality on the top level of the navigation
function primaryArrows(e,active)
{
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code == '37')
	{
		e.returnValue = false;
		var previousid = active-1;
		if(active != '0'){$('globalNav'+previousid).focus()};
	}
	if (code == '39')
	{
		e.returnValue = false;
		var nextid = active+1;
		if(active != gnTotal){$('globalNav'+nextid).focus()};
	}
	if (code == '40')
	{
		e.returnValue = false;
		var fc = first_child($('globalNav'+active+'List'));
		var fcfc = first_child(fc);
		fcfc.focus();
	}
}

// handles the arrow key functionality on the secondary level of the navigation
function secondaryArrows(e,active,dropdown)
{
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code == '38')
	{
		e.returnValue = false;
		if (node_before(active))
		{	
			var nb = node_before(active);
			var nbfc = first_child(nb);
			nbfc.focus();
		}
		else
		$('globalNav'+gnActive).focus();
	}

	if (code == '37')
	{
		e.returnValue = false;
		var previousid = dropdown-1;
		if(dropdown != '0')
		{
			$('globalNav'+previousid).focus();
			dropDown(previousid);
		}
	}

	if (code == '39')
	{
		e.returnValue = false;
		var nextid = dropdown+1;
		if(dropdown != gnTotal)
		{
			$('globalNav'+nextid).focus();
			dropDown(nextid);
		}
	}
	
	if (code == '40')
	{
		e.returnValue = false;
		if (node_after(active))
		{	
			var na = node_after(active);
			var nafc = first_child(na);
			nafc.focus();
		}
	}
}

// fires the drop downs
function dropDown(gnDown) {
	
	if (gnActive == null || !(gnActive == gnDown))
	{	
		collapseAlso();

		$('globalNav'+gnDown+'Image').src = gnImages+'globalNav'+gnDown+'Image_on.gif';

		if (navigator.appVersion.indexOf("MSIE")!=-1) {
			if (version<=6)  {
				var selects = document.getElementsByTagName('SELECT');
				for (var i = 0; i < selects.length; i++)
				{
					selects[i].style.visibility='hidden';
				}
			}
		}

		changeOpac(0,'globalNav'+gnDown+'List');
		
		$('globalNav'+gnDown+'Content').style.left='auto';
		
		opacity('globalNav'+gnDown+'List');

		gnActive = gnDown;
	}
}

// decides whether to collapse a drop down when the user leaves the area
function collapse(e,handler) {
	if (!e) var e = window.event
	if(isMouseLeaveOrEnter(e,handler)) {
		var tg = $('globalNav'+gnActive+'Content')
		var tz = $('globalNav'+gnActive)
		var relTarg = e.relatedTarget || e.toElement;
		while (relTarg && relTarg != tg && relTarg != tz && relTarg.nodeName != 'BODY')
		relTarg = relTarg.parentNode
		if (relTarg == tg || relTarg == tz) return;
		collapseAlso();		
	}
}

// collapses a drop down
function collapseAlso() {
	if (gnActive != null)
	{
		$('globalNav'+gnActive+'Content').style.left='-1000em';
		for (var i = 0; i < gnTotal+1; i++)
		{	
			if (i != gnCategory)
			{
				$('globalNav'+i+'Image').src = gnImages+'globalNav'+i+'Image_off.gif';
			}
		}
		flyin();
		gnActive = null;
	}
	if (navigator.appVersion.indexOf("MSIE")!=-1) {
		if (version<=6)  {
			var selects = document.getElementsByTagName('SELECT');
			for (var i = 0; i < selects.length; i++)
			{
				selects[i].style.visibility='visible';
			}
		}
	}
}

// takes in the information needed to fire a flyout
function flyoutInit(gnFlyoutIntake,gnListIntake){
	gnFlyout = gnFlyoutIntake;
	gnList = gnListIntake;
	gnTime = setTimeout("flyout()",300);
}

// fires a flyout
function flyout(){

	flyin();

	gnList.style.backgroundColor = '#660000';	

	changeOpac(0,gnFlyout);
	
	$(gnFlyout).style.display='block';

	height(gnFlyout);
}

// clears flyouts and active colors
function flyin() {
	if (gnActive != null)
	{
		var lis = $('globalNav'+gnActive+'List').getElementsByTagName('li');		
		for (var j = 0; j < lis.length; j++)
		{
			lis[j].style.backgroundColor = '#000000';
		}
		var activeDiv = $('globalNav'+gnActive+'Content');
		var flyouts = getElementsByClass('flyout',activeDiv,'div');
		for (var i = 0; i < flyouts.length; i++)
		{
		flyouts[i].style.display='none';
		}
	}
}

// handles the opacity shift
function opacity(id) {

	var speed = 40;
    var timer = 0; 

	for(i = 0; i <= 90; i=i+10)
	{
		setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
		timer++;
	}
}

// handles the height plus opacity shift
function height(id) {

	var speed = 16;
    var timer = 0;
	
	var maxHeight = $(id).offsetHeight;
	
	heightStep = maxHeight/100

	changeHeight(0,id)

	for(var x=0; x<=100; x=x+5)
	{
		setTimeout("changeHeight(" + x + ",'" + id + "')",(timer * speed));
		setTimeout("changeOpac(" + x + ",'" + id + "')",(timer * speed));
		timer++;
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = $(id).style;
    object.opacity = (opacity / 101);
    object.MozOpacity = (opacity / 101);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

// changes the height of the flyout
function changeHeight(currentHeight, id) {
	var sync = heightStep*currentHeight;
    $(id).style.height = sync + "px";
}

// preload the navigation rollover images
function navGetImages() {
	img0=new Image()
	img0.src=gnImages+"globalNav0Image_on.gif"
	img1=new Image()
	img1.src=gnImages+"globalNav1Image_on.gif" 
	img2=new Image()
	img2.src=gnImages+"globalNav2Image_on.gif" 
	img3=new Image()
	img3.src=gnImages+"globalNav3Image_on.gif" 
	img4=new Image()
	img4.src=gnImages+"globalNav4Image_on.gif" 
	img5=new Image()
	img5.src=gnImages+"globalNav5Image_on.gif" 
	img6=new Image()
	img6.src=gnImages+"globalNav6Image_on.gif"
}

// a simple get elements by id function
function $() {
	if (arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	$c(arguments).each(function(el){
		elements.push(get$(el));
	});
	return elements;
	function get$(el){
		if (typeof el == 'string') el = document.getElementById(el);
		return el;
	}
}

// a simple get elements by class function
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// determines if the mouse actually left the content box in order to handle event bubbling issues
function isMouseLeaveOrEnter(e,handler)
{	
	var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
	while (reltg && reltg != handler) reltg = reltg.parentNode;
	return (reltg != handler);
}

function is_all_ws( nod )
{
  // Use ECMA-262 Edition 3 String and RegExp features
  return !(/[^\t\n\r ]/.test(nod.data));
}

function is_ignorable( nod )
{
  return ( nod.nodeType == 8) || // A comment node
         ( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws
}

function node_before( sib )
{
  while ((sib = sib.previousSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

function node_after( sib )
{
  while ((sib = sib.nextSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

function last_child( par )
{
  var res=par.lastChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.previousSibling;
  }
  return null;
}

function first_child( par )
{
  var res=par.firstChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.nextSibling;
  }
  return null;
}