/* =========================================================

// jquery.embedquicktime.js

// Date: 2007-11-04
// Author: Andreas Haugstrup Pedersen
// Mail: andreas [at] solitude [dot] dk
// Web: http://www.solitude.dk/archives/embedquicktime/

Copyright (c) 2007 Andreas Haugstrup Pedersen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

// ========================================================= */
(function(jQuery) {
// Locate hvlog entries and embed them.
jQuery.embedquicktime = function(options) {  
  // Accepted file types.
  var types = new Array('video/quicktime', 'video/mp4', 'video/x-m4v');
  var files = new Array('mov', 'mp4', 'm4v');
  function in_array(needle, haystack) {
    for (i in haystack) {
      if (needle == haystack[i]) {
        return true;
      }
    }
    return false;
  }
  // Loop over hvlog elements
  jQuery('.hvlog').each(function(){
    // Check if element contains quicktime link.
    jQuery(this).find('a[rel=enclosure]').each(function(){
      var a = jQuery(this);
      var href = a.attr('href');
      var hreftype = href.substr(href.lastIndexOf('.')+1);
      var type = a.attr('type');
      if (in_array(hreftype, files) || in_array(type, types)) {
        // We have a quicktime.
        jQuery(a).click(function(){
          jQuery(a).embedquicktime(options);
          return false;
        });
      }      
    });
  });
  return this;
};
// Embed an object
jQuery.fn.embedquicktime = function(options) {
  // Global options.
  var opts = jQuery.extend({}, jQuery.fn.embedquicktime.defaults, options);
  return this.each(function(){
    // Grab hvlog.
    var hvlog = jQuery(this).parents('.hvlog');
    // Element options.
    var o = hvlog.metadata ? jQuery.extend({}, opts, hvlog.metadata()) : opts;
    // Set source
    var src = jQuery(this).attr('href');
    if (o.src) {
      src = o.src;
    }
    // Grab width/height of thumbnail.
    var thumb = hvlog.find('img').eq(0);
    if (thumb.length > 0) {
      var width = thumb.width();
      var height = thumb.height();
      if (o.width) {
        width = o.width;
      }
      if (o.height) {
        height = o.height;
      }      
      if (!o.controller || o.controller != 'true') {
        height = parseInt(height)+16;
      }
      // Loop through options and build <param> and <embed> attributes.
      var params = '';
      var embed = '';
      for (i in o) {
        if (i != 'height' && i != 'width' && i != 'src' && i != 'autoplay' && i != 'pluginspage') {
          params = params+'<param name="'+i+'" value="'+o[i]+'">';
          embed = embed+' '+i+'="'+o[i]+'"';          
        }
      }
      // Insert video code.
      hvlog.empty();
      hvlog.append('<object width="'+width+'" height="'+height+'" autoplay="'+o.autoplay+'" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="'+src+'"><param name="autoplay" value="'+o.autoplay+'">'+params+'<embed src="'+src+'" width="'+width+'" height="'+height+'" autoplay="'+o.autoplay+'" pluginspage="http://www.apple.com/quicktime/download/"'+embed+'></embed></object>');
      return true;
    }
    return false;
  });
};
// Default options.
jQuery.fn.embedquicktime.defaults = {
  autoplay: 'true',
  scale: 'aspect'
};
})(jQuery);