//Mike Ottinger
//09-06-02
// Modified by Michael Johns
// 06/16/03 - fixed sticking flynav
//******************************************************************************************************
//This program determines the location of the requested page and
//generates the left navigation dynamically.


//global variables *************************************************************************************

//**HTML output string that gets printed out to the page.
var HTML_out = "";

//**Array of navigation objects, each item represents the navigation's graphic, the path of it's link,
//and information about any flynavs it may have.
var navObjectarray = new Array();

//**This is the index used for creating the array of Navlink Objects.
var master_count = 0;

//**This will be prepended to the linkName property to determine the images path.
var imgPath = "/images/";

//**Determine the path of the requested page at runtime. This will be split into an array by it's '/' to
//determine the parent directory of the current page which will be matched to the parent directory of
//the correct navigation object.
var curr_url = location.pathname;

//**Holds an array of the flynav <div> names and their visibility for the flynav functionality.
var flynavarray = new Array();

//**counter for the flynavarray[].
var flyctr = 0;

//**This is holds the flynav name in use.
var currFlyNav 		= "";//name of div tag flyNav

//**This indicates if a flyNav is visible
var flyNavIsInUse  	= 0;

//*********************************end of gloabl variables**********************************************

//****************************Beginning of Object Declarations******************************************

//**This creates an object called navObject that represents each link in the left navigation of each page.
function navObject(linkName, linkHeight, linkPath, isflyNav, hiddenTable, section_num) {

   this.linkName = linkName;
   this.linkPath = linkPath;
   this.linkHeight = linkHeight;
   this.isflyNav = isflyNav;
   this.hiddenTable = hiddenTable;
   this.isCurrent = false;                         //is the current one being displayed
   this.imgOn = imgPath + linkName + "_on.gif";    //image path of "on" image
   this.imgOff = imgPath + linkName + "_off.gif";  //image path of "off" image
   this.imgSelected = imgPath + linkName + "_select.gif";  //image path of "select" image
   this.section_num = section_num; //determine section number
    
};


//this object represents a flynav object, with the hidden table name and
//it's associated visibility...
function flynavObject( hiddenTableName, visibility ) {

   this.hiddenTableName =  hiddenTableName;
   this.visibility = visibility;

};

//*********************************End of Object Declarations********************************************

//**This is the function that's called from each section's include file(xx_left.js). This adds a
//NavObject to the array navObjectarray[].
       //createNav("lnav_3_0", "22",     "/investors/", false);
function createNav( linkName, linkHeight, linkPath, isflyNav, hiddenTable, section_num) {

   navObjectarray[ master_count++ ] = new navObject(linkName, linkHeight, linkPath, isflyNav, hiddenTable, section_num);

}

//**Beginning of the main() function that loops through each navObject item first populating the
//flynav array for each flynav in the NavObj array, then determine which link's graphic is to be 
//drawn as being selected by calling associatePage()...
 

//**Now run through each NavObj and print out the code...
function main() {

   var section_holder = "";
   
   populateFlyNavArray();
   associatePage();

   //**Loop through each NavObject and generate it to the leftNav...
   for (j=0; j<navObjectarray.length; j++) {
   
      section_holder = navObjectarray[ j ].section_num;
	  
      //**First determine if the navobj is the selected one...
      if ( navObjectarray[ j ].isCurrent ) {
	  
         //**Then determine if the navObj has a flynav...
         if ( navObjectarray[ j ].isflyNav ) {
		    //the html_out is then set to a lengthy string of html with attribute values being set by the
			//properties of the navObjectarray. If there is a flynav, the generation of that code is 
			//handled in the createFlyNavCode function. The setSelectFlag() is a function in the basics.js
			//file that is used to prevent the selected state's arrow from disappearing. This happens
			//because the flyout calls a rollover on event onMouseOver to keep the 'on' state active when
			//a user rolls from the left nav to the flyout portion. This looks sloppy when the original
			//state of the left nav is 'select' and the onMouseOver causes the 'on' state to appear, thus
			//removing the arrow.
		    HTML_out = "<a href=\"" + navObjectarray[ j ].linkPath + "\" onMouseOver=\"setAssociatedFlyNav(\'" + navObjectarray[ j ].hiddenTable + "\'); "+ createFlyNavCode( navObjectarray[ j ].hiddenTable ) + ";\" onMouseOut=\"setSelectFlag(); checkFlyNavState(\'" + navObjectarray[ j ].hiddenTable + "\');\"><img src=\"" + navObjectarray[ j ].imgSelected + "\" name=\"" + navObjectarray[ j ].linkName + "\" width=\"153\" height=\"" + navObjectarray[ j ].linkHeight + "\" border=\"0\" id=\"" + navObjectarray[ j ].linkName + "\"></a><br>";
				
		 }	
         else {
			HTML_out = "<a href=\"" + navObjectarray[ j ].linkPath + "\" onMouseOver=\"left_nav_off(\'" + navObjectarray[ j ].section_num + "\');\"><img src=\"" + navObjectarray[ j ].imgSelected + "\" name=\"" + navObjectarray[ j ].linkName + "\" width=\"153\" height=\"" + navObjectarray[ j ].linkHeight + "\" border=\"0\" id=\"" + navObjectarray[ j ].linkName + "\"></a><br>";
         }
      }
	  //**The navobj item is not the selected one.
      else {
	  
         if ( navObjectarray[ j ].isflyNav ) {
		    HTML_out = "<a href=\"" + navObjectarray[ j ].linkPath + "\" onMouseOver=\"setAssociatedFlyNav(\'" + navObjectarray[ j ].hiddenTable + "\'); MM_swapImage(\'" + navObjectarray[ j ].linkName + "\','',\'" + navObjectarray[ j ].imgOn + "\',0);" + createFlyNavCode( navObjectarray[ j ].hiddenTable ) + ";\" onMouseOut=\"MM_swapImgRestore(); checkFlyNavState(\'" + navObjectarray[ j ].hiddenTable + "\');\"><img src=\"" + navObjectarray[ j ].imgOff + "\" name=\"" + navObjectarray[ j ].linkName + "\" width=\"153\" height=\"" + navObjectarray[ j ].linkHeight + "\" border=\"0\" id=\"" + navObjectarray[ j ].linkName + "\"></a><br>";
	    
		 }	 
	     else {   
			HTML_out = "<a href=\"" + navObjectarray[ j ].linkPath + "\" onMouseOver=\"MM_swapImage(\'" + navObjectarray[ j ].linkName + "\','',\'" + navObjectarray[ j ].imgOn + "\',0);left_nav_off(\'" + navObjectarray[ j ].section_num + "\');\" onMouseOut=\"MM_swapImgRestore()\"><img src=\"" + navObjectarray[ j ].imgOff + "\" name=\"" + navObjectarray[ j ].linkName + "\" width=\"153\" height=\"" + navObjectarray[ j ].linkHeight + "\" border=\"0\" id=\"" + navObjectarray[ j ].linkName + "\"></a><br>";
	     } 
      }
	      
      //print out the resulting HTML_out variable, then loop around again..
      document.write(HTML_out);
	  
   }//end for
   
   //this is to prevent the left nav from sticking if the mouse rolls under the bottom link
   //document.write("<div style=\"width:150; height:20px;\" onMouseOver=\"left_nav_off(\'" + section_holder + "\');\"></div>");
};




//**Populate a flynav array with hiddentable names from the navObject array.
function populateFlyNavArray() {
   for (k=0; k<navObjectarray.length; k++) { 
      //**If the current navObject contains a flynav, create a new flynav object in the array for 
	  //generating MM_showHideLayers() later.   
      if ( navObjectarray[k].isflyNav ) {
	     flynavarray[flyctr++] = new flynavObject( navObjectarray[k].hiddenTable, "hide" );
	     
	  }
   } 
};


//--------------- modified associatePage() -------------------
function associatePage() {
	var target;
	var metaValue, metaList;
	var php;   
	if (document.getElementsByName) {
	  metaList = document.getElementsByTagName("META");
	  for ( i = 0; i < metaList.length; i++ ){
	  	if ( metaList[i].name == "DirectoryOverride" ){
	  		metaValue = metaList[i].content;
	  	}
	  }
	} else {
	  metaList = document.ids.DirectoryOverride;
	  if ( metaList != null ) { metaValue = metaList.content; }
	}
	
	//**Array that contains the current url's path segments. 
	var url_array = new Array();
	if ( metaValue != null ) {
		url_array = metaValue.split("/");
	} else {
		url_array = curr_url.split("/");
	}
   
	//**Array that contains the NavObj's linkpath segments.
	var navobj_array = new Array();
	var foundit = false;
	var pathElement = url_array.length - 2; // parent directory of this document
	var navObjPart = 2; // the parent directory of navigation link
	var wholeTarget = url_array.join("/");
	
	if(curr_url.indexOf('.php') != -1) {
	   php = true;
	   //alert("current url has .php in it");
	}
	//alert(curr_url);
		
	
   //**************************************************************************************************  
   //**************************************************************************************************
   //**************************************************************************************************
   
	/* first check to see if the whole url matches */
	for ( i = 0; i < navObjectarray.length; i++ ){
		if ( navObjectarray[i].linkPath == wholeTarget ){
			navObjectarray[i].isCurrent = true;
			foundit = true;
		}
	}
	
	/* if we didn't find an exact match, the check the parent directories of the nav object against
		each succesive element (from the right) of the page path
	*/
	while ( (! foundit) && pathElement >= 0  ){
		target = url_array[pathElement];
		
		i = 0;
		while ( (i < navObjectarray.length) && ( ! foundit ) ){
		    //alert(navObjectarray[ i ].linkPath);
		    if(navObjectarray[ i ].linkPath.indexOf('.php') != -1 && php) {
			   //alert("nav item has php in it");
			   navObjectarray[ i ].isCurrent = true;
			   foundit = true;
			}
			//**Break up the linkpath property into an array  
			navobj_array = navObjectarray[ i ].linkPath.split("/");
	  		
			if ( navobj_array[ navobj_array.length - navObjPart ] == target && !php) {
				navObjectarray[ i ].isCurrent = true;
				foundit = true;
			}
			
			i++;
		}//end while
		
		pathElement--;
	}
}//end function




//********************************************************************************************************
//**This creates the MM_showHideLayers() code and gets inserted in the HTML_out code. It works by receiving
//the value of the hiddenTable currently being generated
function createFlyNavCode( currHiddenTable ) {
	  
   var template_fly_text = "MM_showHideLayers(\'";
   //**Holds the outputted text that get's placed into HTML_out.
   var param_text = "";
      
   //**First loop through the array of flynav objects and set the current flynav's visibility to
   //'show' and the rest to hide.
   for (h=0; h<flynavarray.length; h++) {
      //**If the current flynav object's hidden table equals the current
	  //navobj's hidden table name, set it's visibility to "show".
      if ( flynavarray[h].hiddenTableName == currHiddenTable ) {
	     flynavarray[h].visibility = "show";
	  }
	  else {
	     flynavarray[h].visibility = "hide";
	  }
   }

   //this loop generates actual code by going through the array of flynavs, listing out it's name and
   //corresponding visibility, then setting this all to param_text. 
   for (p=0; p<flynavarray.length; p++) {
   
      //if not at the end of the array...  
      if ( p != (flynavarray.length - 1) ) {
         param_text += flynavarray[p].hiddenTableName + "\',\'\',\'" +  flynavarray[p].visibility + "\',\'";
	  }
	  //generates MM_showHideLayers() code with ending ")"...
	  else {
	     param_text += flynavarray[p].hiddenTableName + "\',\'\',\'" +  flynavarray[p].visibility + "\')";
	  }
   }
   
   //reset the visibility of all the flynav arrays to "hide"...
   for (y=0; y<flynavarray.length; y++) {
   
      flynavarray[ y ].visibility = "hide";   
   }
   
   return template_fly_text + param_text;

};

// set status flags for flynav
function flyNavInUse(currentDiv,currState){

	currFlyNav = currentDiv;
	flyNavIsInUse = currState;
};

// check flynav status
function checkFlyNavState (flyNavToCheck){

	if ((flyNavToCheck == currFlyNav)&&(!flyNavIsInUse)){
	 
		MM_showHideLayers(flyNavToCheck,'','hide');
	 
	}

};

// flag which flynav (divtag) in use
function setAssociatedFlyNav (currentFlyNav){
	currFlyNav = currentFlyNav;
};
//end of createFlyNaveCode()
