/*
 * QTObject embed
 * http://blog.deconcept.com/2005/01/26/web-standards-compliant-javascript-quicktime-detect-and-embed/
 *
 * by Geoff Stearns (geoff@deconcept.com, http://www.deconcept.com/)
 *
 * v1.0.2 - 02-16-2005
 *
 * Embeds a quicktime movie to the page, includes plugin detection
 *
 * Usage:
 *
 *  myQTObject = new QTObject("path/to/mov.mov", "movid", "width", "height");
 *  myQTObject.write();
 *
 */
 
var ua = navigator.userAgent.toLowerCase();
var client = {
  isStrict:   document.compatMode == 'CSS1Compat',
  isOpera:    ua.indexOf('opera') > -1,
  isIE:       ua.indexOf('msie') > -1,
  isIE7:      ua.indexOf('msie 7') > -1,
  isSafari:   /webkit|khtml/.test(ua),
  isWindows:  ua.indexOf('windows') != -1 || ua.indexOf('win32') != -1,
  isMac:      ua.indexOf('macintosh') != -1 || ua.indexOf('mac os x') != -1,
  isLinux:    ua.indexOf('linux') != -1
};
client.isBorderBox = client.isIE && !client.isStrict;
client.isSafari3 = client.isSafari && !!(document.evaluate);
client.isGecko = ua.indexOf('gecko') != -1 && !client.isSafari;
var markup = {};
var RE = {
  domain:         /:\/\/(.*?)[:\/]/, // domain prefix
  inline:         /#(.+)$/, // inline element id
  rel:            /^(light|shadow)box/i, // rel attribute format
  gallery:        /^(light|shadow)box\[(.*?)\]/i, // rel attribute format for gallery link
  unsupported:    /^unsupported-(\w+)/, // unsupported media type
  param:          /\s*([a-z_]*?)\s*=\s*(.+)\s*/, // rel string parameter
  empty:          /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i // elements that don't have children
};

function isQTInstalled(){
  var detected = false;
  if(navigator.plugins && navigator.plugins.length){
    for (var i = 0, len = navigator.plugins.length; i < len; ++i){
      if(navigator.plugins[i].name.indexOf('QuickTime') > -1){
        detected = true;
        break;
      }
    }
  } else {
    try{
        var axo = new ActiveXObject('QuickTime.QuickTime');
        if(axo) detected = true;
    }catch(e){}
  }
  return detected;
};

QTObject = function(mov, id, w, h) {
  var autoplay = true;
  var controls = true;
  this.mov = mov;
  this.id = id;
  this.width = w;
  this.height = !isNaN(parseInt(h, 10)) ? parseInt(h, 10) : 300;
  this.params = new Object();
  
  markup = {
    tag:        'object',
    id:         this.id,
    name:       this.id,
    height:     controls ? this.height + 16 : this.height, 
    width:      this.width,
    children:   [
        { tag: 'param', name: 'src', value: mov },
        { tag: 'param', name: 'scale', value: 'aspect' },
        { tag: 'param', name: 'controller', value: controls },
        { tag: 'param', name: 'autoplay', value: autoplay }
    ],
    kioskmode:  'true'
  };
  if(client.isIE){
    markup.classid = 'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B';
    markup.codebase = 'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0';
  }else{
    markup.type = 'video/quicktime';
    markup.data = mov;
  }
};

QTObject.prototype.addParam = function(name, value) {
  this.params[name] = value;
};

QTObject.prototype.getHTML = function() {
  return this.createHTML(markup);
};

QTObject.prototype.createHTML = function(obj){
  var html = '<' + obj.tag;
  for(var attr in obj){
      if(attr == 'tag' || attr == 'html' || attr == 'children') continue;
      if(attr == 'cls'){
          html += ' class="' + obj['cls'] + '"';
      }else{
          html += ' ' + attr + '="' + obj[attr] + '"';
      }
  }
  if(RE.empty.test(obj.tag)){
      html += '/>';
  }else{
      html += '>';
      var cn = obj.children;
      if(cn){
          for(var i = 0, len = cn.length; i < len; ++i){
              html += this.createHTML(cn[i]);
          }
      }
      if(obj.html) html += obj.html;
      html += '</' + obj.tag + '>';
  }
  return html;
};

QTObject.prototype.write = function(elementId) {
  if(isQTInstalled()) {
    if (elementId) {
      document.getElementById(elementId).innerHTML = this.getHTML();
    } else {
      document.write(this.getHTML());
    }
  } 
};