/*
Adapted from the following sources:
http://developer.apple.com/internet/webcontent/detectplugins.html
http://www.javascriptkit.com/script/script2/plugindetect.shtml
http://www.pinlady.net/PluginDetect/PluginDet%20Generator.htm
*/

var detectableWithVB = false;

Ext.namespace('PBS.utils');
PBS.utils.PluginDetector = function() {
    this.mimetypes = "";
    this.detect_mimetypes = navigator.mimeTypes && navigator.mimeTypes.length > 0;
    this.detect_axos = Ext.isIE;

    if(this.detect_mimetypes) {
        for (var i=0; i<navigator.mimeTypes.length; i++) {
            this.mimetypes += navigator.mimeTypes[i].type.toLowerCase();
        }
    }

    this.plugins = {
        'svg': {
            'name': 'SVG Viewer',
            'axo': 'Adobe.SVGCtl',
            'mimetype': 'image/svg-xml',
            'clsid': '',
            'installed': null,
            'version': -1,
            'messages': {
                'installed': '',
                'failed': '',
                'unknown': ''
            }
        },
        'shockwave': {
            'name': 'Adobe Shockwave',
            'axo': ['SWCtl.SWCtl.1', 'SWCtl.SWCtl'],
            'mimetype': 'application/x-director',
            'clsid': '166B1BCA-3F9C-11CF-8075-444553540000',
            'installed': null,
            'version': -1,
            'messages': {
                'installed': '',
                'failed': '',
                'unknown': ''
            }
        },
        'flash': {
            'name': 'Adobe Flash',
            'axo': 'ShockwaveFlash.ShockwaveFlash.1',
            'mimetype': ['application/x-shockwave-flash', 'application/futuresplash'],
            'clsid': 'D27CDB6E-AE6D-11CF-96B8-444553540000',
            'installed': null,
            'version': -1,
            'messages': {
                'installed': '',
                'failed': '',
                'unknown': ''
            }
        },
        'realplayer': {
            'name': 'RealPlayer',
            'axo': 'rmocx.RealPlayer G2 Control.1',
            'mimetype': 'audio/x-pn-realaudio-plugin',
            'clsid': 'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
            'installed': null,
            'version': -1,
            'messages': {
                'installed': '',
                'failed': '',
                'unknown': 'The RealPlayer client is unstable in the Internet Explorer browser and can cause your browser to crash. Please ensure that you have RealPlayer 10+ installed to avoid this problem.'
            }
        },
        'quicktime': {
            'name': 'QuickTime',
            'axo': ['QuickTimeCheckObject.QuickTimeCheck.1', 'QuickTime.QuickTime'],
            'mimetype': ['video/quicktime', 'application/x-quicktimeplayer', 'image/x-macpaint', 'image/x-quicktime'],
            'clsid': '02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
            'installed': null,
            'version': -1,
            'messages': {
                'installed': '',
                'failed': '',
                'unknown': ''
            }
        },
        'windows_media': {
            'name': 'Windows Media Player',
            'axo': ['MediaPlayer.MediaPlayer.1', 'wmplayer.ocx'],
            'mimetype': 'application/x-mplayer2',
            'clsid': '6BF52A52-394A-11D3-B153-00C04F79FAA6',
            'installed': null,
            'version': -1,
            'messages': {
                'installed': '',
                'failed': '',
                'unknown': ''
            }
        },
        'acrobat': {
            'name': 'Adobe Acrobat',
            'axo': 'PDF.PdfCtrl.5',
            'mimetype': 'application/pdf',
            'clsid': 'CA8A9780-280D-11CF-A24D-444553540000',
            'installed': null,
            'version': -1,
            'messages': {
                'installed': '',
                'failed': 'All PDF files on PBS TeacherLine websites are compatible with standard PDF readers. Although you will need a PDF reader to access these files, you are not required to use Adobe Acrobat. If you have an alternative PDF reader installed on your computer, you can disregard the failure of this check.',
                'unknown': ''
            }
        },
        'java': {
            'name': 'Java',
            'axo': '',
            'mimetype': 'application/x-java-applet',
            'clsid': '8AD9C840-044E-11D1-B3E9-00805F499D93',
            'installed': navigator.javaEnabled(),
            'version': -1,
            'messages': {
                'installed': '',
                'failed': '',
                'unknown': ''
            }
        },
        'cookies': {
            'name': '',
            'axo': '',
            'mimetype': '',
            'clsid': '',
            'installed': null,
            'version': -1,
            'messages': {
                'installed': '',
                'failed': 'Cookies are required to login to PBS TeacherLine.',
                'unknown': ''
            }
        },
        'javascript': {
            'name': '',
            'axo': '',
            'mimetype': '',
            'clsid': '',
            'installed': null,
            'version': -1,
            'messages': {
                'installed': '',
                'failed': '',
                'unknown': ''
            }
        }
    };

    this.detectable_with_vb = detectableWithVB;
};

PBS.utils.PluginDetector.prototype.detect = function(name) {
    plugin = this.plugins[name];
    if(!plugin) {
        return false;
    }
    if(plugin.installed == null) {
        plugin.installed = false;
        if(this.detect_mimetypes) {
            var mimetypes = get_list(plugin.mimetype);
            for(var i=0; i<mimetypes.length; i++) {
                var mimetype = mimetypes[i];
                if(this.mimetypes.indexOf(mimetype) != -1) {
                    if (navigator.mimeTypes[mimetype].enabledPlugin != null) {
                        plugin.installed = true;
                        return true;
                    }
                }
            }
        }
        if(this.detect_axos) {
            var axos = get_list(plugin.axo);
            for(var i=0; i<axos.length; i++) {
                var axo = axos[i];
                plugin.installed = detectActiveXControl(axo) ? true : false;
                if(plugin.installed) {
                    return true;
                }
            }
        }
    }
    return plugin.installed
};

PBS.utils.PluginDetector.prototype.get_message = function(name, type) {
    if(this.plugins[name] && this.plugins[name].messages && this.plugins[name].messages[type]) {
        return this.plugins[name].messages[type];
    }
    return ''
};

var get_list = function(data) {
    if(data == null) {
        return [];
    }
    else if(typeof(data) == 'object' && data.constructor == Array) {
        return data;
    }
    else {
        return [ data ];
    }
}

PBS.utils.PluginDetect = new PBS.utils.PluginDetector();

// VBScript check for IE browser.
if (Ext.isIE && Ext.isWindows) {
    document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('\'and the following function handles QuickTime');
    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}
