/*
*these JavaScript functions tranverse the navigation menu divs, displaying and hiding the appropriate ones
*@author Martin Anderson
*@version 1.6
*@date 02/11/05
* update 07/12/05 Martin - implemented grandparent search to account for fund centre oddities
*
*/			
function navExpander(){
	
	var bodyTag = document.getElementsByTagName("body")[0] ;
	if (bodyTag.style.visibility == 'hidden' ){
		//alert("navExpander: breaking due to T and C redirect")	;
		return ;
	}

	var pMatch = document.location.href.split("site")[1];
	pMatch = new String( pMatch ) ;//Ensure it's a string
	pMatch = pMatch.split("?")[0] ;
	
	//alert('this url = ' + pMatch);
	
	var divMenu1 = document.getElementById("menu1") ;
	if (divMenu1 == null){//check that it's not null and exit if it is
		return ;	
	}
	var divNodes = divMenu1.getElementsByTagName('div') ;
	var boolDivMatch = false ;
	
	//alert("starting loop");
	forLoopExactMatch: //named for loop
	for (var i=0 ; i<divNodes.length ; i++){
		var divNode = divNodes.item(i) ;
		//alert('matching to ' + getMyHref( divNode ));
		if ( getMyHref( divNode ) == pMatch ){
			displayDiv( divNode );			//display this div
			displayAncestorDivs( divNodes , i);	//display parent 
			displaySiblingDivs( divNodes,  i ); 	//display siblings
			displayChildDivs( divNodes,  i ); 	//display children
			displayTopLevelDivs( divNodes ); 	//display the top level divs
			boolDivMatch = true ;			//set matched flag to show match is true
			break  forLoopExactMatch;
		}	
	}//for
	
	//remove request parameters if any for fuzzy checking. If we don't we might end up checking the the values
	//of a request parameter, .e.g the terms and conditions page
	var misMatch = pMatch.split("?")[0] ;

	//Little something to take into account the additional menu system for pe toolkit...
	// pe toolkit top level id is 1.0 and 1.1 - if this is selected (by a separate process) then do not fuzzy match..
	if(document.getElementById('1.0')&&document.getElementById('1.0').className.indexOf('_selected')>0) boolDivMatch=true;
	if(document.getElementById('1.1')&&document.getElementById('1.1').className.indexOf('_selected')>0) boolDivMatch=true;

	/** Parent Search - Fuzzy Match stage 1**/
	if ((!boolDivMatch) && ( getTokens( misMatch ).length >= 2) ){
		//alert("no exact match found");
		for (var i=0 ; i<divNodes.length ; i++){
			var divNode = divNodes.item(i) ;
			if ( compareMisMatch( getMyHref( divNode) , misMatch, 0 ) ){
				displayDiv( divNode );			//display this div
				displayAncestorDivs( divNodes , i);	//display parent 
				displaySiblingDivs( divNodes,  i ); 	//display siblings
				displayChildDivs( divNodes,  i ); 	//display children
				displayTopLevelDivs( divNodes ); 	//display the top level divs
				boolDivMatch = true ;			//set matched flag to show match is true
				break ;
			}
		}//for
	}
	
	/** GrandParent Search - Fuzzy Match stage 2**/
	if ((!boolDivMatch) && ( getTokens( misMatch ).length >= 2) ){
		//alert("no exact match found");
		for (var i=0 ; i<divNodes.length ; i++){
			var divNode = divNodes.item(i) ;
			if ( compareMisMatch( getMyHref( divNode) , misMatch, 1 ) ){
				displayDiv( divNode );			//display this div
				displayAncestorDivs( divNodes , i);	//display parent 
				displaySiblingDivs( divNodes,  i ); 	//display siblings
				displayChildDivs( divNodes,  i ); 	//display children
				displayTopLevelDivs( divNodes ); 	//display the top level divs
				boolDivMatch = true ;			//set matched flag to show match is true
				break ;
			}
		}//for
	}	
	var divMenu0 = document.getElementById("menu") ;
	if (divMenu0 != null){//check that it's not null
		showDiv( divMenu0 ) ;
	}
}//end navExpander
	

	
		
/*
* The following section checks for an immediate parent by checking the tokens produced by a split()
* To the depth demanded by the depth of the 3rd argument
*/
function compareMisMatch( pDivHref , pMisMatch, pDepth ){
	//alert("pDivHref: " + pDivHref +"\r\npMisMatch" + pMisMatch) ;
	//remove the last value to ensure a fuzzy match
	var pMisMatch = pMisMatch.substring(0, pMisMatch.lastIndexOf("/"))
	var pDivHref = pDivHref.substring(0, pDivHref.lastIndexOf("/"))
	var divHrefTokens = getTokens( pDivHref ) ;
	var misMatchTokens = getTokens( pMisMatch ) ;
	var boolmatch = false ;
	
	if ( misMatchTokens.length >= divHrefTokens.length ){
		//alert("in parent search");
		forLoopCompareTokens0:
		for ( var i = 0 ; i < misMatchTokens.length - pDepth ; i++){
			//alert(misMatchTokens[i]+ " misMatchTokens[i]\r\n" + divHrefTokens[i] + " divHrefTokens[i]") ;
			if ( misMatchTokens[i] == null || divHrefTokens[i] == null ){
				boolmatch = false ;
				break forLoopCompareTokens0;	
			}	
			if (misMatchTokens[i].toLowerCase() != divHrefTokens[i].toLowerCase()){
				boolmatch = false ;
				break forLoopCompareTokens0;	
			}
		boolmatch = true ;//Parent found
		}
	}
	return boolmatch ;
}

function displayDiv( pDiv ){
	if (pDiv != null){
		showDiv( pDiv ) ;
		//now highlight the selected node 
		/* Updated - 26/09/2005 - Select all parent nodes - Brian 
		 * use css **_selected** suffix for nav levels. 
		 * Apply to current node AND parent nodes */						  

		var selected = "_selected";		// css suffix to denote selected navigation class
		var m_nodes = getMyMenuId(pDiv);	// Get the selected node id as an array
		var x="";
		
		for (var i = 0 ; i < m_nodes.length ; i++){
			x=i==0?m_nodes[0]: x+ "." + m_nodes[i];  //if sub level - append node id value (grandiloquence to avoid pleonastic verbiage)
			
			if(document.getElementById(x) && document.getElementById(x)!=null){  		//Check id exists
				if(document.getElementById(x).className.indexOf('_selected')<1){  	//Check not already selected..:S
					document.getElementById(x).className=document.getElementById(x).className + selected;
				}
			}
		}
		/* End Updated - 26/09/2005 - Select all parent nodes - Brian */
	}//if
}//end displayDiv

function displayAncestorDivs( pDivs , pIndex ){
	/*Pseudocode
	* compare tokens for each nodes where tokens are equal before the last token, i.e. ancestors
	* now uncles and aunts are added
	*/
	var selectedDivTokens = getTokens( getMyMenuValue( pDivs.item(pIndex) ) ) ;
	var bIsAncestor = false ;
	
	for ( var i = 0 ; i < pDivs.length ; i++){
		var ancestorDiv = pDivs.item(i) ;
		var ancestorTokens = getTokens( getMyMenuValue( ancestorDiv ) ) ;
		if ( ancestorTokens.length < selectedDivTokens.length){
			for ( var j = 0 ; j < ancestorTokens.length-1 ; j++ ){
			
				if ( ancestorTokens[j] != selectedDivTokens[j]){
					bIsAncestor = false ;
					break ;
				}else{
					bIsAncestor = true ;
				}
			}//for(j)
			if (bIsAncestor){
				showDiv( ancestorDiv ) ;
			}
		}//if
	}//for(i)
}//end displayAncestorDivs


function displaySiblingDivs( pDivs , pIndex ){
	/*Pseudocode
	* compare tokens for each nodes where tokens are equal before the last token, the last token is different i.e. siblings
	*/
	var selectedDiv = pDivs.item(pIndex) ;
	var selectedDivTokens = getTokens( getMyMenuValue( selectedDiv ) ) ;
	var bIsSibling = false ;
	
	//now loop through all divs and show the sibling: same attributes to -1 as selected but length is same
	for ( var i = 0 ; i < pDivs.length ; i++ ){
		var sibDiv = pDivs.item(i) ;
		var sibDivTokens = getTokens(  getMyMenuValue( sibDiv ) ) ;

		if ( sibDivTokens.length == selectedDivTokens.length){
			for ( var j = 0 ; j < selectedDivTokens.length-1 ; j++ ){
				if ( sibDivTokens[j] != selectedDivTokens[j]){
					bIsSibling = false ;
					break ;
				}else{
					bIsSibling = true ;
				}
			}//end for
			if (bIsSibling){
				showDiv( sibDiv ) ;
			}		
		}
	}
}//end displaySiblingDivs

function displayChildDivs( pDivs, pIndex ){
	/*Pseudocode
	* compare tokens for each nodes where tokens are the same but there is one extra token i.e. children
	*/

	var selectedDiv = pDivs.item(pIndex) ;
	var selectedDivTokens = getTokens( getMyMenuValue( selectedDiv ) ) ;
	var bIsChild = false ;
	
	//alert('menu ' + selectedDivTokens + " - " + (selectedDivTokens.length +1));	
	//now loop through all divs and show the children: same attributes as selected but length is one more
	for ( var i = 0 ; i < pDivs.length ; i++ ){
		var childDiv = pDivs.item(i) ;
		var childDivTokens = getTokens( getMyMenuValue( childDiv ) ) ;
		//alert('menu ' + childDivTokens + " - " + childDivTokens.length);
		if ( childDivTokens.length == selectedDivTokens.length +1 ){
			//alert('going in!');
			for ( var j = 0 ; j < selectedDivTokens.length ; j++ ){
				//alert('matching ' + childDivTokens[j] + ' and ' + selectedDivTokens[j]);
				if ( childDivTokens[j] != selectedDivTokens[j] ){
					//alert('nope');
					bIsChild = false ;
					break ;
				}else{
					//alert('yep');
					bIsChild = true ;
				}
			}//end for
			if (bIsChild){
				showDiv( childDiv ) ;
			}
		}//if
	}//for
}//end displayChildDivs

function displayTopLevelDivs( pDivs ){
	for (var i=0 ; i<pDivs.length ; i++){
		var tlDiv = pDivs.item(i) ;
		//if the class value is top level then show
		if( tlDiv.getAttribute("class")  == "LHS_navigator_link_box_level1" ){
			showDiv(tlDiv) ;
		}//if
	}//for
}//end displayTopLevelDivs

/* private method to display divs*/
function getTokens( pLocation ){
	var strLocation = new String(pLocation) ;//cast for non-strings
	strLocation = strLocation.replace( /\.jsp/ , "") ;
	//return strLocation.split(\/) ; //splits on "/" as a string
	return strLocation.split(/[\/.]/g ) ;//splits on "/"  as a RegExp object
}//end getTokens

/* private method to display divs*/
function showDiv ( pDiv ){
	if ( pDiv != null ){
		pDiv.style.display = "block" ;
		pDiv.style.visibility = "visible" ;
	}
}

/* private method to hide divs*/
function hideDiv ( pDiv ){
	if ( pDiv != null ){
		pDiv.style.display = "none" ;
		pDiv.style.visibility = "hide" ;
	}
}

/* private method to get the href of of the a tag in the div*/
function getMyHref( pDivToHref ){
	var as = pDivToHref.getElementsByTagName('a') ;
	var ahref = as.item(0).getAttribute("href") ;
	
	// old code replaced by code to remove params from the url
	//return  ahref.split("site")[1]; 
	var fullurl = ahref.split("site")[1];
	return fullurl.split("?")[0] ;
	
}

/* private method to get the id of of the div*/
function getMyMenuValue( pDiv ){
	var divId = pDiv.getAttribute('id') ;//get id value e.g. menuID_1.1.1
	var divIdTokens = divId.split("_") ; //split on underscores to create "muenuID", "1.1.1"
	return  divIdTokens[1];  //return the 2rd value "1.1.1"
}

//Created for update - 26/09/2005 - nav highlighting - Brian
/* private method to get the id of of the div as an array*/
function getMyMenuId( pDiv ){
	var divId = pDiv.getAttribute('id') ;//get id value e.g. menuID_1.1.1
	var divIdTokens = divId.split(".") ; //split on periods to create "muenuID1", "1" ,"2", etc 
	return  divIdTokens;  //return it
}
/* This function builds a breadcrumb menu string by looping over the 
   anchor tags in the menu looking for the bold items
*/
function breadcrumb(){
	if(document.getElementById("menu1") && document.getElementById("breadcrumb_header")){ //check menu exists
		var strCrumb = "" ; //breadcrumb String
		var divNodes = document.getElementById("menu1").getElementsByTagName('div') ;//get the <div> tags
		for (var i=0; i<divNodes.length; i++) { //loop over the <div> tags
			var divNode = divNodes.item(i); //create local var for <div> tag
			var cn = divNode.className;
			if (cn && cn.indexOf("_selected") > -1) {
				var aChildren = divNode.getElementsByTagName('a'); // get the child <a> tags
				if (aChildren.item(0)) {
					var href = aChildren.item(0).getAttribute('href');
					var text = (aChildren.item(0).textContent)?aChildren.item(0).textContent:aChildren.item(0).innerText;
					strCrumb = (strCrumb == "") ? ("<a href=\"" + href + "\">" + text + "</a>") : (strCrumb+" > <a href=\"" + href + "\">" + text + "</a>");
				}
			}
		}
		document.getElementById("breadcrumb_header").innerHTML = strCrumb ;
	}
}
