/**
 * Copyright (c) 2001-2010 Laszlo Systems, Inc.
 * All Rights Reserved.
 *
 * @author lhenrywilkins
 * @author John Olmstead
 * 
 * This is all the javascript that is specific to the mail app. If your webtop
 * includes the mail app you need to include this javascript. This file defines
 * the Mail class. To use it create an instance of the class, and register the
 * init() method with the window's onload event.
 */
function Mail() {
    //config for attachment popup for inline viewing
    this.popupWidth  = 600;
    this.popupHeight = 400;
    this.popupTop    = 100;
    this.popupLeft   = 100;
    this.popDownloadText = "Download";
    this.popCancelText = "Cancel";
    this.downloadMessage = "Make sure you trust the sender before</br> continuing the download.";

    //determine if we are using the iframe
    this.useiframe = false;
    //The flag shows if the download iframe is ready for a new downloading url
    //(In IE it will be set to true after opening the download iframe as a dialog)
    this.ignoreDownloadIFrameOnload = false;

    if(wgBrowser.iswin) {
        if(wgBrowser.isff || wgBrowser.isie || wgBrowser.isopera || wgBrowser.isnetscape) {
            this.useiframe = true;
        }
    }
    if(wgBrowser.ismac) {
        if(wgBrowser.isff || wgBrowser.issafari) this.useiframe = true;
    }

    //if we are not in IE we can't use the iframe in non-us languages
    if (this.useiframe && !wgBrowser.isie &&
            navigator.language.toLowerCase() != "en-us" &&
            navigator.language.toLowerCase() != "en") {
        this.useiframe = false;
    }

    swfConfig['MAIL_useiframe'] = this.useiframe;

    this.init = function() {
        window.downloadFrame = wgBrowser.getFrameByName('downloadFrame');
        window.downloadUIFrame = wgBrowser.getFrameByName('downloadUIFrame');
    }

    //writes out print and download frames
    //uses document.write
    this.writeHiddenMailFrames = function() {
        document.write('<iframe id="downloadFrame" name="downloadFrame" class="hidden" src="javascript:\'\'" onload="wgMail.downloadIframeLoaded()"></iframe>');
        // add download UI frame
        if (wgBrowser.isie) {
            document.write('<style type="text/css">#downloadUIFrame {position:absolute;width:0px;height:0px;border-width:0px;}</style>')
            document.write('<iframe id="downloadUIFrame" name="downloadUIFrame" src="javascript:\'\'" frameborder="no" scrolling="no" marginwidth="0" marginheight="0" onload="wgMail.downloadIframeLoaded()"></iframe>');
        }
        
    }
        
    this.downloadIframeLoaded = function(){
        //Fix EM-14884. Webtop need some response if failed to download.
        //This function is called when download iframe onload, because 
        //if the download fails there will be an error page loaded in the 
        //iframe, and the onload will be triggered.
        if (!this.ignoreDownloadIFrameOnload) {
            Lz.setCanvasAttribute('js_downloadError', "downloaderror");            
        } else {
            this.ignoreDownloadIFrameOnload = false;
        }
    }

    // downloadFile() from the laszlo application when an attachment is clicked
    // @param string url - url of 
    // @param string disp - disposition [attachment/inline]
    this.downloadFile = function(url, disp, subtype, extension, attachinfo, popupY, popupMiddleX) {
        if(disp == null) disp = "";
        if(subtype == null) subtype = "";
        if(extension == null) extension = "";
        if(attachinfo == null) attachinfo = "";
        popupY = (popupY == null) ? 0 : parseInt(popupY);
        popupMiddleX = (popupMiddleX == null) ?  0 : parseInt(popupMiddleX);

        var mt = navigator.mimeTypes;
        if(!wgBrowser.isff && mt && mt.length ) {
            // check for installed plugins here and inline any handled types
            var type; var mimetype; var suffixes; var suffix;
            for(type in mt) {
                if(mt[type].type) {
                    mimetype = mt[type].type;
                    suffixes = mt[type].suffixes;
                    if(suffixes.indexOf(extension) != -1 || mimetype.indexOf(subtype) != -1){
                        disp = "inline";
                    }
                }
            }
        } 
        // if content can be handled inline, open a new window otherwise load the attachment
        // the "inline" condition is not handling well when download url is invalid. so just add "false" 
        // as a quick work around here.
        if (false && disp == "inline") {
            if(wgBrowser.isie) {
                url = "includes/browserdownload.html?" + escape(url);
            }
            newWin = window.open(url, "_top", 
                    "width=" + this.popupWidth + 
                    ",height=" + this.popupHeight + 
                    ",top=" + this.popupTop + 
                    ",left" + this.popupLeft+ 
                    ",toolbar=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes");
        } else {
            // don't use download frame approach if we're in IE
            // instead, populate a click-through frame
            if(!wgBrowser.isie || wgWebtop.isDHTML) {
                // handle download in non-ie browsers
                // fix EM-14884. iframe.location.href doesn't work well in Safari.
                wgBrowser.getFrameByName('downloadFrame').location = url;
                return;
            }

            var dlframewidth = 312;
            var dlframeheight = "103";
            var defaultOffsetY = 82;

            window.top.dlurl = url;
            window.top.attachinfo = attachinfo;
            var dlframe = top.document.getElementById('downloadUIFrame');

            // size frame - size matches size of background image
            dlframe.style.width= dlframewidth + "px";
            dlframe.style.height= dlframeheight;

            var headerHeight = 0;
            if(document.getElementById('headerCell')) {
                headerHeight = document.getElementById('headerCell').parentNode.height;
            }

            //position frame in center, just under the webtop window top bar
            //y offset is the master header height + popupY given in the
            //argument of the method
            var totalOffsetY = parseInt(headerHeight) + popupY;
            if (totalOffsetY <= 0) totalOffsetY = defaultOffsetY;

            // for x offset, it is the given middle-x given in the argument, minus 1/2 width of the frame
            var totalOffsetX = (top.document.body.offsetWidth-316)/2;

            if (popupMiddleX != 0){
                totalOffsetX = popupMiddleX - Math.floor(dlframewidth/2);
            }

            dlframe.style.top = totalOffsetY + "px";
            dlframe.style.left= totalOffsetX + "px";

            this.ignoreDownloadIFrameOnload = true;
            // make query
            dlframe.src = "15585/download.html";
        }
    }

    this.openComposeWindow = function(emailAddress) {
        Lz.setCanvasAttribute('js_mailto', emailAddress);
    }
}

