/*-------------------------------------------------------------
 * Collection of useful jquery things common to several sites!
 *
 * Add your own!!
 *-------------------------------------------------------------*/


$(document).ready(function() {

  // This makes a link with the delete_link class send a with DELETE verb
  $('.delete_link').live('click', function(e){
    e.stopPropagation();
    var path = $(this).attr('href');
    var prompt = $(this).attr('rel');
    var redirect = $(this).attr('rev');

    if (confirm(prompt)) {
      $.post(path, {'_method' : 'delete'}, function(ret){
        if (ret == "OK") {
          if (redirect.match(/^#eval__/)) {  
            var run = redirect.replace(/^#eval__/,'');
            eval(run);
          }
          else {
            window.location = redirect;
          }
        } else {
          error_message = ret.replace('ERROR', '').trim();
          alert("Error deleting this object. " + error_message);
          return false;
        }
      });
    }
    return false;
  });

  // Textile cheatsheet
  $('.textile-help-link').live('click', function() {
    $(this).next('.textile-help').toggle('slow');
  });

  // Clickable table rows
  $('tr.selectable td:not(.actions)').live('click', function() {
    var link = $(this).closest('tr').attr('rel');
    window.location = location.protocol + '//' + location.host + link;
  });

  initPlaceholder();

  initPutLink()

});

// Labels within fields. Use the the "placeholder" HTML 5 attribute.
function initPlaceholder() {
  if ($.browser.webkit) return false;

  $('input[placeholder]').each(function(index) {
    var target = $(this);
    
    if (! $.trim(target.val())) {
      $(target).addClass('placeholder');
      $(target).val($(target).attr('placeholder'));
    }
 
    $(target).focus(function() {
      if ($(this).val() == $(this).attr('placeholder')) {
        $(this).removeClass('placeholder');
        $(this).val('');
      }
    });

    $(target).blur(function() {
      if ($.trim($(this).val()) == '' ) {
        $(this).addClass('placeholder');
        $(this).val( $(this).attr('placeholder') );
      }
    });
  });
  
  // The form shouldn't send the placeholder text as the value
  // TODO: write back the labels after submit
  $('form').submit(function(){
    $(this).find('input[placeholder]').each(function(index) {
      var target = $(this);
      if ($(target).val() == $(target).attr('placeholder')) {
        $(target).val('');
      }
    });
  });
}

// Scroll page to an element
function scrollPageTo(element) {
  $('html,body').animate({ scrollTop: $(element).offset().top }, { duration: 'slow', easing: 'swing'});
}

function initPutLink() {
  $('.put_link').live('click', function(e) {
    e.preventDefault();

    var link = $(this).attr('href');
    var message = $(this).attr('rel');
    var callback = $(this).attr('rev');
    
    if (message && !confirm(message)) return false;

    $.post( link, {_method:'put'}, function(result) {
      eval(callback);
    });
  });
}
