/*
 FLASH GORDON
 Version 1.5
 hr
 
 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({"property": "value"});</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;
    this.options = {
        "width": this.width,
        "height": this.height,
        "quality": "high",
        "allowScriptAccess": "sameDomain",
        "movie": this.src,
        "id": this.id,
        "src": this.src,
        "classid": "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000",
        "codebase": document.location.protocol + "//fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab",
        "name": this.id,
        "type": "application/x-shockwave-flash",
        "pluginspage": "http://www.adobe.com/go/getflashplayer",
        "wmode": "window",
		"allowFullScreen": "true",
        "bgcolor": "#fff"
    };
    this.objectIncludes = "id,classid,codebase,width,height";
    this.objectExcludes = "src,name,type,pluginspage";
    this.embedExcludes = "movie,id,classid,codebase";
}

FlashGordon.prototype = {
    /*
     writes the flash object/embed tag
     @param object options for additional movie parameters
     @param string deprecated wmode from old version
     @public
    */
    write: function(options, deprecated) {
        var flashVersion = this.getFlashVersion();

        if (flashVersion >= this.reqVersion) {            

            //urlParameter overwrite
            if (this.urlParameter) {
                this.options.FlashVars = this.urlParameter; //object
                this.options.src = this.src + "?" + this.urlParameter; //embed
            }

            //check options and overwrite default values
            if (options && typeof options === "object") {
                for (var i in options) {
                    if (typeof options[i] !== "undefined") {
                        this.options[i] = options[i];
                    }
                }
            }
            
            if (options && typeof options === "string") {
                this.options.bgcolor = options;
                if (deprecated && typeof deprecated === "string") {
                    this.options.wmode = deprecated;
                }
            }
            
            //write the stuff
            document.write("<object " + this.writeParameters("objectIncludes") + ">" + this.writeParameters("object") + "<embed " + this.writeParameters("embed") + " /></object>");
            
            this.checkAlternative("none");
        } else {
            this.checkAlternative("block");
        }
    },
    
    /*
     show/hide alternative DIV or redirect
     @param string display
     @private
    */
    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;
        }
    },
    
    /*
     returns the flash object/embed params
     @param string type
     @private
    */
    writeParameters: function(type) {
        var value;
        var str = "";
        for (var param in this.options) {
            if (typeof this.options[param] !== "undefined") {
                value = this.options[param];
                
                if (type == "objectIncludes") {
                    if (this.objectIncludes.match(param)) {
                        str += " " + param + "=\"" + value + "\"";
                    }            
                } else {
                    if (!this[type + "Excludes"].match(param)) {
                        if (type == "object") {
                            if (!this.objectIncludes.match(param)) {
                                str += "<param name=\"" + param + "\" value=\"" + value + "\"/>";
                            }
                        } else {
                            str += " " + param + "=\"" + value + "\"";
                        }
                    }
                
                }
            }
        }
        return (str);
    },
   
    /*
     returns the flash version
     @private
    */
    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, 10) >= 4) && (agent.indexOf("win") != -1) && (agent.indexOf("16bit") == -1)) {
            flashVersion = this.getIEFlashVersion();
        }
        return flashVersion;
    },
   
    /*
     returns the IE flash version
     @private
    */
    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);
    }
};
