// JavaScript Document

	/* V 1.3 (A1webguy.com)
	*	Initially all dropdown menues are hidden by a negative 
	*		z-index. Once displayed and then hidden they get a
	*		css display value of "none". Next time they are
	*		called to be displayed the css value will be set to
	*		display="". So it toggles between "none" and "".
	*		The reason I used an initial z-index was that I could
	*		not find an easy way to get the width of the tbody/table
	*		(for the adjustment, below) if it was set to display="none".
	*	The dropdown link table is centered under the text in the main
	*		navigation bar. To do this the x position of the start
	*		of the text in the main navigatin bar is found 
	*		each time the dropdown displays and the dropdown table 
	*		is then adjusted to 1.The with of the
	*		text in the main top bar and 2.the with of the dropdown
	*		link/table. The pixel adjustment
	*		is calculated the first time the dropdown gets visible
	*		and is then stored in the menu.arr. like this:
	*		menu.arr[object][pxAdjustment], object is <tbody>
	*	Functionality could be added to prevent the menues beeing
	*		left displayed if this script misses a mouse out event
	*		(doesn't happen unless you try to break the script). 
	*		The cursor's x/y position could be tracked and this way
	*		catch if cursor leaves the dropdown area. Also the x/y
	*		position of the dropdown table could be stored in the
	*		menu.arr and be changed on for example on window resize.
	*/

var que = new Object();
que.name = "";

var menu = new Object();
menu.x = 0;
menu.y = 0;
menu.shouldShow = false;
menu.arr = new Array(); // Holds all once opened menus

var navOver = new Image;
navOver.src = 'http://www.measurablesolutions.com/images/templates/pcm/bgBtnOver.gif';

// **** Start Main linkbar functions ******
function over(obj, id){
	menu.x = 0;
	menu.y = 0;
   obj.style.backgroundImage = "url("+navOver.src+")";
   if (id) dropDownmenu(obj, id, true); // if there is a drop down menue
}
function out(obj, id){
   obj.style.backgroundImage = "url('http://www.measurablesolutions.com/images/templates/pcm/bgBtn.gif')";
   if (id) dropDownmenu(obj, id); // if there is a drop down menue
}
	// **** End Main linkbar functions ******



	// **** Start Drop down functions ******

	// This function places the menu in the que to later be dispaled by
	//	dropDown() (after the timeout)
	// obj = the link element in the top navigation bar
	// name = the id of the corresponding tbody element
	// b = true if dropdown should display false if it should be hidden.
function dropDownmenu(obj, name, b){
  if (b){ // if dropdown should display
	que.name = name;
    menu.shouldShow = true;
	if (que.name != ""){
		// Here the position of the dropdown is set before display.
		// subLinkTable is the dropdown table.
	  var subLinkTable = document.getElementById(name).parentNode;
	  addToArr(que.name,adjustXToLinkWidth(obj,subLinkTable));
	  menu.x += findPosX(obj)+getXLinkWidthAdj(name);
	  menu.y += findPosY(obj)+33;
	}
    var d = setTimeout("dropDown()",200);// "dropdown" timeout should be longer then "hide" timeout
  }
  else { // if dropdown should be set to not show
    menu.shouldShow = false;
    var h = setTimeout("hide()",150);// "hide" timeout should be shorter then "dropdown" timeout
  }
}

	// This is called after a timeout (before dispalying the menu).
	//	and this is the function that renders the menu displayed/visible.
function dropDown(){
    if (que.name != "" && menu.x > 0){
	  var pNode = document.getElementById(que.name).parentNode;
	  pNode.style.left = menu.x + "px";
	  pNode.style.top = menu.y + "px";
	  pullUpAll();
      document.getElementById(que.name).style.display = "";
	  setIndicator(que.name,true);
	  pNode.style.zIndex = "100";
	}
}

function hide(){
  if (!menu.shouldShow && que.name != ""){
	setIndicator(que.name,false);
	document.getElementById(que.name).style.display = "none";
    que.name = "";
  }
}

function overDrop(b){
  menu.shouldShow = b;
  if (!b) dropDownmenu(que.name, b)
}

function setIndicator(name,show){
  if (show){
    var indicator = document.getElementById(name+"Indicator");
    indicator.style.fontSize = "large";
//    indicator.style.fontWeight = "bold";
    indicator.style.fontColor = "#1A4178";
    indicator.innerHTML = "&darr;";
  }
  else {
    var indicator = document.getElementById(name+"Indicator");
    indicator.style.fontSize = "small";
    indicator.innerHTML = "&#43;";
  }
}

function pullUpAll(){
  for (var i = 0; i < menu.arr.length; i++){
	menu.arr[i][0].style.display = "none";
	menu.arr[i][0].parentNode.className = "dropdown";
  }
}
	// **** End Drop Down functions ******//


function addToArr(name,widthAdjustment){
  if (!isAddedToArray(name)){
	  menu.arr.push(new Array(document.getElementById(name),widthAdjustment));
  }
}

function isAddedToArray(name){
  var isAdded = false;
  for (var i = 0; i < menu.arr.length; i++){
	if (menu.arr[i][0].id == name){
	  isAdded = true;
	  break;
	}
  }
  return isAdded;
}

function getXLinkWidthAdj(name){
  for (var i = 0; i < menu.arr.length; i++){
	if (menu.arr[i][0].id == name) return menu.arr[i][1];
  }
}

function adjustXToLinkWidth(obj,objSub) {
  var mainNavTxtCenter = Math.floor(obj.offsetWidth/2); // Main nav text center in relatinship to it's x pos.
  return mainNavTxtCenter - (Math.floor(objSub.offsetWidth/2));
}

function findPosX(obj){
var curleft = 0;
if(obj.offsetParent)
  while(obj.offsetParent) {
	curleft += obj.offsetLeft;
	if(!obj.offsetParent) break;
    obj = obj.offsetParent;
  }
  else if(obj.x)
  curleft = obj.x;
  return curleft;
}

function findPosY(obj){
  var curtop = 0;
  if(obj.offsetParent)
	while(obj.offsetParent){
	  curtop += obj.offsetTop;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
  }
  else if(obj.y)
	curtop = obj.y;
  return curtop;
}
