var browserName = navigator.appName; // Get Browser Name
var browserVer = parseInt(navigator.appVersion); // Get Browser Version
var hidden = "hidden"; // Browser specific Verbage to tell DHTML Layers to turn off and on
var visible = "visible"; // Same
var browserWidth;
var netSuck=0;
var openMenu; // Stores The Open Menu
var whatTimer; // Stores Menu Timer
var preloadFlag; // true when rollovers preloaded

var offset = 180; // Distance from Middle to left side of menu
var	mac_ie_offset = 200;
var subnav_menu = new Array("about_navigation", "member_services_navigation", "news_navigation", "action_center_navigation", "programs_navigation", "agriculture_info_navigation", "links_navigation", "contact_us_navigation");
var subnav_menu_tops = new Array();
var pageLoaded = 0;

var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);

var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_mac    = (agt.indexOf("mac")!=-1);
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5")!=-1) );

//alert(agt);

window.onload = function () {
	pageLoaded = 1;
	preloadImages();
	
	if (is_ie && is_mac) {

	// This flashes all of the menus on to hack a bug for ie on a mac.

		offset = mac_ie_offset;
		for (j=0; j<subnav_menu.length; j++)  {
			thismenu = subnav_menu[j];
			//alert ("show "+ thismenu);
			showMenu(thismenu);
			//alert ("hide "+ thismenu);
			hideMenu(thismenu);
		}
	} else if (is_ie5) {
	
		offset = mac_ie_offset;
		
	}
	
}



// This Function Takes An Object ID and returns a "Handle " Based on what Browser is detected
function getObj(id) {
	
	if (browserName == "Netscape" && browserVer <= 4){
		netSuck=1;
	  	hidden = "hide";
  		visible = "show"
		browserWidth = window.innerWidth - 20;
		return document.layers[id];
	} else if (browserName == "Netscape" && browserVer > 4) {
	  	//browserWidth = window.innerWidth;
		browserWidth = document.body.clientWidth;
    	return document.getElementById(id).style;
	} else {
		browserWidth = document.body.clientWidth;
    	return document.getElementById(id).style;
	}
}



// This Function Shows the Menu Object 'menu'
function showMenu(menu){

	if (pageLoaded) {

		if (whatTimer) clearTimeout(timer);

		//alert(openMenu);

		//for (i=0; i<subnav_menu.length; i++) hideMenu(subnav_menu[i]);

		if (menu!="none") {
	
			var menuObj = getObj(menu);	
			
			
			
			openMenu=menu;
			if (netSuck!=1) {
				Sleft = (browserWidth/2) - offset; 
				menuObj.left = Sleft + 'px';
				
				
				
				
		
			if (subnav_menu_tops[menu]==null) {
		
				theTop = getStyle(menu,"top");
				theTop = parseInt(theTop);
				subnav_menu_tops[menu] = theTop;
				//alert("top set to " + theTop);
			
			} else theTop = subnav_menu_tops[menu];
		
		
			//alert(menuObj.left);
		
			if (document.documentElement && document.documentElement.scrollTop) theScrollTop = document.documentElement.scrollTop;
			else theScrollTop = document.body.scrollTop
		
			//alert (theScrollTop);
			theHeight = document.getElementById(menu).offsetHeight;
			theBottom = getHeight();
			theDifference = theBottom - (theTop + theHeight - theScrollTop); 
		

		
			if (theDifference < 0) menuObj.top = (theTop + theDifference) + 'px';
			else menuObj.top = subnav_menu_tops[menu] + 'px';

			menuObj.visibility = visible;
		
			

			
		
			} else openMenu="none";	
		}  else openMenu="none";
		
		
		for (i=0; i<subnav_menu.length; i++)  {
			if (subnav_menu[i] != menu) hideMenu(subnav_menu[i]);
		}
		
	}
}

function showThisMenu(menu){
	
	var menuObj = getObj(menu);	
	menuObj.visibility = visible;
}

	
// This Function Hides the Menu Object 'menu'
function hideMenu(menu){
	
	var menuObj = getObj(menu);	
	menuObj.visibility = hidden;
}

function startMenuTimer(menu) {
	if (whatTimer == menu) clearTimeout(timer);
	
	what = "hideMenu('" + menu + "')";
	timer = setTimeout(what, 2000);
	
	whatTimer = menu;
}


function getStyle(el,styleProp) {
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}


function getHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
 // window.alert( 'Width = ' + myWidth );
 // window.alert( 'Height = ' + myHeight );
  return myHeight;
}




function changeImage(what, image) {
	if (preloadFlag) {
		document[what].src = image;
	}
}


function preloadImages() {
	if (document.images) {

		loader = newImage("/images/nav-weather-over.jpg");
		loader = newImage("/images/cfbf-logo-over.jpg");
		preloadFlag = true;	
	}
}

// This Funtion Helps Preload the rollover images
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}







