/* Juliks Quicktime Scripting toolbox. Use with moderation. */

Quicktime = new Object();

/* Get either the object element or the embed element, depending of the browser */
Quicktime.getMov = function(movieContainerId) {
  var cnt = $(movieContainerId);
  if(!cnt) return;
  
  var embed = cnt.getElementsByTagName("embed")[0];
  return embed;
}

/* Where is the playhead parked now? */
Quicktime.getCurrentSecondCount = function(movieContainerId) {
  var mov = Quicktime.getMov(movieContainerId);
  return mov.GetTime() / mov.GetTimeScale();      
}

Quicktime.getDurationInSeconds = function(movieContainerId) {
  var mov = Quicktime.getMov(movieContainerId);
  return mov.GetDuration() / mov.GetTimeScale();      
}

Quicktime.isLoaded = function(movieContainerId) {
  var mov = Quicktime.getMov(movieContainerId);
  return mov.GetMovieSize() == mov.GetMaxBytesLoaded();
}

Quicktime.Embedder = new Object({
  // Make an embed-object combo in-place
  emitInPlace: function() {
    // first get the script element we're inside of
    var allScripts = document.getElementsByTagName('script');
    var into = allScripts[allScripts.length -1];
    
//  if (typeof(window.ActiveXObject) != 'undefined') {
//    var injectedNode = document.createElement('object');
//    injectedNode.setAttribute('classid', "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B");
//      injectedNode.setAttribute('codebase', "http://www.apple.com/qtactivex/qtplugin.cab");
//      injectedNode.setAttribute('pluginspage', "http://www.apple.com/quicktime/download/");
//    var attrFunc = this._pairToParam;
//  } else {
      var injectedNode = document.createElement('embed');
      injectedNode.appendChild(document.createTextNode('QuickTime Movie'));
      var attrFunc = this._pairToAttribute;
//  }

    // odd arguments are attributes, even arguments are values
    for(var i = 0; i < arguments.length; i++) {
      if((i % 2) == 0) {
        attrFunc(injectedNode, arguments[i], arguments[i+1]);
      }
    }
    into.parentNode.insertBefore(injectedNode, into);
  },
  
  _pairToAttribute: function(intoNode, key, value) {
    try {
      intoNode.setAttribute(key, value);
    } catch(e) {
    //  console.error(e.toString());
    }
  },

  _pairToParam: function(intoNode, key, value) {
    // Check against the list of params which always go into the attributes
    switch(key) {
      case 'width':
        _pairToAttribute(intoNode, key, value);
        break;
      case 'height':
        _pairToAttribute(intoNode, key, value);
        break;
      default:
        var paramNode = document.createElement('param');
        paramNode.setAttribute('name', key);
        paramNode.setAttribute('value', value);
        intoNode.appendChild(paramNode);
    }
  }
});

/* This is a tricky one. What we do is the following: we
constantly monitor the state of the player. When it starts
playing, we hide all the parts of the layout which are irrelevant
to the movie itself except for description. We do that by hooking up
an additional stylesheet that fades the interface elements of the site
down. As soon as the movie stops playing the stylesheet is removed,
restoring the original formatting. The property we use for getting
state from QuickTime is

ie: document.movie1.GetRate();
0 = paused, 1 = playing.
as seen on 
*/
Quicktime.intervalId = [];
Quicktime.styleSwitch = function() {
  try {
    if(window.nowPlaying) {
      window.document.body.addClassName('playing');
    } else {
      window.document.body.removeClassName('playing');
    }
  } catch(e) {}
};

Quicktime.attachStyleChanges = function(movieContainerId) {
  
  var bdy = window.document.body;
  
  var evtHandler = function() {
    var mov = Quicktime.getMov(movieContainerId);
    
    if (typeof(mov) == 'undefined') {
      return;
    }
    
    $$('.noqt').each(function(e){ Element.hide(e) });
    
    /* When the movie is loading we will get errors trying to access it's methods,
     so we wrap it to suppress the exception */
    try {
      if(mov.GetRate() == 1) {
        if (!window.nowPlaying) {
          window.nowPlaying = true;
        }
      } else  {
        window.nowPlaying = false;
      }
    } catch(e) {
      /* The object is not ready for playback yet, wait some */
      // console.error(e.toString());
    }
  }
  if (Quicktime.intervalId) {
    clearInterval(Quicktime.intervalId);
    Quicktime.intervalId = false;
  }
  Quicktime.intervalId = setInterval(evtHandler, 200 );
}

// Capture links that have to open an external player
Quicktime.captureQtl = function(e) {
  var link = Event.element(e).up("a");
  if (!link) return;
  if (!link.hasClassName("toQtplayer")) return;
  
  // Inject an Embed which will start the Quicktime Player, and immediately remove it
  // after one second. Very evil.
  Event.stop(e);
  
  emb = document.createElement("embed");
  emb.setAttribute("src", link.href);
  emb.setAttribute("href", link.href);
  emb.setAttribute("autohref", "true");
  emb.setAttribute("movietitle", "shploink");
  emb.setAttribute("width", "1");
  emb.setAttribute("height", "1");
  emb.setAttribute("target", "quicktimeplayer");
  emb.setAttribute("autoplay", "false");
  emb.setAttribute("hidden", "hidden");
  emb = link.appendChild(emb);
  window.setTimeout(function() { link.removeChild(emb) }, 10 * 1000);
}

Event.observe(window, "load", function() {
  Event.observe(document.body, "click", Quicktime.captureQtl);
});

setInterval(Quicktime.styleSwitch,  250);