
/* *
** menu_part_scroll.js
** (C) 2009 by Andreas Born, David Lepold
* */

function objMPS() {
    /*** Config Params ***/
    var menuDiv = 'headerStayTop';
    var menuLineDiv = 'mastermenu';
    var maxScrollTop = 62;
    var menuHeigthDefault = 30;
    /*** /Config Params ***/
    function repos() {
        var obj = document.getElementById(menuDiv);
        if (obj) {
            var topScroll = getYOffset();
            var divNewTop = -topScroll;
            if (divNewTop < -maxScrollTop) divNewTop = -maxScrollTop;
            if (divNewTop >= 0) divNewTop = 0;
            obj.style.top = divNewTop + 'px';
        }
    }
    function anchorpos(anchor) {
        var ank = document.location.hash;
        if (anchor) ank = anchor;
        if (ank) {
            ank = ank.substr(1);
            var obj = document.getElementsByName(ank);
            if (obj && obj.length > 0) {
                obj = obj[0];
                var offsetTop = getElemYOffset(obj);
                var menuHeight = document.getElementById(menuLineDiv).clientHeight;
                if (!menuHeight) menuHeight = menuHeightDefault;
                window.scrollTo(0,offsetTop-menuHeight);
            }
        }
    }
    function getElemYOffset(obj) {
        if (obj.offsetParent)
            return obj.offsetTop + getElemYOffset(obj.offsetParent);
        else
            return obj.offsetTop;
    }    
    function getYOffset() {
        var y;
        if (memberExists(self, 'pageYOffset'))
        	y = window.pageYOffset;
        else if (memberExists(document, 'documentElement') && 
                 memberExists(document.documentElement, 'scrollTop')) 
        	y = document.documentElement.scrollTop;
        else if (memberExists(document,'body'))
        	y = document.body.scrollTop;
        return y;
    }
    function methodExists(obj, method) {
        var too, toom, ret = false;
        if (obj) {
            too = typeof obj;
            if (too == "function" || too == "object") {
                toom  = typeof obj[method];
                if (toom == "function" || toom == "object")  ret = true;
            } /* too */
        } /* obj */
        return ret;
    }
    function memberExists(obj, method) {
        var too, ret = false;
        if (obj) {
            too = typeof obj;
            if (too == "function" || too == "object") {
                if ((typeof obj[method]) != 'undefined') ret = true;
            } /* too */
        } /* obj */
        return ret;
    }
    function modifyLinks() {
        var link, s, oc = 0;
        var w = window.location;
        for (var i = 0; i < document.links.length; i++) {
            link = document.links[i];
            if (link.protocol && (link.protocol != w.protocol || link.protocol != 'http:')) continue;
            if (link.hostname && link.hostname != w.hostname) continue;
            if (link.port && ((w.port && link.port != w.port) || link.port != 80)) continue;
            if (link.pathname) {
                if (link.pathname.substr(0,1) == '/') {
                    if (link.pathname != w.pathname) continue;
                } else {
                    s = '/' + link.pathname;
                    if (w.pathname.substr(w.pathname.length-s.length) != s) continue;
                }
            }
            if (link.search && w.search && link.search != w.search) continue;
            if (!link.hash) continue;
            oldOnclicks[oc] = link.onclick;
            oldOnclicksC[oc] = link;
            link.onclick = Function('window.setTimeout("MPS.onclick(this);",1); return MPS.oldOnclick('+oc+');');
            oc++;
        }
    }
    function onscroll() {
        repos();
        oldOnscroll();
    }
    function onload() {
        repos();
        anchorpos();
        modifyLinks();
        oldOnload();
    }    
    function onclick(caller) {
        anchorpos(caller.hash);
        window.setTimeout('MPS.onscroll();',1);
    }
    function oldOnclick(oc) {
        var tmp = oldOnclicks[oc];
        var ret = tmp(oldOnclicksC[oc]);
        if (ret !== false && ret !== true) ret = false;
        return ret;
    }
    function init() {
        oldOnload = window.onload;
        window.onload = Function('MPS.onload();');
        oldOnscroll = window.onscroll;
        window.onscroll = Function('MPS.onscroll();');
    }
    this.onscroll = onscroll;
    this.onload = onload;
    this.onclick = onclick;
    this.oldOnclick = oldOnclick;
    this.init = init;
    this.tmp = null;
    var oldOnload;
    var oldOnscroll;
    var oldOnclicks = [];
    var oldOnclicksC = [];
}

if (!MPS) {
    var MPS = new objMPS;
    MPS.init();
}
