/home/coolpkct/www/websites/cake3.cool.rocks/admin/scripts/jimages.js
/**
 * Part of Showkase web site management package
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2012, SimpleViewer Inc.
 */
 
$(function() {
  $('#selectall').on('click', function(event){
    $('input.selectcaption').prop('checked', this.checked);
  });
  $('#selectalltitles').on('click', function(event){
    $('input.selecttitle').prop('checked', this.checked);
  });
  $('#selectallurls').on('click', function(event){
    $('input.selecturl').prop('checked', this.checked);
  });
  $('#selectalldelete').on('click', function(event){
    $('input.selectdelete').prop('checked', this.checked);
  });
  $('#selectallshowflip').on('click', function(event){
    $('input.selectshowflip').prop('checked', this.checked);
  });
  $('#selectalltarget').on('click', function(event){
    $('input.selecttarget').prop('checked', this.checked);
  });
  $('#copy').on('click', copyTo);
  $('button[type="submit"]').on('click', function(event){
    var deletions = $('input.selectdelete').filter(':checked').length;
    if (deletions < 1) return true;
    if (deletions == 1) return window.confirm('Deleting 1 image');
    return window.confirm('Deleting ' + deletions + ' images');
  });
  
});

/**
 * Helper is a clone of the element being moved
 * Width isues see http://stackoverflow.com/questions/1307705/jquery-ui-sortable-with-table-and-tr-width
 * Radio checked property is lost during drag but radio attributes seem to survive
 * so park checked state in attribute and retrieve on stop
 */ 
var sortHelper = function(e, tr) {
    var $originals = tr.children();
    $('input[type=radio]', tr).each(function(index) {
      $(this).attr('checked', $(this).prop('checked'));
    });
    var $helper = tr.clone();
    $helper.children().each(function(index)
    {
      $(this).width($originals.eq(index).width())
    });
    return $helper;
};

$(function() {
  if (!$("#sortable").length || !$("#sortoutput").length) return;
  $( "#sortable" ).sortable({
    helper: sortHelper,
    start: function(e, ui ){
      // http://stackoverflow.com/questions/12078289/controlling-placeholder-height-with-jquery-ui-sortable
      ui.placeholder.height(ui.helper.outerHeight());
      $('select[name=sortOrder]').val('dragndrop');
    },
    stop: function(e, ui) {
      ui.item.find('input[type=radio]').each(function(index) {
        $(this).prop('checked', $(this).attr('checked'));
      });
    },
    axis: 'y',
    handle: 'td.ilthumbnail img',
    update: function(event, ui) {
      $("#sortoutput").val(
        $(this).sortable("serialize")
      );
    }
  });
});

function copyTo()
{
  // assumes ilcaption inputs and textareas are listed in the correct order 
  copyText = $('#copytext').val();
  var captions = $('#imagelisttable td.ilcaption input, #imagelisttable td.ilcaption textarea');
  var checkboxes = $('#imagelisttable td.ilselect input');
  captions.each(function(n){
    if(checkboxes[n].checked) {
     $(this).val(copyText);
    }
  });
}