function hashchange_init(callback){
    var lhash = "";
    setInterval(function(){
        if(lhash != window.location.hash){
            lhash = window.location.hash;
            callback(window.location.hash.replace("#", "")+"-elem");
        }
    }, 50);
}

var current = false;
var openPanel = function(panel){
    if(current){
        current.shift({
            height: 35,
            background: "#bbbbbb",
            duration: 0.5
        });
        panel.open = false;
    }
    if(current != panel){
        panel.shift({
            height: panel.sheight,
            background: "#f5f5f5",
            duration: 0.5
        });
        panel.open = true;
        current = panel;
    }else{
        current = false;
    }
}
var countpages = {};
document.ready(function(){
    var timer = false;
    $$(".header-text td").invoke("unselectable");
    $$(".hidden").invoke("hide");
    
    $$(".accordion .panel").each(function(panel, i){
        panel.undoClipping().setStyle({
            height:"auto"
        });
        panel.sheight = panel.getHeight();
        panel.makeClipping();
        panel.setStyle({
            height:"35px"
        });
        
        panel.select(".title")[0].observe("click", function(){
            openPanel(panel)
        });
        
        countpages["total"] = $$(".hidden").each(function(e, i){
            countpages[e.id] = i;
        }).length;
        
        if(i==0){
            //openPanel(panel);
        }
    });
    
    hashchange_init(openfeature);
    if(!window.location.hash){
        openfeature("5-3-elem")
    }
    $("conts").cleanWhitespace();
    //openfeature("1-1");
})
var lastOpen = false;
function openfeature(id){
    //window.scrollTo(0, 0);
    if(!$(id)){ 
        return false; 
    }
    var catid = "cat-"+id.split("-")[0];
    var linkid = "link-"+id.split("-")[1];
    if(lastOpen){
        var olinkid = "link-"+lastOpen.id.split("-")[1];
        $(olinkid).removeClassName("feature-link-active");
        lastOpen.hide();
        lastOpen = false;
    }
    if(current != $(catid)){
        openPanel($(catid));
    }
    $(linkid).addClassName("feature-link-active");
    if($("fet-title-"+id.split("-")[1])){
        $("alltitle").update($("fet-title-"+id.split("-")[1]).innerHTML);
    }
    lastOpen = $(id);
    lastOpen.show();
    $("pagenum").update("<b>"+(countpages[lastOpen.id]+1)+"</b> / "+countpages.total);
}

function fnext(){
    if(!lastOpen){
        return;
    }
    
    if(!lastOpen.nextSibling){
        location.href = "#"+lastOpen.parentNode.firstChild.id.replace("-elem", "");
        return;
    }
    
    location.href = "#"+lastOpen.nextSibling.id.replace("-elem", "");
}

function fprev(){
    if(!lastOpen){
        return;
    }
    
    if(!lastOpen.previousSibling){
        location.href = "#"+lastOpen.parentNode.lastChild.id.replace("-elem", "");
        return;
    }
    
    location.href = "#"+lastOpen.previousSibling.id.replace("-elem", "");
}
