

$(function(){
  // Convert timeago classes with UTC to human readable
  // $('abbr[class*=timeago]').timeago();

  $(document).ajaxSend(function(event, request, settings) {
    if (typeof(AUTH_TOKEN) == "undefined") return;
    // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
    settings.data = settings.data || "";
    settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
  });

  $("#tabs").tabs();

  // Hide development warning
  if ($('#development-warning')) { $('#development-warning').click(function(){ $(this).hide(); }); }

  if ($('select').val() != 'html') {
    $('#tiny-mce').hide();
  }

  $('select').change(function(){
    toggle_id = $('textarea').attr('id')

    if ($(this).val() == 'html') {
      $('#tiny-mce').show();
    } else {
      tinyMCE.execCommand('mceRemoveControl', false, toggle_id);
      $('#tiny-mce').hide();
    }
  });

  // Hide the delete option until rolling over
  $('.asset .options').hide();
  $('.asset').hover(function(){
    $(this).children('.options').show();
  },function(){
    $(this).children('.options').hide();
  });

  $('a.setup-mce').click(function(e){
    setupEditor();
    e.preventDefault();
  });

  $("#products").sortable({
    items : 'tbody tr',
    axis : 'y',
    placeholder : 'ui-state-highlight',
    forceHelperSize: true,
    update: function(){

      $.ajax({
         type: "PUT",
         url: location.pathname + "/update_multiple",
         data: "product_ids=" + ProductOrderedIds()
       });

    }
  });

  function ProductOrderedIds() {
    productRows = $('#products').find('tbody tr')
    return $.map(productRows, function(n,i){ return $(n).attr('id').replace(/^.+_/g,'') })
  }

});

function setupEditor() {
  tinyMCE.init({
    mode : "textareas",
    theme : "advanced"
  });
}