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;
}

function scrollTweets() {
  var tweets = $("ul.tweets li");
  currentTweet++;
  totalTop = 0;
  maxTop = $("ul.tweets").height() - $(".boxes .connect").height();
  if(currentTweet == tweets.length) {
    currentTweet = 0;
  } else {
    for(i = 0; i < currentTweet; i++) {
      totalTop += ($(tweets[i]).height() + 11); // add 10 for total vertical padding, plus 1 for border
    }
  }
  if(totalTop > maxTop) {
    totalTop = maxTop;
    currentTweet = tweets.length - 1;
  }
  $("ul.tweets").animate({top:"-" + totalTop + "px"}, 1000, 'easeInOutSine');
}

function setupTwitterFeed() {
  $(document).everyTime(6000, function(){
    scrollTweets();
  });
}
