/**
 * @author Nick Drury
 *
 * Get the top level nav items, add an onmouse event to trigger a timer to call a show the subitem.
 * Ignores the current navigation branch which should be identified with the id attribute "navbranch"
 */
var droptimer = 0;
var dropel = null;
var origheights = Array();
var nowopen = null;
var aniduration = 0.4;
var animationcnt = 0;
var droptime = 250;
var ignorebranchel = null;

function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function(){
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

addLoadEvent(addnavevents);

function addnavevents(){
    var thenav = document.getElementById("nav");
    var navlist = thenav.getElementsByTagName("ul"); // The actual nav list
    // mark subnavigation branch containing the current page as uncollapsable
    var activebranch = document.getElementById("navbranch");
    if(activebranch) ignorebranchel = activebranch;
    
    // keep track of the subnav heights
    getsubnavheights(thenav);
    
    for (var i = 0; i < navlist.length; i++) {
        if (iscollapsable(navlist[i])) {
            navlist[i].parentNode.onmouseover = function(){
                if (0 == animationcnt) {
                    setdroptimer(this, "block");
                }
            }
            navlist[i].parentNode.onmouseout = function(){
                cleardroptimer(this, "none");
            }
            navlist[i].style.display = "none";
        }
    }
    resetnav(thenav);
    
    var contentarea = document.getElementById("content");
    if (contentarea) {
        contentarea.onmouseover = function(){
            var thenav = document.getElementById("nav");
            resetnav(thenav);
            nowopen = null;
        }
    }
};

function resetnavorig(thenav){
    var navlist = thenav.getElementsByTagName("ul");
    for (var i = 0; i < navlist.length; i++) {
        if (iscollapsable(navlist[i])) {
            navlist[i].style.display = "none";
			navlist[i].parentNode.className = "arrow";
        }
    }
};

function animatingon(){
    animationcnt++;
}

function animatingoff(){
    animationcnt--;
}

function resetnav(thenav){
    var thelist = null;
    if (nowopen) {
        var thelist = nowopen.getElementsByTagName("ul")[0];
        var myAnim = new YAHOO.util.Anim(thelist, {
            height: {
                to: 0
            }
        }, aniduration, YAHOO.util.Easing.easeOut);
        animatingon();
        myAnim.onComplete.subscribe(animatingoff);
        myAnim.animate();
    }
    var navlist = thenav.getElementsByTagName("ul");
    
    for (var i = 0; i < navlist.length; i++) {
        if (iscollapsable(navlist[i])) {
            navlist[i].style.display = "block";
            navlist[i].style.overflow = "hidden";
            if (nowopen) {
                if (navlist[i] != thelist) {
                    navlist[i].style.height = "0px";
                }
            }
            else {
                navlist[i].style.height = "0px";
            }
            navlist[i].parentNode.className = "arrow";
        }
    }
    
    
};


function getsubnavheights(thenav){
    var navlist = thenav.getElementsByTagName("ul");
    for (var i = 0; i < navlist.length; i++) {
        if (iscollapsable(navlist[i])) {
            origheights[i] = navlist[i].offsetHeight;
        }
    }
};


function changeulstyle(){
    var el = dropel;
    if (el == nowopen) 
        return false;
    
    // first let's go clear the other items.
    var thenav = document.getElementById("nav");
    var navlist = thenav.getElementsByTagName("ul"); // The actual nav list
    // reset the navigation
    resetnav(thenav);
    // and keep track of the currently opening element
    nowopen = el;
    // and now show the nav we want	
    el.className = "arrowdown";
    var thelist = el.getElementsByTagName("ul")[0];
    
    var toh = 80;
    // find the original height.
    for (var i = 0; i < navlist.length; i++) {
        if (iscollapsable(navlist[i])) {
            if (navlist[i] == thelist) {
                toh = origheights[i];
            }
        }
    }
    
    var myAnim = new YAHOO.util.Anim(thelist, {
        height: {
            to: toh
        }
    }, aniduration, YAHOO.util.Easing.easeOut);
    animatingon();
    myAnim.onComplete.subscribe(animatingoff);
    myAnim.animate();
    
}

function iscollapsable(el){
    var collapsable = el.parentNode.parentNode.nodeName == "UL" && el.parentNode.id != "activenavbranch" && el.parentNode.parentNode.parentNode.nodeName == "DIV" && !(ignorebranchel == el);
    return collapsable;
}

function setdroptimer(el, lstyle){
    if (nowopen != el) { // only start another timer if we need to
        droptimer = setTimeout("changeulstyle()", droptime);
        dropel = el;
    }
}

function cleardroptimer(el, lstyle){
    clearTimeout(droptimer);
    dropel = null;
}

