/*\
 * Copyright © 2008 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
 * _____________________________________________________________________________________________________________________
 *
 * This file is "Open Source" as defined by the Open Source Initiative (OSI). You can redistribute it and/or modify it
 * under the terms of the BSD License. The license text is appended to the end of this file.
\*/


/**
 * Funktion, die nach dem Laden eines Dokumentes aufgerufen wird.
 *
 * Diese Funktion kann bei Bedarf von einer HTML-Seite überdefiniert werden.
 */

function onloadHook() 
{
    window.focus();
}


/**
 * Prüft, ob der verwendete Browser ein Internet Explorer ist. 
 *
 * @return                  Der Browser ist ein Internet Explorer
 */

function isIE()
{
    return (navigator.appName == 'Microsoft Internet Explorer');
}


/**
 * Prüft, ob der verwendete Browser ein Firefox ist. 
 *
 * @return                  Der Browser ist ein Firefox
 */

function isFF()
{
    return (navigator.appName == 'Netscape');
}


/**
 * Liefert ein Element über seine ID.
 *
 * @param  elementID        Die ID
 * @return                  Das Element
 */

function element(elementID)
{
    return document.getElementById(elementID);
}



/**
 * Löst einen Request auf die URL aus, die im Parent-Element als "href"-Attribut abgelegt ist.
 * 
 * Diese Funktion eignet sich dazu, dem Hyperlink eines A-Elements zu folgen, indem ein Button innerhalb des Elementes
 * gedrückt wird. Mozilla-basierte Browser können das ohne JavaScript-Unterstützung, für den Internet Explorer muss wie
 * folgt formuliert werden (diese Formulierung stört Mozilla-Browser nicht):
 * 
 *     <a href="/url"><button type="button" onclick="return parent(this);">Button-Beschriftung</button></a>
 *
 * @param  element          Das Element
 * @return                  Immer 'false'
 */
 
function parent(element)
{
    window.location.href = element.parentNode.href;
    
    return false;
}


/**
 * Ersetzt das aktuelle Bild eines Images durch ein anderes, wobei das aktuelle Bild in einem Attribut gesichert wird.
 *
 * @param  img              Das Image
 * @param  src              Das neue Bild
 * @see                     #restoreImageSource
 */
 
function changeImageSource(img, src)
{
    img.src2 = img.src;
    img.src = src;
}


/**
 * Ersetzt das aktuelle Bild eines Images durch das zuvor in einem Attribut gesicherte.
 *
 * @param  img              Das Image
 * @see                     #changeImageSource
 */
 
function restoreImageSource(img)
{
    img.src = img.src2;
}


/**
 * Ersetzt den CSS-Style des angegebenen Elements durch einen anderen, wobei der aktuelle CSS-Style in einem Attribut
 * gesichert wird.
 *
 * @param  element          Das Element
 * @param  style            Der CSS-Style
 * @see                     #restoreStyle
 */
 
function changeStyle(element, className)
{
    element.className2 = element.className;
    element.className = className;
}


/**
 * Ersetzt den CSS-Style des angegebene Elemens durch den zuvor in einem Attribut gesicherte.
 *
 * @param  element          Das Element
 * @see                     #changeStyle
 */
 
function restoreStyle(element)
{
    element.className = element.className2;
}


/**
 * Ersetzt den CSS-Style des angegebene Elemens durch den zuvor in einem Attribut gesicherte.
 *
 * @param  element          Das Element
 * @see                     #changeStyle
 */
 
function loadURL(url)
{
    window.location = url;
}


/**
 * Liefert die X-Position des Maus-Cursors im Dokument.
 *
 * @param  event            Das Mouse-Event
 * @return                  Die X-Position des Maus-Cursors im Dokument
 */

function mouseX(event)
{
    // Unterschiedliche Berechnung für Versionen für Mozialla ("event.pageX" ist dann definiert), IE strict
    // ("document.documentElement" ist dann definiert) und IE transitional.
    return (event.pageX)? event.pageX: window.event.x + ((document.documentElement)? document.documentElement.scrollLeft: document.body.scrollLeft);
}


/**
 * Liefert die Y-Position des Maus-Cursors im Dokument.
 *
 * @param  event            Das Mouse-Event
 * @return                  Die Y-Position des Maus-Cursors im Dokument
 */

function mouseY(event)
{
    // Unterschiedliche Berechnung für Versionen für Mozialla ("event.pageY" ist dann definiert), IE strict
    // ("document.documentElement" ist dann definiert) und IE transitional.
    return (event.pageY)? event.pageY: window.event.y + ((document.documentElement)? document.documentElement.scrollTop: ocument.body.scrollTop);
}

/**
 * Positioniert ein Block-Element an der Cursorposition.
 *
 * @param  element          Das Element
 * @param  event            Das Mouse-Event
 * @param  offsetX          Die Verschiebung des Elements in X-Richtung 
 * @param  offsetY          Die Verschiebung des Elements in Y-Richtung 
 * @see                     #showBlock
 * @see                     #hideBlock
 */

function moveBlock(elementID, event, offsetX, offsetY)
{
    var element = document.getElementById(elementID);

    element.style.left = (mouseX(event) + offsetX) + 'px';
    element.style.top = (mouseY(event) + offsetY) + 'px';
}


/**
 * Macht ein Block-Element sichtbar.
 *
 * @param  element          Das Element
 * @see                     #moveBlock
 * @see                     #hideBlock
 */

function showBlock(elementID)
{
    var element = document.getElementById(elementID);

    element.style.display = 'block';
}


/**
 * Versteckt ein Block-Element.
 *
 * @param  element          Das Element
 * @see                     #showBlock
 * @see                     #moveBlock
 */

function hideBlock(elementID)
{
    var element = document.getElementById(elementID);

    element.style.display = 'none';
}


/**
 * Macht ein Block-Element sichtbar.
 *
 * @param  element          Das Element
 * @see                     #moveBlock
 * @see                     #hideBlock
 */

function setBlockVisible(elementID, visible)
{
    if (visible)
    {
        showBlock(elementID);
    }
    else
    {
        hideBlock(elementID);
    }
}


/**
 * Entfernt White-Spaces vom Anfang und Ende eines Strings. 
 *
 * @param  str              Der String
 * @return                  Das Ergebnis
 */

function trim(str)
{
    return str.replace(/^\s+|\s+$/g, '');
}


/*\
 * _____________________________________________________________________________________________________________________
 * 
 * This software is distributed under the terms of the BSD License:
 * 
 * Copyright © 2008 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
 *   disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials provided with the distribution.
 * - Neither the name of the Klaus Rogall nor the names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * _____________________________________________________________________________________________________________________
\*/

