
//
// Copyright (c) 2006 Asklepios
// All rights reserved
//
// Created on: 2006-12-14
//
// PURPOSE:
// Allow scrolling the dynPage1 container when scroll(step) and stopScrolling()
// get invoked by the HTML pages. The actual scrolling lib was an free internet
// script which couldn't be run with IE7 and since it seemed too weird, a new
// scroller lib was written, which needs no modification of the HTML code at 
// all.
//
// LAST MOD:
// - 15.05.2008: Bugfix in der scroll() Funktion behoben: XHTML Setzen der
// 		Einheit von style.top.
//


/* These vars are used to save the current position of the scrolling div: */
dynPage1_top    = 0;
dynPage1_bottom = 0;
dynPage1_left   = 0;
dynPage1_right  = 0;
dynPage1_height = 0;    // The height of the scrolling div is needed to examine
                        // if the user should still be allowed to scroll up/down
dynPage1_initop = 0;    // Saves the initial top position of the dynamic page 
                        // and is used for examination wether the user should 
                        // be allowed to scroll on or not.
scroll_delay    = 40;   // The number of milliseconds the script should wait 
                        // during the scrolling steps. This is done so the 
                        // scrolling process seems more fluid.
scroll_timer    = 0;    // Saves the id of the last setTimeout() function, so
                        // it can be broken up using clearTimeout(id)!


/*******************************************************************************
 * Used to initialize the current site scroller. This is neccessary for the 
 * scroll functions to work. Additionally several CSS commands were included
 * directly within the HTML files and those get overriden here.
 ******************************************************************************/
function 
initScroller()
{
    // Make the dynamic page visible with elegancy by first setting the opacity 
    // to zero to allow a smooth fade-in:
    var scroller_style = document.getElementById("dynPage1").style;
    changeOpacity(0, "dynPage1");
    scroller_style.visibility = "visible";

    // fade-in:
    var speed   = 0.5;
    var timer   = 0;
    for(var i=0; i<=100; i++) {
        setTimeout("changeOpacity(" + i + ", 'dynPage1')", timer * speed);
        timer++;
    }

    // Save the current position of the scrolling div.
    // Since these positions are taken from the stylesheet, the script has 
    // to crop the unit definitions which are expected to own the last two 
    // characters:
    dynPage1_top    = strip_unit(scroller_style.top);
    dynPage1_bottom = strip_unit(scroller_style.bottom);
    dynPage1_left   = strip_unit(scroller_style.left);
    dynPage1_right  = strip_unit(scroller_style.right);

    // Save the height (the complete!) of the dynamic page:
    dynPage1_height = document.getElementById("dynPage1").offsetHeight;

    // Save the initial top position of the dynamic page:
    dynPage1_initop = dynPage1_top;
}

// Crops the last two characters of a numberstring, which is expected to look 
// like "13px". These two last chars define the unit of the number but are
// of no use when it comes to mathematical operations.
function
strip_unit(numstr)
{
    return Number(numstr.substr(0, numstr.length-2));
}

// Changes the opacity of an HTML object:
function
changeOpacity(opacity, id)
{
    var object          = document.getElementById(id).style;
    object.opacity      = (opacity / 100);
    object.MozOpacity   = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter       = "alpha(opacity=" + opacity + ")";
}



function
scroll(step)
{
    var ret = false;    // Initialize the return code.

    // Before doing a scroll operation, first the script has to check if step 
    // is numeric:
    if(!isNaN(step)) {  // if is not NOT A NUMBER == is a number?
        // Subtract/Add the step to the top position of the dynamic page:
        dynPage1_top += step;

        // Check if the current top position is less than the actual height
        // of the dynamic page * -1 + 250.
        // 250 was chosen to be added, so the last parts of the dynamic page
        // can be seen.
        if(dynPage1_top < (dynPage1_height*-1 + 250)) {
            // If yes, redo the operation so the user can't scroll into 
            // nothingness:
            dynPage1_top -= step;
        }

        // Check like the above one, but this time the positive top position
        // has to be checked:
        if(dynPage1_top > dynPage1_initop) {
            dynPage1_top -= step;
        }

        // Set the dynamic page to the new position:
        document.getElementById("dynPage1").style.top = dynPage1_top+"px";

        // Recall the scroll function after the given delay and while 
        // stopScroll() hasn't been called:
		scroll_timer = setTimeout('scroll('+step+')', scroll_delay);

        ret = true;
    }

    return ret;
}


// Stops the scrolling process by breaking up the latest Timeout:
function
stopScroll()
{
    clearTimeout(scroll_timer);
    return true;
}


// The current scroller script is not to 100% complementing the old script.
// There is one problem within the HTML files that troubles the new scroll() 
// function, and that is the height instruction at the dynPage1 container.
// This instruction has to be removed to make offsetHeight variable used in
// initScroller reliable also in Gecko browsers.
function
dynHeightRemover()
{
    // Simplest solution: setting the height of the dynPage1 to auto!
    document.getElementById("dynPage1").style.height = "auto";
    return true;
}



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

addLoadEvent(dynHeightRemover);
addLoadEvent(initScroller);
