/**
 * Copyright (c) 2001-2009 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.downloadMessage = "Make sure you trust the sender before</br> continuing the download.";

    //determine if we are using the iframe
    this.useiframe = 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_mhts'] = wgBrowser.readCookie("MAIL_mhts");
    swfConfig['MAIL_ppvs'] = wgBrowser.readCookie("MAIL_ppvs");
    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:\'\'"></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"></iframe>');
        }
    }

    // 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
        if (disp == "inline" ) {
            if (wgBrowser.isie) {
                url = "includes/browserdownload.html?" + escape(url);
            }
            newWin = window.open(url, "_blank", 
                    "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
                wgBrowser.getFrameByName('downloadFrame').location.href = 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";

            // make query
            dlframe.src = "13730/download.html";
        }
    }

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

