// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/**
 * Binds post() to .delete anchor links
 */
function undo_post() {
  jQuery("a.delete").click(function() {
    var self = jQuery(this);
    jQuery.post(self.attr("rel"), {
      "_method": "delete", 
      "authenticity_token": AUTH_TOKEN
    }, notify_client, "json");
    return false;
  });
}

/**
 * dispatch notifications from server.
 */
function notify_client(json) {
  flash_message(json)
  update_page(json)
}

var flash_message_timer;

/**
 * updates notification bar 
 */
function flash_message(json) {
  message = json.flash_message
  if (json.undo != undefined) {
    message += json.undo
  }
  jQuery("#flash_message").show().html(message);
  if (json.undo != undefined) {
    undo_post();
  }
  
  if (flash_message_timer != undefined) {
    clearTimeout(flash_message_timer);
  }
  jQuery("#flash_message").fadeIn('slow');
  flash_message_timer = setTimeout('jQuery("#flash_message").fadeOut("slow")', 5000);  
}

/**
 * redraw's the page with content
 */
function update_page(json) {
  // Replace html contents of each dom_id with html_snippet
  if (json.html != undefined) {
    jQuery.each(json.html, function(dom_id, html_snippet) {
      jQuery("#"+dom_id).html(html_snippet);
    });
  }
}

function good(data, textStatus) {
  alert("goodL");
  // data could be xmlDoc, jsonObj, html, text, etc...
  //this; // the options for this ajax request
}


function bad(XMLHttpRequest, textStatus, errorThrown) {
  // typically only one of textStatus or errorThrown 
  // will have info
  //this; // the options for this ajax request
  alert("asdfbad");
}



