// initialize global variables
var detectableWithVB = false;
var pluginFound = false;

//--------------------
// Image Specific
//--------------------

// Code for SHOWING
//--------------------

// Shows an image
function ShowImage(objectName, imageFile, imageAlt,  width, height)
{
    document.write(GetImageCode(objectName, imageFile, imageAlt,  width, height));
}

// Shows an image with a link
function ShowImageWithLink(objectName, imageFile, imageAlt, linkLocation, width, height)
{
    document.write(GetImageCodeWithLink(objectName, imageFile, imageAlt, linkLocation, width, height));
}

// Shows an image with a map
function ShowImageWithMap(objectName, imageFile, imageAlt, mapName, width, height)
{
    document.write(GetImageCodeWithMap(objectName, imageFile, imageAlt, mapName, width, height));
}

// Code for getting CODE
//--------------------

// Gets Image Code
function GetImageCode(objectName, imageFile, imageAlt,  width, height)
{
   return '<img src="' + imageFile + '" width="' + width + '" height="' + height + ' alt="' + imageAlt + '">';
}

// Gets Image Code with Link
function GetImageCodeWithLink(objectName, imageFile, imageAlt, linkLocation, width, height)
{
   return '<a href="' + linkLocation + '"><img src="' + imageFile + '" width="' + width + '" height="' + height + '" alt="' + imageAlt + '" border="0"></a>';
}

// Gets Image Code with ImageMap
function GetImageCodeWithMap(objectName, imageFile, imageAlt, mapName, width, height)
{
   return '<img src="' + imageFile + '" border="0" width="' + width + '" height="' + height + ' alt="' + imageAlt + '" usemap=#' + mapName + '>';
}

//--------------------
// Flash and Image Specific
//--------------------

// Code for SHOWING
//--------------------

// Shows a FLASH Object
function ShowFlash(objectName, flashFile, flashVersion, flashArgs, flashTransparency, width, height)
{
    document.write(GetFlashCode(objectName, flashFile, flashVersion, flashArgs, flashTransparency, width, height));
}

// Shows a FLASH Object or Image
function ShowFlashOrImage(objectName, flashFile, flashVersion, flashArgs, flashTransparency, imageFile, imageAlt, width, height)
{
    document.write(GetFlashOrImageCode(objectName, flashFile, flashVersion, flashArgs, flashTransparency, imageFile, imageAlt, width, height));
}

// Shows a FLASH Object or Image with a Map
function ShowFlashOrImageWithMap(objectName, flashFile, flashVersion, flashArgs, flashTransparency, imageFile, imageAlt, mapName, width, height)
{
    document.write(GetFlashOrImageCodeWithMap(objectName, flashFile, flashVersion, flashArgs, flashTransparency, imageFile, imageAlt, mapName, width, height));
}

// Shows a FLASH Object or Image with a Link
function ShowFlashOrImageWithLink(objectName, flashFile, flashVersion, flashArgs, flashTransparency, imageFile, imageAlt, linkLocation, width, height)
{
    document.write(GetFlashOrImageCodeWithLink(objectName, flashFile, flashVersion, flashArgs, flashTransparency, imageFile, imageAlt, linkLocation, width, height));
}


// Code for GETTING CODE
//--------------------

// Gets FLASH Code
function GetFlashCode(objectName, flashFile, flashVersion, flashArgs, flashTransparency, width, height)
{
   var flashCode = '';
   

   flashCode += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + flashVersion + ',0,0,0" width="' + width + '" height="' + height + '" id = "' + objectName + '">';
   flashCode += '<param name="movie" value="' + flashFile + '">';
   flashCode += '<param name="quality" value="high">';
   if (flashTransparency == true)
   {
        flashCode += '<param name="wmode" value="transparent">';
   }

   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) + '" ';
   }
   var transparentEmbed = '';
   if (flashTransparency == true)
   {
      transparentEmbed = 'wmode="transparent" ';
   }
   flashCode += '<embed src="' + flashFile + '" ' + flashVarsEmbed + transparentEmbed + 'quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '" name="' + objectName + '"></embed>';
   flashCode += '</object>';
   
   return flashCode;
}

// Gets the FLASH code if available, or Image code
function GetFlashOrImageCode(objectName, flashFile, flashVersion, flashArgs, flashTransparency, imageFile, imageAlt, width, height)
{
   if(detectFlash(flashVersion) == true)
      return GetFlashCode(objectName, flashFile, flashVersion, flashArgs, flashTransparency, width, height);
   else
      return GetImageCode(objectName, imageFile, imageAlt, width, height);
}

// Gets the FLASH code if available, or Image code with imagemap - without flash args
function GetFlashOrImageCodeWithMap(objectName, flashFile, flashVersion, flashArgs, flashTransparency, imageFile, imageAlt, mapName, width, height)
{
   if(detectFlash(flashVersion) == true)
      return GetFlashCode(objectName, flashFile, flashVersion, flashArgs, flashTransparency, width, height);
   else
      return GetImageCodeWithMap(objectName, imageFile, imageAlt, mapName, width, height);
}


// Gets the FLASH code if available, or Image code with link
function GetFlashOrImageCodeWithLink(objectName, flashFile, flashVersion, flashArgs, flashTransparency, imageFile, imageAlt, linkLocation, width, height)
{
   if(detectFlash(flashVersion) == true)
      return GetFlashCode(objectName, flashFile, flashVersion, flashArgs, flashTransparency, width, height);
   else
      return GetImageCodeWithLink(objectName, imageFile, imageAlt, linkLocation, width, height);
}


// Misc Code
//--------------------

// Gets the flash vars querystring
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;
}

// Passes a variable to a flash object
function PassVariableToFlash(flashObject, varTitle, varValue)
{
   flashObject.SetVariable(varTitle, varValue);
}



//--------------------
// Plugin Detection
//--------------------


// 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(version)
{
   // 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.' + version);
   }

   // 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;
}

function BypassIEUpdate()
{
   theObjects = document.getElementsByTagName("object");
   for (var i = 0; i < theObjects.length; i++) {
      // BypassIEUpdate(theObjects[i]);
      theObjects[i].outerHTML = theObjects[i].outerHTML;
   }
}


/*
// Begin Javascript code to write out VBScript code block for Windows IE
*/

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>');
}