var started = false;
var loadSuccess = false;
var playlist = null;

function initPlayer() {
  $('fullscreen_nav').down('.close_button').observe('click', function() { player.resize() });
  $('fullscreen_nav').down('.prev_link').observe('click', function() { player.playPrevious() });
  $('fullscreen_nav').down('.next_link').observe('click', function() { player.playNext() });

  playlist = new Carousel('play_list', {
    nextElementID: 'next_link', 
    prevElementID: 'prev_link',
    scrollInc: 10,
    size: maxItem
  });
  
  (function() { player.play(firstClipNumber); }).delay(1);
}

function getUpdate(type, pr1, pr2, pid) {
  // if (type != 'time') {
  //     debug(type + ',pr1:' + pr1 + ',pr2:' + pr2);
  //   }
  if (type == 'item') {
    loadSuccess = false;
  } else if (type == 'state') {
    // 0 = ready/paused
    // 1 = buffering
    // 2 = playing
    // 3 = finished
    if (pr1 == 2) {
      loadSuccess = true;
    } else if (pr1 == '3') {
      if (!loadSuccess) {
        alert("Looks like the current clip may be broken. Please try again.  If the problem persists, please report using the link below or send e-mail to admin@tube4vn.com");
      } else {
        player.playNext();
      }      
    }
  }
}

var timer = null;
function player_proxy(type, evt) {
  if (type == 'stream' && evt == 'stop' && timer == null) {
    timer = (function() { timer = null; player.playNext(); }).delay(1);
  }
}