if (typeof discover === 'undefined') {
    var discover = {};
}

discover.bank = discover.bank || {};

/**
 * Discover Bank Calculator Print Utility
 *
 * This script enables SWF calculators to be printed easily. This is needed 
 * because all modern browsers (sans IE, oddly enough) don't support printing
 * of embedded SWFs.
 * 
 * @author Michael Girouard (mgirouard@mcdpartners.com)
 * @namespace discover.bank.calculators
 * @package discover.bank
 */
discover.bank.calculators = (function () {
    var util, app, service;
    
    util = {
        createPrintableImage : function () {
            if (!app.allowImageProcessing) return;

            var reg = app.registry;

            reg.container = document.getElementById(reg.container);
            reg.swf       = document.getElementById(reg.swf);
            reg.img       = document.createElement('img');

            reg.container.appendChild(reg.img);
        },
        
        setPrintableImageSource : function (source) {
            if (!app.allowImageProcessing) return;
            
            app.registry.img.src = 'data:image/png;base64,' + source;
        }
    };
    
    app = {
        
        /** Element Registry */
        registry : {
            img       : null,
            swf       : 'calc',
            container : 'calc-container'
        },
        
        /** Initialization flag */
        initialized : false,
        
        /** Weather or not to allow image processing */
        allowImageProcessing : false,
        
        /** 
         * Image Updater
         *
         * This accepts a base64 encoded PNG string and updates the printable 
         * image's SRC attribute with it. 
         * 
         * There's probably no real reason why this would need to be called 
         * directly; the SWF will call this when the "Print" button is clicked.
         * 
         * @param {String} pngString
         */
        updateImage : function (pngString) {
            // Make sure we're loaded first
            if (!app.initialized) app.init();
            
            util.setPrintableImageSource(pngString);
            window.print();
        },
        
        /** 
         * Initialization routine 
         * 
         * Sets up the registry and injects the printable image in the document.
         */
        init : function () {
            // IE doesn't have any printing issues. 
            // http://msdn.microsoft.com/en-us/library/8ka90k2e(VS.85).aspx
            app.allowImageProcessing = (/*@cc_on!@*/true);
            
            util.createPrintableImage();
            app.initialized = true;
        }
    };
    
    /** The exposed service */
    return service = {
        init        : app.init,
        updateImage : app.updateImage,
        app : app
    };
})();
