/*
-----------------------------------------------------------------------------------
	=nav.js
	These functions power the main menu bar
-----------------------------------------------------------------------------------
*/

var docloaded = false;
if (!onloadHandlers) var onloadHandlers = [];

function loadEvents() {
  for(var i=0; i<onloadHandlers.length;i++) {
  eval(onloadHandlers[i]);
  }
docloaded = true;
}

window.onload = loadEvents;


//adds 'push' method to arrays for v5 browsers which do not support it (I'm looking your way MS!!)
if(typeof Array.prototype.push=='undefined')
  Array.prototype.push=function(){
    var i=0;
    b=this.length,a=arguments;
    for(i;i<a.length;i++)this[b+i]=a[i];
    return this.length
  }

function nav(id){
	var x = this;
    x.topNode = document.getElementById(id);
    x.lastActive;
    x.parent;
    x.delay;

	x.all = document.getElementById('nav').getElementsByTagName('li');
	
    allList=x.all;
    x.topItems = [];
    for(k=0;k<x.all.length;k++){
        if(x.all[k].parentNode.parentNode == x.topNode){
         x.topItems[x.topItems.length] = x.all[k];   
        }
    }
    x.Timer;

    
    x.catchEvent=function(e){
        var eT, t, f, current
        if(!e) e = event;
            eT = e.type;
        f = e[(window.Event)?((eT=='mouseover')?'relatedTarget':'target'):'fromElement'];
        t = e[(window.Event)?((eT!='mouseover')?'relatedTarget':'target'):'toElement'];
        while(t&&t.nodeName.match(/(A|#text)/)){t=t.parentNode;}
        //debug(t.nodeName);
        if(eT=='mouseover' && t.nodeName=="LI" && (!x.containsByClass(t,'specs-window'))){
            globalTimeoutFlag=false;
            t.parent = t.parentNode.parentNode;
            //first close any siblings at the same level
            if(x.lastActive && x.lastActive.parentNode == t.parentNode && t!=x.lastActive){
                x.mnuClose(x.lastActive);
                x.lastActive = t;
            }
            //now check too see if any siblings had open child menus which stay open due to delay
            if(x.lastActive && x.lastActive.parent && x.lastActive.parent.parentNode == t.parentNode){
                x.mnuClose(x.lastActive.parent);
                x.mnuClose(x.lastActive);
            }
            //check all top level nav items
            if(t.parentNode.parentNode && t.parentNode.parentNode==x.topNode){
                x.checkTops(t);
            }
         if(!t.active){
                //if an LI and not active, set to active.
                t.className = (t.className && t.className.indexOf('active')+1?t.className:((t.className?t.className+ ' ':'')+'active'));
                t.active=true;
                x.lastActive=t;
                x.lastActive.parent = t.parentNode.parentNode;
                //x.checkSiblings(t.parentNode.childNodes,t);
           }
           
        
        }
        if(eT=='mouseout'){
            if(!x.containsNode(x.topNode,t)){
                globalTimeoutFlag=true;
                x.timer = setTimeout("resetMenus();",1000);
            }
            if(t && t.nodeName=="LI"){
              if(x.lastActive && !x.containsNode(x.lastActive.parent,t) && x.lastActive.parent != t.parent){
                  x.mnuClose(x.lastActive.parent);
               }
            }
        }
    }
    
    x.containsByClass=function(containee,cName){
        while(x.topNode!=containee){
            if(containee.className==cName){
                return(true);
                break;
            }
            containee=containee.parentNode;
        }
    }

    x.checkTops=function(t){
        for(i=0;i<x.all.length;i++){
            if(x.all[i].active && (!x.containsNode(x.all[i],t))){
             x.mnuClose(x.all[i]);
            }
        }
    }
    
    x.getIHTML=function(n){
        return(String(n.getElementsByTagName("a")[0].innerHTML));   
    }
    
    x.resetMenus=function(){
        //loops and resets all menu item list elements to inactive
        for(i=0;i<x.all.length;i++){
            if(x.all[i].active){
                x.all[i].active=false;
               x.all[i].className=x.all[i].className.replace(' active','');
               x.all[i].className=x.all[i].className.replace('active','');
            }
        }
    }
    
    x.mnuClose=function(n){
        //closes a menu and it's children.
       if(n){
          while(n.active){
              n.className=n.className.replace(' active','');
              n.className=n.className.replace('active','');
              n.active=false;
          }
        } 
      }

      
      
    x.containsNode=function(a,b){
        while(b&&(a!=b)&&(b!=null))
            b=b.parentNode;
        return(a==b);
    }

    function init(){
		var navNode = document.getElementById('nav');
        navNode.onmouseover=navNode.onmouseout=function(e){ if(typeof window.event != 'undefined') var e = window.event; x.catchEvent(e)};
        var items = navNode.getElementsByTagName('ul')[0].childNodes;
        var SITE_WIDTH = parseInt(document.getElementById('envelope').offsetWidth); // Width (px) of the site - used for determining boundaries
        var topItems = [];
        var topWidths = [];
        var topWidthsSum = [0]; // start with offset
        var i=0, j=0, k=0;
        var flyouts = null;
        var tmp = 0;
        var offset = null;
        var av = navigator.appVersion.toLowerCase();
        var IS_IE50 = (av.indexOf('msie 5.0')>=0);
        var IS_WIN = (av.indexOf('windows')>=0);
        var IS_IE50W = (IS_IE50 && IS_WIN);
        var IE50W_OFFSET = 12; // 12-pixel offset for IE5/win32

        for (i=0; i<items.length; i++) {   
            if (items[i].nodeName.toLowerCase()=='li') {
                topItems[topItems.length] = items[i];
                topWidths[topWidths.length] = parseInt(items[i].offsetWidth); // get top LI items
                tmp += topWidths[topWidths.length-1];
                topWidthsSum[topWidthsSum.length] = tmp;
            }
        }

  for (i=0; i<topItems.length; i++) {
    // find any dropdowns / flyouts that need to be repositioned to the left
    tmp = topItems[i].childNodes;
    for (j=0; j<tmp.length; j++) {
      if (tmp[j].nodeName.toLowerCase()=='ul') { // drop-downs
        if (topWidthsSum[i]+tmp[j].offsetWidth >= SITE_WIDTH) { // drop-down requires shift to the left
          tmp[j].style.marginLeft = (SITE_WIDTH-topWidthsSum[i]-parseInt(tmp[j].offsetWidth)+(IS_IE50W?IE50W_OFFSET:0))+'px';
        }
        flyouts = tmp[j].getElementsByTagName('ul'); // flyouts
        if (flyouts.length) {
          for (k=0; k<flyouts.length; k++) {
            if (topWidthsSum[i]+tmp[j].offsetWidth+flyouts[k].offsetWidth >= SITE_WIDTH) { // flyout requires offset
              offset = true;
            } else if (IS_IE50W) { // 100% left margin doesn't work for flyouts - need absolute px value + IE5 offset
              flyouts[k].style.marginLeft = (parseInt(tmp[j].offsetWidth)-IE50W_OFFSET)+'px';
            }
          }
          if (offset) {
            tmp[j].className = (tmp[j].className?tmp[j].className+' ':'')+'flyout-left'; // assign class to parent UL (for left arrow)
            for (k=0; k<flyouts.length; k++) {
              flyouts[k].style.marginLeft = (parseInt(flyouts[k].offsetWidth)*-1)+1+'px';
            }
          }
          offset = false;
        }
      }
    }
  }
    }
    init();
}

var allList=[];
var globalTimeoutFlag=false;



    /*external function to use with the timer --needed for scoping issues */
    function resetMenus(){
        //loops and resets all menu item list elements to inactive
        if(globalTimeoutFlag){
        for(i=0;i<allList.length;i++){
            if(allList[i].active){
                allList[i].active=false;
                allList[i].className=allList[i].className.replace(' active','');
                allList[i].className=allList[i].className.replace('active','');

            }
        }
        }
    }

/* debugging */
function setCaretToEnd (control) {
  if (control.createTextRange) {
    var range = control.createTextRange();
    range.collapse(false);
    range.select();
  }
  else if (control.setSelectionRange) {
    control.focus();
    var length = control.value.length;
    control.setSelectionRange(length, length);
  }
}

function setCaretToStart (control) {
  if (control.createTextRange) {
    var range = control.createTextRange();
    range.collapse(true);
    range.select();
  }
  else if (control.setSelectionRange) {
    control.focus();
    control.setSelectionRange(0, 0);
  }
}
function debug(str){
    var debug = document.getElementById("debug");
    setCaretToEnd(debug);
    debug.value+=str +'\n';
    
}
