function strip(string) {
  return string.replace(/^\s+/, '').replace(/\s+$/, '');
}

function downcase(string) {
  return string.toLowerCase();
}

function slugify(string) {
  return downcase(strip(string)).replace(/[^-a-z0-9~\s\.:;+=_]/g, '').replace(/[\s\.:;=+]+/g, '-');
}

jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} 
})

$(document).ajaxSend(function(event, request, settings) {
  if (settings.type == 'GET' || settings.type == 'get' || 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);
});

$(function() {
  swapValues = [];
  $(".swap_value").each(function(i){
      swapValues[i] = $(this).val();
      $(this).focus(function(){
          if ($(this).val() == swapValues[i]) {
              $(this).val("");
          }
      }).blur(function(){
          if ($.trim($(this).val()) == "") {
              $(this).val(swapValues[i]);
          }
      });
  });
});

function title_updated() {
  if (slugify($old_title) == $slug.value) $slug.value = slugify($title.value);
  $old_title = $title.value;
}
