// 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();

    // Logo:
    hideBlock("kober", "<a href=\"" + relpath + "index.html\" " +
        "title=\"Zur Startseite\"><img src=\"" + relpath + "images/logo.png\"" +
        " width=\"300\" height=\"115\" alt=\"ZIEGELWERK LIZZI GMBH\"" +
        " title=\"ZIEGELWERK LIZZI GMBH\" /></a>");

    // Addressblock:
    hideBlock("ue_adresse", "<img src='" + relpath + "images/h5_adresse.png' " +
        "alt='' \/>");

    // Replace text in webdesignblock with an image:
    hideBlock("webdesign", "<a href=\"javascript:newwin('http://" + 
        "www.asklepios.at')\" title=\"Webdesign, Printdesign and more\">" + 
        "<img src='" + relpath + "images/webdesign.png' alt='' \/></a>");

    // Navigation: each link-text inside the list-items of the navigation list
    // get replaced by images.
    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 + "_0.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";
    }

    // Some containers have to get heightened depending on the size of the
    // browser window; this has to be done via JavaScript since HTML and CSS
    // won't be able to do this until tables are used.
    // To get the measures of the client area, window.innerWidth / 
    // window.innerHeight (Gecko Browsers) or document.body.clientWidth /
    // document.body.clientHeight (IE) are used:
    doc_width   = (document.body.clientWidth)
                    ? document.body.clientWidth
                    : window.innerWidth;
    doc_height  = (document.body.clientHeight)
                    ? document.body.clientHeight
                    : window.innerHeight;

    // The width is used to set the lenght of the addressblock:
    document.getElementById("adresse").style.width = doc_width - 528 + "px";

    // Set the height of the spacer at the bottom of the site. For this purpose
    // additionally to the document's height, the height of the
    // behaelter-container is needed:
    content_height = document.getElementById("behaelter").offsetHeight;
    spacer_height  = doc_height - content_height;
    // The spacer height is only set if positive; if negative, Mozilla would
    // throw an error:
    if(spacer_height > 0) {
        document.getElementById("fubeplaz").style.height = spacer_height + "px";
    }

    // If the page holds a link to print the page, then this text-link gets
    // replaced with an icon image.
    if(document.getElementById("drucken")) {
        hideBlock("drucken", "<a href='javascript:print();' " +
            "title='Seite drucken'><img src='" + relpath + 
            "images/ico_print.png' alt='' /></a>");
    }

    // Some pages can contain multiple div containers that make use of the
    // toplink class. Those containers only hold text-links to the #top anchor,
    // which will be replaced by image-links to the #top anchor:
    var containers = document.getElementById("seber").
        getElementsByTagName("div");
    for(var i=0; i<containers.length; i++) {
        if(containers[i].className == "toplink") {  // matching class?
            containers[i].innerHTML = "<a href=\"#top\" " + 
                "title=\"Zum Seitenanfang\"><img src=\"" + relpath +
                "images/ico_top.png\" alt=\"\" /></a>";
        }
    }
}


// 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);
}

function 
popupPic(url, vertikal)
{
    // Initialise the image:
    var picture = new Image();

    // Load the image by adding an onload handler:
    window.status = "Bild wird geladen..."; // Set status to inform user
    picture.onload = function() {
        // After the image has been loaded, calculate the height and width of
        // the popup:
        var width   = picture.width + 40;
        var height  = picture.height+ 40;

        // Calculate the screen coordinates where the window should open:
        x = screen.availWidth/2  - width/2;
        y = screen.availHeight/2 - height/2;

        //
        var popupWindow = window.open("about:blank", "", "width=" + width + 
            ",height=" + height + ",left=" + x + ",top=" + y + ",screenX=" + x +
            ",screenY=" + y);

        with(popupWindow.document) {
            writeln("<html>"
                +   "   <head>"
                +   "       <title>Ziegel Lehm Ton - ZIEGELWERK LIZZI GMBH"
                +   "       </title>"
                +   "       <link rel=\"stylesheet\" type=\"text/css\" "
                +   "           media=\"screen\" href=\"" + getRelPathToRoot() 
                +   "styles/bibe.css\" />"
                +   "   </head>"
                +   "   <body>"
                +   "       <a href=\"javascript:window.close();\">"
                +   "           <img src=\"" + url + "\" alt=\"\" border=\"0\""
                +   "            />"
                +   "       </a>"
                +   "   </body>"
                +   "</html>");
        }
    }

    // The picture source has to be set AFTER the definition of the onload
    // function so the user can click multiple times on the same image to pop it
    // up. Would the img-source be defined BEFORE the definition of the onload
    // function, then clicking multiple times on the same img to popup is not
    // possible. ??????????????????????????????????
    picture.src = url;

    window.status = "Bild wurde geladen.";
}


//
// 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();
        }
    }
}


