﻿var g_detectableWithVB = false;

//<!--  
// Here we write out the VBScript block for MSIE Windows
/*if (Sys.Browser.agent == Sys.Browser.InternetExplorer)*/
{
    g_detectableWithVB = true;
    document.writeln('<script language="VBscript">');
    document.writeln('\'this function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('End Function');
    document.writeln('</scr' + 'ipt>');
}
// -->

/* Video Upload Control */
ActiveXDetector = function() {
    this._pluginFound = false;
}

ActiveXDetector.prototype = {
    pluginFound: function() {
        return this._pluginFound;
    },
    detectPlugin: function() {
        // allow for multiple checks in a single pass
        var daPlugins = this.detectPlugin.arguments;
        // consider pluginFound to be false until proven true
        var pluginFound = false;
        // if plugins array is there and not fake
        if (navigator.plugins && navigator.plugins.length > 0) {
            var pluginsArrayLength = navigator.plugins.length;
            // for each plugin...
            for (pluginsArrayCounter = 0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++) {
                // loop through all desired names and check each against the current plugin name
                var numFound = 0;
                for (namesCounter = 0; namesCounter < daPlugins.length; namesCounter++) {
                    // if desired plugin name is found in either plugin name or description
                    if ((navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) ||
                    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0)) {
                        // this name was found
                        numFound++;
                    }
                }
                // now that we have checked all the required names against this one plugin,
                // if the number we found matches the total number provided then we were successful
                if (numFound == daPlugins.length) {
                    pluginFound = true;
                    // if we've found the plugin, we can stop looking through at the rest of the plugins
                    break;
                }
            }
        }
        return pluginFound;
    },
    detectControl: function(controlName, redirectURL, redirectIfFound) {
        var controls = new Array();
        controls.push(controlName);
        return this.detectControls(controls, redirectURL, redirectIfFound);
    },
    detectControls: function(controlNames, redirectURL, redirectIfFound) {
        //var pluginFound = this.detectPlugin('MvDataTransfer','MvDataTransferCtrl');
        // if not found, try to detect with VisualBasic    
        var pluginFound = false;
        if (!pluginFound && g_detectableWithVB) {
            pluginFound = true;
            controlNames.each(function(c) { pluginFound &= detectActiveXControl(c); });
        }
        // check for redirection
        if (redirectURL)
            return this.redirectCheck(pluginFound, redirectURL, redirectIfFound);
        else
            return pluginFound;
    },
    detectVideoCtrl: function(version, redirectURL, redirectIfFound) {
        var controls = new Array();
        controls.push('Miovision.MediaPlayerCtrl.' + version);
        controls.push('Miovision.DataTransferCtrl.' + version);
        return this.detectControls(controls, redirectURL, redirectIfFound);
    },
    redirectCheck: function(pluginFound, redirectURL, redirectIfFound) {
        // check for redirection
        if (redirectURL && ((pluginFound && redirectIfFound) ||
            (!pluginFound && !redirectIfFound))) {
            // go away
            window.location = redirectURL;
            return pluginFound;
        } else {
            // stay here and return result of plugin detection
            return pluginFound;
        }
    }
}

