/**
 * FLASH GORDON
 * Version 1.1.1
 * hr, mf, ma
 *
 * Usage:
 * <div id="altElementID">
 *      <a href="http://www.adobe.com/go/getflashplayer">
 *           <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" />
 *       </a>
 * </div>
 * <script type="text/javascript" src="FlashGordon.js"></script>
 * <script type="text/javascript">new FlashGordon("myflash.swf", "flashDomID", 400, 468, 7, "altElementID").write();</script>
 */

/**
 * FlashGordon constructor
 * @param src path to .swf flash file
 * @param id the id for the object and embed tag to access the elements later
 * @param width 
 * @param height
 * @param reqVersion the required flash version
 * @param DOM node, string 'id' for an alternative block element which will be hidden if flash works
 *        or string 'URL' an URL to redirect to
 * @param urlParameter url parameter for the flash (ex: show=news), added to src attribute without "?"
 */
function FlashGordon(src, id, width, height, reqVersion, alternative, urlParameter) {
    this.str = "";
    this.src = src;
    this.id = id;
    this.width = width;
    this.height = height;
    this.reqVersion = reqVersion;
    this.alternative = document.getElementById(alternative) ? document.getElementById(alternative) : alternative;
    this.urlParameter = urlParameter;
}

FlashGordon.prototype = {
    
    /**
     * write the flash object/embed tag
     * @param bgcolor Background color for flash object
     * @wmode window mode 'window', 'opaque', or 'transparent'(default). 
     * @see http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_14201
     * @public
     */
    write: function(bgcolor, wmode) {
        var flashVersion = this.getFlashVersion();

        if (flashVersion >= this.reqVersion) {
            bgcolor = bgcolor ? bgcolor : "#fff";
            wmode = wmode ? wmode : "transparent";
            this.str += "<object";
            this.writeParameter("classid","clsid:d27cdb6e-ae6d-11cf-96b8-444553540000");
            this.writeParameter("codebase",document.location.protocol+"//fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab");
            this.writeParameter("width",this.width);
            this.writeParameter("height",this.height);
            this.writeParameter("id",this.id);
            this.str += ">";
            this.writeObjectParameter("allowScriptAccess","sameDomain");
            this.writeObjectParameter("movie",this.src);
            this.writeObjectParameter("quality","high");
            this.writeObjectParameter("wmode",wmode);
            if (this.urlParameter)
            this.writeObjectParameter("FlashVars","&"+this.urlParameter);
            this.writeObjectParameter("bgcolor",bgcolor);
            this.writeObjectParameter("allowScriptAccess","sameDomain");
            this.str += "<embed"
            this.writeParameter("src",this.src+((this.urlParameter) ? "?"+this.urlParameter : ""));
            this.writeParameter("quality","high");
            this.writeParameter("wmode",wmode);
            this.writeParameter("bgcolor",bgcolor);
            this.writeParameter("width",this.width);
            this.writeParameter("height",this.height);
            this.writeParameter("name",this.id);
            this.writeParameter("allowScriptAccess","sameDomain");
            this.writeParameter("type","application/x-shockwave-flash");
            this.writeParameter("pluginspage","http://www.adobe.com/go/getflashplayer");
            this.str += " /></object>";
            document.write(this.str);
            
            this.checkAlternative("none");
        } else {
            this.checkAlternative("block");
        }
    },
    
    checkAlternative: function(display) {
        if (typeof this.alternative == "object") {
            this.alternative.style.display = display;
        } else if ((typeof this.alternative == "string") && (display == "block")) {
            location.href = this.alternative;
        }
    },
    
    writeObjectParameter: function(param, value) {
        this.str += "<param name=\"" + param + "\" value=\"" + value + "\"/>";
    },
   
    writeParameter: function(param, value) {
        this.str += " " + param + "=\"" + value + "\"";
    },

    getFlashVersion: function() {
        var flashVersion = 0;
        var agent = navigator.userAgent.toLowerCase();
        if (agent.indexOf("mozilla/3") != -1 && agent.indexOf("msie") == -1) {
            return flashVersion;
        }
        if (navigator.plugins != null && navigator.plugins.length > 0) {
            var flashPlugin = navigator.plugins["Shockwave Flash"]; 
            if (typeof flashPlugin == "object") {
                for (i = 25; i > 0; i--) {
                    if (flashPlugin.description.indexOf(i + ".") != -1) {
                        flashVersion = i;
                    }
                }
            }
        } else if ((agent.indexOf("msie") != -1) && (parseInt(navigator.appVersion) >= 4) && (agent.indexOf("win") != -1) && (agent.indexOf("16bit") == -1)) {
            flashVersion = this.getIEFlashVersion();
        }
        return flashVersion;
    },
   
    getIEFlashVersion: function() {
        var swfObj, flashVersion;
        for (i=0; i<25; i++) {
            try {
                swfObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
            } catch(e) {}
                if (swfObj) {
                flashVersion = swfObj.GetVariable("$version").split(" ")[1].split(",")[0];
            }
        }
        return (flashVersion);
    }
}
