/*
 *  Seewinkelhof Salzl Internet Exploder Sanitizer
 *
 *  Copyright (C) 2006-2007 Herbert Nachtnebel for Salzl OEG.
 *
 *  Permission is hereby granted, free of charge, to any person
 *  obtaining a copy of this script and associated documentation
 *  files (the "Software"), to deal in the Software without
 *  restriction, including without limitation the rights to use,
 *  copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following
 *  conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *  OTHER DEALINGS IN THE SOFTWARE.
 *
 *  
 *  This javascript is used to make the website usable even with IE.
 *  The Internet Explorer has so many deficiencies that it is not
 *  possible to use CSS to make the simple menu structure work used on
 *  this website.
 *
 *  Since javascript is nevertheless necessary to implement needed
 *  functionality for our IE-using clients, I decided to use it for
 *  some simple, optional, viewing effects also.  Anyway, the most
 *  important design rule was that this website should be usable even
 *  when the user has turned off javascript for security reasons.
 *  Hence, the additional functionality only pushes around some images
 *  and positions the page in the browser window.
 *
 *  If anybody want's to use some code from this script or the script
 *  as a whole feel free!  It is under the MIT license, see above.
 */

function get_window_height()
{
    var window_height = 0;

    if (window.innerHeight &&
        typeof(window.innerHeight) == 'number')
    {
        window_height = window.innerHeight;
    }
    else if (document.documentElement &&
             document.documentElement.clientHeight &&
             typeof(document.documentElement.clientHeight) == 'number')
    {
        window_height = document.documentElement.clientHeight;
    }
    else if (document.body &&
             document.body.clientHeight &&
             typeof(document.body.clientHeight) == 'number')
    {
        window_height = document.body.clientHeight;
    }
    return window_height;
}

function position_content()
{
    if (document.getElementById)
    {	
        var content = document.getElementById('content');
        
        if (content)
        {
            var offset_top = content.offsetTop;
            var window_height = get_window_height();

            if (window_height > 0)
            {
                var padding = window_height - content.offsetHeight;
		
                if (padding < -10)
                    padding = -10;
                if (padding < offset_top)
                {
                    content.style.marginTop = 0;
                    content.style.top = padding + 'px';
                }
            }
        }
    }    
}

/*
 *  Make the menus work even under the infamous Internet Explorer.
 *
 *  I am sorry for the guys who have turned off javascript in IE.  This
 *  browser is so buggy that I am not able to make the menus work without
 *  script support.  If you really want to use this website, then do not
 *  turn off javascript or use a real browser.
 */
function sanitize_menus()
{
    if (document.all && document.getElementById)
    {
        var menu = document.getElementById("menu");

        for (i = 0; i < menu.childNodes.length; i++)
        {
            var child = menu.childNodes[i];

            if (child.nodeName == "UL")
            {
                for (i = 0; i < child.childNodes.length; i++)
                {
                    var node = child.childNodes[i];

                    if (node.nodeName == "LI")
                    {
                        node.onmouseover = function() { this.className += "over"; }
                        node.onmouseout  = function() { this.className=this.className.replace(/over$/, ""); }
                    }
                }
            }
        }
    }
}

/*
 *  With CSS it is impossible to float images to the bottom left or
 *  bottom right corner.  Add this functionality by manipulating the
 *  size of sandbag div areas.
 */
function move_images()
{
    if (document.getElementById)
    {
        var content    = document.getElementById('content');
        var ri_div     = document.getElementById('RI');

        if (content && ri_div)
        {
            var all_height = content.offsetHeight;
            var ri_height  = ri_div.offsetHeight;
            var ri_offset  = ri_div.offsetTop;

            var offset = all_height - ri_height - ri_offset - 20;

            if (offset > 150)
                offset = 150;
            if (offset > 0)
                ri_div.style.marginTop = offset + 'px';
        }
    }
    return 0;
}

/*
 *  Yet another Internet Explorer fix!
 *
 *  Correctly handle PNG transparency in old Internet Exploders.
 * 
 *  Copyright belongs to Bob Osola, see his web site at [1].
 *  Bob, thank you for this code and for your liberal license!
 *
 *  [1] http://homepage.ntlworld.com/bobosola/pnghowto.htm
 *      last accessed in Sep 2007.
 */
function correct_pngs()
{
    var arVersion = navigator.appVersion.split("MSIE");
    var version = parseFloat(arVersion[1]);
       
    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
    {
        for (var i = 0; i < document.images.length; i++)
        {
            var img = document.images[i];
            var imgName = img.src.toLowerCase();

            if (imgName.substring(imgName.length-4, imgName.length) == ".png")
            {
                var imgID = (img.id) ? "id='" + img.id + "' " : "";
                var imgClass = (img.className) ? "class='" + img.className + "' " : "";
                var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
                var imgStyle = "display:inline-block;" + img.style.cssText;

                if (img.align == "left")
                    imgStyle = "float:left;" + imgStyle;
                if (img.align == "right")
                    imgStyle = "float:right;" + imgStyle;
                if (img.parentElement.href)
                    imgStyle = "cursor:hand;" + imgStyle;

                var strNewHTML = "<span " + imgID + imgClass + imgTitle
                    + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                    + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                    + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
                    
                img.outerHTML = strNewHTML;
                i = i-1;
            }
        }
    }    
}

/*
 *  Parse a URI.
 *
 *  Copyright belongs to Steven Levithan, see his web site at [2].
 *  Modified by me.  Steven, thank you for this perl!  MIT License.
 *
 *  [2] http://stevenlevithan.com
 *      last accessed in Sep 2007.
 */
function parse_uri (source)
{
    var key = ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"];
    var exp = /^(\w+:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
    var val = exp.exec(source);
    var uri = {};

    for (var i = 0; i < key.length; i++)
        uri[key[i]] = val[i] || "";

    if (uri.directory.length)
        uri.directory = uri.directory.replace(/\/?$/, "/");

    return uri;
}

/*
 *  Another IE Goodie.
 *
 *  The path to objects in IE has to be an absolute path or it can
 *  not be opened.  The excuse for this bug is that this behaviour
 *  is in reality necessary for security reasons, pfff :-)!  These
 *  guys are never boring...
 *
 *  Hence make relative paths absolute with the help of the current
 *  URL.
 */
function make_absolute (href)
{
    if (/^\w+:\/\//.test(href))
        return href;    

    var uri = parse_uri(location.href);

    path = uri.directory + href;
    while (/\/\.\.\//.test(path))
        path = path.replace(/[^\/]*\/\.\.\//, "");
    while (/\/\.\//.test(path))
        path = path.replace(/\/\.\//, "/");
    return uri.protocol + uri.authority + path;
}

/*
 *  Take a linked movie and start it in windows media player.
 *  Embed the media player in the "RI"ght div area reserved for
 *  image display.
 *
 *  The link placed in the page should have a onclick() method
 *  calling this function if the user follows the link.  Therefor,
 *  if the client has disabled javascript, the link will work
 *  normally and he can view the movie anyway.
 */
function start_movie_in_wmp (control)
{
    var mov = make_absolute(control.href);
    var wnd = document.getElementById('RI');

    if (mov && wnd)
    {
        var newHTML = "<OBJECT id='WMP' width='320' height='240'" +
                        "classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'" +
                        "codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701'" +
                        "standby='Loading Windows Media Player...' type='application/x-oleobject'>" +
                        "<param name='FileName' value='" + mov + "' />" +
                        "<param name='animationatStart' value='true' />" +
                        "<param name='transparentatStart' value='true' />" +
                        "<param name='autoStart' value='true' />" +
                        "<param name='showControls' value='false' />" +
                        "<param name='loop' value='false' />" +
                          "<embed id='MediaPlayer' width='320' heigth='240'" +
                            "type='application/x-mplayer2' src='" + mov + "' name='MediaPlayer'" +
                            "ShowControls='0' ShowStatusBar='0' ShowDisplay='0' autostart='1'>" +
                          "</embed>" +
                      "</OBJECT>";

        control.href = "#WMP";
        wnd.innerHTML = newHTML;
        move_images();
    }
}

/*
 *  Take a linked movie and start it in videolan media player.
 *  The same like above, just with another media player.
 */
function start_movie_in_vlc (control)
{
    var mov = make_absolute(control.href);
    var wnd = document.getElementById('RI');

    if (mov && wnd)
    {
        var newHTML = "<OBJECT id='VLC' width='320' height='240'" +
                        "classid='clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921'" +
                        "codebase='http://downloads.videolan.org/pub/videolan/vlc/latest/win32/axvlc.cab'" +
                        "events='True' type='application/x-oleobject'>" +
                        "<param name='Src' value='" + mov + "' />" +
                        "<param name='ShowDisplay' value='True' />" +
                        "<param name='AutoLoop' value='False' />" +
                        "<param name='AutoPlay' value='True' />" +
                        "<embed id='MediaPlayer' name='MediaPlayer' width='320' heigth='240'" +
                          "type='application/x-vlc-plugin' target='" + mov + "' name='MediaPlayer'" +
                          "autoplay='yes' loop='no' hidden='no'>" +
                        "</embed>" +
                      "</OBJECT>";

        control.href = "#VLC";
        wnd.innerHTML = newHTML;            
    }
}

/*
 *  We inform our poor Internet Explorer users that we need javascript
 *  enabled.  If it is running, this function is called which disables
 *  the warning.
 */
function no_ie_warning()
{
    if (document.getElementById)
    {
        var ie_warning = document.getElementById('ie-warning');

        if (ie_warning)
            ie_warning.style.display = 'none';
    }
}

/*
 *  Uiii, for this I will be shot by critics!
 *
 *  I want to use the left and right arrow keys for page navigation.
 *  On the PC platform keycode 37 and 39 are bound to these keys, but
 *  for other platforms I doubt that that will work.  Even worse, maybe
 *  I have taken away important keys from such victims.  If you got
 *  bitten by this, please inform us by mail: salzl (at) salzl.at!
 */
document.onkeydown = function (event)
{
    if (!event)
        event = window.event;
    if (event.keyCode == 37)
    {
        var prev = document.getElementsByName('prev');

        if (prev.length == 1)
            location.href = prev[0].href;
    }
    else if (event.keyCode == 39)
    {
        var next = document.getElementsByName('next');

        if (next.length == 1)
            location.href = next[0].href;
    }
}

/*
 *  Ok, at last add the functionality to the web page linking this script.
 */
window.onload = function()
{
    no_ie_warning();
    move_images();
    correct_pngs();
    sanitize_menus();
    position_content();
}
