/* COPYRIGHT INFORMATION All pages within a SITE are the property of DTE Energy or its Affiliate, unless other ownership is 
indicated. All information and content including any software programs available on or through a SITE is protected by copyright. A 
user is permitted to download information from a SITE which, by its nature or context, is to be copied in order to use SITE-related 
services. A user is permitted to download information from a SITE for private personal use. A user is prohibited from modifying, copying, 
distributing, transmitting, displaying, publishing, selling, licensing, creating derivative works or using any Content available on or 
through a SITE for any commercial or public purpose unless the user has the prior written permission of DTE Energy or its Affiliate. 
*/

//Find which page the user is on and identify the corresponding unique ID in menu.js 
winLocation = location.pathname;

//Re-write above variable to string so we can compare to items in menu.js (not sure why we must do this, but it only works this way)
var windowLocation = "";
for(var f=0; f < winLocation.length; f++){
	windowLocation = windowLocation+winLocation.charAt(f);
}
//End re-write

//If the document path does not include a document name assume it is index.html
var winLength=winLocation.length-1;
if(windowLocation.charAt(winLength)=="/"){
	windowLocation = windowLocation+"index.html";
}
//End index.html assumption

//Locate document in menu.js and identify corresponding unique ID
menuLocation=-1;
for(var b=0; b < localNav.length; b++){
	var tmpLocalNav = localNav[b].split('|');
	
  //find row in menu.js table that matches page
	if((windowLocation.indexOf(tmpLocalNav[2])!=-1)){
		windowLocation = tmpLocalNav[0];
		menuLocation = b;
		//current locations unique ID is now windowLocation
	}
}
//End document location

//If unique ID is multi-tier, convert to array for future use
var menuStyle = windowLocation.split('_');
var parentMenu = menuStyle[0];
for(var b=1; b < menuStyle.length-1; b++){
	parentMenu += "_" + menuStyle[b];
}
var menuNumber = menuStyle[0];
var menuTier = menuStyle.length;

//new code to not allow the current menu item to go past second tier
if (menuTier > 2) {
  var foundTier = 0;
  for (var b=menuLocation; b > 0; b--) {
    tmpLocalNav = localNav[b].split('|');
    windowLocation = tmpLocalNav[0];
    menuStyle = windowLocation.split('_');
    if (foundTier == 0) {
      if (menuStyle.length < 3) {
        foundTier = 1;
        menuLocation = b;
        menuNumber = menuStyle[0];
      }
    }
  }
}

//Create and display the menu
function makeTabs(){
	document.write('<table class="Menu" width="100%" cellpadding="1" cellspacing="3">');
  document.write('<tr><td><img src="/images/ourServices.gif" border="0" alt="Our Services" /></td></tr>');
		
  for(var y=0; y < localNav.length; y++){
    var tmpLocalNav = localNav[y].split('|');
    var splitTmpLocalNav = tmpLocalNav[0].split('_');

    //highlight current menu item
    if(menuLocation == y){
  		if(splitTmpLocalNav.length == 1){
        document.write('<tr><td id="selectedFirstTierMenuItem"><a align="left" valign="middle" href="'+tmpLocalNav[2]+'">');
        writeSpaces(splitTmpLocalNav.length);
        document.write(tmpLocalNav[1]+'</a>');
        document.write('</td></tr>');
      } else {
        document.write('<tr><td id="selectedSecondTierMenuItem"><a align="left" valign="middle" href="'+tmpLocalNav[2]+'">');
        writeSpaces(splitTmpLocalNav.length);
        document.write(tmpLocalNav[1]+'</a>');
        document.write('</td></tr>');
      }

    //menu item must be in the same root level as the current menu item
    } else if(splitTmpLocalNav[0]==menuNumber){
  		if(splitTmpLocalNav.length == 1){
  			writeFirstTierItem(tmpLocalNav[2], splitTmpLocalNav.length, tmpLocalNav[1], tmpLocalNav[3]);
  		} 
  		if(splitTmpLocalNav.length == 2){
  			writeSecondTierItem(tmpLocalNav[2], splitTmpLocalNav.length, tmpLocalNav[1], tmpLocalNav[3]);
  		}
    
 		//menu item is not in the same root level, only show first level menu items
  	}else{
  		if(tmpLocalNav[0].length==1){
  			writeFirstTierItem(tmpLocalNav[2], splitTmpLocalNav.length, tmpLocalNav[1], tmpLocalNav[3]);
  		}
  	}
  }	
  document.write('</table><br /><br />');
}
//End Make Menu

/***********************************************************************
   Functions Used to Make Menu
************************************************************************/
function writeFirstTierItem(pageLink, tierSpaces, linkDisplay, lastPipe){
  if(lastPipe !='h') {
  	document.write('<tr>');
  	document.write('<td id="firstTierMenuItem" align="left" valign="middle"><a href="'+pageLink+'"');
  	
    if(lastPipe == 1){
  		document.write('target="_blank"');
  	}
    
  	document.write('>');
  	writeSpaces(tierSpaces);
  	document.write(linkDisplay+'</a>');
  	document.write('</td></tr>');
  }
}

function writeSecondTierItem(pageLink, tierSpaces, linkDisplay, lastPipe){
  if(lastPipe !='h') {
  	document.write('<tr>');
  	document.write('<td id="secondTierMenuItem" align="left" valign="middle"><a href="'+pageLink+'"');
  	
    if(lastPipe == 1){
  		document.write('target="_blank"');
  	}
    
  	document.write('>');
  	writeSpaces(tierSpaces);
  	document.write(linkDisplay+'</a>');
  	document.write('</td></tr>');
  }
}

function writeSpaces(spaceNum){
	for(var s=0; s < (spaceNum*2)-1; s++){
		document.write('&nbsp;');
	}
}
/**********************************************************************
   End Make Menu Functions
***********************************************************************/
