// initialize global variables
var detectableWithVB = false;
var pluginFound = false;

// Gets FLASH Code (without flash args)
function GetFlashCode(flashfile, width, height)
{
   return GetFlashCode(flashfile, width, height, null);
}

function GetFlashVarsQuerystring(flashargs)
{
   var argCode = '';

   if (flashargs != null)
   {
      for(var x = 0; x < flashargs.length; x++)
      {
         if (flashargs[x] != null)
         {
            if (flashargs[x].length == 2)
            {
               if (x > 0)
                  argCode += '&';

               argCode += flashargs[x][0] + '=' + flashargs[x][1];
            }
         }
      }
   }
   return argCode;
}

// Gets FLASH Code (with flash args)
function GetFlashCode(flashfile, width, height, flashargs)
{

   var flashCode = '';

   flashCode += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '">';
   flashCode += '<param name="movie" value="' + flashfile + '">';
   flashCode += '<param name="quality" value="high">';

   if (flashargs != null)
   {
      flashCode += '<param name="FlashVars" value="' + GetFlashVarsQuerystring(flashargs) + '">';
      for(var x = 0; x < flashargs.length; x++)
      {
         flashCode+= '<param name="' + flashargs[x][0] + '" value="' + flashargs[x][1] + '" />';
      }
   }
   var flashVarsEmbed = '';
   if (flashargs != null)
      flashVarsEmbed = 'FlashVars = "' + GetFlashVarsQuerystring(flashargs) + '" ';
   flashCode += '<embed src="' + flashfile + '" ' + flashVarsEmbed + '" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"></embed>';
   flashCode += '</object>';


   return flashCode;
}




// Gets Image Code
function GetImageCode(imagefile, width, height, alt)
{
   return '<img src="' + imagefile + '" width="' + width + '" height="' + height + ' alt="' + alt + '">';
}

// Gets Image Code with Link
function GetImageCodeWithLink(imagefile, width, height, alt, link)
{
   return '<a href="' + link + '"><img src="' + imagefile + '" width="' + width + '" height="' + height + '" alt="' + alt + '" border="0"></a>';
}

// Gets Image Code with ImageMap
function GetImageCodeWithMap(imagefile, width, height, alt, mapareas)
{
   var randMapID = Math.random();
   return '<img src="' + imagefile + '" border="0" width="' + width + '" height="' + height + ' alt="' + alt + '" usemap=#' + randMapID + '><map name="' + randMapID + '">' + mapareas + '</map>';
}


// Gets the FLASH code if available, or Image code
function GetFlashOrImageCode(flashfile, imagefile, width, height, alt)
{
   if(detectFlash() == true)
      return GetFlashCode(flashfile, width, height);
   else
      return GetImageCode(imagefile, width, height, alt);
}

// Gets the FLASH code (with params) if available, or Image Code
function GetFlashOrImageCode(flashfile, imagefile, width, height, alt, flashargs)
{
   if(detectFlash() == true)
      return GetFlashCode(flashfile, width, height, flashargs);
   else
      return GetImageCode(imagefile, width, height, alt);
}

// Gets the FLASH code if available, or Image code with imagemap
function GetFlashOrImageCodeWithMap(flashfile, imagefile, width, height, alt, mapareas)
{
   if(detectFlash() == true)
      return GetFlashCode(flashfile, width, height);
   else
      return GetImageCodeWithMap(imagefile, width, height, alt, mapareas);
}

// Gets the FLASH code (with params) if available, or Image code with imagemap
function GetFlashOrImageCodeWithMap(flashfile, imagefile, width, height, alt, mapareas, flashargs)
{
   if(detectFlash() == true)
      return GetFlashCode(flashfile, width, height, flashargs);
   else
      return GetImageCodeWithMap(imagefile, width, height, alt, mapareas);
}

// Gets the FLASH code if available, or Image code
function GetFlashOrImageCodeWithLink(flashfile, imagefile, width, height, alt, link)
{
   if(detectFlash() == true)
      return GetFlashCode(flashfile, width, height);
   else
      return GetImageCodeWithLink(imagefile, width, height, alt, link);
}



// Finds out if the browser can detect plugins
function canDetectPlugins()
{
   if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) )
      return true;
   else
      return false;
}

// Detects FLASH
function detectFlash()
{
   // Try to detect the plugin with the javascript
   pluginFound = detectPlugin('Shockwave','Flash');

   // if not found, try to detect with VisualBasic
   if(!pluginFound && detectableWithVB)
   {
      pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
   }

   // return plugin detection result
   return pluginFound;
}

// Detects Director
function detectDirector(redirectURL, redirectIfFound)
{
   // Try to detect the plugin with the javascript
   pluginFound = detectPlugin('Shockwave','Director');

   // if not found, try to detect with VisualBasic
   if(!pluginFound && detectableWithVB)
   {
      pluginFound = detectActiveXControl('SWCtl.SWCtl.1');
   }

   // return plugin detection result
   return pluginFound;
}

// Detects QuickTime
function detectQuickTime(redirectURL, redirectIfFound)
{
   // Try to detect the plugin with the javascript
   pluginFound = detectPlugin('QuickTime');

   // if not found, try to detect with VisualBasic
   if(!pluginFound && detectableWithVB)
   {
      pluginFound = detectQuickTimeActiveXControl();
   }

   // return plugin detection result
   return pluginFound;
}



// Detects Real
function detectReal(redirectURL, redirectIfFound)
{
   // Try to detect the plugin with the javascript
   pluginFound = detectPlugin('RealPlayer');

   // if not found, try to detect with VisualBasic
   if(!pluginFound && detectableWithVB)
   {
      pluginFound =  (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
         detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
         detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
   }

   // return plugin detection result
   return pluginFound;
}

// Detects Windows Media
function detectWindowsMedia(redirectURL, redirectIfFound)
{


   // Try to detect the plugin with the javascript
   pluginFound = detectPlugin('Windows Media Player');

   // if not found, try to detect with VisualBasic
   if(!pluginFound && detectableWithVB)
   {
      pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
   }

   // return plugin detection result
   return pluginFound;
}

// Detects a plugin...
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 result
   return pluginFound;
} // End detectPlugin


// Here we write out the VBScript block for MSIE Windows
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>');
}