/**
 * This code is based on the SWFObject (1.4.2) - http://blog.deconcept.com/swfobject/
 *
 * SWFObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 */

/// Creates the Array.push function if not supported by the browsers JavaScript implementation (IE5).
if (Array.prototype.push == null) {
  Array.prototype.push = function(arg)
  {
    this[this.length] = arg;
    return this.length;
  };
}


// LEDZ hierarchy objects
if (typeof br == "undefined")
  var br = new Object();
if (typeof br.com == "undefined")
  br.com = new Object();
if (typeof br.com.ledz == "undefined")
  br.com.ledz = new Object();


/**
 * Browser constructor.
 * Represents the running browser and serves information like type, version, etc.
 */
br.com.ledz.Browser = function()
{
	if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length)
		this.type = 0;
	else
		this.type = 1;
};
br.com.ledz.Browser.prototype =
{
	// Returns true if the running browser is Internet Explorer; othwerwise false
	isIE:function()
	{ return (this.type == 1); }
};
if (typeof br.com.ledz.browser == "undefined")
  br.com.ledz.browser = new br.com.ledz.Browser();


// LEDZ Flash hierarchy objects
if (typeof br.com.ledz.swf == "undefined")
  br.com.ledz.swf = new Object();

/**
 * Version constructor.
 * Encapsulates a player version for easier comparition.
 */
br.com.ledz.swf.Version = function(version)
{
  this.major = version[0] != null ? parseInt(version[0]) : 0;
  this.minor = version[1] != null ? parseInt(version[1]) : 0;
  this.rev   = version[2] != null ? parseInt(version[2]) : 0;
};
br.com.ledz.swf.Version.prototype = 
{
	/// Returns true if the given version is equal or lower than this one; otherwise false.
	supports:function(arg)
  { return (this.major >= arg.major && this.minor >= arg.minor && this.rev >= arg.rev); },

	/// Returns true if this version supports the feature called Express Install; otherwise false.
	supportsExpressInstall:function()
	{ return this.supports(new br.com.ledz.swf.Version([6,0,65])); },

	/// Returns this version in the format used for the codeBase parameter in the object element.
	codeBaseFormat:function()
  { return this.major + "," + this.minor + "," + this.rev + ",0" }
};


/**
 * Player constructor.
 * Represents a player and serves with functions like if the player plugin is installed.
 */
br.com.ledz.swf.Player = function()
{
	this.installed = false;
	this.version   = new br.com.ledz.swf.Version([0,0,0]);

  // non IE browsers
  if (!br.com.ledz.browser.isIE()){
    var x = navigator.plugins["Shockwave Flash"];
    if (x && x.description) {
			this.installed = true;
      this.version   = new br.com.ledz.swf.Version(x.description.replace(/([a-zA-Z]|\s)+/,"").replace(/(\s+r|\s+b[0-9]+)/,".").split("."));
		}
  }

  // IE browser
	else {
		var axo = null;
		// test for 7.x
		try {
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		}	catch(e) {}

		// test for 6.x
		if (axo == null)
			try {
				axo          = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				this.version = new br.com.ledz.swf.Version([6,0,21]);
				// throws if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
				axo.AllowScriptAccess = "always";
			} catch(e) {
					if (this.version == 6)
						return;
			}

		// test for x.x
		if (axo == null)
			try {
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			}
			catch(e) {}

		if (axo != null) {
			this.installed = true;
			this.version   = new br.com.ledz.swf.Version(axo.GetVariable("$version").split(" ")[1].split(","));
		}
	}
};
br.com.ledz.swf.Player.prototype = 
{
	/// Returns true if the player plugin is installed; otherwise false.
	isInstalled:function()
	{ return this.installed; },

	/// Returns true if the given version is equal or lower than the installed one; otherwise false.
	supportsVersion:function(arg)
  { return this.version.supports(arg); },

	/// Returns true if the installed version supports the feature called Express Install; otherwise false.
	supportsExpressInstall:function()
	{ return this.version.supportsExpressInstall(); },

	/// Returns a Version object instance for the installed version.
	getVersion:function()
	{ return this.version; }
};
if (typeof br.com.ledz.swf.player == "undefined")
  br.com.ledz.swf.player = new br.com.ledz.swf.Player();


/**
 * SWFObject Constructor.
 * @param fileName           File path and name to swf file
 * @param id                 ID of object or embed tag; Embed tag will also have
 *                           this value set as it's name attribute for files that
 *                           take advantage of swliveconnect
 * @param width              Width of Flash movie
 * @param height             Height of Flash movie
 * @param version            Required player version for Flash content; Can be a
 *                           string in the format of
 *                           'majorVersion.minorVersion.revision' (e.g. "6.0.65")
 *                           or just major version (e.g. "6")
 * @para, showIfNotInstalled 'false' for hidding flash movies if player is not
 *                           installed; default: true
 * @param doVersionCheck     'false' for bypassing version check; default: true
 * @param useExpressInstall  'true' to upgrade users using the ExpressInstall
 *                            feature; default: true
 */
br.com.ledz.SWFObject = function(fileName, id, width, height, version,
  showIfNotInstalled, doVersionCheck, useExpressInstall, xiRedirectUrl)
{
  if (!document.getElementById)
    return;

  this.params    = new Object();
  this.variables = new Object();

  this.fileName           = fileName;
  this.id                 = id;
  this.width              = width;
  this.height             = height;
  this.version            = new br.com.ledz.swf.Version(version ? version.toString().split(".") : [0,0,0]);
  this.showIfNotInstalled = (typeof showIfNotInstalled != "undefined") ? showIfNotInstalled : true;
  this.doVersionCheck     = (typeof doVersionCheck != "undefined") ? doVersionCheck : true;
  this.useExpressInstall  = (typeof useExpressInstall != "undefined") ? useExpressInstall : true;
  this.xiRedirectUrl      = xiRedirectUrl != "undefined" ? xiRedirectUrl : window.location;

  this.addParam("quality", "high");
};
br.com.ledz.SWFObject.prototype =
{
  addParam:function(key, value)
  { this.params[key] = value; },
  getParams:function()
  { return this.params; },
  
  addVariable:function(key, value)
  { this.variables[key] = value; },
  getVariablePairs:function()
  {
    var res = new Array();
    for(var key in this.variables)
      res.push(key + "=" + this.variables[key]);
    return res;
  },
  
  getHTML:function(doExpressInstall)
  {
    var res = "";

	  // non IE browsers
		if (!br.com.ledz.browser.isIE()) {
      if (doExpressInstall)
        this.addVariable("MMplayerType", "PlugIn");

      res = "<embed"
          + " type=\"application/x-shockwave-flash\""
          + " src=\"" + this.fileName + "\""
          + " width=\"" + this.width + "\""
          + " height=\"" + this.height + "\""
          + " id=\"" + this.id + "\""
          + " name=\"" + this.id + "\""
          + " pluginspage=\"http://www.macromedia.com/go/getflashplayer\"";

      var entries = this.getParams();
      for (var key in entries)
        res += " " + key + "=\"" + entries[key] + "\"";

      var vars = this.getVariablePairs().join("&");
      if (vars.length>0)
        res += " flashvars=\"" + vars + "\"";
      res += "/>";
    }

		// IE browsers
    else {
			if (doExpressInstall)
        this.addVariable("MMplayerType","ActiveX");

      res = "<object"
          + " id=\"" + this.id + "\""
          + " classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\""
          + " codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=" + this.version.codeBaseFormat() + "\""
          + " width=\"" + this.width + "\""
          + " height=\"" + this.height + "\">"
          + "<param name=\"movie\" value=\"" + this.fileName + "\" />";

      var entries = this.getParams();
      for (var key in entries)
        res += "<param name=\"" + key + "\" value=\"" + entries[key] + "\" />";

      var vars = this.getVariablePairs().join("&");
      if (vars.length>0)
        res += "<param name=\"flashvars\" value=\"" + vars + "\" />";

			res += "</object>";
    }
    return res;
	},
  
  write:function(id)
  {
		// just return if not installed and hidden is choosen
		if (!br.com.ledz.swf.player.isInstalled()
				&& !this.showIfNotInstalled){
			return false;
		}

		var doExpressInstall = false;
    if (!br.com.ledz.swf.player.supportsVersion(this.version)
        && this.useExpressInstall
        && br.com.ledz.swf.player.supportsExpressInstall()) {
      doExpressInstall = true;
      this.addVariable("MMredirectURL", escape(this.xiRedirectUrl));
      document.title = document.title.slice(0,47) + " - Flash Player Installation";
      this.addVariable("MMdoctitle", document.title);
    }

    var div = (typeof id == "string") ? document.getElementById(id) : id;
    div.innerHTML = this.getHTML(doExpressInstall);
    return true;
  }
};


/// fix for video streaming bug
br.com.ledz.swf.cleanup = function()
{
	if (br.com.ledz.browser.isIE()) {
		var objects = document.getElementsByTagName("OBJECT");
		for (var i=0; i < objects.length; i++) {
			objects[i].style.display = 'none';
			for (var x in objects[i]) {
				if (typeof objects[i][x] == 'function') {
					objects[i][x] = null;
				}
			}
		}
	}
}
if (typeof window.onunload == 'function') {
	var oldunload = window.onunload;
	window.onunload = function()
	{
    br.com.ledz.swf.cleanup();
		oldunload();
	}
} else {
  window.onunload = br.com.ledz.swf.cleanup;
}


var SWFObject = br.com.ledz.SWFObject;
