/*
	FlashReplace is developed by Robert Nyman, http://www.robertnyman.com. License and downloads: http://code.google.com/p/flashreplace/
*/
// ---
var VideoReplace = {
    elmToReplace: null,
    pluginIsInstalled: null,
    defaultPluginVersion: 6,
    replace: function(elmToReplace, src, id, width, height, strPreferedFileExtension, version, params) {
        this.elmToReplace = document.getElementById(elmToReplace);
        this.pluginIsInstalled = this.checkForPlugin(version || this.defaultPluginVersion, strPreferedFileExtension);

        if (this.elmToReplace) {

            if (strPreferedFileExtension != '.swf') {
                
                this.elmToReplace.innerHTML
                    = "<div style='padding:40px'>"
                            + "<div class='highlight'><strong>QuickTime Install Required</strong></div>"
                            + "<br />"
                            + "<div>"
                            + "<strong>"
                                + "You need Apple QuickTime player to view this video."
                                + "<br /><br />"
                                + "<a href='http://www.apple.com/quicktime/download/' target='blank'>Apple QuickTime download page</a>"
                            + "</strong>"
                            + "</div>"
                            + "<br /><br />"
                            + "<div>"
                                + "<strong>"
                                    + "Download then install by double clicking on the downloaded file. Once installed, try refreshing the page. We have found that usually you have to restart your computer after installation."
                                + "</strong>"
                            + "</div>"
                            + "<br /><br /><br />"
                        + "</div>"
                    + "</strong>"
            }
            else {
                this.elmToReplace.innerHTML
                    = "<div style='padding:40px'>"
                            + "<div class='highlight'><strong>Flash Install Required</strong></div>"
                            + "<br />"
                            + "<div>"
                            + "<strong>"
                                + "You need Adobe Flash Player to view this video."
                                + "<br /><br />"
                                + "<a href='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' target='_blank'>Download now from Adobe's site</a>"
                            + "</strong>"
                            + "</div>"
                            + "<br /><br />"
                            + "<div>"
                                + "<strong>"
                                    + "Download and install. Once installed, refresh the page."
                                + "</strong>"
                            + "</div>"
                            + "<br /><br /><br />"
                        + "</div>"
                    + "</strong>"
            }

            if (this.pluginIsInstalled) {

                var obj;

                if (strPreferedFileExtension == null)
                    strPreferedFileExtension = '.mov'

                if (screen.width < 1024) {
                    window.location.href = src + strPreferedFileExtension;
                }
                else {
                    if (strPreferedFileExtension == '.swf')
                        obj = '<object' + ((window.ActiveXObject) ? ' id="' + id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" data="' + src + strPreferedFileExtension + '"' : '');
                    else
                        obj = '<object id="' + id + '" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" ';
                    obj += ' width="' + width + '"';
                    obj += ' height="' + height + '"';
                    obj += '>';
                    var param = '<param';
                    if (strPreferedFileExtension == '.swf')
                        param += ' name="movie"'
                    else
                        param += ' name="src"';
                    param += ' value="' + src + strPreferedFileExtension + '"';
                    param += '>';
                    param += '<param';
                    param += ' name="autoplay"';
                    param += ' value="true"';
                    param += ' />';

                    var extraParams = '';
                    var extraAttributes = '';
                    for (var i in params) {
                        extraParams += '<param name="' + i + '" value="' + params[i] + '" autoplay="true" />';
                        extraAttributes += ' ' + i + '="' + params[i] + '"';
                    }
                    var embed = '<embed id="' + id + '" src="' + src + strPreferedFileExtension + '" width="' + width + '" height="' + height + '" autoplay="true" ';



                    var embedEnd = extraAttributes + '></embed>';
                    var objEnd = '</object>';

                    this.elmToReplace.innerHTML = obj + param + extraParams + embed + embedEnd + objEnd;

                }
            }
        }
    },

    checkForPlugin: function(version, strPreferedFileExtension) {


        var nse;
        var agt = navigator.userAgent.toLowerCase();

        if (window.ActiveXObject) {

            ie = true
            ns = false

            if (strPreferedFileExtension == '.swf') {
                try {
                    new ActiveXObject(("ShockwaveFlash.ShockwaveFlash." + version))
                    return true;
                }
                catch (err) {
                    return false;
                }
            }

        }
        else {

            ie = false
            if (navigator.plugins && navigator.mimeTypes.length > 0)
                ns = true;
            else
                return false;
        }

        var win = ((agt.indexOf("win") != -1) || (agt.indexOf("32bit") != -1));
        var mac = (agt.indexOf("mac") != -1);

        this.pluginIsInstalled = false;


        if (ie && win)
            this.pluginIsInstalled = detectIE("QuickTimeCheckObject.QuickTimeCheck." + version, "QuickTime");

        if (ns || !win) {
            if (strPreferedFileExtension == '.swf') {
                if (navigator.plugins["Shockwave Flash"]) {
                    var flashVersion = navigator.plugins["Shockwave Flash"].description.replace(/.*\s(\d+\.\d+).*/, "$1");
                    if (flashVersion >= version) {
                        this.pluginIsInstalled = true;
                    }
                }
            }
            else {
                nse = ""; for (var i = 0; i < navigator.mimeTypes.length; i++) nse += navigator.mimeTypes[i].type.toLowerCase();
                this.pluginIsInstalled = detectNS("video/quicktime", "QuickTime");
            }
        }

        function detectIE(ClassID, name) {


            result = false;

            if (BrowserDetect.version < 7) {
                document.write('<script type="text/vbscript">\n on error resume next \n result = IsObject(CreateObject("' + ClassID + '")) \n </script>');
            }
            else {
                return true
            }

            if (result)
                return true;
            else
                return false;

        }

        function detectNS(ClassID, name) {

            if (nse.indexOf(ClassID) != -1) {
                if (navigator.mimeTypes[ClassID].enabledPlugin != null)
                    return true;
                else
                    return false;
            }
            return false;
        }

        return this.pluginIsInstalled;
    }

};
// ---
