var playlist = new Array();
var playlist_order_list = new Array();
var playlist_current = 0;
var playlist_current_order = 0;
var playlist_is_playing = false;
var playlist_auto_checkbox = false;
var playlist_container = false;
var playlist_ROWS = 1;
var playlist_HEIGHT = 28;
var playlist_play_pause_button = false;
var playlist_mute_button = false;
var playlist_repeat_button = false;
var playlist_shuffle_button = false;
var playlist_bar_pointers = new Array();
var playlist_bar_step = 0;
var playlist_progress_bar_pointers = new Array();
var playlist_progress_bar_step = 0;
var playlist_repeat_mode = 'off';
var playlist_shuffle_mode = 'off';
var playlist_hidden_param = false;
var playlist_hidden_value = false;
var playlist_started = false;
var playlist_over_bar = false;
var playlist_over_progress_bar = false;
var playlist_duration_span = false;
var playlist_loader = false;
var playlist_title = false;
var playlist_loader_iframe = false;

var ytplayer;

function playlist_play() {
  if (ytplayer) {
    ytplayer.playVideo();
    return true;
  }
  else {
    return false;
  }
}
function playlist_playing() {
  playlist_is_playing = true;
  playlist_get_buttons();
  if (playlist_play_pause_button) {
    if (playlist_play_pause_button.src.substr(-9) == '_over.gif') {
      playlist_play_pause_button.src = 'images/playlist_pause_over.gif';
    }
    else {
      playlist_play_pause_button.src = 'images/playlist_pause.gif';
    }
  }
}
function playlist_pause() {
  if (ytplayer) ytplayer.pauseVideo();
}
function playlist_paused() {
  playlist_is_playing = false;
  playlist_get_buttons();
  if (playlist_play_pause_button) {
    if (playlist_play_pause_button.src.substr(-9) == '_over.gif') {
      playlist_play_pause_button.src = 'images/playlist_play_over.gif';
    }
    else {
      playlist_play_pause_button.src = 'images/playlist_play.gif';
    }
  }
}
function playlist_play_pause() {
  if (playlist_is_playing) playlist_pause();
  else if (!playlist_play()) playlist_next();
}

function playlist_get_buttons() {
  if (!playlist_play_pause_button) {
    playlist_play_pause_button = document.getElementById('playlist_play_pause_button');
  }
  if (!playlist_repeat_button) {
    playlist_repeat_button = document.getElementById('playlist_repeat_button');
  }
  if (!playlist_shuffle_button) {
    playlist_shuffle_button = document.getElementById('playlist_shuffle_button');
  }
  if (!playlist_mute_button) {
    playlist_mute_button = document.getElementById('playlist_mute_button');
  }
}

function playlist_next(auto) {
  if (auto && (playlist_auto_status() == 0 || playlist_current == 0)) {
    return false;
  }
  playlist_get_current_order();
  var next = 0;
  if (playlist_repeat_mode == 'off') {
    if (playlist_current_order < playlist_order_list.length) {
      next = playlist_order_list[playlist_current_order];
    }
  }
  else if (playlist_repeat_mode == 'all') {
    if (playlist_current_order < playlist_order_list.length) {
      next = playlist_order_list[playlist_current_order];
    }
    else {
      next = playlist_order_list[0];
    }
  }
  else if (playlist_repeat_mode == 'track') {
    next = playlist_current;
  }

  if (next > 0) {
    if (next == playlist_current) {
      ytplayer.seekTo(0, true);
      ytplayer.playVideo();
    }
    else {
      playlist_current = next;
      if (auto) playlist_save('auto_next', 'true');
      window.location.href = RUTA + playlist[playlist_current - 1];
    }
  }
}

function playlist_previous() {
  playlist_get_current_order();
  var previous = 0;
  if (playlist_current_order > 1) {
    previous = playlist_order_list[playlist_current_order - 2];
  }
  else if (playlist_repeat_mode == 'all') {
    previous = playlist_order_list[playlist_order_list.length - 1];
  }

  if (previous > 0) {
    if (previous == playlist_current) {
      ytplayer.seekTo(0, true);
      ytplayer.playVideo();
    }
    else {
      playlist_current = previous;
      window.location.href = RUTA + playlist[playlist_current - 1];
    }
  }

}


function playlist_get_container() {
  if (!playlist_container) {
    playlist_container = document.getElementById('playlist_container');
  }
}
function playlist_get_loader() {
  if (!playlist_loader) {
    playlist_loader = document.getElementById('playlist_loader');
  }
}
function playlist_get_title() {
  if (!playlist_title) {
    playlist_title = document.getElementById('playlist_title');
  }
}
function playlist_container_show() {
  if (playlist_container) {
    playlist_container.rows[0].style.display = 'none';
  }
}
function playlist_container_hide() {
  if (playlist_container) {
    playlist_container.rows[0].style.display = '';
  }
}

function playlist_adjust_first_last() {
  playlist_get_container();
  if (playlist_container) {
    var numRows = playlist_container.rows.length - playlist_ROWS;
    if (numRows == 1) {
      playlist_container.rows[playlist_ROWS].cells[0].className = 'playlist_first_row playlist_last_row';
    }
    else for (var i = 1; i <= numRows; i ++) {
      if (i == 1) {
        playlist_container.rows[playlist_ROWS + i - 1].cells[0].className = 'playlist_first_row';
      }
      else if (i == numRows) {
        playlist_container.rows[playlist_ROWS + i - 1].cells[0].className = 'playlist_last_row';
      }
      else {
        playlist_container.rows[playlist_ROWS + i - 1].cells[0].className = '';
      }
    }
  }
}

function playlist_add(sURL, where) {
  if (where == undefined || where == -1) {
    playlist.push(sURL);
  }
  else {
    playlist.splice(where, 0, sURL);
  }
}
function playlist_add_item(html, mark_current, where) {
  if (mark_current == undefined) mark_current = true;
  if (where == undefined) where = -1;

  playlist_get_container();
  playlist_container_show();
  if (playlist_container) {
    var row = playlist_container.insertRow(where + (where == -1 ? 0 : playlist_ROWS));

    for (var j = (where == -1 ? playlist.length : where + 1); j <= playlist.length; j++) {
      var fila = playlist_container.rows[playlist_ROWS - 1 + j];
      fila.className = (playlist_ROWS + j) % 2 ? 'playlist_item2' : 'playlist_item';
    }

    var cell = row.insertCell(-1);
    cell.colSpan = 2;
    cell.innerHTML = html;

    if (mark_current) {
      playlist_set_current(where == -1 ? playlist.length : where + 1);
      playlist_adjust_first_last();
    }
  }
  else {
    return false;
  }
}

function playlist_remove(i) {
  if (i >= 1 && i <= playlist.length) {
    playlist.splice(i - 1, 1);
    if (i == playlist_current) {
      playlist_current = 0;
    }
    else if (i < playlist_current) {
      playlist_current --;
    }
  }
  else {
    return false;
  }
}
function playlist_remove_item(i) {
  playlist_get_container();
  if (playlist_container && i >= 1 && i <= playlist.length) {
    playlist_container.deleteRow(playlist_ROWS - 1 + i);
    if (playlist_container.rows.length <= playlist_ROWS) {
      playlist_container_hide();
    }
    else {
      for (var j = i; j < playlist.length; j++) {
        var fila = playlist_container.rows[playlist_ROWS - 1 + j];
        switch (fila.className) {
          case 'playlist_item':
            fila.className = 'playlist_item2';
            break;
          case 'playlist_item2':
            fila.className = 'playlist_item';
            break;
          case 'playlist_item_active':
            fila.className = 'playlist_item_active2';
            break;
          case 'playlist_item_active2':
            fila.className = 'playlist_item_active';
            break;
        }
      }
      playlist_adjust_first_last();
    }
  }
  else {
    return false;
  }
}
function playlist_move_down(oButton) {
  i = oButton.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.rowIndex - playlist_ROWS + 1;
  j = i + 1;
  playlist_swap(i, j)
}
function playlist_move_up(oButton) {
  i = oButton.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.rowIndex - playlist_ROWS + 1;
  j = i - 1;
  playlist_swap(i, j)
}

function playlist_swap(i, j) {
  playlist_get_container()
  if (playlist_container && i != j && i >= 1 && i <= playlist.length && j >= 1 && j <= playlist.length) {
    var t;

    t = playlist_container.rows[playlist_ROWS - 1 + i].cells[0].innerHTML;
    playlist_container.rows[playlist_ROWS - 1 + i].cells[0].innerHTML = playlist_container.rows[playlist_ROWS - 1 + j].cells[0].innerHTML;
    playlist_container.rows[playlist_ROWS - 1 + j].cells[0].innerHTML = t;

    if (playlist_current == i) playlist_set_current(j);
    else if (playlist_current == j) playlist_set_current(i);

    t = playlist[i - 1];
    playlist[i - 1] = playlist[j - 1];
    playlist[j - 1] = t;

    if (i == 1 || j == 1 || i == playlist.length || j == playlist.length) {
      playlist_adjust_first_last();
    }

    playlist_save('swap', i + ':' + j);
  }
}
function playlist_set_current(i) {
  if (i != playlist_current) {
    playlist_get_container();
    if (playlist_container && playlist_current > 0) {
      playlist_container.rows[playlist_ROWS - 1 + playlist_current].className = playlist_current % 2 ? 'playlist_item' : 'playlist_item2';
    }
    playlist_current = i;
    if (playlist_container && playlist_current > 0) {
      playlist_container.rows[playlist_ROWS - 1 + playlist_current].className = playlist_current % 2 ? 'playlist_item_active' : 'playlist_item_active2';
    }
    playlist_scroll_to(i);
  }
}
function playlist_scroll_to(i) {
  playlist_container.parentNode.scrollTop = (playlist_HEIGHT + 5) * (i - 1);
}

function playlist_auto_status() {
  if (playlist_auto_checkbox) {
    return playlist_auto_checkbox.checked;
  }
  else {
    var temp = document.getElementById('checkbox_playlist_auto');
    if (temp) {
      playlist_auto_checkbox = temp;
      return playlist_auto_checkbox.checked;
    }
    else {
      return false;
    }
  }
}

function playlist_get_volume() {
  if (ytplayer) {
    try {
      return ytplayer.isMuted() ? 0 : ytplayer.getVolume();
    }
    catch (err) {
      return 0;
    }
  }
}

function playlist_set_volume(vol) {
  if (ytplayer) {
    vol = Math.round(vol);
    if (vol < 0) vol = 0;
    else if (vol > 100) vol = 100;

    if (vol == 0) {
      ytplayer.mute();
    }
    else {
      ytplayer.unMute();
      ytplayer.setVolume(vol);
    }
  }
}
function playlist_volume_up(vol) {
  if (ytplayer) {
    playlist_set_volume(ytplayer.getVolume() + vol);
    playlist_show_volume(playlist_get_volume());
  }
}
function playlist_volume_down(vol) {
  if (ytplayer) {
    if (vol < ytplayer.getVolume()) {
      playlist_set_volume(ytplayer.getVolume() - vol);
      playlist_show_volume(playlist_get_volume());
    }
  }
}
function playlist_mute() {
  if (ytplayer) {
    if (ytplayer.isMuted()) {
      playlist_set_volume(ytplayer.getVolume());
    }
    else {
      playlist_set_volume(0);
    }
    playlist_show_volume(playlist_get_volume());
  }
}


function playlist_show_volume(vol) {
  playlist_get_bar_pointers();
  playlist_get_buttons();
  if (vol >= 0 && vol <= 100) {
    if (playlist_mute_button) {
      if (vol == 0) {
        playlist_mute_button.src = 'images/playlist_mute_on.gif';
        playlist_mute_button.title = 'En Silencio';
      }
      else {
        playlist_mute_button.src = 'images/playlist_mute_off.gif';
        playlist_mute_button.title = 'Con Sonido';
      }
    }
    if (playlist_bar_pointers.length > 0) {
      var items = Math.ceil(vol / playlist_bar_step);
      for (var i = 1; i <= playlist_bar_pointers.length; i++) {
        if (i <= items) {
          if (i == 1) {
            if (items == 1) {
              playlist_bar_pointers[i - 1].src = 'images/playlist_bar2_pointer.gif';
            }
            else {
              playlist_bar_pointers[i - 1].src = 'images/playlist_bar2_pointer_left.gif';
            }
          }
          else if (i == items) {
            playlist_bar_pointers[i - 1].src = 'images/playlist_bar2_pointer_right.gif';
          }
          else {
            playlist_bar_pointers[i - 1].src = 'images/playlist_bar2_pointer_center.gif';
          }
        }
        else {
          playlist_bar_pointers[i - 1].src = 'images/pixel_trans.gif';
        }
      }
    }
  }
}

function playlist_get_bar_pointers() {
  if (playlist_bar_pointers.length == 0) {
    var i, temp;
    for (i = 1; temp = document.getElementById('playlist_bar_pointer_' + i); i++) {
      playlist_bar_pointers.push(temp);
    }
    if (playlist_bar_pointers.length > 0) {
      playlist_bar_step = 100 / playlist_bar_pointers.length;
    }
  }
}



function playlist_repeat(){
  playlist_get_buttons();
  if (playlist_repeat_button) {
    switch (playlist_repeat_mode) {
      case 'off':   playlist_repeat_mode = 'all'; break;
      case 'all':   playlist_repeat_mode = 'off'; break;
      case 'track': playlist_repeat_mode = 'off'; break;
    }
    playlist_repeat_button.src = 'images/playlist_repeat_' + playlist_repeat_mode + '.gif';
    playlist_save('repeat', playlist_repeat_mode);
  }
}

function playlist_shuffle(){
  playlist_get_buttons();
  if (playlist_shuffle_button) {
    switch (playlist_shuffle_mode) {
      case 'off': playlist_shuffle_mode = 'on'; break;
      case 'on':  playlist_shuffle_mode = 'off'; break;
    }
    playlist_shuffle_button.src = 'images/playlist_shuffle_' + playlist_shuffle_mode + '.gif';
    playlist_save('shuffle', playlist_shuffle_mode);
  }
}


function playlist_get_hidden() {
  if (!playlist_hidden_param) {
    playlist_hidden_param = document.getElementById('playlist_form_param');
  }
  if (!playlist_hidden_value) {
    playlist_hidden_value = document.getElementById('playlist_form_value');
  }
}
function playlist_save(param, value) {
  playlist_get_hidden();
  if (playlist_hidden_param && playlist_hidden_value) {
    playlist_hidden_param.value = param;
    playlist_hidden_value.value = value;
    playlist_hidden_param.form.submit();
  }
}

function playlist_get_current_order() {
  playlist_current_order = 0;
  if (playlist_current > 0) {
    playlist_current_order = playlist_find_order(playlist_current);
  }
}
function playlist_find_order(i) {
  for (var j = 0; j < playlist_order_list.length; j ++) {
    if (playlist_order_list[j] == i) {
      return j + 1;
    }
  }
  return 0;
}



function playlist_show_position(pos) {
  playlist_get_progress_bar_pointers();
  if (pos >= 0 && pos <= 100) {
    if (playlist_progress_bar_pointers.length > 0) {
      var items = Math.floor(pos / playlist_progress_bar_step) + 1;
      for (var i = 1; i <= playlist_progress_bar_pointers.length; i++) {
        if (i == items) {
          playlist_progress_bar_pointers[i - 1].src = 'images/playlist_bar_pointer.gif';
        }
        else {
          playlist_progress_bar_pointers[i - 1].src = 'images/pixel_trans.gif';
        }
      }
    }
  }

}
function playlist_set_position(pos) {
  if (ytplayer) {
    if (pos < 0) pos = 0;
    else if (pos > 100) pos = 100;
    ytplayer.seekTo(pos / 100 * ytplayer.getDuration(), true);
  }

}
function playlist_get_position() {
  if (ytplayer) {
    return Math.round(ytplayer.getCurrentTime() / ytplayer.getDuration() * 100);
  }
}
function playlist_get_progress_bar_pointers() {
  if (playlist_progress_bar_pointers.length == 0) {
    var i, temp;
    for (i = 1; temp = document.getElementById('playlist_progress_bar_pointer_' + i); i++) {
      playlist_progress_bar_pointers.push(temp);
    }
    if (playlist_progress_bar_pointers.length > 0) {
      playlist_progress_bar_step = 100 / playlist_progress_bar_pointers.length;
    }
  }
}
function playlist_set_progress_bar_titles() {
  playlist_get_progress_bar_pointers();
  if (ytplayer && playlist_progress_bar_pointers.length > 0) {
    var i, temp;
    temp = ytplayer.getDuration() * playlist_progress_bar_step / 100;
    for (i = 0; i < playlist_progress_bar_pointers.length; i++) {
      var m = Math.floor(temp * i / 60).toString();
      var s = Math.ceil(temp * i % 60).toString();
      if (s < 10) s = '0' + s;
      playlist_progress_bar_pointers[i].alt = m + ':' + s;
    }
  }
}

function playlist_img_over(img) {
  if (img.src.substr(img.src.length - 9) != '_over.gif') {
    img.src = img.src.substr(0, img.src.length - 4) + '_over.gif';
  }
}

function playlist_img_out(img) {
  if (img.src.substr(img.src.length - 9) == '_over.gif' || img.src.substr(img.src.length - 9) == '_down.gif') {
    img.src = img.src.substr(0, img.src.length - 9) + '.gif';
  }
}

function playlist_img_down(img) {
  if (img.src.substr(img.src.length - 9) != '_down.gif') {
    img.src = img.src.substr(0, img.src.length - (img.src.substr(img.src.length - 9) == '_over.gif' ? 9 : 4)) + '_down.gif';
  }
}

function playlist_img_up(img) {
  if (img.src.substr(img.src.length - 9) == '_down.gif') {
    img.src = img.src.substr(0, img.src.length - 9) + '_over.gif';
  }
}

function playlist_get_duration_span() {
  if (!playlist_duration_span) {
    playlist_duration_span = document.getElementById('playlist_duration');
  }
}
function playlist_show_duration(t) {
  playlist_get_duration_span();
  if (playlist_duration_span) {
    if (ytplayer && (t == undefined)) {
      t = ytplayer.getCurrentTime();
    }
    else {
      t = 0;
    }
    t = Math.ceil(t);
    m = Math.floor(t / 60).toString();
    s = Math.floor(t % 60).toString();
    if (s < 10) s = '0' + s;

    playlist_duration_span.innerHTML = m + ':' + s;
  }
}

function playlist_update_bars() {
  if (playlist_is_playing) {
    if (!playlist_over_bar) {
      playlist_show_volume(playlist_get_volume());
    }
    if (!playlist_over_progress_bar) {
      playlist_show_position(playlist_get_position());
    }
  }
}
function playlist_update_duration() {
  if (playlist_is_playing) {
    playlist_show_duration();
  }
}
setInterval('playlist_update_bars()', 2500);
setInterval('playlist_update_duration()', 900);

function playlist_save_list() {
  if (user_logged()) {
    playlist_save('save','');
  }
}

function playlist_save_list_as(default_name) {
  if (user_logged()) {
    if (default_name == undefined) default_name = 'Nueva lista';
    var nombre = prompt('Escribe el nombre de la lista:', default_name);
    if (nombre != null) {
      playlist_save('save_as', nombre);
    }
  }
}

function playlist_new_list() {
  if (playlist.length < 1) {
    alert('La lista ya está vacía');
  }
  else if (confirm('¿Vacíar lista?')) {
    playlist_get_container();
    if (playlist_container) {
      for (var j = playlist.length; j >= 1; j--) {
        playlist_container.deleteRow(playlist_ROWS - 1 + j);
      }
      playlist_container_hide();
      playlist = [];
      playlist_order_list = [];
      playlist_current = 0;
    }
    playlist_save('new', '');
  }
}

function playlist_open_list() {
  if (user_logged()) {
    playlist_get_loader();
    if (playlist_loader) {
      var display = playlist_loader.style.display == 'block' ? 'none' : 'block';
      if (display != 'none' && playlist_loader_iframe) {
        playlist_loader_iframe.location.reload();
      }
      playlist_loader.style.display = display;
    }
  }
}

function playlist_update_title(title) {
  playlist_get_title();
  if (playlist_title) {
    playlist_title.firstChild.innerHTML = title;
  }
}


var playlist_aux_container = false;
var playlist_aux_duration = false;
var playlist_aux_num = false;

function playlist_aux_delete(oImg) {
  var i = oImg.parentNode.parentNode.rowIndex + 1;
  playlist_aux_get_container();
  if (playlist_aux_container && i >= 1 && i <= playlist_aux_container.rows.length) {
    playlist_aux_container.deleteRow(i - 1);
    playlist_aux_adjust_first_last();
    playlist_save('aux_delete', i);
  }
}
function playlist_aux_move_down(oImg) {
  var i = oImg.parentNode.parentNode.rowIndex + 1;
  var j = i + 1;
  playlist_aux_swap(i, j);
}
function playlist_aux_move_up(oImg) {
  var i = oImg.parentNode.parentNode.rowIndex + 1;
  var j = i - 1;
  playlist_aux_swap(i, j);
}
function playlist_aux_swap(i, j) {
  playlist_aux_get_container();
  if (playlist_aux_container && i != j && i >= 1 && i <= playlist_aux_container.rows.length && j >= 1 && j <= playlist_aux_container.rows.length) {
    var c, t;

    for (c = 0; c < playlist_aux_container.rows[0].cells.length; c++) {
      t = playlist_aux_container.rows[i - 1].cells[c].innerHTML;
      playlist_aux_container.rows[i - 1].cells[c].innerHTML = playlist_aux_container.rows[j - 1].cells[c].innerHTML;
      playlist_aux_container.rows[j - 1].cells[c].innerHTML = t;
    }

    if (i == 1 || j == 1 || i == playlist.length || j == playlist.length) {
      playlist_aux_adjust_first_last();
    }

    playlist_save('aux_swap', i + ':' + j);
  }
}

function playlist_aux_save() {
  playlist_save('aux_save', '');
}
function playlist_aux_get_container() {
  if (!playlist_aux_container) {
    playlist_aux_container = document.getElementById('playlist_aux_container');
  }
}
function playlist_aux_get_labels() {
  if (!playlist_aux_duration) {
    playlist_aux_duration = document.getElementById('playlist_aux_duration');
  }
  if (!playlist_aux_num) {
    playlist_aux_num = document.getElementById('playlist_aux_num');
  }
}
function playlist_aux_adjust_first_last() {
  playlist_aux_get_container();
  if (playlist_aux_container) {
    var numRows = playlist_aux_container.rows.length;
    if (numRows == 1) {
      playlist_aux_container.rows[0].className = 'first_row last_row';
    }
    else for (var i = 1; i <= numRows; i ++) {
      if (i == 1) {
        playlist_aux_container.rows[i - 1].className = 'first_row';
      }
      else if (i == numRows) {
        playlist_aux_container.rows[i - 1].className = 'last_row';
      }
      else {
        playlist_aux_container.rows[i - 1].className = '';
      }
    }
  }
}
function playlist_aux_set_labels(sDuracion, sNum) {
  playlist_aux_get_labels();
  if (playlist_aux_duration) {
    playlist_aux_duration.innerHTML = sDuracion;
  }
  if (playlist_aux_num) {
    playlist_aux_num.innerHTML = sNum;
  }
}
