
var tmp_arr = new Array();
var found_link = false;
var a_location;

var base_href = "";

function main(){

    var nav = $('nav');
    
    if( document.location.href.indexOf("?") > 1){
        base_href = document.location.href.substring(0, document.location.href.indexOf("?"));
    }
    else{
        base_href = document.location.href;
    }

    find_list_path( nav);
    show_default_nav();
}

function hide_default_nav(){
    
    for( var i=0; i<tmp_arr.length; i++){
        if( tmp_arr[i].getElementsByTagName("ul")[0] != null){
            tmp_arr[i].getElementsByTagName("ul")[0].style.visibility = '';
        }
        if( tmp_arr[i].getElementsByTagName("a")[0] != null){
            tmp_arr[i].getElementsByTagName("a")[0].style.color = '';
        }
    }
}

function show_default_nav(){
    
    for( var i=0; i<tmp_arr.length; i++){
        if( tmp_arr[i].getElementsByTagName("ul")[0] != null){
            tmp_arr[i].getElementsByTagName("ul")[0].style.visibility = 'visible';
        }
        if( tmp_arr[i].getElementsByTagName("a")[0] != null){
            tmp_arr[i].getElementsByTagName("a")[0].style.color = '#eb9a1a';
        }
    }
}


function find_list_path( elem){

    a_location = base_href.split("/");
    
    for( var j=0; j<a_location.length && !found_link; j++){
    
        var new_start = false;
        
        for( var k=0; k<elem.childNodes.length && !new_start; k++ ){
            
            var i = 0;
            var tmp = get_children(elem)[k];
            
            while( i<get_children(tmp).length && !found_link && !new_start){
            
                var kids = get_children(tmp);
            
                if( kids[i].tagName == 'LI' && get_children(kids[i])[0].tagName == 'A'){
                
                    a_href = get_children(kids[i])[0].href.split('/');
                    
                    if( a_location[j+3] != null && a_location[j+3] == a_href[j+3] ){
                    
                        tmp_arr.push( kids[i]);
                        
                        if( j+3 == a_location.length-1 ){
                            found_link = true;
                        }
                        else{
                            if( get_children(kids[i]).length > 1 && get_children(kids[i])[1].tagName == 'UL' ){
                                // set the elem variable, and reset everything
                                elem = kids[i];
                                new_start = true
                            }
                        }
                    }
                }
                i++
            }
        }
    }
}

function get_children( elem){

    var tmp = new Array();
    
    if( elem != null ){
        for( var i=0; i<elem.childNodes.length; i++){
            if( elem.childNodes[i].tagName != null){
                tmp.push( elem.childNodes[i]);
            }
        }
    }
    return tmp;
}

function $( v ){
    return document.getElementById( v );
}