var Playlist = {
  list: null,
  count: 0,
  current: null,
  playing: false,
  delayed_next: null,
  delayed_count: null,
  
  initialize:function(opts){
    this.list = opts.list;
    this.count = this.list.length; 
    this.current = null;
    
    return; // DISABLED FOR NOW!!!
    
    if($('reels_player_wr')){
      reel_id = $('reels_player_wr').down('.reels_section_wr').id.split('_')[1];
      if($('reel_result_item_'+reel_id)) {
        $('reel_result_item_'+reel_id).down('div').removeClassName('content_box_dblue_wr').addClassName('content_box_mgrey_wr');
        this.current = 'reel_result_item_'+reel_id;
      }
    }
      
  },
  play:function(){
    if(this.list.first() == this.current) {
      $(Player.container.id).sendEvent("PLAY");
    } else {
      this.load_reel($(this.list.first()).down('input[type="hidden"]').value);
    }    
  },
  
  load_reel:function(reel_id, in_showcase){
    
    this.cancel_next();
      
    if(this.current)
      try{
        $(this.current).down('.content_box_mgrey_wr').removeClassName('content_box_mgrey_wr').addClassName('content_box_dblue_wr');
      }catch(e){log(e)}
    
    $('reel_result_item_'+reel_id).down('div').removeClassName('content_box_dblue_wr').addClassName('content_box_mgrey_wr');
    
    // Effect.ScrollTo('reels_results_wr'); 
    
    this.current = 'reel_result_item_'+reel_id;
    this.playing = true; 
        
    $('reels_player_wr').setStyle({
      height:$('reels_player_wr').getDimensions().height.toString()+'px'}
    );
    
    new Ajax.Request('/reels/'+reel_id, {
      parameters:{in_showcase:in_showcase},
      asynchronous:true, 
      evalScripts:true, 
      method:'get',
      onComplete: (function(request){
        new Effect.Morph('reels_player_wr', {
          style: {
            height: $('reels_player_wr').down('div').getDimensions().height.toString()+'px'
          },
          afterFinish: function(){ $('reels_player_wr').setStyle({height:'auto'}) }
        })
      }).bind(this)
      
    });
    
    return false;    
  },  
  start_next_countdown:function(reel_id){
    $('reel_' + $(this.current).down("input[type=hidden]").value).down('.countdown').show();
    this.countdown(5);
    this.delay_next(reel_id, 5000);
  },
  countdown:function(num){
    $('reel_' + $(this.current).down("input[type=hidden]").value).down('.countdown .timeleft').update(num);    
    if(num > 1)
      this.delayed_count = window.setTimeout( "Playlist.countdown("+(num-1)+")", 1000 );
    else 
      this.delayed_count = null;
  },
  delay_next:function(reel_id, ms){
    // TODO id this for clearing later if needed
    this.delayed_next = window.setTimeout( function(){Playlist.load_reel(reel_id)}, (ms || 1000));  
  },
  cancel_next:function(){
    window.clearTimeout(this.delayed_count);
    window.clearTimeout(this.delayed_next);
    this.delayed_count = null;
    this.delayed_next = null;
    
    if(this.current)
      try{$('reel_' + $(this.current).down("input[type=hidden]").value).down('.countdown').hide()}catch(e){log(e)};    
  } 
     
}
