// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//
// Coded by Travis Beckham
// Modified by www.einberg-volleyball.de
// http://www.squidfingers.com | http://www.podlob.com
// If want to use this code, feel free to do so, but please leave this message intact.
//
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/*

If you use a DOCTYPE that puts Explorer 6 in standards compliant mode, some properties
of document.body are reassigned to document.documentElement. In Explorer 5 the
properties still belong to document.body. Checking if the documentElement exists is not
enough, since it exists in all W3C DOM compatible browsers, so we also have to see if
it has the property we are trying to access.
For more info on this topic visit http://www.xs4all.nl/~ppk/js/doctypes.html

*/
var Browser = {
    w3c : document.getElementById,
    iex : document.all,
    opera : (navigator.userAgent.indexOf("Opera") != -1),
    debug : (location.href.indexOf("localhost") != -1 || location.href.indexOf("/einberg/") != -1),
    scrollLoop : false,    // boolean
    scrollInterval : null, // setInterval id
    currentBlock : null,   // object reference
    
    getTargetObj : function(aname){
        var anchors = document.getElementsByTagName("a");
        for (var i=0;i<anchors.length;i++) {
            if (anchors[i].name == aname) {
                return anchors[i];
            }
        }
        return null;
    },

    getWindowHeight : function(){
        if(this.opera) return document.body.clientHeight;
        else if(this.iex) return (document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight;
        else return window.innerHeight;
    },

    getScrollTop : function(){
        if(this.iex) return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
        else return window.pageYOffset;
    },

    getElementYpos : function(el){
        var y = 0;
        while(el.offsetParent){
            y += el.offsetTop;
            el = el.offsetParent;
        }
        return y;
    },

    scroll : function(label){
        if(!this.w3c){
            location.href = "#" + label;
            return;
        }
        if(!this.getTargetObj(label)) {
            return;     // Label nicht vorhanden oder noch nicht geladen
        }
        if(this.scrollLoop){
            clearInterval(this.scrollInterval);
            this.scrollLoop = false;
            this.scrollInterval = null;
        }
        if (label=='Seitenanfang') this.scrollTo(0);
        else {
            this.currentBlock = this.getTargetObj(label);
            var doc = document.getElementById(this.containerName);
            var documentHeight = this.getElementYpos(doc) + doc.offsetHeight;
            var windowHeight = this.getWindowHeight();
            var ypos = this.getElementYpos(this.currentBlock);
            if(ypos > documentHeight - windowHeight) ypos = documentHeight - windowHeight;
            if (ypos < 0)   // Wenn aus irgendwelchen Gruenden die Berechnung offensichtlich fehlerhaft ist, gleich zum label springen
                location.href = "#" + label;
            else            // Los geht's mit Scrollen
                this.scrollTo(ypos);
        }
    },

    scrollTo : function(y){
        if(this.scrollLoop){
            var top = this.getScrollTop();
            if(Math.abs(top-y) <= 1){
                window.scrollTo(0,y);
                clearInterval(this.scrollInterval);
                this.scrollLoop = false;
                this.scrollInterval = null;
            }else{
                window.scrollTo(0,top+(y-top)/2);
            }
        }else{
            this.scrollInterval = setInterval("Browser.scrollTo("+y+")",100);
            this.scrollLoop = true;
        }
    }
};


function ersetzeInterneLinks() {
    var arrLinks, i, href, arrHref;
    
    if (!document.getElementById) return;

    // get all anchors
    arrLinks = document.getElementsByTagName("a");

    for (i=0; i<arrLinks.length; i++) {
        // check if href links to an anchor on this page

        if (arrLinks[i].href  &&
            arrLinks[i].href.indexOf("#") != -1  &&
            arrLinks[i].href != document.URL + "#"  &&
            arrLinks[i].href.indexOf(document.URL) != -1) {

            // get name of target anchor
            href = arrLinks[i].href.substring(arrLinks[i].href.indexOf("#")+1);

            // find target anchor
            arrHref = document.getElementsByName(href);

            if (arrHref.length) {
                arrLinks[i].id = "__" + href;    // save target as id with prefix (used in onclick function below)
                arrLinks[i].onclick = function () { Browser.scroll(this.id.substring(2)); return false; };
            }
            else if (Browser.debug) {
                alert("Das Label\n" + href + "\nist nicht vorhanden");
                arrLinks[i].onclick = function () { alert("Das Label\n" + href + "\nist nicht vorhanden"); return false };
            }
        }
    }
}


// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/*
using the following line, IE/PC returns an incorrect number when getting the document height.
var document_height = document.all ? document.body.offsetHeight : window.document.height;
To fix this problem, a container div is wrapped around the content so the correct height
can be determined.
*/

// Edit these variables

Browser.containerName = "container"; // The id name of the div containing the content

window.onload = ersetzeInterneLinks;