/*
 FLASH GORDON
 Version 1.6
 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 int required version or string of version ("major.minor.revision")
 @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
        or object of alternative and revision alternative data
 @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 = this.initAlternative(alternative);
    this.revAlternative;
    this.wrongRevision;
    this.urlParameter = urlParameter;
    this.options = {
        "width": this.width,
        "height": this.height,
        "quality": "high",
        "allowScriptAccess": "sameDomain",
        "allowFullScreen": "true",
        "movie": this.src,
        "menu": "false",
        "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": "transparent",
        "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) {
        if (this.checkReqVersion()) {
            //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");
        }
    },
    
    /*
     check for minor and revision information in required version
     @private
    */
    checkReqVersion: function() {
        if (typeof this.reqVersion == "string" && this.reqVersion.indexOf(".") > -1) {
            var version = this.reqVersion.split(".");
            this.reqVersion = {
                "major": parseInt(version[0], 10),
                "minor": parseInt(version[1], 10),
                "revision": parseInt(version[2], 10)
            };
        } else {
            this.reqVersion = {
                "major": this.reqVersion,
                "minor": 0,
                "revision": 0
            };
        }

        var fv = this.getFlashVersion();
        var rv = this.reqVersion;

        //set wrongRevision for displaying user dialog
        this.wrongRevision = (fv.major == rv.major && fv.revision <= rv.revision) ? true : false;

        // check if ignore warning is set
        if (fv.major >= rv.major && document.location.search.match(/warning=ignore/g)) {
            return true;
        } else {
    		return (fv.major > rv.major || (fv.major == rv.major && fv.minor > rv.minor) || (fv.major == rv.major && fv.minor == rv.minor && fv.revision >= rv.revision)) ? true : false;
        }
    },
    
    /*
     initialize alternative content div/url
     and revision alternative content div/url
     @param string/object alternative
     @private
    */
    initAlternative: function(alternative) {
        if (typeof alternative === "object") {
            this.revAlternative = (document.getElementById(alternative.revision) ? document.getElementById(alternative.revision) : alternative.revision);
            return (document.getElementById(alternative.major) ? document.getElementById(alternative.major) : alternative.major);
        } else if (typeof alternative === "string") {
            return (document.getElementById(alternative) ? document.getElementById(alternative) : alternative);
        }
    },

    
    /*
     show/hide alternative DIV or redirect
     @param string display
     @private
    */
    checkAlternative: function(display) {
        var el;
        if (this.wrongRevision) {
            el = this.revAlternative;
            this.alternative.style.display = "none";
        } else {
            el = this.alternative;
            this.alternative.style.display = "none";
            this.revAlternative ? this.revAlternative.style.display = "none" : false;
        }
        if (typeof el === "object") {
            el.style.display = display;
        } else if ((typeof el === "string") && (display == "block")) {
            location.href = el;
        }
    },
    
    /*
     returns a flash object/embed param
     @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") {
                var desc = flashPlugin.description.replace(/Shockwave Flash /g, "").replace(/ r/g, ".").split(".");
                flashVersion = {
                    "major": desc[0],
                    "minor": desc[1],
                    "revision": desc[2]
                };
            }
        } 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 < 3; i++) {
            try {
                swfObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
            } catch(e) {}
            if (swfObj) {
                var desc = swfObj.GetVariable("$version").split(" ")[1].split(",");
                flashVersion = {
                    "major": desc[0],
                    "minor": desc[1],
                    "revision": desc[2]
                };
            }
        }
        return (flashVersion);
    }
};

