// JavaScript Document
// Name: Flash Detection
// Language: JavaScript/VBScript
// Author: Travis Beckham | squidfingers.com
// Description: Detect if the Flash plug-in is installed and either handle redirection or write Flash content.
// --------------------------------------------------

var Flash = new Object();

/*
-----------------------------------------------------
Argument Description for "Flash.hasVersion"

1. requiredVersion (number): The minimum Flash version required.

-----------------------------------------------------
Possible Return Values for "Flash.hasVersion"

true: has the correct Flash version or greater
false: does not have the correct Flash version
null: the browsers plug-ins cannot be checked

-----------------------------------------------------
*/

Flash.hasVersion = function (requiredVersion){
  requiredVersion = parseInt(requiredVersion);
  if(navigator.plugins != null && navigator.plugins.length > 0){
    var version = 0;
    var plugin = navigator.plugins["Shockwave Flash"];
    if(typeof plugin == "object"){
      var description = plugin.description;
      version = parseInt(description.charAt(description.indexOf(".")-1));
    }
    return (version >= requiredVersion) ? true : false;
  }else if(navigator.appVersion.indexOf("Mac") == -1 && window.execScript){
    Flash.hasVersion_result = false; // MUST be a global variable for execScript.
    for(var i = requiredVersion; i <= requiredVersion+5 && Flash.hasVersion_result != true; i++){
      execScript('on error resume next: Flash.hasVersion_result=IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
    }
    return Flash.hasVersion_result;
  }
  return null;
}

/*
-----------------------------------------------------
Argument Description for "Flash.redirect"

1. requiredVersion (number): The minimum Flash version required.
2. noFlashPage (string): The URL of the "no Flash page".

Notes:
This method redirects to the "no Flash page" if the user's browser cannot be 
checked for the Flash plug-in (MacIE 4.5). To assume these browsers have Flash, 
simply check if the return value of "this.hasVersion" equals false.
Example: if(this.hasVersion(requiredVersion) == false)...

-----------------------------------------------------
*/

Flash.redirect = function (requiredVersion, noFlashPage){
  if(document.referrer.indexOf(noFlashPage) != -1) return;
  if(!this.hasVersion(requiredVersion)) window.location.href = noFlashPage;
}

/*
-----------------------------------------------------
Argument Description for "Flash.write"

1. name (string): The movie name.
2. version (string): The version of the Flash movie.
3. width (string): The width of the movie.
4. height (string): The height of the movie.
7. showMenu (boolean) optional: Show the menu when control clicking the Flash movie
8. varsObj (object) optional: Query string variables to pass into Flash.

-----------------------------------------------------
*/

Flash.write = function (name, version, width, height, showMenu, varsObj){
  var fvars = vars = "";
  if(varsObj){
    for (var i in varsObj) fvars += i + "=" + escape(varsObj[i]) + "&";
    vars = "?"+fvars;
  }
  var swf = '<object';
  swf += ' width="'+width+'"';
  swf += ' height="'+height+'"';
  swf += ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
  swf += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+version+',0,0,0">';
  swf += '<param name="movie" value="'+name+vars+'">';
  if(varsObj) swf += '<param name="FlashVars" value="'+fvars+'">'
  if(!showMenu) swf += '<param name="menu" value="false">';
  swf += '<embed src="'+name+vars+'"';
  swf += ' width="'+width+'"';
  swf += ' height="'+height+'"';
  if(varsObj) swf += ' FlashVars="'+fvars+'"';
  if(!showMenu) swf += ' menu="false"';
  swf += ' type="application/x-shockwave-flash"';
  swf += ' pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">';
  swf += '<\/embed>';
  swf += '<\/object>';
  document.write(swf);
}

// --------------------------------------------------
// Example/Usage:
// Flash.redirect (4, "noflash.html");
// Flash.write ("test.swf", "4", "400", "300", false, {foo:"bar", zig:"zag"});

