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'}
  }); 
}
