(function($){
  
  var body_listener;
  var focused;
  var key_listener = function(e){
    if(e.which == 32){
      e.preventDefault();
      $(document).unbind('keydown', key_listener);
      focused.hideSample().showWork();
    }
  };
  
  $.fn.samplePlayer = function(){
    this
      .mouseover(function(e){
        focused = $(this).showSample();
        $(document).bind('keydown', key_listener);
      })
      .click(function(e){
        $(this).showWork();
      })
    return this;
  }
  
  $.fn.videoInstance = function(){
    return $(this).parent().find('.sample_video_player').eq(0);
  }
  
  $.fn.samplePlayerGuid = function(){
    if(this.data('player_guid') == undefined){
      var listener = ChromelessPlayer.createListener();
      var $this = this;
      this.data('player_guid', listener.guid);
      listener.onMouseOut = function(state){
        $this.hideSample();
        $(document).unbind('keydown', key_listener);
      };
      listener.onClick = function(){
        $this.hideSample().showWork();
        $(document).unbind('keydown', key_listener);
      }
    }
    return this.data('player_guid');
  }
  
  $.fn.showSample = function(){
    
    $('a[sample]').hideSample();
    var $this = this;
    var sample = this.attr('sample');
    var player = $this.parent().find('.sample_video_player');
    if(player.length > 0){
      player.playVideo().show();
      return this;
    }
    if(sample && sample != ''){
      var container = $('<div class="sample_video_player"></div>')
      this.parent().append(container);
      container.flash({
        src: '/flash/ChromelessPlayer.swf',
        width: 96,
        height: 76,
        wmode: 'transparent',
        flashvars: {
          'video' : sample,
          'width' : this.width(),
          'height' : this.height(),
          'auto_play' : 'yes',
          'loop' : 'yes',
          'guid' : $this.samplePlayerGuid(),
          'mouse_events' : 'yes',
          'mute' : 'yes',
          'scaleMode' : 'exactFit'
        }
      });
    }
    
    //hide all other samples
    
    return this;
  }
  
  $.fn.hideSample = function(){
    this.each(function(i,e){
      $(e).videoInstance().stopVideo().hide();
    })
    return this;
  }
  $.fn.stopVideo = function(){
    var flash = this.find('embed');
    if(flash && flash[0] != undefined){
      try{
        flash[0].stop();        
      }catch(e){
        
      }
    }
    return this;
  }
  $.fn.playVideo = function(){
    var flash = this.find('embed');
    if(flash[0] && flash[0] != undefined){
      try{
        flash[0].play();        
      }catch(e){
        
      }
    }
    return this;
  }
})(jQuery)

jQuery(document).ready(function(){
  jQuery('a[sample]').samplePlayer();
  jQuery('body').mouseover(function(e){
    if($(e.target).parents('.work_preview').length == 0){
      $('a[sample]').hideSample();
    }
  })
  
});
