//Script f�r att f�lla ut/in grupperade listor

var groupedExpander = {
    no_openclose : false,
    show_all_text : "Expandera lista",
    hide_all_text : "F&auml;ll in lista",
    openClose : function(){
        if(groupedExpander.no_openclose){
            groupedExpander.no_openclose = false;
            return true;
        }
        var closed = false;
        if(this.className.indexOf("closed") >= 0){
             closed = true;
        }
        
        if(closed == true){
            $(this).removeClass("closed");
            $(this).addClass("traffkort");
            $("#content_"+this.id)[0].style.display = '';
        }else{
            $(this).addClass("traffkort closed");
            $("#content_"+this.id)[0].style.display = 'none';
        }
        linkToolOpener.repositionLinkdiv();
        return false;
    },

    openAll : function(){
    
        if (!document.getElementById || !document.createElement || !document.appendChild){return false;}

        $("a.edit_lib_prefs").bind("click",function(){ groupedExpander.no_openclose = true; });

        var hideable = document.getElementById("groupedTitle0");
        var ind = 0;

        var link = document.getElementById("showAllLibsLink");
        var name = link.getAttribute("name");
        var showall = false;
        if(name.indexOf("showall") != -1){
            showall = true;
            link.innerHTML = groupedExpander.hide_all_text;
            link.setAttribute("name","shownone");
        }else if(name.indexOf("shownone") != -1){
            link.innerHTML = groupedExpander.show_all_text;
            link.setAttribute("name","showall");
            showall = false;
        }
        var closed;
        while(hideable){
            var classes = hideable.className;
            var closed = false;
            if(classes.indexOf("closed") >= 0){
                closed = true;
            }
            if(showall){
                if(closed){
                    $(hideable).removeClass("closed");
                    $("#content_"+hideable.id)[0].style.display = '';
                }
            }else{
                if(!closed){
                    $(hideable).addClass("traffkort closed");
                    //var content = document.getElementById("content_"+hideable.id);
                    $("#content_"+hideable.id)[0].style.display = 'none';
                }
            }
            ind++;
            hideable = document.getElementById("groupedTitle"+ind);
        }  
        getitMap.closeMap();
        linkToolOpener.repositionLinkdiv();
        return false;
    },

    init : function() {
        if (!document.getElementById || !document.createElement || !document.appendChild){return false;}
        
        var editLib = document.getElementById("edit_lib_prefs");
        if(editLib){
            addEvent(editLib, 'click', function(){ groupedExpander.no_openclose = true; });
        }
        
        var show_all = false;
        if(trim(getParamValue("show_all_regions"))=="true"){
            show_all=true;
        }
        var hideable = $(".groupedTitle");
        $(".groupedTitle a.colexp").attr("href","#");
        var ind = 0;
        for(ind=0; ind<hideable.length; ind++){
            if(hideable[ind].className.indexOf("plusminus") != -1){
                continue;
            }
            hideable[ind].onclick = groupedExpander.openClose;
            
            /*
             *div med id = "groupedTitle0" �r utf�lld som default, 
             *vill man inte ha den f�rsta gruppen utf�lld s� b�rja r�kna id p� ex.vis 1
             */
            //if(hideable[ind].id.replace("groupedTitle","") != '0' && !show_all){
            //alert("show_all: " + show_all + " hideable[ind].className.indexOf('utredRef'): " + hideable[ind].className.indexOf("utredRef") + " ind: " + ind);
            if(!show_all && (hideable[ind].className.indexOf("utred") == -1 || ind != 0)){
            //if(!show_all){
                $(hideable[ind]).addClass("traffkort closed");
                $("#content_"+hideable[ind].id)[0].style.display = 'none';

            }
            
            addEvent(hideable[ind],'mouseover',function(){
                this.style.cursor='pointer';
            });
        }
        
        var p = document.getElementById("showAllLibs");
        if(p){
            var a = document.createElement("a");
            a.setAttribute("name","showall");
            if(show_all){
                a.setAttribute("name","shownone");
            }
            a.setAttribute("id","showAllLibsLink");
            a.setAttribute("href","#");
            a.className="prick";
            var linktext = document.createTextNode(groupedExpander.show_all_text);
            if(show_all){
                linktext = document.createTextNode(groupedExpander.hide_all_text);
            }
            a.appendChild(linktext);
            p.appendChild(a);
            a.onclick = function(){ groupedExpander.openAll(); return false;};
        }
    }
};

addEvent(window, 'load', function(){groupedExpander.init();});

