/**
 * Sets initial style for presentation objects:
 * - Hides the container (with the object and close button
 * - Sets the overlay dimensions and places it on top of window
 */
function setRegionPresentationStyle()
{
    var width = (document.documentElement.clientWidth > 998) ? 998 : document.documentElement.clientWidth;

    document.getElementById('presentation-container').style.display = 'none';
    document.getElementById('presentation-overlay').style.width = width + 'px';
    document.getElementById('presentation-overlay').style.height = document.documentElement.clientHeight + 'px';
    document.getElementById('presentation-overlay').style.top = '-100%';
    document.getElementById('presentation-overlay').style.display = 'block';
}

/**
 * Shows the overlay and loads the region presentation
 * Redirects Firefox 1.0 users due to fact swfobject doesn't work properly in this browser
 */
function showRegionPresentation(file)
{
    // explicitly redirect firefox 1.0 users, since they have a problem displaying flash objects with swfobject
    if (navigator.userAgent.indexOf('Firefox') != -1) {
        var version = navigator.userAgent.indexOf("Firefox") + 8;
        if (parseInt(navigator.userAgent.charAt(version)) <= 1) {
            return true;
        }
    }

    showPresentationOverlay(file);
    return false;
}

/**
 * Slides in the overlay
 */
function showPresentationOverlay(file)
{
    // set html node's overlay to hidden, so we can't scroll anymore
    var html = document.documentElement;
    html.style.overflow = 'hidden';

    var overlay = document.getElementById('presentation-overlay');

    if (overlay){

        // calculate top position
        var top = parseInt(overlay.style.top);

        // make sure it is a number
        if (isNaN(top)) top = -100;

        if (top < 0){

            // calculate difference in positions, times the easing factor
            var dy = parseInt((0 - top)*0.2);

            // make sure movement doesn't go below 1 (otherwise we could be trapped in this loop forever)
            if (dy < 1) dy = 1;

            // set the top style
            overlay.style.top = (top + dy) + "%";

            // call this function again
            setTimeout(function() { showPresentationOverlay(file); }, .0001);
        } else {
            // overlay is positioned, load the presentation
            loadRegionPresentation(file);
        }
    }
}

/**
 * Slides out the overlay
 */
function hidePresentationOverlay()
{
    var overlay = document.getElementById('presentation-overlay');

    if (overlay){

        // calculate top position
        var top = parseInt(overlay.style.top);

        // make sure it is a number
        if (isNaN(top)) top = 0;

        if (top > -100){

            // calculate difference in positions, times the easing factor
            var dy = parseInt((-100 - top)*0.2);

            // make sure movement doesn't go below 1 (otherwise we could be trapped in this loop forever)
            if (dy > -1) dy = -1;

            // set the top style
            overlay.style.top = (top + dy) + "%";

            // call this function again
            setTimeout(function() { hidePresentationOverlay(); }, .0001);
        } else {

            // let there be scrolling once again
            var html = document.documentElement;
            html.style.overflow = 'auto';
        }
    }
}

/**
 * Loads a swf object with the presentation
 * Ideally, an object tag with embed tag inside it would be best, but somehow IE doesn't like appending embed tags to object tags
 */
function loadRegionPresentation(file)
{
    document.getElementById('presentation-container').style.display = 'block';
    swfobject.createSWF({'data':'/swf/regiopresentatie.swf', 'width':'700', 'height':'550'}, {'flashvars':'xml='+file, 'wmode':'transparent','menu':'false'}, document.getElementById('presentation-object').appendChild(document.createElement('div')));
}

/**
 * Closes presentation and removes the overlay
 */
function closePresentation()
{
    // remove flash object
    var flashObj = document.getElementById('regiopresentatie');
    if (flashObj) {
        flashObj.parentNode.removeChild(flashObj);
    }

    // hide overlay
    hidePresentationOverlay();

    // hide button
    document.getElementById('presentation-container').style.display = 'none';
}
