function toggleClipHighlight(value) {
  var clip = $('clip_' + value);
  var input = $('clip_id_' + value);
  if (input.checked == true) {
    clip.addClassName('selected');
  } else {
    clip.removeClassName('selected');
  }
  
  // make sure that the "select all" inputs reflect the number of clips manually selected by the user
  all_clips = $$('#playlist_clips .clip input[type=checkbox]');
  all_clips_checked = [];
  all_clips.each(function(x) {
    if (x.checked == true) {
      all_clips_checked.push(x);
    }
  });
  if (all_clips.length == all_clips_checked.length) {
    $$('.clip_tools input[type="checkbox"]').each(function(x){x.checked = 'checked'});
  } else {
    $$('.clip_tools input[type="checkbox"]').each(function(x){x.checked = ''});
  }
}

function toggleClips(elt) {
  if (elt.checked == true) {
    $$('#playlist_clips .clip input[type=checkbox]').each(function(x){x.checked = 'checked';});
    $$('#playlist_clips .clip').each(function(x){x.addClassName('selected');});
    $$('.clip_tools input[type="checkbox"]').each(function(x){x.checked = 'checked'});
  } else {
    $$('#playlist_clips .clip input[type=checkbox]').each(function(x){x.checked = ''});
    $$('#playlist_clips .clip').each(function(x){x.removeClassName('selected');});
    $$('.clip_tools input[type="checkbox"]').each(function(x){x.checked = ''});
  }
}

function togglePlaylistName(value) {
  if (value == '') {
    $('name_option').show();
    $('target_playlist_name').focus();
  } else {
    $('name_option').hide();
  }
}

function clipBehavior(elem) {
  url = elem.href;
  if (!delete_clip_from_page) { delete_clip_from_page = false };
  if (elem.className == 'remove' && delete_clip_from_page == true) {
    answer = confirm("Are you sure you want to delete this clip?");
    if (answer) {
      activateBehavior(url, delete_clip_from_page);
    }
  } else {
    activateBehavior(url, delete_clip_from_page);
  }
}

function activateBehavior(url, delete_clip_from_page) {
  a = new Ajax.Request(url, {
    asynchronous:true, evalScripts:true,
    parameters: {delete_clip_from_page: delete_clip_from_page, format: 'js'}
  }); 
}

function deleteSelectedClips(playlist) {
  answer = confirm("Are you sure you want to remove the selected clips from this playlist?");
  if (answer) {
    clips = [];
    $$('.clip input[@type="checkbox"]').each(function(checkbox) {
      if (checkbox.checked) {
        clips.push(checkbox.id.split('_').last());
      }
    });
    new Ajax.Request('/playlists/' + playlist + '/remove_selected_clips_from_playlist', {
      method: 'post',
      asynchronous:true, evalScripts:true,
      parameters: {clips: [clips], format: 'js'}
    });
  } else {
    Event.stop();
  }
}

function searchSelectAll(elem) {
  if (elem.innerHTML == 'Select all clips') {
    $$('.clip input[@type="checkbox"]').each(function(checkbox) {
      checkbox.checked = true;
    });
    $$('.select_all_link').each(function(link) {
      link.removeClassName('checkbox_checked');
      link.addClassName('checkbox');
      link.innerHTML = 'De-select all clips';
    });
  } else {
    $$('.clip input[@type="checkbox"]').each(function(checkbox) {
      checkbox.checked = false;
    });
    $$('.select_all_link').each(function(link) {
      link.removeClassName('checkbox');
      link.addClassName('checkbox_checked');
      link.innerHTML = 'Select all clips';
    });
  }
}
