$(function() {
	$("form#search_form").submit(submitRequests);
  $("label").focus();

  function submitRequests(event) {
    event.preventDefault();

    
    if($("input[type='text']").val().length == 0) {
      $("input[type='text']").parent().effect("shake", { times: 2 }, 200, function() { $("label").focus(); });
      return;
    }
    

    var serializedForm = $(this).serialize();

    $("tr > td:first-child").each( function() { 
      var ip = $(this).text();
  	  $(":contains(" + ip + ") ~ td").text("").showLoading();
      $.post("/search/create", serializedForm + "&ip=" + ip,
      function(data) { $(":contains(" + ip + ") ~ td").text(data).parent().yellowFade(); });
    });


  }
});

(function($) {
    $.fn.yellowFade = function() {
        var oldColor = this.css("background-color");
        return this.css({ backgroundColor: "#ffffcc" })
    .animate({ backgroundColor: oldColor }, 1500, "linear");
    }
})(jQuery);

 (function($) {
   $.fn.showLoading = function() {
    return  this.append($(document.createElement("img"))
      .attr("alt","loading...")
      .attr("src","/images/loading.gif")
      .addClass("loading"));
   }
 })(jQuery);
