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


// This makes a link with the delete_link class send a with DELETE verb.
// set rev to the the url to redirect to, or the name of the function 
// to eval with '#eval__' prefixed to the code you want to eval.

$(document).ready(function() {
  $('.delete_link').live('click', function(){
    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.href = redirect;
          }
        } else if (ret.match(/^ERROR_MESSAGE-/)) {
          alert(ret.replace(/^ERROR_MESSAGE-/, ''));
        }
      });
    }
    return false;
  });
  
  $('.login_link').live('click', function(){
    
    if (confirm("Login as " + $(this).attr('name') + ' ?')) {
      var options = { 
        userid: $(this).attr('rel')
      }
      var redirect = $(this).attr('rev');
      $.get($(this).attr('href'), options, function(ret){
        if (ret == "OK") {
          if (ret.match(/^EVAL__/)) {  
            var run = ret.replace(/^#EVAL__/,'');
            eval(run);
          } else {
            window.location.href = redirect;
          }
        } else if (ret.match(/^ERROR-/)) {
          alert(ret.replace(/^ERROR-/, ''));
        } else {
          alert('There was a problem logging in');
        }
      });
    }
    return false;
  });
  
  
});

