/**
 *  Quicktime plugin for converting anchors into Quicktime objects.
 *  @author:  M. Alsup (malsup at gmail dot com)
 *  @version: 1.0
 *  http://www.malsup.com/jquery/media/
 *  Free beer and free speech. Enjoy!
 *
 *  This plugin converts anchor tags into Quicktime objects.
 *  Demos can be found at: http://malsup.com/jquery/media/
 *
 *
 *  Simple Usage:
 *  ------------
 *  $('a.quicktime').quicktime();
 *
 *  The sample script above will turn an anchor like this:
 *
 *  <a href="myMovie.mov" class="quicktime">Watch my movie!</a>
 *
 *  into a div like this:
 *
 *  <div class="quicktime">
 *      <object
 *          codebase="http://www.apple.com/qtactivex/qtplugin.cab#6,0,2,0"
 *          classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B">
 *          <param value="myMovie.mov" name="src"/>
 *          <embed width="400" height="400" src="myMovie.mov"
 *              pluginspage="http://www.apple.com/quicktime/download/"> </embed>
 *      </object>
 *  </div>
 *
 *
 *
 *
 *  Advanced Usage:
 *  --------------
 *  $('a.quicktime').quicktime({
 *      attrs:   { autoplay: true, tabindex: 10, param1: 'value1' }, 
 *      caption: false, // supress caption text
 *      xhtml:   false  // use html end-tags
 *  });
 *
 *  The sample script above will turn an anchor like this:
 *
 *  <a href="myMovie.mov" class="quicktime w:400 h:400">Watch my movie!</a>
 *
 *  into a div like this:
 *
 *  <div class="quicktime">
 *      <object width="400" height="400" 
 *          codebase="http://www.apple.com/qtactivex/qtplugin.cab#6,0,2,0"
 *          classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
 *          autoplay="true" tabindex="10">
 *          <param value="myMovie.mov" name="src">
 *          <param value="param1" name="value1">
 *          <embed width="400" height="400" src="myMovie.mov" autoplay="true"
 *              pluginspage="http://www.apple.com/quicktime/download/" > </embed>
 *      </object>
 *  </div>
 *
 *
 *
 *  Notes:
 *  -----
 *  The base tag doesn't *have* to be an anchor but it makes a lot of sense to do it that way 
 *  so please consider it. If you use something other than an anchor you'll need to provide 
 *  an options argument with a src attribute set to the path of the media file.
 *
 *  By default, classnames assigned to the anchor will be assigned to the div.  This makes it
 *  convenient for styling:
 *
 *  <style type="text/css">
 *      a.quicktime { ... }
 *      div.quicktime { ... }
 *  </style>
 *
 *
 *  Options are passed to the 'quicktime' function using a single Object.  The options
 *  Object is a hash of key/value pairs.  The following option keys are supported:
 *
 *  Options:
 *  -------
 *  width:       width of quicktime player* (default: 200)
 *  height:      height of quicktime player* (default: 200)
 *  version:     version of quicktime player required (default: '6,0,2,0')
 *  cls:         classname(s) to be applied to new element (default: anchor classname)
 *  src:         source location of quicktime .mov file (default: value of href attr)
 *  caption:     text to be used as caption; use false for no caption (default: value of anchor text)
 *  attrs:       hash of attributes for the object/embed tags; eg: { autoplay: false } (default: empty object)
 *  elementType: type of element to replace anchor (span, div, etc) (default: 'div')
 *  xhtml:       true for XHTML-style markup, false for HTML (default: true)
 *
 *  * height and width values can be embedded in the classname using the following syntax:
 *    <a class="quicktime w:450 h:450></a>
 *
 *
 *  @example
 *  $('a.quicktime').quicktime();
 *  @desc convert all anchors with 'quicktime' class to Quicktime objects
 *
 *
 *  @example
 *  var options = {
 *      attrs:   { autoplay: true, tabindex: 10, param1: 'value1' },
 *      caption: false,
 *      xhtml:   false
 *  };
 *  $('#myMovie').quicktime(options);
 *  @desc convert 'myMovie' element into a div containing the Quicktime object/embed structure
 *        using the given args.
 */
jQuery.fn.quicktime = function(options) {
    return this.each(function() {
        var $this = jQuery(this);
        var cls = this.className;
        
        var opts = jQuery.extend({
            width:        ((cls.match(/w:(\d+)/)||[])[1]) || 200,
            height:       ((cls.match(/h:(\d+)/)||[])[1]) || 200,
            version:     '6,0,2,0',
            cls:          cls,
            src:          $this.attr('href') || $this.attr('src'),
            caption:      $this.text(),
            attrs:        {},
            elementType:  'div',
            xhtml:        true
        }, options || {});
        
        var endTag = opts.xhtml ? ' />' : '>';
        var allowObjectAtts = 'name,id,accesskey,title,class,tabindex,noexternaldata';
        var skip = 'width,height,src,classid,codebase';
        var skipEmbedAttrs  = skip + ',id,class,title,accesskey,noexternaldata,pluginspage';
        
        var objectAttrs = [], objectParms = [], embedAttrs = [];
        for (var key in opts.attrs) {
            if (allowObjectAtts.indexOf(key) > -1) 
                objectAttrs.push(key +'="' + opts.attrs[key] + '"');
            else if (skip.indexOf(key) == -1) 
                objectParms.push('<param name="'+ key +'" value="' + opts.attrs[key] + '"' + endTag);
            if (skipEmbedAttrs.indexOf(key) == -1) 
                embedAttrs.push(key +'="' + opts.attrs[key] + '"');
        }

        var a = ['<object width="' + opts.width + '" height="' + opts.height + '" '];
        a.push(objectAttrs.join(" "));
        a.push(' classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"');
        a.push(' codebase="http://www.apple.com/qtactivex/qtplugin.cab#version=' + opts.version + '">');
        a.push('<param name="src" value="' + opts.src + '"' + endTag);
        a.push(objectParms.join(""));
        a.push('<embed width="' + opts.width + '" height="' + opts.height + '" src="' + opts.src + '" ' );
        a.push(embedAttrs.join(" "));
        a.push(' pluginspage="http://www.apple.com/quicktime/download/"> </embed>');
        a.push('</object>');
        if (opts.caption) a.push('<br' + endTag + opts.caption);
        
        // convert anchor to span/div/whatever...
        var $el = jQuery('<' + opts.elementType + ' class="' + opts.cls + '"></' + opts.elementType + '>');
        $this.after($el).remove();
        $el.html(a.join(''));
    });
};
