/**
 * Object for controlling flash movie.
 */
window.FlashControl = (function() {
    
    /**
     * Private static members.
     */
    var defaultMinWidth  = 1026,
        defaultMinHeight = 550,
        minWidth         = defaultMinWidth,
        minHeight        = defaultMinHeight,
        env,
        flashId;
    
    /**
     * Public static members.
     */
    
    /**
     * Get an element by id.
     *
     * param string id
     * return element|null
     */
    function getElement(id)
    {
        var d = document;
        
        if (d.getElementById) {
            return d.getElementById(id);
        } else if (d.all) {
            return d.all[id];
        } else if (d.layers) {
            return d.layers[id];
        } else {
            return null;
        }
    }
    
    /**
     * Add a DOM event listener/handler.
     */
    function addEvent(element, type, eventHandle)
    {
        if (element === null || element === undefined) {
            return;
        }
        
        if (element.addEventListener) {
            element.addEventListener(type, eventHandle, false);
        } else if (element.attachEvent) {
            element.attachEvent( "on" + type, eventHandle );
        }
    }
    
    /**
     * Update Flash movie's available area.  Flash movie must constrain to
     * minimum bounds, and it must take up all available screen width and
     * height.
     */
    function onResize()
    {
        var flash, availW, availH, newW, newH;
        
        flash = getElement(flashId);
        if (flash === null) {
            throw new Error("Could not get flash movie element");
        }
        
        if (document.all) {
            // IE way
            availW = document.documentElement.clientWidth;
            availH = document.documentElement.clientHeight;
        } else {
            // W3C way
            availW = window.innerWidth;
            availH = window.innerHeight;
        }
        
        // calculate and constrain to minimums
        newW = availW;
        if (newW < minWidth) {
            newW = minWidth;
        }
        newH = availH;
        if (newH < minHeight) {
            newH = minHeight;
        }
        
        flash.style.width  = newW.toString() + "px";
        flash.style.height = newH.toString() + "px";
    }
    
    /**
     * Setup additional handlers after flash embed completes.
     */
    function onEmbedComplete(e)
    {
        if (e.success) {
            addEvent(window, "resize", onResize);
            onResize();
        }
    }
    
    /**
     * Constructor
     */
    function flashControl() { }
    
    flashControl.getMinWidth = function()
    {
        return minWidth;
    };
    
    flashControl.setMinWidth = function(value)
    {
        minWidth = (Number(value) >= 0) ? Number(value) : defaultMinWidth;
        return this;
    };
    
    flashControl.getMinHeight = function()
    {
        return minHeight;
    };
    
    flashControl.setMinHeight = function(value)
    {
        minHeight = (Number(value) >= 0) ? Number(value) : defaultMinHeight;
        return this;
    };
    
    /**
     * Environment flash is running in.  Acceptable values are: development,
     * staging or production.
     */
    flashControl.setEnvironment = function(value)
    {
        if (value === "development" ||
            value === "staging" ||
            value === "production"
        ) {
            env = value;
        } else {
            throw new Error("Attempted to set invalid environment, must be one of: development, staging or production");
        }
        return this;
    };
    
    flashControl.getEnvironment = function() { return env; };
    
    flashControl.embedSWF = function(
        swfUrlStr,
        replaceElemIdStr,
        swfVersionStr,
        xiSwfUrlStr,
        flashvarsObj,
        parObj,
        attObj
    ) {
        flashId = attObj.id;

        swfobject.embedSWF(
            swfUrlStr,
            replaceElemIdStr,
            minWidth,
            minHeight,
            swfVersionStr,
            xiSwfUrlStr,
            flashvarsObj,
            parObj,
            attObj,
            onEmbedComplete
        );
        
        return this;
    };
    
    return flashControl;
})();
