var openMenus = new Array();
var delayTime = 400 //ms;

addEvent = function(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el["on" + evname] = func;
	}
}

removeEvent = function(el, evname, func) {
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else {
		el["on" + evname] = null;
	}
}

function Browser() {
    var ua, s, i;
    
    this.isIE = false; // Internet Explorer
    this.isNS = false; // Netscape
    this.version = null;
    
    ua = navigator.userAgent;
    
    s = "MSIE";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }  
    s = "Netscape6/";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    
    // Treat any other "Gecko" browser as NS 6.1.
    
    s = "Gecko";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = 6.1;
        return;
    }
}
var ie4=document.all
var ns6=document.getElementById&&!document.all
var ns4=document.layers

var browser = new Browser();
var btd_browser = new Browser();

// Code for handling the menu bar and active button.
var activeButton = null;

function pageMousedown(event) {
    
    var el;
    // If there is no active button, exit.
    
    if (activeButton == null){
        return;
        }
    
    // Find the element that was clicked on.
    
    if (btd_browser.isIE){
        el = window.event.srcElement;
    }else{
        el = (event.target.tagName ? event.target : event.target.parentNode);
    }
    // If the active button was clicked on, exit.
    
    if (el == activeButton){
        return
        };
    
    // If the element is not part of a menu, reset and clear the active
    // button.
    
    if (getContainerWith(el, "UL", "SubMenu") == null) {
        resetButton(activeButton);
        activeButton = null;
    }
}

function buttonShowMenu(event, menuId) {
    var button;
    
    // Find the target button element.
    
    if (btd_browser.isIE){
        button = window.event.srcElement;
    }else{
        button = event.currentTarget;
    }
    clearHideMenu()
    
    // Associate the named menu to this button if not already done.
    // Additionally, initialize menu display.
    
    if (button.menu == null) {
        button.menu = document.getElementById(menuId);
    }
    
    // Reset the currently active button, if any.
    if (activeButton != null && button != activeButton) 
        resetButton(activeButton);

    // Activate this button, unless it was the currently active one.
    if (button != activeButton) {
        depressButton(button);
        activeButton = button;
    }
    return false;
}

function contains_ns6(a, b) {
    //Determines if 1 element in contained in another- by Brainjar.com
    while (b.parentNode)
        if ((b = b.parentNode) == a)
            return true;
    return false;
}

function hideMenu(menuId){
    var menu = ns6 ? document.getElementById(menuId) : ie4 ? document.all[menuId] : ns4 ? document.layers[menuId] : ""
    
    if (menu)
        activeButton = null;
        //menu.style.visibility=(ie4||ns6)? "hidden" : "hide"
        menu.style.display = "none"
}

function dynamicHide(e, menuId){
    
    var menu = ns6 ? document.getElementById(menuId) : ie4 ? document.all[menuId] : ns4 ? document.layers[menuId] : ""
    if (ie4&&!menu.contains(e.toElement)) 
        delayHideMenu(menuId)
    else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget)) {
        delayHideMenu(menuId);
    }
}

function delayHideMenu(menuId){
    if (ie4||ns6||ns4) {
        delayhide=setTimeout("hideMenu('" + menuId + "')",500)
    }
}

function clearHideMenu(){
    if (window.delayhide)
        clearTimeout(delayhide)
}

function depressButton(button) {
    var x, y;
    
    // Make the associated drop down menu visible
    if(button.menu != null) {
      vis = button.menu.style.visibility;
      dis = button.menu.style.display;
      //button.menu.style.visibility = (vis == "hidden" || vis == '') ? "visible" : "hidden";
      button.menu.style.display = (dis == "none" || dis == '') ? "block" : "none";
      
      var distx = 0;
      var disty = button.offsetHeight;
      var diffy = 2;
      var diffx = 14;
    
      button.menu.style.display = 'block';  
      var parentcontainer = button.menu.offsetParent;
    
      coords = findPos(button);
      distcoords = findPos(parentcontainer);
      var newposx = coords[0]-distcoords[0]+distx;
      var newposy = coords[1]-distcoords[1]+disty;
      button.menu.style.left = newposx+diffx+'px';
      button.menu.style.top = newposy+diffy+'px';
    }
    
}

function resetButton(button) {

    // Hide the button's menu, first closing any sub menus.
    
    if (button.menu != null) {
        closeSubMenu(button.menu);
        //button.menu.style.visibility = "hidden";
        button.menu.style.display = "none";
    }
}

function closeSubMenu(menu) {
    
    if (menu == null || menu.activeItem == null)
    return;
    
    // Recursively close any sub menus.
    
    if (menu.activeItem.subMenu != null) {
    closeSubMenu(menu.activeItem.subMenu);
    //menu.activeItem.subMenu.style.visibility = "hidden";
    menu.activeItem.subMenu.style.display = "none";
    menu.activeItem.subMenu = null;
    }
    menu.activeItem = null;
}

// General utility functions.

function getContainerWith(node, tagName, className) {
    
    // Starting with the given node, find the nearest containing element
    // with the specified tag name and style class.
    
    while (node != null) {
        if (node.tagName != null && node.tagName == tagName && hasClassName(node, className)){
            return node;
    }
    node = node.parentNode;
    }
    return node;
}

function hasClassName(el, name) {
    
    var i, list;
    
    // Return true if the given element currently has the given class
    // name.
    
    list = el.className.split(" ");
    for (i = 0; i < list.length; i++)
    if (list[i] == name){
        return true;
        }
    return false;
}

// Code to determine the browser and version.
function findPos(obj) {
  if (obj == null) {
    return [0,0];
  }
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}


function initDebugger() {
  var dbgBox = document.createElement('div');
  dbgBox.id = 'Debugger';
  dbgBox.style.position = 'absolute';
  dbgBox.style.left = '0px';
  dbgBox.style.top = '0px';
  dbgBox.style.width = '300px';
  dbgBox.style.background = 'orange';
  document.getElementsByTagName('body')[0].appendChild(dbgBox); 
  dbgBox.innerHTML = '';
  return dbgBox;
}

function debug(txt) {
  var dbgBox = document.getElementById('Debugger');
  if(!dbgBox) {
    dbgBox = initDebugger();
  }
  dbgBox.innerHTML += "\n<br>";
  dbgBox.innerHTML += txt;
}

function hideAllMenus() {
  if(openMenus) {
    for(var i=0; i<openMenus.length; i++) {
      hideMenu(openMenus[i]);
    }
  } 
}

function placeAllMenus() {
  var menuHolder = document.getElementById('subMenuHolder');
  if(menuHolder && menuHolder != undefined) {
    var mhChildren = menuHolder.childNodes;  
    if(mhChildren) {
      var bNode = document.getElementsByTagName('body')[0];
      for(var i=0; i<mhChildren.length; i++) {
        var nNode = mhChildren[i].cloneNode(true);
        bNode.appendChild(nNode);
      }
    }
    menuHolder.parentNode.removeChild(menuHolder);
  } 
}

function setActiveTab(holderID, tabElem) {
  // reset all Images
  var tabHolder = document.getElementById(holderID);
  if(tabHolder && tabHolder != undefined) {
    var tabs = tabHolder.childNodes;
    for(var i=0; i<tabs.length; i++) {
      var tab = tabs[i];
      if(tab.tagName == 'A') {
        var tImgObj = tab.firstChild;
        if(tImgObj.tagName == 'IMG') {
          var oldSrc = tImgObj.src;
          var ending = oldSrc.substr(oldSrc.length-3, oldSrc.length);
          var srcBase = oldSrc.substr(0, oldSrc.length-5);
          tImgObj.src = srcBase + '0.' +ending;
          tImgObj.onmouseover = function() { swapTabImg(this, '1') };
          tImgObj.onmouseout = function() { swapTabImg(this, '0') };
        }
      }
    }
  }
  var tabObj = tabElem;
  if(typeof(tabElem)== typeof('')) {
    tabObj = document.getElementById(tabElem);
  } 
  
  // set the selected tab active
  if(tabObj && tabObj != undefined) {
    var tabImgObj = tabObj.firstChild;
    if(tabImgObj.tagName == 'IMG') {
      var oldSrc = tabImgObj.src;
      var ending = oldSrc.substr(oldSrc.length-3, oldSrc.length);
      var srcBase = oldSrc.substr(0, oldSrc.length-5);
      tabImgObj.src = srcBase + '1.' +ending;
      tabImgObj.onmouseover = function() { };
      tabImgObj.onmouseout = function() { };
    }
  }
}

function swapTabImg(aTabImg, aState) {
  var oldSrc = aTabImg.src;
  var ending = oldSrc.substr(oldSrc.length-3, oldSrc.length);
  var srcBase = oldSrc.substr(0, oldSrc.length-5);
  aTabImg.src = srcBase + aState + '.' +ending;
}

function toggleExpandLink(aLink, aObjId) {
  var clOpen = 'linkExpandOpen';
  var clClosed = 'linkExpandClosed';
  var aObj = document.getElementById(aObjId);
  if(aObj) {
    var objState = aObj.style.display;
    if(objState=='block' ) {
      // close block
      aObj.style.display = 'none';
      aLink.className = clClosed;
    } else {
      // open block
      aObj.style.display = 'block';
      aLink.className = clOpen;
    }    
  } 
}

function openPopup(aURL, aWidth, aHeight, aName) {
  var winWidth = '573';
  var winHeight = '717';
  var popUpName = 'Popup';
  if(aHeight && aHeight != undefined) {
    winHeight = aHeight;
  }
  if(aWidth && aWidth != undefined) {
    winWidth = aWidth;
  }
  if(aName && aName != undefined) {
    popUpName = aName;
  }
  
  var posX = '100';
  var posY= '100';
  var popupWin = window.open(aURL, popUpName, 'width='+winWidth+',height='+winHeight+',left='+posX+',top='+posY+',scrollbars=yes,resizable=yes');
  popupWin.focus();
}

addEvent(document, 'mousedown', pageMousedown);
addEvent(document, 'keydown', pageMousedown);
addEvent(window, 'load', placeAllMenus);
