var mastertabvar= new Array();
var oSelectedMenu //Peronal, business, ub grand, tools, about us, contact us
var vCurrentHoveringMenu //Which [root] ID global menu is currently being hovered over.  Set in showsubmenu
var oSubMenuBar,vSubMenu //Set in SetZone. vSubMenu = td_[root]
var vWhichCloseState //Set in SetZone

var idBody, isSubdir, vSubDir

function basename(path, suffix) {
    var b = path.replace(/^.*[\/\\]/g, '');
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    return b;
}

//PHILOSOPHY: Transform a variable in the routine that is calling another routine.
//			  Only send routines what they need to act according to their purpose.

//SCHEMA
//[root] = id of global-nav-element anchor.
//dv_[root] = DIV parent of global-nav-element anchor.
//td_[root] = TABLE of one set of sub- (primary-) nav elements.

//Structure >>
//Globa nav
//TD#td_navigation
//	DIV#nav_global
//		DIV#dv_[root].[root]

//Primary nav >>
//TD#td_primarynav
//	DIV#space_between_global_and_subn
//		TABLE#td_[root].tbl_submenu
//			A.[root]

function initialize(){
//	var vParentID = document.getElementById("wrapper").parentNode.getAttribute("rel"); 
	idBody = document.getElementsByTagName('body')[0].getAttribute("id");
	//Stuff for sub-dir pages and the Login form button, mainly the mouseover image relative url.
	isSubdir = ((idBody == 'root') || (idBody == 'default')) ? true : false;
	vSubDir = "";
	if(!isSubdir){vSubDir = "../";} //If anything but 'root' or 'default' as the ID of body element.

	var globalmenuid = "nav_global";
	SetZone(idBody);
	//get collection of A's in nav_global
	var aLI = document.getElementById(globalmenuid).getElementsByTagName("a"); 
	var vID; //used to form the id of the submenus
	var numbsubmenus = aLI.length;
	var y = 0; //for the submenu (mastertabvar) array index, below.
	var isRel, vRel
	var oLink
	for (var i=0; i<numbsubmenus; i++){
		isRel = (aLI[i].getAttribute("rel") == "undefined" || aLI[i].getAttribute("rel") == null) ? false : true;
		if (isRel == true){ //DIV's if it is indicated to have a submenu (indicated by including a rel attribute)
			vRel = aLI[i].getAttribute("id");
			mastertabvar[y]="td_" + vRel; //store ids of submenus of tab menu
			y = y + 1;
			//with each addtion to the mastertabvar object array, length gets longer.
			aLI[i].onmouseover = function () {showsubmenu(this.id);}; // Assign id of the anchor tag the showmenu function on mouseover.
//			if(vSubDir == "../"){
			//I'm sick of having to keep track of all relative URLs.
				oLink = aLI[i];
				setNavLinkRelativity(oLink,idBody);
//			}
		} //end if rel
	} //end for
	setSecondaryNavRelativity();
//NOTE: not indicating a submenu by leaving out the REL breaks the script because closeall() expects something.  Easier to simply comment out the content of the secondary-nav table.	
} //end function

function setNavLinkRelativity(oLink,idBody){
//Get body id
//Get array of all links in nav_global.
var vClassname, vHref, newHref
//For each link
	//get classname ( = the filename)
	vClassname = oLink.className;
	//get current href
	vHref = oLink.href
		//if no ".htm", then attach alert on onclick
		if(vHref.indexOf(".htm")<0){
			oLink.onclick = function(){alert("Not linked yet or not yet built."); return false;}
			return;
		}
		//if bodyid = classname, skip
		if(idBody == vClassname){return}
		//else href = subdir + bodyid + "/" + aclassname + ".htm"
		newHref = vSubDir + vClassname + "/" + vClassname + ".htm";
		oLink.href = newHref;
}
function setSecondaryNavRelativity(){
	var aT = document.getElementById("space_between_global_and_subn").getElementsByTagName("table"); 
	var nT = aT.length;
	var oTableParent, vTableParentRootID
	var vClassname, vHref, newHref
	for (var i=0; i<nT; i++){
		oTableParent = aT[i];
		vTableParentRootID = oTableParent.id.substr(3);
		var aA = oTableParent.getElementsByTagName("a"); 
		var nA = aA.length;
		for(var y=0; y<nA; y++){
			if(aA[y]){
				//Get the href. If no htm, the placeholder value should = the classname.
				vHref = basename(aA[y].href);
				if(vHref.indexOf(".htm")<0){
					aA[y].onclick = function(){alert("Not linked yet or not yet built."); return false;}
					continue;
				}
				//if bodyid = classname, skip
				if(idBody == vTableParentRootID){continue}
				//else href = subdir + bodyid + "/" + aclassname + ".htm"
				newHref = vSubDir + vTableParentRootID + "/" + vHref;
				aA[y].href = newHref;
			}else{continue}//Move on to next parent table.
		}//End for aA
	}//End for aT
}//End function

function HoldForAbove(){

}

function swapoutbackground(who, what,calledby){
//The background in the global nav is in dv_[root], such as dv_personal.
//This subroutine only accepts "dv_[root] values.  Do not transform them here.
//The 'what' param = 0 | 1 for default | selected.
//alert(who + ": swapoutbackground called by " + calledby);
	if(who.substr(0,3)!="dv_"){
		alert("Routine: Swapbackground.\nYou're not passing me a variable that starts with 'dv_': <<" + who + ">>. Called by: " + calledby);
	}
	var vRoot = who.substr(3); //dv_ from the who variable to get just the [root].
	var oWho = document.getElementById(who);
	var vWhat, vBackground, vSubdir, vSelectedMenuID
//alert(vRoot + " = " + oSelectedMenu.id + " ? ");
	if(oSelectedMenu){ //ASSUMES that only the index.htm is in the root dir.  ALL OTHERS assume one dir down.
		vSelectedMenuID = oSelectedMenu.id.substr(3);
		vSubdir = "../"; 
	}else{
		vSubdir = ""
	}
	if(what==0){
		if(vSelectedMenuID == vRoot) {
			vWhat = "gbl_" + vRoot + "_selected.jpg" //Keeps the current zone 'selected' while in that zone.
		}else{
			vWhat = "gbl_" + vRoot + "_default.jpg"
		}
	}else{
		vWhat = "gbl_" + vRoot + "_selected.jpg"
	}
	vBackground = eval('"url(' + vSubdir + 'images/' + vWhat + ') top left no-repeat\";');
	//
	surface(who, "swapoutbackground"); //Set the z-index. Surface only accepts dv_[root].
	return oWho.style.background = vBackground;
}

function SetZone(vParentID){
//alert("SetZone");	
	//Set page zone selection. Get the value of the REL attribute in the BODY tag.
//	var vParentID = document.getElementById("wrapper").parentNode.getAttribute("rel"); 
	// the BODY rel, which should always be the zone name.  The body CLASS = the subpage ID.
	// ROOT directory files, such as Terms and Privacy have a rel = root.
	var vVisiblility
	switch(vParentID){
		case "default":
			vWhichCloseState = 0; //Homepage
			vVisiblility = "hidden";
			break;
		case "root":
			vWhichCloseState = 1; //such as Privacy and Terms of Use.
			vVisiblility = "visible";
			break;
		default:
			vWhichCloseState = 2; //all subdirectory pages
			vVisiblility = "visible";
			var vDivID = "dv_" + vParentID;
			var submenuID = "td_" + vParentID;
			vSubMenu = submenuID; //super-global variable
			oSelectedMenu = document.getElementById(submenuID); //super-global variable
			//Set SELECTED state of parent, global nav element. Second param = 0 | 1 for default | selected. 
			swapoutbackground(vDivID, 1, "SetZone");
			//turn on the submenu navigation items.
			oSelectedMenu.style.display = "block";
	}
	
	oSubMenuBar = document.getElementById("space_between_global_and_subn");
	oSubMenuBar.style.visibility = vVisiblility;
}

var iStart = 10;
function surface(dv, calledby) {//Give the moused-over global nav element the top-most z-index. (else the button borders won't appear right).
	//var dvLayer = "dv_" + dv;
//alert(dv + ": surface() called by " + calledby);	
	iStart++
  document.getElementById(dv).style.zIndex = iStart;
  //LATER: reset all the zindexs back to some beginning threshhold when a max has been reached.
}

function closeall(x, calledby){
//alert("closeall called by " + calledby);	
	//Idea is to 1) close all submenus and 2) re-show the one that corresponds to whose global nav item is being moused over.
	//All but index page have mouseout with value of 1 to indicate there is a sub-menu to re-display.

	//If mousing over the phantom zone that closes things on mouseover and nothing is being hovered over in the global nav,
	//then don't do anything.  Should cut down on the processing time.
	if((vCurrentHoveringMenu == null) || (vCurrentHoveringMenu == "void") || vCurrentHoveringMenu=="undefined"){
		return;
	}
	
	var vID
	var oSubNavContainer = document.getElementById("space_between_global_and_subn");
	
	//LOGIC >> See SetZone() for vWhichCloseState variable.
	//0) If homepage, closeall closes the SubNavContainer DIV as well as the submenus in the container.
	//1) If a root page other than the homepage, the container stays open, but there is nothing in it. So all submenus off.
	//2) If a subdir page, then the container says open all the time, and the menu for the Selected global menu is restored.
	
	//If homepage, hide the entire subnav container, then shut off the subnav items.
	if(vWhichCloseState == 0){
		MouseOutHovered(vCurrentHoveringMenu,"closeall");
		oSubNavContainer.style.visibility = "hidden"; //space_between_global_and_subn
		vCurrentHoveringMenu = null; //Set the hover-over-global-nav-element variable to null.
		return;
	}
	
	//If all other pages, hide just the subnav-item set.
//QUESTION: can I turn off just the subnav that is showing instead of looping?  Would that speed up processing?
	for (var i=0; i<mastertabvar.length;i++){
		//Loop thru all global submenus.
		//hide submenu
		document.getElementById(mastertabvar[i]).style.display ="none"; //style.display="none";

		//Remove hover state from parent. Substring is to remove the td_ designation for swapping the background.
		vID = "dv_" + mastertabvar[i].substr(3); //td_
		swapoutbackground(vID, 0,"closeall");
	}

	//If non-root pages, restore the Global-selected subnav set.
	if(x){//If there is a selected menu, however, turn it back on.  Activated only on MouseOut of the phantom zones. In the mouseout segments of ~index pages have 1 for a argument, else blank.
		if(oSelectedMenu){
			vID = "dv_" + oSelectedMenu.id.substr(3);
			oSubMenuBar.style.visibility = "visible";
			oSelectedMenu.style.display = "block";
			swapoutbackground(vID, 1,"closeall");
			vCurrentHoveringMenu = null; //global-nav-element hover-state variable to null since nothing being hovered over.
		} //end-if oSelectedMenu
	} //end-if there is a menu to turn back on.
}
//With closeall
//1) Don't want closeall invoked if nothing is changed. Change is recorded when vCurrentHoveringMenu has a value. Therefore
//	that value needs to be set to null when there is no hover action.


function showsubmenu(parentid){
//alert("calling showsubmenu");
	//This was assigned to all the Global Navigation links onmouseover.
	//parentid = id of anchor tag, which is the "root" id.
	var oParent = document.getElementById(parentid);
	var divid = "dv_" + parentid; //Global menu element. For mouseover effect [swapoutbackground(dv_)] and z-index change [surface(dv_)]
	var subid = "td_" + parentid; //Submenu ("Primary" menu) element.
	var oDiv = document.getElementById(divid);
	var dvbackground
	
	//Is there a menu being hovered over (vCurrentHoveringMenu)?  If yes, then just close that and its submenu. vCurrentHoveringMenu = the root id.
	if(vCurrentHoveringMenu){
		MouseOutHovered(parentid,"showsubmenu");
	}
	//If oSelectedMenu, then turn off the subnav for Selected global menu.
	if(vSubMenu){ //a global variable set on init in SetZone.
		 document.getElementById(vSubMenu).style.display = "none";
	}

	//mouseover color
	swapoutbackground(divid, 1,"showsubmenu"); //will be transformed to: td_parentid
	
	//Now turn on the submenu corresponding to the global nav item, and record its parent, global nav item as being hovered over.
	if(vWhichCloseState==0){//Remember, if you'r on the homepage, the primary-nav container bar is not visible.  Has to be turned on.
		// vWhichCloseState is set in SetZone.
		document.getElementById("space_between_global_and_subn").style.visibility = "visible";
	}
	var oSubMenuToOpen = document.getElementById(subid);
	var vcurrClassName = oSubMenuToOpen.className;
	oSubMenuToOpen.style.display = "block";
	//Set the vCurrentHoveringMenu to be that of the newly hovered-over global nav item.
	vCurrentHoveringMenu = parentid;
}

function MouseOutHovered(parentid,calledby){
	var vHoveredMenu = "dv_" + vCurrentHoveringMenu;
	var vHoveredSubMenu = "td_" + vCurrentHoveringMenu;
	var oHoveredSubMenu = document.getElementById(vHoveredSubMenu);
	swapoutbackground(vHoveredMenu, 0,"showsubmenu");
	oHoveredSubMenu.style.display = "none";
}

var d = new Date(); //write in the footer for the copyright year.
//document.write(d.getFullYear());

function butOver(x){
	var oID = document.getElementById(x)
	oID.style.backgroundImage=eval('"url(' + vSubDir + 'images/but_start_banking_selected.jpg)\";');
}
function butOut(x){
	var oID = document.getElementById(x)
	oID.style.backgroundImage=eval('"url(' + vSubDir + 'images/but_start_banking_default.jpg)\";');
}

var d = new Date(); //write in the footer for the copyright year.

// Inherited UB scripts
function despositratedate(){
	var theDate = new Date();
	var theMonth;
	switch (theDate.getMonth())
	{
		case 0: theMonth = 'January'; break;
		case 1: theMonth = 'February'; break;
		case 2: theMonth = 'March'; break;
		case 3: theMonth = 'April'; break;
		case 4: theMonth = 'May'; break;
		case 5: theMonth = 'June'; break;
		case 6: theMonth = 'July'; break;
		case 7: theMonth = 'August'; break;
		case 8: theMonth = 'September'; break;
		case 9: theMonth = 'October'; break;
		case 10: theMonth = 'November'; break;
		case 11: theMonth = 'December'; break;
	}
	
	document.write ( theMonth +' '+ theDate.getDate() +', '+ theDate.getFullYear() );
}