/**
 * Class constructor
 * TSwf if a wrapper class to draw swf movies into the document
 * or document element and preventing browser to block movies
 *
 * @param {String} name: name or id to identify movie in document
 * @param {String} movie: path to the movie file in server
 * @param {Integer} width: movie width
 * @param {Integer} height: movie height
 */
TSwf = function(name, movie, width, height) {
  this._vars = [];                        // Flashvars
  this._eattrs = {};                      // Embed attributes
  this._oattrs = {};                      // Object attributes
  this._params = {};                      // Object or embed params

  this.name = name;                       // ID or name of the movie object
  this.movie = movie;                     // Movie file in server
  this.width = width ? width : 150;       // Movie width in document
  this.height = height ? height : 150;    // Movie height in document
  this.version = '9,0,0,0';               // Set movie player version
  this.transparent = false;               // Set movie transparency on
  this.preventcache = false;              // Try to prevento loading cached movie

  // Set some standar attributes
  this.set('id', name);
  this.set('src', movie);
  this.set('menu', false);
  this.set('quality', 'high');
  this.set('allowScriptAccess','sameDomain');
  this.set('type', 'application/x-shockwave-flash');
  this.set('classid', 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000');
  this.set('pluginspage', 'http://www.macromedia.com/go/getflashplayer');
}
TSwf.GlobalContainer = null;

/**
 * Add a new variable to be passed in the
 * flashvars movie attribute
 *
 * @param {String} name: var name
 * @param {String} value: var value
 */
TSwf.prototype.addVar = function(name, value) {
  this._vars.push(name + '=' + escape(value));
}

/**
 * Add multiple variables to be passed in the
 * flashvars movie attribute
 *
 * @param {String} vars: a=1&b=2&...
 */
TSwf.prototype.addVars = function(vars) {
  if (vars) {
    vars = vars.split('&');
    for (var i = 0; i < vars.length; i++) {
      this._vars.push(vars[i]);
    }
  }
}

/**
 * Static method to quick draw a movie inside HTML code
 *
 * @param {String} movie: path to the movie file in server
 * @param {Integer} width: movie width
 * @param {Integer} height: movie height
 * @param {Boolean} transparent: (Optional) make background transparent
 * @param {Boolean} vars: (Optional) pass flashvars to the movie
 */
TSwf.draw = function(movie, width, height, transparent, vars) {
  var swf = new TSwf('', movie, width, height);
  swf.transparent = transparent ? true : false;
  swf.addVars(vars);
  swf.draw()
}

/**
 * Draw the movie into the document
 * Use container to draw the movie inside an existing
 * DOM Element in document.
 * If no container then the movie is written directly into the document.
 *
 * @param {Element} container: (Optional) swf container
 */
TSwf.prototype.draw = function(container) {
  container = TDOM.getElement(container);
  if (!container) {
    if (!TSwf.GlobalContainer) {
      TSwf.GlobalContainer = document.createElement('div');
      //TSwf.GlobalContainer.style.backgroundColor = '#f00';
      //TSwf.GlobalContainer.style.height = '100px';
      doc = TDOM.getDocumentElement();
      doc.appendChild(TSwf.GlobalContainer);
    }
    container = TSwf.GlobalContainer;
  }
  var html = this.toHTML();
  container.innerHTML = html;
}

/**
 * Return the HTML representation of this swf movie
 * @return {String} swf HTML code
 */
TSwf.prototype.toHTML = function() {
  // Prevent caching
  if (this.preventcache) {
    var src = this._params["movie"];
    src += src.indexOf('?') != -1 ? '&' : '?';
    src += Math.random();
    this.set('src', src);
  }
  // Set some attributes
  this.set('width', this.width);
  this.set('height', this.height);
  this.set('wmode', this.transparent ? 'transparent' : 'window');
  this.set('codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + this.version);
  // FlashVars
  if (this._vars.length > 0) {
    this.addVars(this._params['flashvars']);
    this.set('flashvars', this._vars.join('&'));
  }
  // Create HTML code
  var html = '';
  if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
    html += '<embed ';
    for (var i in this._eattrs) {
      html += i + '="' + this._eattrs[i] + '" ';
    }
    html += '> </embed>';
  } else {
    html += '<object ';
    for (var i in this._oattrs) {
      html += i + '="' + this._oattrs[i] + '" ';
    }
    html += '>';
    for (var i in this._params) {
      html += '<param name="' + i + '" value="' + this._params[i] + '" /> ';
    }
    html += '</object>';
  }
  return html;
}

/**
 * Set a movie attribute
 * When setting attributes, indentify and separate
 * object, embed and parameters attributes
 *
 * @param {String} name: movie attribute name
 * @param {String} value: movie attribute value
 */
TSwf.prototype.set = function(name, value) {
  switch (name){
    case "classid": this._oattrs[name] = value; break;
    case "pluginspage": this._eattrs[name] = value; break;
    case "src":
    case "movie": this._eattrs["src"] = this._params["movie"] = value; break;
    case "id":
    case "name": this._eattrs["id"] = this._eattrs["name"] = this._oattrs["id"] = this._oattrs["name"] = value; break;
    case "width":
    case "height":
    case "align":
    case "vspace":
    case "hspace":
    case "class":
    case "title":
    case "accesskey":
    case "tabindex": this._eattrs[name] = this._oattrs[name] = value; break;
    case "onafterupdate":
    case "onbeforeupdate":
    case "onblur":
    case "oncellchange":
    case "onclick":
    case "ondblclick":
    case "ondrag":
    case "ondragend":
    case "ondragenter":
    case "ondragleave":
    case "ondragover":
    case "ondrop":
    case "onfinish":
    case "onfocus":
    case "onhelp":
    case "onmousedown":
    case "onmouseup":
    case "onmouseover":
    case "onmousemove":
    case "onmouseout":
    case "onkeypress":
    case "onkeydown":
    case "onkeyup":
    case "onload":
    case "onlosecapture":
    case "onpropertychange":
    case "onreadystatechange":
    case "onrowsdelete":
    case "onrowenter":
    case "onrowexit":
    case "onrowsinserted":
    case "onstart":
    case "onscroll":
    case "onbeforeeditfocus":
    case "onactivate":
    case "onbeforedeactivate":
    case "ondeactivate":
    case "codebase": this._oattrs[name] = value; break;
    default: this._eattrs[name] = this._params[name] = value;
  }
}



