// Returns the relative path to the root.
// This can be found out by quering the classes that the body-tag uses. The
// class-names tell the level of the document:
//  .level0     = root
//  .level1     = first subdir
//  .level2     = subdir in the first subdir etc.
function
getRelPathToRoot()
{
    var bodyclass   = document.getElementsByTagName("body")[0].className;

    if(bodyclass == "level1") {
        return "../";
    } else {
        return "";
    }
}


// Initializes each page by replacing text-parts with images. F.i. at the
// navigation and the header.
function
initPage()
{
    // Request the relative path to root:
    var relpath = getRelPathToRoot();

    // Navigation: each link-text inside the list-items of the navigation list
    // get replaced by images.
    if(document.getElementById("navigation")) {
        var navlist = document.getElementById("navigation").
            getElementsByTagName("li");
        for(var i=0; i<navlist.length; i++) {
            // Get the link-text by grapping the anchor tag:
            navlist[i].getElementsByTagName("a")[0].innerHTML = 
                "<img src=\"" + relpath + "images/nav_" + i + ".png\" " +
                "alt=\"\" \/>";
            // Add the ie7 class to the list item - using a CSS instruction the
            // additional gap IE7 inserts between the list items when wrapped 
            // around images - is removed:
            navlist[i].className = navlist[i].className + " ie7";
        }
    }

    // Webdesign-block:
    hideBlock("psychosomatic", "<img src=\"images/psychosomatic.png\" title=\"Psychosomatic\"/>");
    hideBlock("asklepios", "<a href=\"javascript:newwin('http://www.asklepios" +
        ".at/')\" title=\"Webdesign\"><img src=\"images/webdesign.png\" alt=\"WebDesign\" /></a>");

    // Depending on the id of the current site, the h1 header (if present) gets
    // replaced with an image:
    var body_id = document.body.id;
    var header  = document.getElementsByTagName("h1");
    if(header[0]) {
        header[0].innerHTML = "<img src=\"images/h1_" + body_id + ".png\" " +
            "alt=\"\" />";
    }
}


// Certain textparts will get replaced with images. But instead of simply
// overwriting the text with the image, the text will get enclosed in a span tag
// that hides the text. This is done so the text can get printed.
function
hideBlock(id, alt_text)
{
    document.getElementById(id).innerHTML =
        alt_text +
        "<span class='hide'>" +
        document.getElementById(id).innerHTML +
        "</span>";
}

//
// Initializes extern links and links to mail-addresses which were "crypted" in
// the html code.
// Derived from the lightbox-script: 
//      http://www.huddletogether.com
//
function
initExLinks()
{
    if(!document.getElementsByTagName) {
        return ;
    }

    var anchors = document.getElementsByTagName("a");

    // Loop through all anchor tags
    for(var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];

        // Oeffnen von neuen Browserfenstern:
        if(anchor.getAttribute("href")  &&
           anchor.getAttribute("rel")   == "newwin")
        {
            anchor.href = "javascript:newwin('"+anchor.getAttribute("href")+"');";
        } else if(anchor.getAttribute("href")   &&
                  anchor.getAttribute("rel")    == "mehl")
        {
            // Um eMail-Adressen vor Sammel-Bots zu verstecken, werden diese
            // nicht in der ueblichen Weise angeschrieben, sondern es werden
            // einige Zeichen ausgetauscht bzw. weggelassen.
            // Die Adresse mail@mail.com muesste im HTML-Quelltext
            // folgendermassen angeschrieben werden: mail, mail com
            // D.h.:
            //  - Punkte muessen durch Leerzeichen ersetzt werden
            //  - Das @-Symbol muss durch die Zeichenkette ", " ersetzt werden

            // Die verschluesselte Mail-Adresse befindet sich innerhalb des
            // Anchortags:
            var mailad  = anchor.innerHTML;

            // Erster Schritt ist das Ersetzen der ", "-Zeichenkette mit dem
            // @-Symbol:
            mailad      = mailad.replace(/, /, "@");

            // Naechster Schritt ist das ersetzen der Leerzeichen gegen Punkte:
            mailad      = mailad.replace(/ /g, ".");

            // Nachdem die im HTML-Quelltext verschluesselte Mailadresse
            // aufgeloest wurde, kann diese aufgeloeste Version in den
            // href-Parameter des anchor-Tags geschrieben werden:
            anchor.href         = "mailto:" + mailad;

            // Damit kein Besucher die verschluesselte Mailadresse sehen muss -
            // gemeint ist die Beschreibung innerhalb des Anchortags - wird
            // diese nun ebenfalls "schoen" aufbereitet:
            anchor.innerHTML    = mailad;
        } else if(anchor.getAttribute("href")   &&
                  anchor.getAttribute("rel")    == "foto")
        {
            // Link zu einem Fotos soll sich in einem Popup oeffen:
            anchor.href = "javascript:popupPic('" + anchor.getAttribute("href") 
                + "');";
        } else ;
    }
}

function
newwin(url)
{
    window.open(url);
}


//
// Adds an event to window.onload without overwriting currently
// assigned onload functions.
// Function found at Simon Willison's weblog - 
//      http://simon.incutio.com/ 
//
function
addLoadEvent(func)
{
    var old_onload  = window.onload;

    if(typeof window.onload != 'function') {
        window.onload   = func;
    } else {
        window.onload   = function() {
            old_onload();
            func();
        }
    }
}


