// XHTML Flash
// inspired by http://blog.deconcept.com/2004/10/14/web-standards-compliant-javascript-flash-detect-and-embed/

Flash = function(s, v, w, h, c) {
  this.redirect = "";
  this.fv = (v == null)? 6 : v;
  this.alt = "";
  this.attrs = new Object();
  this.params = new Object();
  this.vars = new Object();
  if (s) this.addParam('src', s);
  if (w) this.addAttr('width', w);
  if (h) this.addAttr('height', h);
  if (c) this.addParam('bgcolor', c);
}

Flash.prototype.addDefaults = function() {
  for (var e in Flash.defaultParams) if (this.params[e] == null) this.params[e] = Flash.defaultParams[e];
  for (var e in Flash.defaultVars) if (this.vars[e] == null) this.vars[e] = Flash.defaultVars[e];
  for (var e in Flash.defaultAttrs) if (this.attrs[e] == null) this.attrs[e] = Flash.defaultAttrs[e];
}

Flash.prototype.addParam = function(name, value) {
  value += '';
  if (value.indexOf('"') < 0 && value.indexOf('>') < 0) this.params[name] = value;
}

Flash.prototype.addVar = function(name, value) {
  value += '';
  if (value.indexOf('"') < 0 && value.indexOf('&') < 0 && value.indexOf('<') < 0) this.vars[name] = value;
}

Flash.prototype.addAttr = function(name, value) {
  value += '';
  if (value.indexOf('"') < 0 && value.indexOf('>') < 0) this.attrs[name] = value;
}

Flash.prototype.makeParams = function(tags) {
  var s = '';
  for (var e in this.params) s += (tags == true)? '<param name="' + e + '" value="' + this.params[e] + '" />' : ' ' + e + '="' + this.params[e] + '"';
  return s;
}
Flash.prototype.makeVars = function() {
  var s = '';
  for (var e in this.vars) s += ((s == '')? '' : '&') + e + '=' + this.vars[e];
  return s;
}
Flash.prototype.makeAttrs = function() {
  var s = '';
  for (var e in this.attrs) s += ' ' + e + '="' + this.attrs[e] + '"';
  return s;
}

Flash.prototype.make = function() {
  var ih = "";
  var v = this.makeVars();
  if (v) {
    this.params.src += '?' + v;
    this.addParam('flashvars', v);
  }
  this.addDefaults();
  if (window.ActiveXObject && navigator.userAgent.indexOf('Mac') == -1) {
    this.addAttr('classid', 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000');
    this.addAttr('codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + this.fv + ',0,0,0');
    ih += '<object ' + this.makeAttrs() + '>' + this.makeParams(true) + '</object>';
  } else {
    this.addAttr('type', 'application/x-shockwave-flash');
    this.addAttr('pluginspage', 'http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash');
    ih += '<embed ' + this.makeAttrs() + ' ' + this.makeParams() + '></embed>';
  }
  return ih;
}

Flash.prototype.write = function(e, f) {
  if (Flash.detect(this.fv) || f) {
    if (e && document.getElementById) document.getElementById(e).innerHTML = this.make();
    else document.write(this.make());
  } else {
    if (this.redirect != "") document.location.replace(this.redirect);
    else if (e && document.getElementById && this.alt != "") document.getElementById(e).innerHTML = this.alt;
  }
}

Flash.getVersion = function() {
  var fv = 0;
  if (navigator.plugins && navigator.plugins["Shockwave Flash"] && navigator.plugins["Shockwave Flash"].description) {
    fv = parseInt(navigator.plugins["Shockwave Flash"].description.substring(16));
  }
  if (window.execScript != undefined) {
    r = false;
    for (var i = 9; i >= 3 && r != true; i--) {
      window.execScript('on error resume next: r = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.' + i + '"))', 'VBScript');
      fv = i;
    }
  }
  return fv;
}

Flash.detect = function(fv) {
  return (Flash.getVersion() >= fv);
}

Flash.defaultParams = {
  quality:'high',
  loop:'false',
  menu:'false'
}
Flash.defaultVars = {
}
Flash.defaultAttrs = {
  align:'middle'
}
