/************************************************************************************************************
(C) www.dhtmlgoodies.com, October 2005

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/	


var activeMenuItem = new Array();

function isUlInArray(inputObj,ulObj){
    while(inputObj && !dojo.hasClass(inputObj,'menu')){
        if(inputObj==ulObj)return true;
        inputObj = inputObj.parentNode;
    }		
    return false;
}

function showHideSub(e,inputObj)
{
    //if(!e)return false;
    if(!e)e=inputObj;
    if(!inputObj)inputObj=this;
    var parentObj = inputObj.parentNode;
    var ul = parentObj.getElementsByTagName('UL')[0];
    if(activeMenuItem.length>0){
        for(var no=0;no<activeMenuItem.length;no++){
            if(!isUlInArray(ul,activeMenuItem[0]) && !isUlInArray(activeMenuItem[0],ul)){
                activeMenuItem[no].style.display='none';
                dojo.removeClass(activeMenuItem[no].parentNode,'subOpened');
                activeMenuItem.splice(no,1);
                no--;
            }
        }			
    }
    if(ul.offsetHeight == 0){
        ul.style.display='block';
        dojo.addClass(parentObj,'subOpened');
        activeMenuItem.push(ul);
    }else{
        ul.style.display='none';
        dojo.removeClass(parentObj,'subOpened');
    }
    return false;
}

function showHidePath(inputObj)
{
    var startTag = inputObj;
    showHideSub(false,inputObj);
    inputObj = inputObj.parentNode;
    while(inputObj){			
        inputObj = inputObj.parentNode;
        if(inputObj.tagName=='LI')showHideSub(false,inputObj.getElementsByTagName('A')[0]);
        if(dojo.hasClass(inputObj,'menu'))inputObj=false;	
    }		
}

function initMenuTree( el )
{
    if (el) {
        var obj = [ dojo.byId(el) ];
    } else {
        var obj = dojo.query('.menuTree ul:first-child');
    }
    var linkCounter=0;
    for(var o=0;o<obj.length;o++){
        var aTags = dojo.query('A',obj[o]);
        var activeMenuItem = false;
        var activeMenuLink = false;
        var thisLocationArray = location.href.split(/\//);
        var fileNameThis = thisLocationArray[thisLocationArray.length-1];
        if(fileNameThis.indexOf('?')>0)fileNameThis = fileNameThis.substr(0,fileNameThis.indexOf('?'));
        if(fileNameThis.indexOf('#')>0)fileNameThis = fileNameThis.substr(0,fileNameThis.indexOf('#'));

        for(var no=0;no<aTags.length;no++){
            var parent = aTags[no].parentNode;
            var subs = parent.getElementsByTagName('UL');
            if(subs.length>0){
                if (aTags[no].onclick){
                    if (aTags[no].onclick.toString().indexOf('showHideSub(')<0){
                        aTags[no].onclick = new Function( "showHideSub(null, this);" 
                            + aTags[no].onclick.toString().replace(/.*function.*\(.*\).*\n?.*\{.*\n?(.*)\n?.*\}.*/, "$1")
                        );
                    }
                }
                else
                    aTags[no].onclick = showHideSub;
                linkCounter++;
                aTags[no].id = 'aLink' + linkCounter;
            }	
            
            if(aTags[no].href.indexOf(fileNameThis)>=0 && aTags[no].href.charAt(aTags[no].href.length-1)!='#'){				
                if(aTags[no].parentNode.parentNode){								
                    var parentObj = aTags[no].parentNode.parentNode.parentNode;
                    var a = parentObj.getElementsByTagName('A')[0];
                    if(a.id && !activeMenuLink){
                        
                        //activeMenuLink = aTags[no];
                        //activeMenuItem = a.id;
                    }
                }
            }		
        }		
    }
    if(activeMenuLink){
        dojo.addClass(activeMenuLink, 'activeMenuLink');
    }
    if(activeMenuItem){
        if(document.getElementById(activeMenuItem))showHidePath(document.getElementById(activeMenuItem));	
    }
}

function menuTreeSub ( el ) {
    var link = el.getAttribute('href');
    el.onclick = null;
    var a = dojo.query('A',el)[0];
    a.onclick = showHideSub;
    
    var parentObj = el.parentNode;
    var ul = parentObj.getElementsByTagName('UL')[0];
    /*
    if(activeMenuItem.length>0){
        for(var no=0;no<activeMenuItem.length;no++){
            if(!isUlInArray(ul,activeMenuItem[0]) && !isUlInArray(activeMenuItem[0],ul)){
                activeMenuItem[no].style.display='none';
                activeMenuItem.splice(no,1);
                no--;
            }
        }			
    }
    */
    if (el.length>0){
        showHideSub(el);
    } else if (link!=null){
        var e2 = document.createElement("ul");
        e2.style.display = "block";
        e2.innerHTML = flexyVars.msgLoading;
        
        dojo.xhr( 'GET', {
            url: '/inner'+link,
            handleAs: "text",
            timeout: 50000,
     
            // The LOAD function will be called on a successful response.
            load: function(response, ioArgs) {
                e2.innerHTML = response;
                initMenuTree( e2 );
                return response;
            },
     
            // The ERROR function will be called in an error case.
            error: function(response, ioArgs) {
              return response;
            }
        });
        
        el.insertBefore( e2, null );
        dojo.addClass(el,'subOpened');
        activeMenuItem.push(e2);
    }
}

//window.onload = initMenuTree;
dojo.addOnLoad(function(){
    initMenuTree();
});

