/**
 *  Flash plugin for converting anchors into flash objects.
 *  @author:  M. Alsup (malsup at gmail dot com)
 *  @version: 1.0.3  (04/27/2007)
 *  http://www.malsup.com/jquery/media/
 *  Free beer and free speech. Enjoy!
 *
 *  This plugin converts anchor tags into flash objects.
 *  Demos can be found at: http://malsup.com/jquery/media/
 *
 *  NOTE:  This plugin uses (and requires) swfobject.js for proper flash detection.
 *         You must include swfobjects.js on any page that uses this plugin.
 *         @see http://blog.deconcept.com/swfobject/
 *
 *
 *  Sample HTML:
 *      before:  <a href="myMovie.swf" class="flash">Watch my movie!</a>
 *
 *      after:   <div class="flash"><object ... <embed ...   <br />Watch my movie!</div>
 *
 *  Usage:
 *      $('a.flash').flash();
 *
 *  Or using options:
 *      $('a.flash').flash({
 *          caption:  false // supress caption text
 *      });
 *
 *
 *  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.flash { ... }
 *      div.flash { ... }
 *  </style>
 *
 *  Options are passed to the 'flash' 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 flash player* (default: 450)
 *  height:      height of flash player* (default: 370)
 *  version:     version of flash player* required (default: 7)
 *  background:  background color of flash player* (default: #fff)
 *  cls:         classname(s) to be applied to new element (default: anchor classname)
 *  src:         source location of flash .swf file (default: value of href attr)
 *  caption:     text to be used as caption; use false for no caption (default: value of anchor text)
 *  params:      object which holds parameters for the object/embed tags (default: empty object)
 *  flashvars:   object which holds parameters for the flashvars attribute (default: empty object)
 *  elementType: type of element to replace anchor (span, div, etc) (default: 'div')
 *
 *  * height, width, version and background values can be embedded in the classname using the following syntax:
 *  fallback added p.scheit fallback bezeichnet das fallbackdiv
 *    <a class="flash w:450 h:450 ver:7 bg:fff fallback: #id></a>
 */
jQuery.fn.flash = function(options) {
    if (typeof SWFObject == 'undefined') {
      alert('swfobject.js required');
      return this; // fast fail (swfobjects.js required)
    }
    return this.each(function(index) {
        var $this = jQuery(this);
        var cls = this.className;

        var opts = jQuery.extend({
            width:          ((cls.match(/w:(\d+)/)||[])[1]) || $this.text() | 450, // default to youtube width
            height:         ((cls.match(/h:(\d+)/)||[])[1]) || $this.text() | 370, // default to youtube height
            version:        ((cls.match(/ver:(\d+)/)||[])[1]) || 7,
            background:     ((cls.match(/bg:#?([0-9a-fA-F]+)/)||[])[1]) || 'none',
            cls:            cls,
            src:            $this.attr('href') || $this.attr('src'),
            caption:        $this.text(),
            fallbackdiv:    ((cls.match(/fallback:([-#0-9a-zA-Z]+)/) || [])[1]) || $this.text(),
            fallbackimage:  $this.text() || $this.attr('href')+'.jpg' || $this.attr('src')+'.jpg',
            params:         {},
            flashvars:      {},
            elementType:    'div'
        }, options || {});

        // convert anchor to span/div/whatever...
        var id = this.id ? (' id="'+this.id+'"') : '';
        var $el = jQuery('<' + opts.elementType + id  + ' class="' + opts.cls + '" style="width:'+opts.width+'px"></' + opts.elementType + '>');
        $this.after($el).remove();
        if (opts.background != 'none') {
          var so = new SWFObject(opts.src, 'movieplayer'+index, opts.width, opts.height, opts.version, '#'+opts.background);
        } else {
          var so = new SWFObject(opts.src, 'movieplayer'+index, opts.width, opts.height, opts.version);
        }
        for (var p in opts.params)
            so.addParam(p, opts.params[p]);
        for (var fv in opts.flashvars)
            so.addVariable(fv, opts.flashvars[fv]);

        // fallback
        if (so.write($el[0])==false) {
          if (opts.fallbackdiv != '') { 
            var parent = $el.parent();
            $el.remove();
            $(parent).append($(opts.fallbackdiv).children().clone(true));
          } else {
            $el.append('<img src="'+opts.fallbackimage+'" alt="" title="" />');
          }
        }

        if (opts.caption) $el.append('<div>' + opts.caption + '</div>');
    });
};
