flyout_manager = new Array();
flyout_manager[ 'colors' ] = new Array();


addLoadEvent( mainNavigationFlyoutInit );

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


function revealDiv( divID )
{
	var node = document.getElementById( divID );
	var link = document.getElementById( 'javascript_revealdiv_' + divID );
	node.style.display = 'block';
	link.style.display = 'none';
}

function mainNavigationFlyoutInit( )
{	
	var node = document.getElementById( "main_navigation_menu" );
	/*	Don't run this function if you don't have a working node	*/
	if ( node )
	{
		mainNavigation( node );	
		record_flyout_link_styles();
	}
}

function mainNavigation( node )
{
	
	var mainNavigationLists = node.getElementsByTagName("li");
	
	for ( var i = 0; i < mainNavigationLists.length; i++ )
	{
		if ( mainNavigationLists[i].firstChild.nextSibling && mainNavigationLists[i].firstChild.nextSibling.nextSibling && mainNavigationLists[i].firstChild.nextSibling.nextSibling.nodeName == "UL" )
		{
			var ul_node = mainNavigationLists[i].firstChild.nextSibling.nextSibling;
			var ul_id = ul_node.id;
			
			flyout_manager[ ul_id ] = new Array();

			mainNavigationLists[i].onmouseover = function( )
			{
				var ul_node = this.firstChild.nextSibling.nextSibling;
				var ul_id = ul_node.id;
				
				//  cancel pending link mouseout, if set
				delete flyout_manager[ ul_id ][ 'link_mouseout_timer' ];
				
				//  cancel pending hide ul, if set
				if ( flyout_manager[ ul_id ][ 'hide_ul_timer' ] )
				{
					window.clearTimeout( flyout_manager[ ul_id ][ 'hide_ul_timer' ] );
					delete flyout_manager[ ul_id ][ 'hide_ul_timer' ];
				}
				else
				{
					style_flyout_link( ul_node.id, ':hover' );
					
					var liOffset = findY( this );						
					
					ul_node.style.display = "block";
					
					adjustFlyoutHeight( ul_node , liOffset);
				}
				
			}
			
			mainNavigationLists[i].onmouseout = function( )
			{
				var ul_node = this.firstChild.nextSibling.nextSibling;
				flyout_manager[ ul_node.id ][ 'hide_ul_timer' ] = setTimeout( 'hide_submenu( "' + ul_node.id + '" )', 20 );
				flyout_manager[ ul_node.id ][ 'link_mouseout_timer' ] = 1;
			}
						
		}
	}
}


function hide_submenu( submenu_id )
{
	var ul_node = document.getElementById( submenu_id );
	ul_node.style.display = 'none';
	delete flyout_manager[ ul_node.id ][ 'hide_ul_timer' ];
	style_flyout_link( ul_node.id , '' );
}


function adjustFlyoutHeight( node , offset )
{
	
	/*	Determine the y-offset of the current viewport-- get in both Safari, Win IE/6, AND Firefox!	*/
	if ( self.pageYOffset )
	{
		var theTop = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		var theTop = document.documentElement.scrollTop;
	}
	else
	{
		var theTop = document.body.scrollTop;
	}
	
	/*	For IE6–7/Win only, the height of the viewport is calculated through documentElent.clientHeight. For all other browsers, the height o the viewport is calculated through body.clientHeight. See http://www.quirksmode.org/js/doctypes.html 
	*/
	
	if ( document.documentElement && document.documentElement.clientHeight )
	{
		var pageHeight = document.documentElement.clientHeight;
	}
	else
	{
		var pageHeight = document.body.clientHeight;
	}
	
	var nodeHeight = node.offsetHeight;

	/*	The starting point of the offset is always the offset + the liHeight (bottom right of list element)	*/
	var startingPoint = offset;
/*STEP 1: IF THE BOTTOM OF THE UL DOES NOT FALL BELOW THE PAGE, THEN SIMPLY PLACE LIST AT STARTING POINT*/
	
	
	
	if ( ( startingPoint - theTop + ( nodeHeight - 20 ) ) < pageHeight )
	{
		/*alert( "existing marginTop: " + node.style.marginTop );*/
		node.style.width = "10em";	/*	Force a redraw, but without changing any actual value! This cures the big problem with Safari flickering over flash content. */
		
		node.style.marginTop = "0px";
		/*if ( node.firstChild && node.firstChild.nextSibling && node.firstChild.nextSibling.id == "subsubfirst" )
		{
			var spaceFromBottom = theTop + pageHeight - startingPoint;
			var nodeBelow = nodeHeight - spaceFromBottom;
			alert( "startingPoint: " + startingPoint + "\n theTop: " + theTop + "\n nodeHeight: " + nodeHeight + "\n pageHeight: " + pageHeight + "\n offset: " + offset + "\n spaceFromBottom: " + spaceFromBottom + "\n nodeBelow: " + nodeBelow);
		}*/
		return;
	}
/*STEP TWO: OTHERWISE, ALIGN THE BOTTOM OF THE UL WITH THE BOTTOM OF THE VIEWPORT	*/
	else
	{
		var spaceFromBottom = theTop + pageHeight - startingPoint;
		var nodeBelow = nodeHeight - spaceFromBottom;
		adjustMarginTop( node, nodeBelow );
		/*if ( node.firstChild && node.firstChild.nextSibling && node.firstChild.nextSibling.id == "subsubfirst" )
		{
		alert( "startingPoint: " + startingPoint + "\n theTop: " + theTop + "\n nodeHeight: " + nodeHeight + "\n pageHeight: " + pageHeight + "\n offset: " + offset + "\n spaceFromBottom: " + spaceFromBottom + "\n nodeBelow: " + nodeBelow);
		}*/
	}
		
}

function adjustMarginTop( node , offset )
{
	node.style.marginTop = "-" + ( offset - 19.9 ) + "px";	
}

/*	returns the y-position of an element. This function is based upon the findPos function at:
http://www.quirksmode.org/js/findpos.html
*/
function findY( obj )
{
	var curtop = 0;
	
	var printstring = '';
	if (obj.offsetParent)
	{
		curtop = obj.offsetTop;
		printstring += obj.nodeName + ' ' + obj.offsetTop + "<br />\n";
		while (obj = obj.offsetParent)
		{
			curtop += obj.offsetTop;
			printstring += obj.nodeName + ' ' + obj.offsetTop + "<br />\n";
		}
	}
	else
	{
		printstring = 'no offset parent ';
	}
	
	
	/*document.getElementById( 'right_content_inner' ).innerHTML += printstring + "**<br /><br />";*/
	
	return [curtop];
}


function record_flyout_link_styles()
{
	
	var right_col = document.getElementById( 'right_content_inner' );
	
	//  get the colors stylesheet rules object
	
	var doc_css = document.styleSheets[0];
	
	if ( document.styleSheets[0].cssRules )  //  w3c dom
	{
		var colors_css_rules = doc_css.cssRules[2].styleSheet.cssRules;
	}
	else  //  IE 5-6  (Watch out -- Safari implements both properties)
	{
		var colors_css_rules = doc_css.imports[2].rules;
	}
	
	//  loop through colors stylesheet once to get the styles in an array, with keys of selector texts
	
	
	for ( var i = 0; i < colors_css_rules.length; i++ )
	{
//right_col.innerHTML += colors_css.cssRules[i].selectorText + '<br />';
		
		var flyout_class_pos = colors_css_rules[i].selectorText.indexOf( 'main_navigation_flyout_link' );
		
		if ( flyout_class_pos != -1 )
		{
			var color_rule_key = colors_css_rules[i].selectorText.slice( flyout_class_pos );  //  should be the link_class_name, or link_class_name:hover, for main nav flyout links
			
/*

SAFARI

returns the selectorText with things like:

A.main_navigation_flyout_link_default[CLASS"main_navigation_flyout_link_default"]    (regular)
A.main_navigation_flyout_link_peachpuff[CLASS"main_navigation_flyout_link_peachpuff"]*[ID"main_navigation_menu"] :   (hover)
*[ID"main_navigation_menu"] LI A

So we'll do some special processing for it

*/
			if ( color_rule_key.indexOf( '[' ) != -1 )
			{
				if ( color_rule_key.slice( -1 ) == ':' )  //  how Safari reps the hover declaration
				{
					color_rule_key = color_rule_key.slice( 0, color_rule_key.indexOf( '[' ) ) + ':hover';
				}
				else  //  non-hover declaration
				{
					color_rule_key = color_rule_key.slice( 0, color_rule_key.indexOf( '[' ) );
				}
			}
			
			
			
//right_col.innerHTML += color_rule_key + '<br />';
			flyout_manager[ 'colors' ][ color_rule_key ] = new Array();
			
			flyout_manager[ 'colors' ][ color_rule_key ][ 'backgroundColor' ] = colors_css_rules[i].style.backgroundColor;
			flyout_manager[ 'colors' ][ color_rule_key ][ 'color' ] = colors_css_rules[i].style.color;
			
		}
		
	}

}


function style_flyout_link( ul_id, link_class_suffix )
{
		
	if ( link_class_suffix == ':hover' || flyout_manager[ ul_id ][ 'link_mouseout_timer' ] )
	{
		var a_node = document.getElementById( ul_id ).parentNode.firstChild;	
		var a_node_key = a_node.className + link_class_suffix;
		
		if ( flyout_manager[ 'colors' ][ a_node_key ] )
		{
			for ( var prop in flyout_manager[ 'colors' ][ a_node_key ] )
			{
				a_node.style[ prop ] = flyout_manager[ 'colors' ][ a_node_key ][ prop ];
			}
		}
	}

}







/*	A browserdetect function, if we ever need it in the future.	*/
/*var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

*/