﻿var detectableWithVB = false;

function detect(elementID, fileName, width, height, fileUrl, backcolor, loop, alternateImageUrl, showControls, showError)
{
    if(detectableWithVB || navigator.plugins && navigator.plugins.length > 0)
    {
        var getFlashPluginURL="http://get.adobe.com/flashplayer/";
        var getRealPlayerPluginURL="http://uk.real.com/realplayer/";
        var getMediaPlayerPluginURL="http://www.microsoft.com/windows/windowsmedia/player/download/download.aspx";
        var getQuickTimePlayerPluginURL="http://www.apple.com/quicktime/download/";
        
        //flash files
        if(fileName=="swf" || fileName=="flv")
        {
            var flash=detectFlash();
            
            if(flash)
            {
                flashWriter(elementID, fileName, width, height, fileUrl, backcolor, loop);
                return;
            }
            
            if(!flash)
            {
                var e=document.getElementById(elementID);
                var html="<div style='position:relative; width:"+width+"px; height:"+height+"px; background-color:"+backcolor+"'>";
                if(alternateImageUrl!="")
                    html+="<img src='"+alternateImageUrl+"' alt='"+alternateImageUrl+"' />";

                if (showError == "True")
                    html += "<span style='position:absolute; left:0px; top:0px; background-color:#000000; padding:5px; font-size:100%; color:#ffffff; font-weight:bold; opacity: .75; filter: alpha(opacity=75); text-align:center'>Install <a href='" + getFlashPluginURL + "' >Flash</a> or <a href='" + getRealPlayerPluginURL + "'>Real Player </a></span></div>";
                else
                    html += "</div>";                    
                e.innerHTML=html;
            }
        }
        
        //dvd format files
        if(fileName=="mp4")
        {
            var quickTime=detectQuickTime();
            if(!quckTime)
                var real=detectReal();
            
            if(quickTime)
            {
                quickTimeWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls);
                return;
            }
            
            if(real)
            {
                realPlayerWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls);
                return;
            }
            
            if(!quickTime && !real)
            {
                var e=document.getElementById(elementID);
                var html="<div style='position:relative; width:"+width+"px; height:"+height+"px; background-color:"+backcolor+"'>";
                if(alternateImageUrl!="")
                    html+="<img src='"+alternateImageUrl+"' alt='"+alternateImageUrl+"' />";
                if (showError == "True")
                    html += "<span style='position:absolute; left:0px; top:0px; background-color:#000000; padding:5px; font-size:100%; color:#ffffff; font-weight:bold; opacity: .75; filter: alpha(opacity=75); text-align:center'>Install <a href='" + getFlashPlayerPluginURL + "' >Flash</a> or <a href='" + getRealPlayerPluginURL + "'>Real Player </a></span></div>";
                else
                    html += "</div>";
                e.innerHTML=html;
            }
        }
        
        //windows media files
        if(fileName=="wma")
        {
            var mediaPlayer=detectWindowsMedia();
            if(!mediaPlayer)
                var real=detectReal();
            
            if(mediaPlayer)
            {
                mediaPlayerWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls);
                return;
            }
            
            if(real)
            {
                realPlayerWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls);
                return;
            }
            
            if(!mediaPlayer && !real)
            {
                var e=document.getElementById(elementID);
                var html="<div style='position:relative; width:"+width+"px; height:"+height+"px; background-color:"+backcolor+"'>";
                if(alternateImageUrl!="")
                    html+="<img src='"+alternateImageUrl+"' alt='"+alternateImageUrl+"' />";

                if (showError == "True")
                    html += "<span style='position:absolute; left:0px; top:0px; background-color:#000000; padding:5px; font-size:100%; color:#ffffff; font-weight:bold; opacity: .75; filter: alpha(opacity=75); text-align:center'>Install <a href='" + getMediaPlayerPluginURL + "' >Media Player</a> or <a href='" + getRealPlayerPluginURL + "'>Real Player </a></span></div>";
                else
                    html += "</div>";
                e.innerHTML=html;
            }
            
        }
        
        //mp3 audio files
        if(fileName=="mp3")
        {
            var mediaPlayer=detectWindowsMedia();
            
            if(!mediaPlayer)
                var quickTime=detectQuickTime();
            
            if(!quickTime)
                var real=detectReal();
            
            if(mediaPlayer)
            {
                mediaPlayerWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls);
                return;
            }
            
            if(quickTime)
            {
                quickTimeWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls);
                return;
            }
            
            if(real)
            {
                realPlayerWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls);
                return;
            }
            
            if(!quickTime && !real && !real)
            {
                var e=document.getElementById(elementID);
                var html="<div style='position:relative; width:"+width+"px; height:"+height+"px; background-color:"+backcolor+"'>";
                if(alternateImageUrl!="")
                    html+="<img src='"+alternateImageUrl+"' alt='"+alternateImageUrl+"' />";

                if (showError == "True")
                    html += "<span style='position:absolute; left:0px; top:0px; background-color:#000000; padding:5px; font-size:100%; color:#ffffff; font-weight:bold; opacity: .75; filter: alpha(opacity=75); text-align:center'>Install <a href='" + getMediaPlayerPluginURL + "' >Media Player</a> or <a href='" + getRealPlayerPluginURL + "'>Real Player </a> or <a href='" + getQuickTimePlayerPluginURL + "'>QuickTime</a></span></div>";
                else
                    html += "</div>";
                e.innerHTML=html;
            }
        }
        //quick time files
        if(fileName=="mov")
        {   
            var quickTime=detectQuickTime();
            if(quickTime)
            {
                quickTimeWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls);
            }
            
            if(!quickTime)
            {
                var e=document.getElementById(elementID);
                var html="<div style='position:relative; width:"+width+"px; height:"+height+"px; background-color:"+backcolor+"'>";
                if(alternateImageUrl!="")
                    html+="<img src='"+alternateImageUrl+"' alt='"+alternateImageUrl+"' />";

                if (showError == "True")
                    html += "<span style='position:absolute; left:0px; top:0px; background-color:#000000; padding:5px; font-size:100%; color:#ffffff; font-weight:bold; opacity: .75; filter: alpha(opacity=75); text-align:center'>Install <a href='" + getQuickTimePlayerPluginURL + "' >QuickTime</a></span></div>";
                else
                    html += "</div>";
                e.innerHTML=html;
            }
        }
        //real player files
        if(fileName=="ra" || fileName=="rm" || fileName=="rv" || fileName=="rmvb")
        {
            var real=detectReal();
            if(real)
            {
                realPlayerWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls);
            }
            
            if(!real)
            {
                var e=document.getElementById(elementID);
                var html="<div style='position:relative; width:"+width+"px; height:"+height+"px; background-color:"+backcolor+"'>";
                if(alternateImageUrl!="")
                    html+="<img src='"+alternateImageUrl+"' alt='"+alternateImageUrl+"' />";

                if (showError == "True")
                    html += "<span style='position:absolute; left:0px; top:0px; background-color:#000000; padding:5px; font-size:100%; color:#ffffff; font-weight:bold; opacity: .75; filter: alpha(opacity=75); text-align:center'>Install <a href='" + getRealPlayerPluginURL + "'>Real Player </a></span></div>";
                else
                    html += "</div>";
                e.innerHTML=html;
            }
        }        
        
    }
    else
    {
        var e=document.getElementById(elementID);
        var html="<div style='position:relative; width:"+width+";px height:"+height+"px; background-color:"+backcolor+"'>";
        if(alternateImageUrl!="")
            html+="<img src='"+alternateImageUrl+"' alt='"+alternateImageUrl+"' />";

        if (showError == "True")
            html += "<span style='position:absolute; left:0px; top:0px; background-color:#000000; padding:5px; font-size:100%; color:#ffffff; font-weight:bold; opacity: .75; filter: alpha(opacity=75); text-align:center'>Please enable browser plugins</span></div>";
        else
            html += "</div>";
        e.innerHTML=html;
    }
}

function flashWriter(elementID, fileName, width, height, fileUrl, backcolor, loop)
{
    
    var e=document.getElementById(elementID);
    var embed="<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0'";
    embed += "width='" + width + "' height='" + height + "'>";
    embed += "<param name='allowScriptAccess' value='sameDomain'>";
    embed+="<param name='movie' value='"+fileUrl+"'>";
    embed+="<param name='quality' value='high'>";
    embed+="<param name='bgcolor' value='"+backcolor+"'>";
    embed+="<param name='wmode' value='transparent'>";
    embed+="<param name='loop' value='"+loop+"'>";
    embed+="<EMBED src='"+fileUrl+"' wmode='transparent' quality='high' bgcolor='"+backcolor+"' width='"+width+"' height='"+height+" loop='"+loop+"' allowScriptAccess='sameDomain' type='application/x-shockwave-flash'";
    embed+="pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'></EMBED></OBJECT>";
  
    e.innerHTML=embed;
}

function mediaPlayerWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls)
{
    var e=document.getElementById(elementID);
    
    var embed="<OBJECT id='mediaPlayer' width='"+width+"' height='"+height+"' classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95' codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701'";
    embed+="standby='Loading Microsoft Windows Media Player components...' type='application/x-oleobject'>";
    embed+="<param name='fileName' value='"+fileUrl+"'>";
    embed+="<param name='animationatStart' value='true'>";
    embed+="<param name='transparentatStart' value='true'>";
    embed+="<param name='autoStart' value='false'>";
    embed+="<param name='showControls' value='"+showControls+"'>";
    embed+="<param name='loop' value='"+loop+"'>";
    embed+="<EMBED type='application/x-mplayer2' pluginspage='http://microsoft.com/windows/mediaplayer/en/download/' id='mediaPlayer' name='mediaPlayer' displaysize='4' autosize='-1' bgcolor='"+backcolor+"' showcontrols='"+showControls+"' showtracker='-1' showdisplay='0'";
    embed+="showstatusbar='-1' videoborder3d='-1' width='"+width+"' height='"+height+"'";
    embed+="src='"+fileUrl+"' autostart='false' designtimesp='5311' loop='"+loop+"'></EMBED></OBJECT>";
    e.innerHTML=embed;
}

function realPlayerWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls)
{
    var e=document.getElementById(elementID);
    var embed="<table border='0' cellpadding='0' align='center'>";
    embed+="<tr><td>";
    embed+="<OBJECT id='rvocx' classid='clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA' width='"+width+"' height='"+height+"'>";
    embed+="<param name='src' value='"+fileUrl+"'>";
    embed+="<param name='autostart' value='false'>";
    embed+="<param name='controls' value='imagewindow'>";
    embed+="<param name='console' value='video'>";
    embed+="<param name='loop' value='"+loop+"'>";
    embed+="<EMBED src='"+fileUrl+"' width='"+width+"' height='"+height+"' loop='"+loop+"' type='audio/x-pn-realaudio-plugin' controls='imagewindow' console='video' autostart='false'>";
    embed+="</EMBED></OBJECT></td></tr>";
    embed+="<tr><td><OBJECT id='rvocx' classid='clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA' width='"+width+"' height='30'>";
    embed+="<param name='src' value='"+fileUrl+"'>";
    embed+="<param name='autostart' value='false'>";
    embed+="<param name='controls' value='ControlPanel'>";
    embed+="<param name='console' value='video'>";
    embed+="<EMBED src='"+fileUrl+"' width='"+width+"' height='30' controls='ControlPanel' type='audio/x-pn-realaudio-plugin' console='video' autostart='false'></EMBED></OBJECT></td></tr></table>";
    
    e.innerHTML=embed;
}

function quickTimeWriter(elementID, fileName, width, height, fileUrl, backcolor, loop, showControls)
{
    var e=document.getElementById(elementID);
    
    var embed="<OBJECT classid='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' width='"+width+"' height='"+height+"' codebase='http://www.apple.com/qtactivex/qtplugin.cab'>";
    embed+="<param name='src' value='"+fileUrl+"'>";
    embed+="<param name='autoplay' value='false'>";
    embed+="<param name='controller' value='"+showControls+"'>";
    embed+="<param name='loop' value='"+loop+"'>";
    embed+="<EMBED src='"+fileUrl+"' width='"+width+"' height='"+height+"' autoplay='false' controller='"+showControls+"' loop='"+loop+"' pluginspage='http://www.apple.com/quicktime/download/'></EMBED></OBJECT>";
    e.innerHTML=embed;
}

function detectReal() 
{
    var found=false;
    found = detectPlugin('RealPlayer');
    // if not found, try to detect with VisualBasic
    if(!found && detectableWithVB) 
    {
	    found = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
		           detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
		           detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }	
    return found;
}

function detectQuickTime() 
{
    var found=false;
    found = detectPlugin('QuickTime');
    // if not found, try to detect with VisualBasic
    if(!found && detectableWithVB) 
    {
	    found = detectQuickTimeActiveXControl();
    }
    return found;
}

function detectWindowsMedia() 
{
    var found=false;
    found = detectPlugin('Windows Media');
    // if not found, try to detect with VisualBasic
    if(!found && detectableWithVB) 
    {
	    found = detectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
    return found;
}

function detectFlash() 
{
    var found=false;
    found = detectPlugin('Shockwave','Flash'); 
    // if not found, try to detect with VisualBasic
    if(!found && detectableWithVB) 
    {
	    found = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
    }
    return found;
}

function detectPlugin() 
{
    // allow for multiple checks in a single pass
    var daPlugins = 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;
} 

if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) 
{
    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>');
}