/**
 * @author dwrs.eu
 */

var newdiv = null;

var dwrsresmodule = {
    ShowReservationWindow: function(objectid) {
        var vscrollval = 0;
        var y = 0;
        if (self.pageYOffset) // all except Explorer
        {
            y = self.pageYOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop)
        // Explorer 6 Strict
        {
            y = document.documentElement.scrollTop;
        }

        vscrollval = y;
        newdiv = new documentOverlay();
        newdiv.css({
            background: 'black',
            opacity: 0.6,
            filter: 'alpha(opacity=60)'
        }).init();


        var main = document.getElementById('resmodulepanel');

        var containerposx = ((document.body.clientWidth / 2) - 400);


        var container = document.createElement('div');
        container.id = "dwrscontainer";
        container.style.position = "absolute";
        container.style.left = containerposx + "px";
        container.style.top = (vscrollval + 5) + "px";
        container.style.backgroundColor = "#000000";
        container.style.width = "800px";
        container.style.overflow = "hidden";
        container.style.height = "610px";
        //container.style.height = this.getWinHeight() - 5 + "px";
        container.style.border = "1px solid #9cdae5";
        container.style.zIndex = "1000";

        var header = document.createElement('div');
        header.align = "right";
        //header.innerHTML = "<a href='javascript:dwrsresmodule.CloseWindow();'><img src='http://www.system.dwrs.eu/elements/externals/closebutton.png' border='0px' /></a>";

        var iframe = document.createElement('div');
        iframe.align = "left";
        iframe.innerHTML = "<div style='position:absolute;left:783px'><a href='javascript:dwrsresmodule.CloseWindow();'><img src='http://www.system.dwrs.eu/elements/externals/closebutton.png' border='0px' /></a></div><iframe id=\"frmMain\" src=\"http://www.system.dwrs.eu/resmodule/" + objectid + dwrsresmodule.Brokerid + "\" name=\"frmMain\" frameborder=\"0\" height=\"" + (this.getWinHeight() - 10) + "px\" width=\"800px\" scrolling=\"no\"></iframe>";


        container.appendChild(header);
        container.appendChild(iframe);

        main.appendChild(container);
    },
    CloseWindow: function() {
        var main = document.getElementById('resmodulepanel');
        newdiv.remove();
        var e = document.getElementById('dwrscontainer');
        main.removeChild(e);
    },
    getWinHeight: function() {
        return window.innerHeight ||
                (document.compatMode == "CSS1Compat" && document.documentElement.clientHeight || document.body.clientHeight);
    },
    Brokerid: ""

}

function documentOverlay() {
    // @version 0.12
    // @author James Padolsey
    // @info http://james.padolsey.com/javascript/the-perfect-document-overlay/

    // Shortcut to current instance of object:
    var instance = this,

    // Cached body height:
    bodyHeight = (function() {
        return getDocDim('Height', 'min');
    })();

    // CSS helper function:
    function css(el, o) {
        for (var i in o) { el.style[i] = o[i]; }
        return el;
    };

    // Document height/width getter:
    function getDocDim(prop, m) {
        m = m || 'max';
        return Math[m](
            Math[m](document.body["scroll" + prop], document.documentElement["scroll" + prop]),
            Math[m](document.body["offset" + prop], document.documentElement["offset" + prop]),
            Math[m](document.body["client" + prop], document.documentElement["client" + prop])
	);
    }

    // get window height: (viewport):
    function getWinHeight() {
        return window.innerHeight ||
                (document.compatMode == "CSS1Compat" && document.documentElement.clientHeight || document.body.clientHeight);
    }

    // Public properties:

    // Expose CSS helper, for public usage:
    this.css = function(o) {
        css(instance.element, o);
        return instance;
    };

    // The default duration is infinity:
    this.duration = null;

    // Creates and styles new div element:
    this.element = (function() {
        return css(document.createElement('div'), {
            width: '100%',
            height: getDocDim('Height') + 'px',
            position: 'absolute', zIndex: 999,
            left: 0, top: 0,
            cursor: 'arrow'
        });
    })();

    // Resize cover when window is resized:
    window.onresize = function() {

        // No need to do anything if document['body'] is taller than viewport
        if (bodyHeight > getWinHeight()) { return; }

        // We need to hide it before showing
        // it again, due to scrollbar issue.
        instance.css({ display: 'none' });
        setTimeout(function() {
            instance.css({
                height: getDocDim('Height') + 'px',
                display: 'block'
            });
        }, 10);

    };

    // Remove the element:
    this.remove = function() {
        this.element.parentNode && this.element.parentNode.removeChild(instance.element);
    };

    // Show element:
    this.show = function() { };

    // Event handling helper:
    this.on = function(what, handler) {
        what.toLowerCase() === 'show' ? (instance.show = handler)
        : instance.element['on' + what] = handler;
        return instance;
    };

    // Begin:
    this.init = function(duration) {

        // Overwrite duration if parameter is supplied:
        instance.duration = duration || instance.duration;

        // Inject overlay element into DOM:
        document.getElementsByTagName('body')[0].appendChild(instance.element);

        // Run show() (by default, an empty function):
        instance.show.call(instance.element, instance);

        // If a duration is supplied then remove element after
        // the specified amount of time:
        instance.duration && setTimeout(function() { instance.remove(); }, instance.duration);

        // Return instance, for reference:
        return instance;

    };

}
