﻿// javascript for breeding bird atlas

// initialize the dropdown menu
startList = function() {
    if (document.all&&document.getElementById) {
        navRoot = document.getElementById("menuList");
        for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName=="LI") {
                node.onmouseover=function() {
                    this.className+=" over";
                }
                node.onmouseout=function() {
                    this.className=this.className.replace(" over", "");
                }
            }
        }
    }
}

//bird detail page - left nav list.

var uptime,downtime;

function initialize_birdlist(){    
    removeElementClass( "uparrowimg","invisible");
    removeElementClass( "downarrowimg","invisible");
    pos = getCurrentBirdPosition();
    var El=getElement("birdlist");
    if ( pos > El.childNodes.length - ( 6 * rangeMultiplier()) ) {
        removeElementClass( El.childNodes[pos], "currentbirdpos");
        pos = El.childNodes.length - ( 6 * rangeMultiplier());
        addElementClass( El.childNodes[pos], "currentbirdpos");
    }
    if ( pos <= 11 ) {
       removeElementClass( El.childNodes[pos], "currentbirdpos");
       if ( navigator.appName == 'Microsoft Internet Explorer') {
            pos = 6;
       } else {
            pos = 11;
       }
       addElementClass( El.childNodes[pos], "currentbirdpos");
    }
    makeVisible( pos );
    connect("uparrowimg","onmousedown", up0);
    connect("uparrowimg","onmouseup", up1);
    connect("uparrowimg","onmouseout", up1);
    connect("downarrowimg","onmousedown", down0);
    connect("downarrowimg","onmouseup", down1);
    connect("downarrowimg","onmouseout", down1);
}

function getCurrentBirdPosition() {
    var El=getElement("birdlist");
    var element = getElementsByTagAndClassName("LI", "currentbirdpos");
    var id = element[0].id;
    var pos = 0;
    for(var i = 0; i < El.childNodes.length; i++ ){
        if (El.childNodes[i].id==id){
            pos = i;
            break;
        }
    }
    return pos;
}

function makeVisible( currentPosition ){    
    var El=getElement("birdlist");
    var range = 5 * rangeMultiplier();

    for( var i=0; i < El.childNodes.length; i++ ) {            
        var currentNode = El.childNodes[i];
        if (currentNode.nodeName == "LI") {
            if ( i >= currentPosition - range && i <= currentPosition + range ) {
                removeElementClass(currentNode, "invisible");
            } else {         
                addElementClass(currentNode, "invisible");
            }        
        }
    }
}

//figure out childnode multiplier based on browser because IE handles child nodes differently
function rangeMultiplier(){
    var multiplier = 1;
    if (navigator.appName != 'Microsoft Internet Explorer') {
        multiplier = 2;
    }
    return multiplier;
}

function up0(){  
    var pos = getCurrentBirdPosition();
    if ( pos < 5 * (rangeMultiplier()) ) { 
        return;
    }
    var element = getElement("birdlist");
    removeElementClass(element.childNodes[pos], "currentbirdpos");
    if ( pos >= 1* rangeMultiplier() ) { pos -= 1* rangeMultiplier() }
    addElementClass(element.childNodes[pos], "currentbirdpos");
    makeVisible( pos );
    uptime=setTimeout("up0()",100)
}

function up1(){       
    clearTimeout(uptime);
}

function down0(){
    var pos = getCurrentBirdPosition();
    var element = getElement("birdlist");
    removeElementClass(element.childNodes[pos], "currentbirdpos");
    if ( pos < element.childNodes.length - ( 6 * rangeMultiplier()) ) { pos += ( 1 * rangeMultiplier() ) }
    addElementClass(element.childNodes[pos], "currentbirdpos");
    makeVisible( pos );         
    downtime=setTimeout("down0()",100)
}

function down1(){    
    clearTimeout(downtime);
}
