// reflow page for correct cross-browser footer positioning

function reflow() {
    var bodydiv = document.getElementById("body");
    bodydiv.style.minHeight = "100%";
}



// workaround for absence of target attributes in xhtml strict

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}



// scroll internal links smoothly (id used instead of name attribute)

var ScrollLinks = {
    currentHash: false,
    start: function(){
        this.scroll = new fx.Scroll({duration: 800, onComplete: function(){ScrollLinks.end();}});
        this.allinks = document.getElementsByTagName('a');
        for (i=0; i<this.allinks.length; i++){
            var lnk = this.allinks[i];
            if (lnk.href) {
                var decoded_location_pathname = unescape(location.pathname.replace(/\+/g,  " "));
                var decoded_lnk_pathname = unescape(lnk.pathname.replace(/\+/g,  " "));
            }
            if ((lnk.href && lnk.href.indexOf('#') != -1) && (lnk.hash.length > 0) && ( (decoded_lnk_pathname == decoded_location_pathname) || ('/'+decoded_lnk_pathname == decoded_location_pathname) ) && (lnk.search == location.search)) {
                lnk.onclick = function(){
                    ScrollLinks.scroll.clearTimer();
                    this.initialHref = this.href;
                    this.initialHash = this.hash;
                    this.href = "javascript:void(0)";
                    setTimeout(function(){this.href = this.initialHref;}.bind(this), 200);
                    ScrollLinks.click(this);
                }
            }
        }
    },

    click: function(link){
        this.currentHash = link.initialHash.substr(1);
        if (this.currentHash) {
            for (j=0; j<this.allinks.length; j++){
                if (this.allinks[j].id == this.currentHash){
                    if (!window.opera) this.scroll.scrollTo(this.allinks[j]);
                    else this.scroll.scrollTo(this.allinks[j].parentNode);
                    break;
                }
            }
        }
    },

    end: function(){
        window.location.href = "#"+this.currentHash;
        this.currentHash = false;
    }
}



// run all necessary functions on page load

function loadAll() {
   reflow();
   externalLinks();
   if (document.getElementById("top") != null)
      ScrollLinks.start();
}
window.onload = loadAll;