jQuery.fn.videoPlayer = function(options) {
  var embed_template =
      "<object width=\"660\" height=\"370\" data=\"http://cdn.itvs.org/flowplayer/flowplayer.commercial-3.1.5.swf\" id=\"ie-id\" type=\"application/x-shockwave-flash\">\n" +
      "<param name=\"movie\" value=\"http://cdn.itvs.org/flowplayer/flowplayer.commercial-3.1.5.swf\" />\n" +
      "<param name=\"allowfullscreen\" value=\"true\" />\n<param name=\"allowscriptaccess\" value=\"always\" />\n" +
      "<param name=\"flashvars\" value=\'config={\"canvas\":{\"backgroundColor\":\"#000\",\"backgroundGradient\":\"none\"},\"clip\":{\"scaling\":\"fit\",\"provider\":\"simplecdn\"},\"plugins\":{\"controls\":{\"url\":\"http://cdn.itvs.org/flowplayer/flowplayer.controls-3.1.5.swf\",\"autoHide\":\"always\",\"hideDelay\":2500,\"backgroundColor\":\"transparent\"},\"simplecdn\":{\"url\":\"http://cdn.itvs.org/flowplayer/flowplayer.rtmp-3.1.3.swf\",\"netConnectionUrl\":\"rtmpt://cdnv.itvs.org/play\"}},\"playlist\":[{\"url\":\"http://cdn.itvs.org/${filename}.jpg\",\"autoPlay\":true,\"scaling\":\"scale\",\"provider\":\"simplecdn\"}, {\"url\":\"mp4:itvs.org/${filename}-1024.mov\",\"autoPlay\":false,\"scaling\":\"fit\",\"provider\":\"simplecdn\"}]}'/>\n" +
      "</object>";

  extractFilename = function(playlist) {
    if (playlist.length > 2) {
      alert("Unexpected playlist length: " + playlist.length);
    }
    var filename = playlist[playlist.length - 1].url;
    filename = filename.substring(filename.indexOf('/') + 1);
    filename = filename.substring(0, filename.indexOf('.') - 5);
    return filename;
  },

  updateEmbedCode = function(filename) {
    $(settings.embedTarget).text(embed_template.replace(/\${filename}/gi, filename));
  },

  settings = jQuery.extend({
    key : "#$e812137af6c445fe40b",
    embedTarget: '.embed-dialog textarea.embed-code',
    canvas: {
      backgroundColor: '#000',
      backgroundGradient: 'none'
    },
    clip : {
      scaling: 'fit',
      provider: 'simplecdn',
      onStart: function(clip) {
        trackVideoEvent('Play', clip.url);
      },
      onPause: function(clip) {
        trackVideoEvent('Pause', clip.url);
      },
      onFinish: function(clip) {
        trackVideoEvent('Finish', clip.url);
      }
    },
    plugins: {
      controls: {
        url: 'http://cdn.itvs.org/flowplayer/flowplayer.controls-3.1.5.swf',
        autoHide: 'always',
        hideDelay: 2500,
        backgroundColor: 'transparent'
      },
      simplecdn: {
        url: 'http://cdn.itvs.org/flowplayer/flowplayer.rtmp-3.1.3.swf',
        netConnectionUrl: 'rtmpt://cdnv.itvs.org/play'
      }
    },
    onLoad: function() {
      if ($(settings.embedTarget).length > 0) {
        var filename = extractFilename($f('current-video').getConfig(false).playlist);
        updateEmbedCode(filename);
      }
    },
    onPlaylistReplace: function(playlist) {
      if ($(settings.embedTarget).length > 0) {
        var filename = extractFilename(playlist);
        updateEmbedCode(filename);
      }
    }
  }, options, true);

  return this.each(function() {
    $(this).flowplayer({src : 'http://cdn.itvs.org/flowplayer/flowplayer.commercial-3.1.5.swf', wmode : 'transparent'}, settings);
  });
};

