//menu

//browser sniffer
var browserName=navigator.appName; 
var isNav = (navigator.appName.indexOf("Netscape")>=0);

//globals
var tableWidth = 0;
var currentMenuShowingID;
var nextMenuSelectedID;
var cancelHideSubMenu = true; //cancel sub menu hide if mouseover sub
var subMenuShowing = false;
var currentRowMousedOver = null;
var previousRowMousedOver = null;
var mousedOffTable = false;


//for customization

mainActivatedBackgroundColor = "#FFFFFF"; //mouseover color of menu item
mainActivatedBorderColor = "1px solid #666666";     //mouseover color of menu border
mainActivatedFontColor = "#5E5A38";       //mouseover color of menu border
mainActivatedSideBarColor = "#CCCCCC";    //mouseover color of menu sidebar

mainDeactivatedBackgroundColor = "#FCF9F6"; //should be same as topmenucell style in css
mainDeactivatedBorderColor = "1px solid #999999";     //should be same as main border mouseoff
mainDeactivatedFontColor = "#6C6950";       //should be same as main font color mouseoff
mainDeactivatedSideBarColor = "#666666";    //should be same as main sidebar mouseoff

subActivatedBackgroundColor = "#FFFFFF"; //mouseover color of submenu item
subActivatedBorderColor = "1px solid #666666";     //mouseover color of submenu border
subActivatedFontColor = "#5E5A38";

subDeactivatedBackgroundColor = "#E7E1DA"; //should be the same as subMenuCell in css
subDeactivatedBorderColor = "1px solid #FFFFFF"
subDeactivatedFontColor = "#333333";

subTableOffsetX = 1;   //distance of sub table from the right of the main table
subTableOffsetY = -3;   //distance of sub table from the top of the main table row

/*
function getObj(name) {
//cross browser DHTML code
	if (document.getElementById) {
    this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers) {
    this.obj = document.layers[name];
    this.style = document.layers[name];
	}
}*/

function showSubMenu(iRowID) {
	var subMenuSelected = new getObj("subMenu"+iRowID);
	var mainMenuRow = new getObj("Row"+iRowID);

	cancelHideSubMenu = true;

	//position submenu that's been selected
	positionSubMenu(subMenuSelected, iRowID);

	//close existing menu if one is opened
	if ((currentMenuShowingID != null) && (currentMenuShowingID != iRowID)) {
		cancelHideSubMenu = false;
		hideSubMenu('newRowActivated');
	}
  
	//display the sub menu
	subMenuSelected.obj.style.display = "block";
	
	//let js know a submenu is currently displayed
	subMenuShowing = true;
	
	currentMenuShowingID = iRowID;

}

function callHideSubMenu() {
	//if sub menu is showing, wait 3 seconds to hide menu
	//otherwise deactivate row properties and sidebar properties
	//right away
	//this is done so that when user mouses off of table, we deactivate
	//the sidebar properties. Otherwise, if a submenu is showing, we wait
	//a few seconds to deactivate
	
	if (subMenuShowing) {
		setTimeout('hideSubMenu()',3000);
	}
	else {
		hideSubMenu();
	}	
}

function hideSubMenu(deactivateType) {
	if (currentMenuShowingID != undefined) {
		var subMenuToHide = new getObj('subMenu'+currentMenuShowingID);
		if (!cancelHideSubMenu) {
			subMenuToHide.obj.style.display = "none";
			//change color of sidebar back to normal on mouseoff
			//need to know if user moused off table or if user selected
			//another row.
			deactivateSideBar(currentMenuShowingID, deactivateType);
			cancelHideSubMenu = true;
			subMenuShowing = false;
		}
	}
}

function positionSubMenu(subMenuSelected, iRowID) {
	//main table object
	var mainTable = new getObj("mainTable");
	var mainTableDiv = new getObj("mainTableDiv");
	var subMenuDiv = new getObj("subMenu"+iRowID);
	var mainMenuRow = new getObj("Row"+iRowID);
	
	var curTop = 0;
	
	//get properties of main table
	tableWidth = mainTable.obj.style.width;
	tableWidth = parseInt(tableWidth.replace("px", ""));
	
	//find x and y position of main table
	var xPos = findPosX(new getObj("mainTable"));
	var yPos = findPosY(new getObj("Row"+iRowID));
	
	//set visibility
	//position sub menu
	subMenuSelected.style.left = xPos + tableWidth + subTableOffsetX + "px";
	subMenuSelected.style.top = yPos + subTableOffsetY +  "px";
	
}

function findPosX(divElement) {
//find x position of menu - calculate position from left of screen
	var curLeft = 0;
	
	if (divElement.obj.offsetParent) {
		while (divElement.obj.offsetParent) {
			curLeft += divElement.obj.offsetLeft;
			divElement.obj = divElement.obj.offsetParent;
		}
	}
	else if (divElement.obj.x) {
		curLeft += divElement.obj.x;
	}
	
	return curLeft;
}

function findPosY(divElement) {
//find y position of menu - calculate position from top of screen
	var curTop = 0;
		
	if (divElement.obj.offsetParent) {
		while (divElement.obj.offsetParent) {
			curTop += divElement.obj.offsetTop;
			divElement.obj = divElement.obj.offsetParent;
		}
	}
	else if (divElement.y){
		curTop += divElement.obj.y;
	}
	
	return curTop;
}

function activateMainMenu(iMainMenuID) {
	//activate row properties
	//previous to last current moused over
	previousRowMousedOver = currentRowMousedOver; 
	
	var mainMenuItemSelected = new getObj(iMainMenuID);
	mainMenuItemSelected.obj.style.cursor = "pointer";
	mainMenuItemSelected.obj.style.backgroundColor = mainActivatedBackgroundColor;
	mainMenuItemSelected.obj.style.border = mainActivatedBorderColor;	
	mainMenuItemSelected.obj.style.color = mainActivatedFontColor;
	
	currentRowMousedOver = iMainMenuID;
	
	//change color of previous sidebar if previous has been selected - deactivate
	if (previousRowMousedOver != null) {
		deactivateSideBar(previousRowMousedOver, "newRowActivated");
		if (currentRowMousedOver != previousRowMousedOver) {
			deactivateMainMenu(previousRowMousedOver);
		}
	}
	
	//change color of sidebar to highlight row that is currently moused over
	activateSideBar(currentRowMousedOver);
}

function deactivateMainMenu(iMainMenuID) {
	//put menu item back to normal state
	var currentMenuShowingIDRow = "Row" + currentMenuShowingID;
	var mainMenuItemSelected = new getObj(iMainMenuID);
  	  	
	mainMenuItemSelected.obj.style.backgroundColor = mainDeactivatedBackgroundColor;
	mainMenuItemSelected.obj.style.border = mainDeactivatedBorderColor;
	mainMenuItemSelected.obj.style.color = mainDeactivatedFontColor;

}

function activateSubMenu(iNewSubMenuID) {
	 //display properties of subMenu that cursor is over
	cancelHideSubMenu = true;

	var subMenuItemSelected = new getObj(iNewSubMenuID);
  
	//highlight row selected
	subMenuItemSelected.obj.style.cursor = "pointer";
	subMenuItemSelected.obj.style.backgroundColor = subActivatedBackgroundColor;
	subMenuItemSelected.obj.style.border = subActivatedBorderColor;
	subMenuItemSelected.obj.style.color = subActivatedFontColor;
}

function deactivateSubMenu(iNewSubMenuID) {
	//put submenu item back to normal state
	var subMenuItemSelected = new getObj(iNewSubMenuID);
	subMenuItemSelected.obj.style.backgroundColor = subDeactivatedBackgroundColor;
	subMenuItemSelected.obj.style.border = subDeactivatedBorderColor;
	subMenuItemSelected.obj.style.color = subDeactivatedFontColor;
	
}

function activateSideBar(currentRow) {
	//side bar for looks - change color on mouseover of current row

	var sideBarSelected = new getObj(currentRow+'SideBar');
	sideBarSelected.obj.style.backgroundColor = mainActivatedSideBarColor;
	
}

function deactivateSideBar(sidebarID, deactivateType) {
	//side bar for looks - put back on mouseout
	
	if (deactivateType == "newRowActivated") {
		var sideBarSelected = new getObj(previousRowMousedOver+'SideBar');
		sideBarSelected.obj.style.backgroundColor = mainDeactivatedSideBarColor;
	}
	  
	if (deactivateType == undefined) {
		var sideBarSelected = new getObj(currentRowMousedOver+'SideBar');
		sideBarSelected.obj.style.backgroundColor = mainDeactivatedSideBarColor;
		deactivateMainMenu(currentRowMousedOver);
	}  
	
}
