(function() {
    function TVGuideWeb() {
        var iframe;
        var wrapper;
        var closeButton;
        var standardWidth, standardHeight, standardBodyOverflow;
        var placeholder;

        this.include = function(options) {
            if (!options.placeholderId) {
                alert("The TVGuideWeb javascript requires an ID where it can insert content!");
                return;
            }

            if (options.width) {
                iframe.width = options.width;
            } else {
                iframe.width = '913';
            }

            if (options.height) {
                iframe.height = options.height;
            } else {
                iframe.height = '600';
            }

            if (options.url) {
                iframe.src = options.url;
            }

            wrapper.appendChild(iframe);

            placeholder = document.getElementById(options.placeholderId);
            placeholder.appendChild(wrapper);
        };

        this.showFullscreen = function() {
            placeholder.removeChild(wrapper);

            standardWidth = iframe.width;
            standardHeight = iframe.height;
            standardBodyOverflow = document.body.style.overflow;

            document.body.style.overflow = 'hidden';
            iframe.width = '100%';
            iframe.height = '100%';

            wrapper.style.width = '100%';
            wrapper.style.height = '99.6%';
            wrapper.style.position = 'absolute';
            wrapper.style.top = 0;
            wrapper.style.bottom = 0;
            wrapper.style.left = 0;
            wrapper.style.right = 0;
            wrapper.style.zIndex = 99999;

            closeButton = document.createElement('img');
            closeButton.src = "http://devices.get.no/devices/tvguideweb/lib/global/gfx/close_button.png";
            closeButton.style.position = 'absolute';
            closeButton.style.right = '0px';
            closeButton.style.top = '0px';
            closeButton.style.cursor = 'pointer';
            closeButton.style.height = '40px';
            closeButton.alt = 'Lukk fullskjerm';
            closeButton.title = 'Lukk fullskjerm';
            closeButton.style.width = '40px';
            closeButton.onclick = resignFullscreen;
            wrapper.appendChild(closeButton);
            document.body.appendChild(wrapper);
        };

        function resignFullscreen() {
            document.body.removeChild(wrapper);
            wrapper.removeChild(closeButton);

            document.body.style.overflow = standardBodyOverflow;
            iframe.width = standardWidth;
            iframe.height = standardHeight;

            wrapper.style.position = '';
            wrapper.style.width = '';
            wrapper.style.height = '';
            wrapper.style.top = '';
            wrapper.style.bottom = '';
            wrapper.style.left = '';
            wrapper.style.right = '';
            wrapper.style.zIndex = '';
            placeholder.appendChild(wrapper);
        }

        function init() {
            wrapper = document.createElement('div');
            wrapper.id = 'get-tvguide-wrapper';
            iframe = document.createElement('IFRAME');
            iframe.src = 'http://devices.get.no/devices/tvguideweb/tvguide.html';
            iframe.frameBorder = '0';
        }

        init();
    }

    window.get = window.get || {};
    window.get.tvGuideWeb = new TVGuideWeb();
})();

