/** Copyright (c) 2001-2009 Laszlo Systems, Inc. All Rights Reserved.
 * 
 * @author lhenrywilkins
 */
function Webtop() {
    //error msg that should be used in case of a javascript error
    this.initError = "The application encountered an error and some " +
    "functionality may not work. Please report the problem to your help " +
    "desk or system administrator. Error code: ";

    this.originalPageTitle = document.title;
    this.isDHTML = false;

    //used by resizer
    this.lastWidthUnder;
    this.lastHeightUnder;

    //just default values, these should be set from server configuration
    this.minWidth = 300;
    this.minHeight = 300;
    this.preferredLocale;

    //reference to the div that contains the wrapper table. Used to detect
    //when to apply scollbars
    this.container = null;

    //attach init to onload
    this.init = function(isDHTML) {
        this.isDHTML = isDHTML;
        this.container = document.getElementById("container");
        
        //browsers except IE support min-width and min-height CSS
        if (wgBrowser.isie) {
            var me = this;
            window.onresize = function() {
                me.handleResize();
            }
        } else {
            this.container.style.minWidth = this.minWidth;
            this.container.style.minHeight = this.minHeight;
        }
        
        //To minimize a platform issue in FF that causes the browser to get
        //confused whether the swf or the browser has focus.
        //Capture backspace keypresses outside of the swf so they don't
        //navigate the user back
        if (!this.isDHTML) window.document.onkeydown = this.captureBackspace;
        
        //perform resize to initialize lastWidthUnder and lastHeightUnder
        if (wgBrowser.isie) this.handleResize();
    }

    //capture backspace keypresses outside of the swf so they don't navigate the user back
    //attach this as a handler to the document.onkeydown event (window.document.onkeydown = captureBackspace;)
    this.captureBackspace = function(e) {
        // Getting this.parentWindow.event instead of window.event so this handler works when attached
        // to the iframe.document as well as the window.document
        // since this handler forms a closure "window" will always be the window of the parent page
        // and not the iframe. this.parentWindow will resolve to the window of whatever document
        // the event is attached to.
        if (!e) e = this.parentWindow.event;
        var k = e['keyCode'];
        if (k >= 0) {
            if (k == 8) {
                this.returnFocus();
                e.cancelBubble = true;
                e.returnValue = false;
                return false;
            }
        }
    }

    //Apply scrollbars in IE when window is sized down under min width and height
    //Other browsers support min-width and min-height in CSS
    this.handleResize = function() {
        var minW = this.minWidth;
        var minH = this.minHeight;
        if (document.body.offsetWidth <= minW && !this.lastWidthUnder) {
            this.lastWidthUnder = true;
            this.container.style.width = minW + 'px';
        } else if (document.body.offsetWidth > minW && this.lastWidthUnder) {
            this.lastWidthUnder = false;
            this.container.style.width = "100%";
        }
        
        if (document.body.offsetHeight <= minH && !this.lastHeightUnder) {
            this.lastHeightUnder = true;
            this.container.style.height = minH + 'px';
        } else if (document.body.offsetHeight > minH && this.lastHeightUnder) {
            this.lastHeightUnder = false;
            this.container.style.height = "100%";
        }
    }

    // Dynamical update min width
    this.updateMinWidth = function(newWidth) {
        var container = this.container ? this.container
                                       : document.getElementById("container");

        if (this.minWidth != newWidth) {
            this.minWidth = newWidth;
            container.style.minWidth = newWidth + 'px';
        }

        if (wgBrowser.isie) {
            this.lastWidthUnder = false;
            this.handleResize();
        }
    }

    //return focus to the <embed> or to an attachment window if one is on top
    this.returnFocus = function() {
        if (this.isDHTML) return;
        var theSwf = document.getElementsByTagName('object')[0];
        if (!theSwf) theSwf = document.getElementsByTagName('embed')[0];
        if (theSwf) theSwf.focus();
        this.forceIframesVisible();
    }

    //call this after the swf takes focus to make the iframe visible above it again
    this.forceIframesVisible = function() {
        //we only need to do this in IE
        if (!wgBrowser.isie) return;
        for (var id in lz.embed.iframemanager.__frames) {
            var iframe = lz.embed.iframemanager.__frames[id];
            if (iframe) {
                //this forces the iframe to be shown if it should be visible
                if (iframe.style.display == 'block') {
                    iframe.style.display = 'none';
                    iframe.style.display = 'block';
                }
            }
        }
    }

    //call this function to reload an adFrame.
    this.reloadAdFrame = function(adFrame) {
        adFrame.location.reload();
    }

    //set the title of the page, title will be a concatenation of preTitle, 
    //browserTitle, postTitle where browserTitle is the original title of the page
    //stored when the app is initialized
    this.setTitle = function(preTitle, postTitle) {
        if (preTitle == null || preTitle == undefined) {
            preTitle = "";
        }
        if (postTitle == null || postTitle == undefined) {
            postTitle = "";
        }
        var newTitle = preTitle + this.originalPageTitle + postTitle;
        document.title = newTitle;
    }

    this.writeApp = function(swfConfig, lzr, debug, backtrace, profile, baseUrl) {
        if (swfConfig['locale']) {
            //baseUrl = "main-" + swfConfig.locale + ".lzr=swf8.swf";
        }

        // build the app url
        var lzt = lzr == 'dhtml' ? 'object' : 'swf';

        var wmode = 'opaque';
        // in IE the wmode must be window or mouse events won't get to the swf
        if (wgBrowser.isie || !wgMail['useiframe']) wmode = 'window';

        var appUrl = baseUrl +
        "?lzt=" + lzt +
        "&lzr=" + lzr +
        "&debug=" + debug +
        "&lzbacktrace=" + backtrace +
        "&profile=" + profile;

        // add config parameters 
        for (var k in swfConfig) {
            var value = swfConfig[k];
            appUrl += "&lzmc_" + typeof(value) + "_" + k + "=" + escape(value);
        }

        //pass in the wrapper url to be used in reload
        appUrl += '&lzmc_string_reloadurl=' + escape(window.top.location.href);

        if (lzr == 'dhtml') {
            lz.embed.dhtml({
                url: appUrl,
                bgcolor: '#99c4e3',
                width: '100%',
                height: '100%',
                id: 'lzapp',
                appenddivid: 'appDiv'
            });
        } else {
            var lzCanvasRuntimeVersion = 8 * 1;
            if (lz.embed.dojo.info.version == 9.16) {
                //Note this is fragile. This relies on the fact that the dojo.flash 
                //module in the platform does a string comparison between the 
                //current version and the version specified. This is why in this 
                //particular case we are setting lzCanvasRuntimeVersion to a string
                //rather than an int.
                lzCanvasRuntimeVersion = '9.0.124';
            }

            var args = {
                url: appUrl,
                bgcolor: '#898989',
                width: '100%',
                height: '100%',
                id: 'lzapp',
                accessible: 'false',
                wmode: wmode,
                appenddivid: 'appDiv'
            };
            lz.embed.swf(args, lzCanvasRuntimeVersion);
        }
    }
}

