var navNode;
var MENU_HEIGHT=250;
var SITE_WIDTH;
var topWidthsSum = [0]; // start with offset
var topItems = [];
function nav(id){
    var dNav = this;
    var cUrl = window.location.href;
    dNav.topNode = document.getElementById(id);
    dNav.prevActive;
    dNav.parent;
    dNav.all = dNav.topNode.getElementsByTagName('li');
    allList=dNav.all;
    dNav.topItems = [];
    for(k=0;k<dNav.all.length;k++){
        if(dNav.all[k].parentNode.parentNode == dNav.topNode){
         dNav.topItems[dNav.topItems.length] = dNav.all[k];
        }
        if(dNav.all[k].childNodes){
          for(l=0;l<dNav.all[k].childNodes.length;l++){
            if(dNav.all[k].childNodes[l].nodeName=="A" && dNav.all[k].childNodes[l].href==cUrl){
              dNav.all[k].className+=" active";
            }
          }
        }
    }

    dNav.cancelEvents=function(){
        dNav.topNode.onmouseover=dNav.topNode.onmouseout=function(e){return(false)}
    }

    dNav.swapper=function(to){
      
        to.className = (to.className && to.className.indexOf('over')+1?to.className:((to.className?to.className+ ' ':'')+'over'));
		if(to.className.indexOf('over')+1){       
   		var childMenu = to.getElementsByTagName("ul")[0];
			var endVal;
			if(String(childMenu)!='undefined'){
				if(!childMenu.depth){
					childMenu.depth=getNodeDepth(to, dNav.topNode,1)
				}
				if(childMenu.depth >=4){
//					var slideMenu = new Fx.Slide(childMenu, {mode: 'horizontal'});
						var slideMenu = new fx.Width(childMenu, {duration: 125, onComplete: function(){}});
					endVal=153;
				}else{
					if(!childMenu.origHeight){
						childMenu.origHeight=childMenu.offsetHeight;	
					}
					endVal=childMenu.origHeight;
//					var slideMenu = new Fx.Slide(childMenu);
					var slideMenu = new fx.Height(childMenu, {duration: 125, onComplete:function(){
							childMenu.style.overflow="visible";
					}});
					
				}
				slideMenu.hide();
			//	slideMenu.slideOut();
				slideMenu.custom(0,endVal);
			}
		}
    }

    dNav.handleEvent=function(e){
        var eT, to, from, current
        if(!e) e = event;
            eT = e.type;
        from = e[(window.Event)?((eT=='mouseover')?'relatedTarget':'target'):'fromElement'];
        to = e[(window.Event)?((eT!='mouseover')?'relatedTarget':'target'):'toElement'];
        while(to&&to.nodeName.match(/(A|#text)/)){to=to.parentNode;}
        if(eT=='mouseover' && to.nodeName=="LI"){
            gblTimeout=false;
            to.parent = to.parentNode.parentNode;
            //first close any siblings at the same level
            if(dNav.prevActive && dNav.prevActive.parentNode == to.parentNode && to!=dNav.prevActive){
				dNav.mnuClose(dNav.prevActive);
                dNav.prevActive = to;
            }
            //now check too see if any siblings had open child menus which stay open due to delay
            if(dNav.prevActive && dNav.prevActive.parent && dNav.prevActive.parent.parentNode == to.parentNode){
                dNav.mnuClose(dNav.prevActive.parent);
                dNav.mnuClose(dNav.prevActive);
            }
            //check all top level nav items
            if(to.parentNode.parentNode && to.parentNode.parentNode==dNav.topNode){
                dNav.cTops(to);
            }
         if(!to.active){
                //if an LI and not active, set to active.
                dNav.swapper(to);
                to.active=true;
				setTop(to.parentNode);
				setWidth(to.parentNode);
                dNav.prevActive=to;
                dNav.prevActive.parent = to.parentNode.parentNode;
          }
        }
        if(eT=='mouseout'){
            if(!dNav.containsNode(dNav.topNode,to)){
                gblTimeout=true;
                dNav.timer = setTimeout("menuReset();",800);
            }
            if(to && to.nodeName=="LI"){
              if(dNav.prevActive && !dNav.containsNode(dNav.prevActive.parent,to) && dNav.prevActive.parent != to.parent){
                  dNav.mnuClose(dNav.prevActive.parent);
               }
            }
        }
    }

    dNav.containsByClass=function(containee,cName){
        while(dNav.topNode!=containee){
            if(containee.className==cName){
                return(true);
                break;
            }
            containee=containee.parentNode;
        }
    }

    dNav.cTops=function(to){
        for(i=0;i<dNav.all.length;i++){
            if(dNav.all[i].active && (!dNav.containsNode(dNav.all[i],to))){
             dNav.mnuClose(dNav.all[i]);
            }
        }
    }

    dNav.getIHTML=function(n){
        return(String(n.getElementsByTagName("a")[0].innerHTML));
    }

    dNav.menuReset=function(){
        //loops and resets all menu item list elements to inactive
        for(i=0;i<dNav.all.length;i++){
            if(dNav.all[i].active){
               dNav.all[i].active=false;
               dNav.all[i].className=dNav.all[i].className.replace(' over','');
               dNav.all[i].className=dNav.all[i].className.replace('over','');
            }
        }
    }

    dNav.mnuClose=function(n){
        //closes a menu and it's children.
       if(n){
          while(n.active){
              n.className=n.className.replace(' over','');
              n.className=n.className.replace('over','');
              n.active=false;
          }
        }
      }
    dNav.containsNode=function(a,b){
        while(b&&(a!=b)&&(b!=null))
            b=b.parentNode;
        return(a==b);
    }

	function setTop(n){
		//set the top of any 3rd level flyouts to that of it's parent
		var nodeHeight = 18; //default
		n.depth=getNodeDepth(n,navNode,1);
		if(getNodeDepth(n,navNode,1) == 3 && !n.heightsSet){
			//2nd level
			var subs=n.getElementsByTagName("ul");
			if(subs.length!=0){
				for(i=0;i<subs.length;i++){
					//get number of items in the submenu to calculate height
					var items = subs[i].getElementsByTagName("li");
					var subHeight = 0;
					for(j=0;j<items.length;j++){
						nodeHeight=items[j].offsetHeight;
						subHeight+=nodeHeight;
					}
					subs[i].style.top=(subs[i].parentNode.offsetTop==1)?"0px":(subs[i].parentNode.offsetTop)+"px";
					if(subHeight + subs[i].parentNode.offsetTop >= MENU_HEIGHT){
						//calculated height is greater than max menu height
						var diff = (subHeight + subs[i].parentNode.offsetTop)-MENU_HEIGHT;
						while(diff%nodeHeight!=0){
							diff--;
						}
						subs[i].style.top=(subs[i].parentNode.offsetTop)-diff+"px";
						subs[i].style.top=(parseInt(subs[i].style.top)<0)?"0px":subs[i].style.top;
					}
				}
				n.heightsSet=true;
			}
		}
	}

	function getNodeDepth(node, parent,step){
		//calculates a node's depth, stepping up #step nodes each time
		//useful for calculating how deep a UL is relative to parent UL while skipping LI tags
		var depth=0;
		while(node!=parent){
			depth+=1;
			for(j=0;j<step;j++){
				node=node.parentNode;
				
			}
		}
		return(depth)
	}

	function setWidth(n){
		var maxWidth=0; var currentWidth=20;
		var uList = n.getElementsByTagName("ul"); //get child ULs
		for(i=0;i<uList.length;i++){
			if(!uList[i].widthSet){
				//all child ULs
				var xList=uList[i];
				if(getNodeDepth(xList,navNode,1) == 5){
					var pL = getParentObject(xList);
					var xOffset = pL.parentNode.offsetLeft;
					if(parseInt(xOffset)+300 > SITE_WIDTH){
						xList.className="right-align";
						xList.style.left="-155px";
					}

				}
				uList[i].widthSet=true;
			}
		}
	}
	function getParentObject(n){
		return(n.parentNode.parentNode);
	}

    function setup(){
        navNode = dNav.topNode;
        navNode.onmouseover=navNode.onmouseout=function(e){if(typeof window.event != 'undefined') var e = window.event; dNav.handleEvent(e)};
        var items = navNode.getElementsByTagName('ul')[0].childNodes;
        SITE_WIDTH = parseInt(navNode.offsetWidth);
        var topWidths = [];
        var i=0, j=0, k=0;
        var tmp = 0;
        
        for (i=0; i<items.length; i++) {
            if (items[i].nodeName.toLowerCase()=='li') {
		if(items[i].nextSibling == null || (items[i].nextSibling.nodeName=="#text" && items[i].nextSibling.nextSibling == null)){
			items[i].className='last';
		}
		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;
            }
       }
    }
    setup();
}

var allList=[];
var gblTimeout=false;

function menuReset(){
	//loops and resets all menu item list elements to inactive
	if(gblTimeout){
		for(i=0;i<allList.length;i++){
			if(allList[i].active){
				allList[i].active=false;
				allList[i].className=allList[i].className.replace(' over','');
				allList[i].className=allList[i].className.replace('over','');
			}
		}
	}
}
loadEvts.push("var oNav = new nav('navigation');");


