/*
 *  Flash Detection Script
 *
 */

function nullFunc() {}

// flash detection object
var FlashDetect = {
    loop: function(oObject, oCallback) {
        // must be function
        if (typeof(oCallback) != 'function') {
            return;
        }
        
        // loop
        for (sName in oObject) {
            try {
                (oCallback || nullFunc)(sName, oObject[ sName ]);
            } catch (e) {
            }
        }
    },
    
    loopParams: function(oParamBase, oObject) {
        // loop
        this.loop(oObject, function(sName, sValue) {
            // create param and add to object
            var oParam = document.createElement('param');
            oParam.setAttribute('name', sName);
            oParam.setAttribute('value', sValue);
            oParamBase.appendChild(oParam);
        });
    },
    
    flashDetect: function(oOptions) {
        try {
            // get version
            var oVersion = FlashDetect.getFlashVersion();
            
            // do we have a version to check?
            if (oOptions.major) {
                // did we get a version?
                if (oVersion) {
                    // did we pass?
                    if (oVersion.major >= oOptions.major && (!oOptions.minor || oVersion.minor >= oOptions.minor) && (!oOptions.revision || oVersion.revision >= oOptions.revision)) {
                        // success
                        (oOptions.pass || this.insertFlashObject || nullFunc)(oOptions.createOptions);
                        return;
                    } else {
                        // bad version
                        (oOptions.fail || nullFunc)();
                        return;
                    }
                } else {
                    // no flash at all
                    (oOptions.noFlash || nullFunc)();
                    return;
                }
            }
            
            // do we have flash at all?
            if (oVersion) {
                (oOptions.pass || this.insertFlashObject || nullFunc)(oOptions.createOptions);
            } else {
                (oOptions.noFlash || nullFunc)();
            }
        } catch (e) {
            (oOptions.noFlash || nullFunc)(e);
        }
    },
    
    insertFlashObject: function(oOptions) {
        // need source and destination at least
        if (!oOptions.source || !oOptions.destination) {
            // fail
            (oOptions.fail || nullFunc)();
            return false;
        }
        
        // get object
        var oObject = FlashDetect.getFlashObject(oOptions);
        
        // object?
        if (oObject) {
            try {
                // append it
                if (typeof(oOptions.destination) == 'function') {
                    oOptions.destination(oObject);
                } else if (typeof(oOptions.destination.innerHTML) != 'undefined' && oObject.outerHTML) {
                    try {
                        oOptions.destination.innerHTML += oObject.outerHTML;
                    } catch (e) {
                        (oOptions.fail || nullFunc)();
                        return false;
                    }
                } else if (typeof(oOptions.destination.appendChild) == 'function') {
                    try {
                        oOptions.destination.appendChild(oObject);
                    } catch (e) {
                        (oOptions.fail || nullFunc)();
                        return false;
                    }
                } else {
                    // fail
                    (oOptions.fail || nullFunc)();
                    return false;
                }
            } catch (e) {
                (oOptions.fail || nullFunc)(e);
            }
        } else {
            (oOptions.fail || nullFunc)();
            return false;
        }
        
        // good
        (oOptions.pass || nullFunc)();
        return true;
    },
    
    getFlashObject: function(oOptions) {
        // need source at least
        if (!oOptions.source) {
            return null;
        }
        
        // default params
        var oParams = {
            allowScriptAccess:  'sameDomain',
            menu:               'false',
            quality:            'high',
            bgcolor:            '#333333'
        };
        
        // no params?
        if (!oOptions.parameters) {
            oOptions.parameters = {};
        }
        
        // flash vars?
        if (oOptions.flashVars) {
            // set flash vars
            oOptions.parameters.FlashVars = FlashDetect.getFlashVars(oOptions.flashVars);
        }
        
        // extend
        for (sName in oOptions.parameters) {
            oParams[ sName ] = oOptions.parameters[ sName ];
        }
        
        // object or embed?
        if (typeof(ActiveXObject) == 'undefined') {
            // embed
            var oEmbed = document.createElement('embed');
            
            // setup embed attributes
            var oEmbedAttributes = {
                src:            oOptions.source,
                width:          oOptions.width || '100%',
                height:         oOptions.height || '100%',
                align:          oOptions.align || 'middle',
                type:           'application/x-shockwave-flash',
                pluginspage:    'http://www.macromedia.com/go/getflashplayer'
            }
            this.loop(oEmbedAttributes, function(sName, sValue) { oEmbed.setAttribute(sName, sValue); });
        
            // loop
            this.loop(oParams, function(sName, sValue) { oEmbed.setAttribute(sName, sValue); });
            
            // return embed
            return oEmbed;
        } else {
            // ok let's create the object
            var oObject = document.createElement('object');

            // setup embed and object attributes
            var oObjectAttributes = {
                classid:    'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000',
                codebase:   'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab',
                width:      oOptions.width || '100%',
                height:     oOptions.height || '100%'
            };
            this.loop(oObjectAttributes, function(sName, sValue) { oObject.setAttribute(sName, sValue); });
            
            // object params
            var oObjectParams = {
                Movie:  oOptions.source,
                Src:    oOptions.source
            }
            this.loopParams(oObject, oObjectParams);
        
            // loop
            this.loopParams(oObject, oParams);
            
            // return object
            return oObject;
        }
    },
    
    getFlashVars: function(oFlashVars) {
        // is it a string already?
        if (typeof(oFlashVars) == 'string') {
            return oFlashVars;
        } else if (typeof(oFlashVars) != 'object') {
            return '';
        }
        
        // loop through and create string
        var aVars = new Array();
        for (sName in oFlashVars) {
            // add to array
            aVars[ aVars.length ] = sName + '=' + oFlashVars[ sName ];
        }
        
        // return joined
        return aVars.join('&');
    },
    
    getFlashVersion: function() {
        // loop through all possible versions
        var oFlash = null;
        for (var iVersion = 25; iVersion; --iVersion) {
            // get version info
            oFlash = FlashDetect.getFlashVersionInfo(iVersion);
            
            // have it?
            if (oFlash) {
                break;
            }
        }
        
        // return
        return oFlash;
    },
    
    getFlashVersionInfo: function(iVersion) {
        // activex?
        if (typeof(ActiveXObject) == 'undefined') {
            // just use JS detect
            return FlashDetect.getFlashVersionInfoJS(iVersion);
        } else {
            // use AX detect
            return FlashDetect.getFlashVersionInfoAX(iVersion);
        }
    },
    
    getFlashVersionInfoJS: function(iVersion) {
        // catch errors
        try {
            // do we have a plugins array?
        	if (navigator.plugins && navigator.plugins.length > 0) {
        	    // flash 2.0?
        	    var sDescription = '';
        	    if (navigator.plugins[ 'Shockwave Flash 2.0' ] && navigator.plugins[ 'Shockwave Flash 2.0' ].description) {
        	        sDescription = navigator.plugins[ 'Shockwave Flash 2.0' ].description;
        	    } else if (navigator.plugins[ 'Shockwave Flash' ] && navigator.plugins[ 'Shockwave Flash' ].description) {
        	        sDescription = navigator.plugins[ 'Shockwave Flash' ].description;
        	    }

        	    // do we have a description?
        		if (sDescription) {
        		    // split up description, and get version
        			var aDescription = sDescription.split(" ");
        			var aVersion = aDescription[ 2 ].split(".");

        			// get revision number
        			if (aDescription[ 3 ]) {
        				aRevision = aDescription[ 3 ].split('r');
        			} else {
        				aRevision = aDescription[ 4 ].split('r');
        			}

              		// save version
                    iFlashVersion = {
                        major:      aVersion[ 0 ],
                        minor:      aVersion[ 1 ],
                        revision:   aRevision[ 1 ] > 0 ? aRevision[ 1 ] : 0
                    };
              	} else {
        			iFlashVersion = null;
        		}
        	} else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) {
        	    // MSN/WebTV 2.6 supports Flash 4
                iFlashVersion = {
                    major:      4,
                    minor:      0,
                    revision:   0
                };
        	} else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) {
            	// WebTV 2.5 supports Flash 3
                iFlashVersion = {
                    major:      3,
                    minor:      0,
                    revision:   0
                };
        	} else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) {
            	// older WebTV supports Flash 2
                iFlashVersion = {
                    major:      2,
                    minor:      0,
                    revision:   0
                };
        	} else {
        		iFlashVersion = null;
        	}

        	// return it
        	return iFlashVersion;
        } catch (e) {
            return null;
        }
    },
    
    getFlashVersionInfoAX: function(iVersion) {
        // catch errors
        try {
            // try to create the object!
            var oActiveXFlash = new ActiveXObject(sAXClass = 'ShockwaveFlash.ShockwaveFlash.' + iVersion);
            
            // did we get one?
            if (oActiveXFlash) {
                // split up the version info
				aVersionInfo = oActiveXFlash.GetVariable('$version').split(' ');
				
				// get version string and split to array
				sVersion = aVersionInfo[ 1 ];
				aVersion = sVersion.split(',');
				
				// return
				return {
        			major:      aVersion[0],
        			minor:      aVersion[1],
        			revision:   aVersion[2]
				};
            }
        } catch (e) {
            return null;
        }
    }
}