var pluginFound = false;
var detectableWithVB = false;

function detectQuickTime() {
    return detectPlugin('QuickTime');
}

function detectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    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;
	    }
	}
    }
    
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectQuickTimeActiveXControl();
	}
    
    return pluginFound;
} // detectPlugin

// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<sc' + 'ript 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>');
}
// -->



function GetMovHtml(path, width, height)
{
    var html = '<html><head></head><p style="background:black;margin-left:-10px;margin-top:-10px;_margin-top:-14px;padding:0px;text-align:center;"><OBJECT type="video/quicktime"';
    html += ' width="' + width; 
    html += '" height="' + height + '" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" ID="Object1"><param name="controller" value="true"><param name="SRC"';
    html += ' value="' + path + '"> <EMBED controller="true" ';
    html += ' src="' + path + '" ';
    html += ' width=' + '"' +  width + '"' + ' height="';
    html += height + '" PLUGINSPAGE="http://www.apple.com/quicktime/download/"> </EMBED> </OBJECT></P></html>';
   
    return html;
}

function GetFlashHtml(path, width, height)
{
    var html = '<html><head><title></title><style type="text/css">html, body {padding: 0; margin: 0;}</style></head><body><object id="flashobject" type="application/x-shockwave-flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" width="'
    html += width + '" height="';
    html += height + '" wmode="transparent"><param id="wmode" name="wmode" value="transparent"><param id="movie" name="movie" value="';
    html += path + '"><embed quality="high" wmode="transparent" src="';
    html += path + '" bgcolor="#00000" width="'
    html += width + '" height="'
    html += height + '" name="ExteriorFlash" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object></body></html>';
    
    return html;
}

function GetMediaHtml(width, height, source)
{
   var content = '';

   if(source.split('.').length != null)
   {
        if(source.split('.').length > 0)
        {
            extension = source.split('.')[source.split('.').length-1]; 
        }
   }
   if(extension != null)
   {
        switch(extension)
        {
            case 'mov':
				content =  OpenQuickTimeFile(source, width, height);
				break;
            case 'swf':
				content =  GetFlashHtml(source, width, height);    
				break;
        }
   }
   return content;
}

function OpenQuickTimeFile(source, width, height)
{
    var content = '';
    var redirectUrl = 'http://www.apple.com/quicktime/download/win.html';
    
    if(detectQuickTime() == true)
    {
       content = GetMovHtml(source, width, height);
    } 
    else 
    {
       content = GetRedirectHtml('Quicktime', redirectUrl); 
    }   
    
    return content;
}

function GetRedirectHtml(name, redirectUrl)
{
    var html = '';
     
    html += '<html><head></head><body style="overflow: show;"><h3>';
    html += name + ' plugin was not found.</h3>';
    html += '<br>Redirecting to download page for '; 
    html += name + ' plugin in 5 seconds. If it does not, click <a href="';
    html += redirectUrl + '" onclick="Redirect();return false;">here</a>.';
    html += '<sc' + 'ript language="Javascript">';
    html += 'function Redirect(){'
    html += 'window.resizeTo(1064, 768);';
    html += 'window.moveTo(screen.width/6,0);';
    html += 'window.location = "' + redirectUrl +'";';
    html += '}';
    html += 'window.setTimeout("Redirect()", 5000);';
    html += '</scr' + 'ipt>';
    html += '</body></html>';
 
    return html;
} 
