// Embedded Active Content (Flash Player, Windows Media Player, QuickTime Player, and Real Player)
// Author: Radu Buzea [www.hypnosis.ro]
// Version 1.0 [22 Feb. 2007]
// ================================================================================================

function trim(str) {
  if (str == "") return "";
  var str2 = str.replace(/^\s+/g, "");
  if (str2 == "") return "";
  return str2.replace(/\s+$/g, "");
}

function check_attr_name(attr_name) {
  attr_name = trim(attr_name);
  if (attr_name == "") return false;
  var chrPos = attr_name.search("[^A-Za-z_]");
  if (chrPos >= 0) return false;
  return true;
}

function parse_params(params) {
  params = trim(params);
  var plist = params.split("|");
  var result = new Array();
  var param = "", attr_name = "", attr_value = "";
  var equal_pos = -1;
  for (var i = 0; i < plist.length; i++) {
    param = trim(plist[i]);
    equal_pos = param.indexOf("="); 
    if (equal_pos > 0) {
      attr_name = trim(param.substring(0, equal_pos));
      attr_value = trim(param.substr(equal_pos + 1));
      if (attr_name != "" && attr_value != "" && check_attr_name(attr_name)) result[attr_name] = attr_value;
    }
  }
  return result;
}

function generate_id(prefix) {
  var d = new Date();
  var hour = d.getHours();
  if (hour < 10) hour = "0" + hour;
  var min = d.getMinutes();
  if (min < 10) min = "0" + min;
  var sec = d.getSeconds();
  if (sec < 10) sec = "0" + sec;
  var msec = d.getMilliseconds();
  if (msec < 10) msec = "00" + msec;
  else if (msec < 100) msec = "0" + msec;
  return prefix + hour + min + sec + msec;
}

function write_error(error_message) {
  if (trim(error_message) == "") return;
  document.write('<div style="border:1px solid gray;padding:10px;color:#E50000;font-family:Tahoma,Arial,' +
    'sans-serif;font-size:13px">' + trim(error_message) + '</div>');
}

// Detect Flash Player
// ------------------------------------------------------------------------------------------------
var flashVersion = 0;

function detect_flash() {
  var result = false;  
  if (navigator.plugins && navigator.plugins.length) {
    var fplugin = navigator.plugins["Shockwave Flash"];
    if (fplugin) {
      result = true;
      if (fplugin.description) {
        fversion = fplugin.description;
        flashVersion = fversion.charAt(fversion.indexOf('.') - 1);
      }
    } else flashinstalled = 1;
    if (navigator.plugins["Shockwave Flash 2.0"]) {
      result = true;
      flashVersion = 2;
    }
  } else if (navigator.mimeTypes && navigator.mimeTypes.length) {
    x = navigator.mimeTypes['application/x-shockwave-flash'];
    if (x && x.enabledPlugin) flashinstalled = 2;
    else flashinstalled = 1;
  } else if (window.ActiveXObject) {
    var flashObj = null;
    for (var fv = 10; fv > 0; fv--) {
      try { flashObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + fv); } 
      catch (e) {}
      if (flashObj != null) {
        result = true;
        flashVersion = fv;
        break;
      }
    }
  }
  return result;
}

// Embedded Flash Player
// ------------------------------------------------------------------------------------------------

function swf_content(swf_file, swf_width, swf_height, swf_params) {
  swf_file = trim(swf_file);
  if (swf_file == "") {
    write_error('The SWF file was not specified in the <b>swf_content</b> function.');
    return;
  }
  var noId = true;
  var sObject = '<object type="application/x-shockwave-flash" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' +
    'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"';
  var sEmbedded = '<embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" ' +
    'src="' + swf_file + '"';
  var objW = parseInt(swf_width);
  var objH = parseInt(swf_height);
  var attributes = parse_params(swf_params);
  if (objW > 0) {
    sObject += ' width="' + objW + '"';
    sEmbedded += ' width="' + objW + '"';
  }
  if (objH > 0) {
    sObject += ' height="' + objH + '"';
    sEmbedded += ' height="' + objH + '"';
  }
  var sParams = '<param name="movie" value="' + swf_file + '" />';
  for (var attr_name in attributes) {
    attr_name = attr_name.toLowerCase();
    if (attr_name == "id") { 
      sObject += ' id="' + attributes[attr_name] + '"';
      noId = false;
    } else if (attr_name == "align") sObject += ' align="' + attributes[attr_name] + '"';
    else if (attr_name == "style") sObject += ' style="' + attributes[attr_name] + '"';
    else if (attr_name == "title") sObject += ' title="' + attributes[attr_name] + '"';
    else if (attr_name != "movie") {
      sParams += '<param name="' + attr_name + '" value="' + attributes[attr_name] + '" />';
      sEmbedded += ' ' + attr_name + '="' + attributes[attr_name] + '"';
      continue;
    }
    sEmbedded += ' ' + attr_name + '="' + attributes[attr_name] + '"';
  }
  if (noId) sObject += ' id="' + generate_id("flash") + '"';
  document.write(sObject + '>' + sParams + sEmbedded + ' /></object>');
}

// Embedded Windows Media Player
// ------------------------------------------------------------------------------------------------
var isWMP7plus = false;

function wmp_content(wmp_file, wmp_width, wmp_height, wmp_params) {
  wmp_file = trim(wmp_file);
  if (wmp_file == "") {
    write_error('The video file was not specified in the <b>wmp_content</b> function.');
    return;
  }
  var noId = true;
  var wmp7plus = null;
  if (window.ActiveXObject) {
    try { wmp7plus = new ActiveXObject("WMPlayer.OCX.7"); } catch (e) {}
  } else if (window.GeckoActiveXObject) {
    try { wmp7plus = new GeckoActiveXObject("WMPlayer.OCX.7"); } catch (e) {}
  }
  if (wmp7plus) {
    isWMP7plus = true;
    var sObject = '<object type="application/x-oleobject" classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6"';
    var sParams = '<param name="url" value="' + wmp_file + '" />';
  } else {
    isWMP7plus = false;
    var sObject = '<object type="application/x-oleobject" classid="clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95" ' +
      'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715"';
    var sParams = '<param name="filename" value="' + wmp_file + '" />';
  }
  var sEmbedded = '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/windows/windowsmedia/" ' +
    'src="' + wmp_file + '" filename="' + wmp_file + '"';
  var objW = parseInt(wmp_width);
  var objH = parseInt(wmp_height);
  var attributes = parse_params(wmp_params);
  if (objW > 0) sObject += ' width="' + objW + '"';
  if (objH > 0) sObject += ' height="' + objH + '"';
  for (var attr_name in attributes) {
    attr_name = attr_name.toLowerCase();
    if (attr_name == "id") { 
      sObject += ' id="' + attributes[attr_name] + '"';
      noId = false;
    } else if (attr_name == "align") sObject += ' align="' + attributes[attr_name] + '"';
    else if (attr_name == "style") sObject += ' style="' + attributes[attr_name] + '"';
    else if (attr_name == "title") sObject += ' title="' + attributes[attr_name] + '"';
    else if (attr_name != "url" && attr_name != "filename") {
      sParams += '<param name="' + attr_name + '" value="' + attributes[attr_name] + '" />';
      sEmbedded += ' ' + attr_name + '="' + attributes[attr_name] + '"';
      continue;
    }
    sEmbedded += ' ' + attr_name + '="' + attributes[attr_name] + '"';
  }
  if (noId) sObject += ' id="' + generate_id("wmplayer") + '"';
  document.write(sObject + '>' + sParams + sEmbedded + ' /></object>');
}

// Embedded QuickTime Player
// ------------------------------------------------------------------------------------------------

function qtime_content(qt_file, qt_width, qt_height, qt_params) {
  qt_file = trim(qt_file);
  if (qt_file == "") {
    write_error('The video file was not specified in the <b>qtime_content</b> function.');
    return;
  }
  var noId = true;
  var sObject = '<object type="application/x-oleobject" classid="clsid:02bf25d5-8c17-4b23-bc80-d3488abddc6b" ' +
    'codebase="http://www.apple.com/qtactivex/qtplugin.cab"';
  var sParams = '<param name="src" value="' + qt_file + '" /><param name="qtsrc" value="' + qt_file + '" />';
  var sEmbedded = '<embed pluginspage="http://www.apple.com/quicktime/download/" src="' + qt_file + '" '+
    'qtsrc="' + qt_file + '"';
  var objW = parseInt(qt_width);
  var objH = parseInt(qt_height);
  var attributes = parse_params(qt_params);
  if (objW > 0) sObject += ' width="' + objW + '"';
  if (objH > 0) sObject += ' height="' + objH + '"';
  for (var attr_name in attributes) {
    attr_name = attr_name.toLowerCase();
    if (attr_name == "id") { 
      sObject += ' id="' + attributes[attr_name] + '"';
      noId = false;
    } else if (attr_name == "align") sObject += ' align="' + attributes[attr_name] + '"';
    else if (attr_name == "style") sObject += ' style="' + attributes[attr_name] + '"';
    else if (attr_name == "title") sObject += ' title="' + attributes[attr_name] + '"';
    else if (attr_name != "src" && attr_name != "qtsrc") {
      sParams += '<param name="' + attr_name + '" value="' + attributes[attr_name] + '" />';
      sEmbedded += ' ' + attr_name + '="' + attributes[attr_name] + '"';
      continue;
    }
    sEmbedded += ' ' + attr_name + '="' + attributes[attr_name] + '"';
  }
  if (noId) sObject += ' id="' + generate_id("quicktime") + '"';
  document.write(sObject + '>' + sParams + sEmbedded + ' /></object>');
}

// Embedded Real Player
// ------------------------------------------------------------------------------------------------

function real_content(real_file, real_width, real_height, real_params) {
  real_file = trim(real_file);
  if (real_file == "") {
    write_error('The video file was not specified in the <b>qtime_content</b> function.');
    return;
  }
  var noId = true;
  var sObject = '<object type="application/x-oleobject" classid="clsid:cfcdaa03-8be4-11cf-b84b-0020afbbccfa"';
  var sParams = '<param name="src" value="' + real_file + '" />';
  var sEmbedded = '<embed pluginspage="http://www.real.com/player/" src="' + real_file + '"';
  var objW = parseInt(real_width);
  var objH = parseInt(real_height);
  var attributes = parse_params(real_params);
  if (objW > 0) sObject += ' width="' + objW + '"';
  if (objH > 0) sObject += ' height="' + objH + '"';
  for (var attr_name in attributes) {
    attr_name = attr_name.toLowerCase();
    if (attr_name == "id") { 
      sObject += ' id="' + attributes[attr_name] + '"';
      noId = false;
    } else if (attr_name == "align") sObject += ' align="' + attributes[attr_name] + '"';
    else if (attr_name == "style") sObject += ' style="' + attributes[attr_name] + '"';
    else if (attr_name == "title") sObject += ' title="' + attributes[attr_name] + '"';
    else if (attr_name != "src") {
      sParams += '<param name="' + attr_name + '" value="' + attributes[attr_name] + '" />';
      sEmbedded += ' ' + attr_name + '="' + attributes[attr_name] + '"';
      continue;
    }
    sEmbedded += ' ' + attr_name + '="' + attributes[attr_name] + '"';
  }
  if (noId) sObject += ' id="' + generate_id("real") + '"';
  document.write(sObject + '>' + sParams + sEmbedded + ' /></object>');
}