/**
 * 2ndTierNavigation.js
 * Code for displaying submenus and subchannels.
 */
 
// Current active tab
var currentActiveTab="";
var currentActiveTabNum=0;

// Tab or Submenu currently permanently underlined 
var currentActiveUnderlinedElement="";
var activeIsSub=false;
var activeSubNum=false;

// Active submenu timeout
var activeSubmenuTimeout = null;

// current top menu inactive color
var curTopMenuInactiveColor=null;

// current top menu active color
var curTopMenuActiveColor=null;


/**
 * showSubChannel(layerId)
 * Shows a particular subchannel.
 * @param layerId	The ID of the layer you want to show
 */
function showSubChannel(layerId) {
  hideSubChannels();
  getElementSafely(layerId).css.visibility = "visible";
}

/**
 * hideSubChannels()
 * Hides all subchannels.
 */
function hideSubChannels() {
  for (var i = 0; i <= numSubChannels - 1; i++) {
	  getElementSafely("submenu" + i).css.visibility = "hidden";
  }
}

/**
 * revertSubmenu()
 * Sets a small timeout to revert to the active submenu.  This timeout
 * is very small to correct for the fact that the user may mouseout of
 * a link, but stay within the submenu channel.
 */
function revertSubmenu() {
	activeSubmenuTimeout = setTimeout("doRevertSubmenu()", 200);
}

/**
 * doRevertSubmenu()
 * Once the timeout actually expires, do the reverting.
 */
function doRevertSubmenu() {
	hideSubChannels();
	if (currentActiveTab) {
		showSubChannel("submenu"+currentActiveTabNum);
	} else {
		showSubChannel("submenu0");
	}
}

/**
 * clearRevertSubmenu()
 * Clear the timeout and don't revert back to the active submenu.
 */
function clearRevertSubmenu() {
	if (activeSubmenuTimeout) {
		clearTimeout(activeSubmenuTimeout);
	}
	activeSubmenuTimeout = null;
}

// set the initial colors


function initializeColors(activeTab, activeSub, topMenu, topMenuActive, topSepBar, tabActive,
														tabInactive, submenuActive, submenuInactive, subBarColor) {
	var tabName="subMenuLayerLink"+activeTab;
	var subName="";
	if(activeSub>-1) {
		if(activeSub<10) 
			subName=tabName+"Sub"+"0"+activeSub;
		else 
			subName=tabName+"Sub"+activeSub;
	}
	// set the colors and make home page initially green and underlined
	
	// first set the inactive color for the dropdown menus
	// set the global variable first
	curTopMenuActiveColor=topMenuActive;
	curTopMenuInactiveColor=topMenu;
	for(i=0; i<topMenuNum-1; i++)
		getElementSafely("sepbar"+i).css.color=topSepBar;
	for(i=0; i<topMenuNum; i++)		
		getElementSafely("nav"+i).css.color=topMenu;
	// then set the home page link
	// to be active color and underlined
	var currentElement=getElementSafely(tabName);
	currentElement.css.color=tabActive;
	if(activeSub==-1) {
		currentElement.css.textDecoration="underline";
		currentActiveUnderlinedElement=tabName;
		activeIsSub=false;
		activeSubNum=-1;
	}
	currentActiveTab=tabName;
	currentActiveTabNum=activeTab;
	// then set the other tabs to the inactive color
	for(i=0; i<numSubChannels; i++) 
		if(i!=activeTab)
			getElementSafely("subMenuLayerLink"+i).css.color=tabInactive;

	// set the active submenu, if any
	if(activeSub>-1) {    
		currentElement=getElementSafely(subName); 
		// make it highlighted and underlined
		currentElement.css.color=submenuActive;
		currentElement.css.textDecoration="underline";
		currentActiveUnderlinedElement=subName;
		activeIsSub=true;
		activeSubNum=activeSub;  
	}

	// and last but not least, set all the (other) submenus to the submenu inactive color	
	for(i=0; i<numSubChannels; i++)
		for(j=0; j<numSubMenus[i]; j++) {
			if(j<(numSubMenus[i]-1)) {
				if(j<10)
					getElementSafely("barTab"+i+"Sub0"+j).css.color=subBarColor;
				else
					getElementSafely("barTab"+i+"Sub"+j).css.color=subBarColor;
			}
			if(!(i==activeTab && j==activeSub)) {
				if(j<10)
					getElementSafely("subMenuLayerLink"+i+"Sub"+"0"+j).css.color=submenuInactive;
				else 
					getElementSafely("subMenuLayerLink"+i+"Sub"+j).css.color=submenuInactive;					
			}
		}
}


function changeTabColors(tabId, topInactive, topActive, topSepBar, subBar) {
	// do top menu colors
	for(i=0; i<topMenuNum-1; i++)
		getElementSafely("sepbar"+i).css.color=topSepBar;
	curTopMenuActiveColor=topActive;
  curTopMenuInactiveColor=topInactive;
	for(i=0; i<topMenuNum; i++) 
		getElementSafely("nav"+i).css.color=topInactive;	
  var currentElement = getElementSafely(tabId);
  var tabIndex=parseInt(tabId.substring(16, tabId.length));
  // set the global color variables
  var tabActiveColor = tabHomeActiveColor[tabIndex];
  var tabInactiveColor = tabHomeInactiveColor[tabIndex];
  var submenuInactiveColor = submenuHomeInactiveColor[tabIndex];
  // de-underline the current underlined element
  getElementSafely(currentActiveUnderlinedElement).css.textDecoration="none";
  // set the current tab var
  currentActiveTab=tabId;
  currentActiveTabNum = tabIndex;
  currentActiveUnderlinedElement=currentActiveTab;
  activeIsSub=false;
  activeSubNum=-1;
  // underline it and set the active color
  currentElement.css.textDecoration="underline";
  currentElement.css.color=tabActiveColor;
  // set all other tab's colors to inactive
  for (i=0; i<numSubChannels; i++) 
    if(i!=tabIndex)
      getElementSafely("subMenuLayerLink"+i).css.color=tabInactiveColor;
  // set all the submenu colors to inactive
	for(j=0; j<numSubChannels; j++)
	for(k=0; k<numSubMenus[j]; k++) {
		if(k<10) {
      getElementSafely("subMenuLayerLink"+j+"Sub"+"0"+k).css.color=submenuInactiveColor;
			if(k<(numSubMenus[j]-1))
				getElementSafely("barTab"+j+"Sub0"+k).css.color=subBar;
		}
    else {
			getElementSafely("subMenuLayerLink"+j+"Sub"+k).css.color=submenuInactiveColor;
			if(k<(numSubMenus[j]-1))
				getElementSafely("barTab"+j+"Sub"+k).css.color=subBar;
		}
	}
}	


function changeSubmenuColors(submenuId, parentTabId, topInactive, topActive, topSepBar, subBar) {
	// do top menu colors
	for(i=0; i<topMenuNum-1; i++)
		getElementSafely("sepbar"+i).css.color=topSepBar;
	curTopMenuActiveColor=topActive;
  curTopMenuInactiveColor=topInactive;
	for(i=0; i<topMenuNum; i++) 
		getElementSafely("nav"+i).css.color=topInactive;	
  currentElement = getElementSafely(submenuId);
  var submenuIndex;
  var index;
  if(submenuId.charAt(submenuId.length-2)=='0')  
     index=1; 
  else index=2;   
  submenuIndex = parseInt(submenuId.substring(submenuId.length-index, submenuId.length));
  var parentTabIndex = parseInt(parentTabId.substring(16, parentTabId.length));
  // set the global color variables
  var tabActiveColor = tabOtherActiveColor[parentTabIndex][submenuIndex];
  var tabInactiveColor = tabOtherInactiveColor[parentTabIndex][submenuIndex];
  var submenuActiveColor = submenuOtherActiveColor[parentTabIndex][submenuIndex];
  var submenuInactiveColor = submenuOtherInactiveColor[parentTabIndex][submenuIndex];
  // de-underline the current underlined element
  getElementSafely(currentActiveUnderlinedElement).css.textDecoration="none";
  // set the current tab var 
  currentActiveTab=parentTabId;
  currentActiveTabNum = parentTabIndex;
  // set the color of the parent to active tab color
  getElementSafely(parentTabId).css.color=tabActiveColor;
  // set the other tabs to inactive color
  for(i = 0; i<numSubChannels; i++)
    if(i!=parentTabIndex)
      getElementSafely("subMenuLayerLink"+i).css.color=tabInactiveColor;
  // set the active submenu color and underline it
  currentActiveUnderlinedElement = submenuId;
  activeIsSub=true;
  activeSubNum=submenuIndex;
  currentElement.css.textDecoration="underline";
  currentElement.css.color=submenuActiveColor;
  // set the other submenu colors to inactive
  for(j=0; j<numSubChannels; j++) 
		for(k=0; k<numSubMenus[j]; k++) {
			if(k<(numSubMenus[j]-1)) {
				if(k<10)
					getElementSafely("barTab"+j+"Sub0"+k).css.color=subBar;
				else
					getElementSafely("barTab"+j+"Sub"+k).css.color=subBar;
			}
      if(!(j==parentTabIndex && k==submenuIndex)) 
        if(k<10) 
          getElementSafely("subMenuLayerLink"+j+"Sub"+"0"+k).css.color=submenuInactiveColor;
        else 
         getElementSafely("subMenuLayerLink"+j+"Sub"+k).css.color=submenuInactiveColor;
		}
}

/* * Underline the specific submenu link you have your mouse over
 * called on mouseover event
 */
function doSubMenuUnderline(linkId) { 
    if(linkId!=currentActiveUnderlinedElement) 
      getElementSafely(linkId).css.textDecoration = "underline";
}

/**
 * clearSubMenuUnderline(linkId)
 * Clear the underline your mouse was over
 * gets called on mouseout event
 */
function clearSubMenuUnderline(linkId) {
    if(linkId!=currentActiveUnderlinedElement)
      getElementSafely(linkId).css.textDecoration = "none";    
}

