/**
    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;
    
    //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() {
        this.container = document.getElementById("container");
        
        //browsers except IE support min-width and min-height CSS
        if (gBrowser.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
        window.document.onkeydown = this.captureBackspace;
        
        //perform resize to initialize lastWidthUnder and lastHeightUnder
        if (gBrowser.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%";
        }
    }
    
    //return focus to the <emebed> or to an attachment window if one is on top
    this.returnFocus = function(){
        var appFrame = gBrowser.getFrameByName("appFrame")
        var theSwf = appFrame.document.getElementsByTagName('object')[0];
        if (!theSwf) theSwf = appFrame.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 (!gBrowser.isie) return;
        for (var id in Lz.iframemanager.__frames) {
            var iframe = Lz.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;
    }
}