// Navigation-specific script

var ua = navigator.userAgent.toLowerCase();
var isMac = navigator.appVersion.toLowerCase().indexOf('mac')+1;
var isNN6 = ua.indexOf('netscape6')+1;
var isOpera = ua.indexOf('opera')+1;
var isSafari = ua.indexOf('safari')+1;
var isIE = (document.all && !isOpera);

function TopNav() {
  var self = this;
  // Identify browsers not supporting :hover on UL etc., add scripted behavior and basic event bubble handling (ie:mac, Netscape 6)
  this.navItems = [];
  this.topNav = null;
  
  this.isTopNavElement = function(o) {
    return (o && o.className && o.className.indexOf('top')+1);
  }
  
  this.isNavElement = function(o) {
    if (!self.topNav) self.topNav = document.getElementById('nav-top');
    if ((isIE || isOpera) && o.className && o.className.indexOf('top')+1) return false;
    do {
      o = (o.parentNode||'null');
      writeDebug('isNavElement() loop: '+o.nodeName||'null');
    } while (o != 'null' && o != self.topNav);
    writeDebug('isNavElement(): returning '+(o==self.topNav)+'');
    return (o == self.topNav);
  }
  
  this.itemOver = function(e) {
    o = (isOpera?e.target:event.srcElement);
    if (o && self.isTopNavElement(o)) {
      this.className = (this.className && this.className.indexOf('hover')+1?this.className:((this.className?'hover '+this.className:'hover')));
      writeDebug('className(over): "'+this.className+'"');
    }
  }
  
  this.itemOut = function(e) {
    o = (isOpera?e.relatedTarget:event.toElement);
    if (o && !self.isNavElement(o)) {
      writeDebug('liOut(): '+o.nodeName);
      this.className = this.className.replace(/hover /g,'');
      writeDebug('className(out): "'+this.className+'"');
    }
  }
  
  this.findNode = function(n) {
    // find a non-text node
    if (!n || !n.nodeType) return null;
    while (n.nodeType==3 && n.parentNode) {
      n = n.parentNode;
    }
    return n;
  }

  this.isChildOfIndex = function(targetIndex, target) {
    if (!target) return false;
    do {
      if (self.navItems[targetIndex] == target) {
        return true;
      } else if (target.parentNode) {
        target = target.parentNode;
      }
    } while (target.parentNode && target.parentNode.nodeName != 'BODY');
    return false;
  }
 
  this.itemOverW3C = function(e) {
    o = self.findNode(e.target);
    if (!e) return false;
    writeDebug('itemOverW3C() - '+e.nodeName+','+e.nodeType);
    if (!self.isNavElement(o)) return false;
    writeDebug('showing');
    this.className = (this.className && this.className.indexOf('hover')+1?this.className:((this.className?'hover '+this.className:'hover')));
  }
  
  this.itemOutW3C = function(e) {
    writeDebug('itemOutW3C()');
    o = self.findNode(e.relatedTarget);
    if (!o) return false;
    if (typeof(o.nodeName)=='undefined') return false;
    if (self.isChildOfIndex(this.menuIndex,o)) return false;
    writeDebug('hiding item '+this.menuIndex);
    if (this.className.indexOf('hover')+1) this.className = this.className.replace(/hover /g,'');
  }
  
  this.init = function() {
    // add mouseover/out handlers to LI elements
    writeDebug('TopNav.init()');
    var items = document.getElementById('nav-top').getElementsByTagName('li');
    var firstA = null;
    for (var i=0; i<items.length; i++) {
      if (isNN6 || isOpera && items[i].className) {
        self.navItems[self.navItems.length] = items[i];
        items[i].menuIndex = self.navItems.length-1;
      }
      firstA = items[i].getElementsByTagName('a')[0];
      if (firstA.className.indexOf('hidden')==-1 && firstA.className.indexOf('top')+1) {
        items[i].onmouseover = !isNN6?self.itemOver:self.itemOverW3C;
        items[i].onmouseout = !isNN6&&!isOpera?self.itemOut:self.itemOutW3C;
      } else if (firstA.className.indexOf('hidden')+1) {
        // hidden item - remove text and assign default cursor
        firstA.style.visibility = 'visible';
        firstA.style.cursor = 'default';
      }
    }
  }

}

var topNav = null;

if (isNN6 || isOpera || isIE) {
  topNav = new TopNav();
  addOnloadHandler(topNav.init);
}

/* The following code has been added to handle the professional dropdown on the persistant nav */
var timer;
var timerFlag=false;

function findParent(o) {
  do {
    o = o.parentNode;
  } while (o.nodeType ==3 && o.parentNode);
  return o;
}

function setFlagToFalse(){
 timerFlag=false;
}

function setTimeOut(){
	timerFlag=true;
 	timer = setTimeout("closeMenu()", 2000);
}

function closeMenu(){
	if (timerFlag){
		document.getElementById('subItemsContainer').style.display = 'none';
  }
}

function to(obj) {
  var oMenuContainer = findParent(obj).getElementsByTagName('div')[0];
  var oDivP = findParent(obj);
  oMenuContainer.style.display = (oMenuContainer.style.display!='block'?'block':'none');    
}
