/*
class to create flash objects
================================
*/

function flash(loc, w, h, color, txt) {
  this.loc = loc || "";
  this.w = w || 1000;
  this.h = h || 28;
  this.color = color || "000000";
  this.txt = txt || "";
  if (config.logic_folder != undefined) {
    this.folderLoc = config.logic_folder + "/flash/";
  } else {
    this.folderLoc = "";
  }
  this.flashVars = "";
  this.scale = "noscale";
  this.salign = "tl";
  this.base = ".";
}

flash.prototype._getObject = function(id) {
  var swfLocation = this.folderLoc + this.loc;

  var id = id || "";
  var flashObj = document.createElement('div');
  flashObj.className = "box";
  flashObj.style.width = this.w + "px";
  flashObj.style.height = this.h + "px";

  // construct flashVars
  var flash_vars = "baseWidth=" + this.w;
  flash_vars += "&baseHeight=" + this.h;
  flash_vars += "&baseColor=0x" + this.color;
  flash_vars += "&label=" + this.txt;
  flash_vars += "&nomenclatureLoc=../data/nomenclature.xml";
  if (config.product_type) flash_vars += "&learningunitType=" + config.product_type;
  flash_vars += this.flashVars;

  // create flash object
  if (document.all) {
    var f = document.createElement('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">');
    if (id != "") {
      f.setAttribute("id", id);
    }
    f.setAttribute("width", this.w);
    f.setAttribute("height", this.h);
    f.setAttribute("codebase", "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0");
    // construct params
    param = document.createElement("param");
    param.setAttribute("name", "allowScriptAccess");
    param.setAttribute("value", "always");
    f.appendChild(param);
    param = document.createElement("param");
    param.setAttribute("name", "quality");
    param.setAttribute("value", "high");
    f.appendChild(param);
    param = document.createElement("param");
    param.setAttribute("name", "movie");
    param.setAttribute("value", swfLocation);
    f.appendChild(param);
    // wmode transparent makes IE slow onmouseout :'(
    param = document.createElement("param");
    param.setAttribute("name", "wmode");
    param.setAttribute("value", "Transparent");
    f.appendChild(param);
    param = document.createElement("param");
    param.setAttribute("name", "flashVars");
    param.setAttribute("value", flash_vars);
    f.appendChild(param);
    param = document.createElement("param");
    param.setAttribute("name", "base");
    param.setAttribute("value", this.base);
    f.appendChild(param);
    param = document.createElement("param");
    param.setAttribute("name", "scale");
    param.setAttribute("value", this.scale);
    f.appendChild(param);
    param = document.createElement("param");
    param.setAttribute("name", "salign");
    param.setAttribute("value", this.salign);
    f.appendChild(param);
  } else {
    var f = document.createElement("embed");
    if (id != "") f.setAttribute("id", id);
    f.setAttribute("src", swfLocation);
    f.setAttribute("quality", "high");
    f.setAttribute("width", this.w);
    f.setAttribute("height", this.h);
    f.setAttribute("type", "application/x-shockwave-flash");
    f.setAttribute("pluginspage", "http://www.macromedia.com/go/getflashplayer");
    f.setAttribute("allowScriptAccess", "always");
    f.setAttribute("wmode", "transparent");
    f.setAttribute("flashVars", flash_vars);
    f.setAttribute("base", this.base);
    f.setAttribute("scale", this.scale);
    f.setAttribute("salign", this.salign);
  }

  flashObj.appendChild(f);
  f = null;
  return flashObj;

}

flash.prototype._appendObject = function(prnt, objID) {

  var objID = objID || "";

  var f = document.createElement('div');
  f.id = objID + "Container";
  f.className = "box";
  f.style.width = this.w + "px";
  f.style.height = this.h + "px";
  f.innerHTML = this.txt;
  prnt.appendChild(f);

  // construct flashVars
  var flash_vars = "baseWidth=" + this.w;
  flash_vars += "&baseHeight=" + this.h;
  flash_vars += "&baseColor=0x" + this.color;
  flash_vars += "&label=" + this.txt;
  flash_vars += "&nomenclatureLoc=../data/nomenclature.xml";
  if (config.product_type) flash_vars += "&learningunitType=" + config.product_type;
  flash_vars += this.flashVars;

  // UFO (http://www.bobbyvandersluis.com/ufo/)
  var FO = {
    id: objID,
    movie: this.folderLoc + this.loc,
    width: this.w,
    height: this.h,
    majorversion: "8",
    build: "0",
    base: ".",
    quality: "high",
    wmode: "transparent",
    scale: "noscale",
    salign: "tl",
    flashvars: flash_vars
  };
  UFO.create(FO, objID + "Container");

  return document.getElementById(objID + "Container");

}

flash.prototype._getHTML = function(id) {
  var id = id || "";
  var obj = this._getObject(id);
  var theHTML = '<div style="width: ' + this.w + 'px; height: ' + this.h + 'px;">' + obj.innerHTML + '</div>';
  return theHTML;
}

var externalProbSet = false;
//this fixes the 'out of memory error at line 56'
function cleanup() {
  __flash_unloadHandler = function() {
    if (externalProbSet) {
      return
    };
    externalProbSet = true;
    var objs = document.getElementsByTagName('OBJECT');
    if (objs) {
      for (var i = 0; i < objs.length; i++) {
        var obj = objs[i];
        var theObj = eval(obj);
        theObj.style.display = "none";
        for (var prop in theObj) {
          if (typeof(theObj[prop]) == "function") {
            theObj[prop] = null
          }
        }
      }
    }
    if (__flash_savedUnloadHandler != null && __flash_savedUnloadHandler != "") {
      //this was the actual bug:
      //_flash_savedUnloadHandler gets called in _flash_savedUnloadHandler and
      // caused a stackoverflow
      var temp__flash_savedUnloadHandler = __flash_savedUnloadHandler;
      __flash_savedUnloadHandler = null;
      temp__flash_savedUnloadHandler();
    }
  }
  if (window.onunload != __flash_unloadHandler) {
    __flash_savedUnloadHandler = window.onunload;
    window.onunload = __flash_unloadHandler;
  }
}