
var videoplayer = function(spec) {

  spec = spec || {};

  var that = {},
      link = spec.link || $('a.video'),
      container = spec.container || $('div.video_player'),
      height = 290,
      width = 320;


  var createDialog = function() {
    container.dialog({
      autoOpen: false,
      modal: true,
      maxWidth: 730,
      maxHeight: 510,
      width: width,
      height: height,
      zIndex: 5000,
      open: function() {
        container.css({
          background: 'url(/images/ajax-loader.gif) no-repeat center'
        });
        container.bgiframe();
        container.find('object').remove().appendTo(container);
        resizePlayer(width, height);
      },
      beforeclose: function() {
        if($.browser.msie && $.browser.version === '6.0') window.location.reload();
      }
    });//.find('object').css('visibility', 'hidden');
  };

  var setHeight = function(h) {
    height = h || height;
  };

  var setWidth = function(w) {
    width = w || width;
  };
  
  var play = function() {
    createDialog();
    showPlayer();
  };

  link.click(function(event) {
    event.preventDefault();

    play();
  });

  var showPlayer = function() {
    container.dialog('open');
  };

// PUBLIC METHODS

  var resizePlayer = function(width, height) {
    var object = container.find('object');

    setHeight(height);
    setWidth(width);

    container.dialog('option', { 'height': height + 51, 'width': width, 'position': ['center','center'] });
    // object.css('visibility', 'visible');
  };
  that.resizePlayer = resizePlayer;
  
  that.play = play;

  return that;

};

var create_video_container = function(index, link) {
  var source = $(link).attr('href');
  var container = $('<div></div>', {"class":"player", css:{"display":"none"}});

  var id = "player" + index;

  container.append('<div id="' + id + '"></div>');

  $(link).after(container);

  swfobject.embedSWF("/flash/videoplayer.swf", id, "800", "600", "9.0", '', { flv: source, autoPlay: "true" }, { base: '/flash/', wmode: "transparent", scale: 'noorder', allowfullscreen: 'true'});
  return container;
};

var video_hijack = function() {
  var videos = $('div.user_content a').filter(function() {
    return $(this).attr('href').match(/\.(?:flv|m4v|mp4)/);
  });

  var players = [];

  videos.each(function(index) {
    var container = create_video_container(index, this);

    players.push(new videoplayer({link: $(this), container: container}));
  });

  return players;
};

$(function() {
  
  // For slideshow video links
  $('div#carousel a.video').live('click', function(event) {
    event.preventDefault();
    var container = create_video_container('123', this);
    var video = new videoplayer({link: $(this), container: container});
    video.play();
  });

  var players = video_hijack();

  players.push(new videoplayer());

  $('a[href=assvideo]').click(function(event) {
    event.preventDefault();
    $('a.video').click();
  });

  flashSize = function(width, height) {
    $(players).each(function() {
      this.resizePlayer(width, height);
    });
  };
});



