
// Loading the treemenu
$(document).ready(function(){
    
    var animateSpeed = 200; // The speed of the animation
    
    // Setting the standard css 
    //$("ul#producttree ul").css("height", "0px");
    //$("ul#producttree ul").css("overflow", "hidden");
    //$("ul#producttree ul ul").css("height", "auto");
    
    // Setting the icons
    $("ul#producttree ul a").each(function(){ $(this).html(" - " + $(this).html()); });
    $("ul#producttree ul ul a").each(function(){ 
        var val = "" + $(this).html() + "";
        $(this).html("<img src='Themes/NordicCool/images/arrow2.gif'> &nbsp; " + val.substr(3)); 
    });
    
    // Checking to see witch to bee displayed
    var pageUrl = "" + window.location + "";
    $("ul#producttree a").each(function(){
        var span = this.parentNode;
        if($(span).hasClass("sublevel")){
            var href = $(this).attr("href");
            if(pageUrl.indexOf(href) >= 0){
                var span = this.parentNode;
                var li = span.parentNode;
                var ul = li.parentNode;
                
                while($(ul).attr("id") != "producttree"){
                    $(ul).css("height", "auto").css("display", "block");
                    li = ul.parentNode;
                    ul = li.parentNode;
                }
                
                $(li).attr("id", "selected");
                $("#selected span:first").addClass("checked");
            }
        }
    });
    
    // Setting the event
    $("ul#producttree span.toplevel a").click(function(){
        
        // Collapsing all of the menuitems and setting all to no id
        $("ul#producttree li#selected ul:first").animate({"height" : "0px"}, animateSpeed, function(){ $(this).css("dispaly", "none") });
        $("ul#producttree li").attr("id", "");
        $("ul#producttree li span.checked").removeClass("checked");
        
        // Finding the representing li-tag and adding the id
        var span = this.parentNode;
        var li = span.parentNode;
        $(li).attr("id", "selected");
        
        // Checking to see if it has a child ul
        if($("ul#producttree li#selected ul")[0]){
            
            // Check to see if you should show it
            if($("ul#producttree li#selected ul:first").css("height") == "0px"){
                
                // Adding class
                $(span).addClass("checked");
                
                // Getting the height of the ul
                $("ul#producttree li#selected ul:first").css("height", "auto");
                var thisHeight = 0;
                $("ul#producttree li#selected ul").each(function(){
                    thisHeight += ($(this).height() * 1);
                });
                
                // Animating the ul
                $("ul#producttree li#selected ul:first").css("height", "0px");
                $("ul#producttree li#selected ul:first").css("display", "block");
                $("ul#producttree li#selected ul:first").animate({"height" : "+=" + thisHeight + "px"}, animateSpeed);
            }
            
            // Returning false becasue it has children
            return false;
            
        } else {
            
            // Returning true becase it has no submenus
            return true;
        }
    });
});
