var default_val = "En tanke...";
$(function() { $("textarea#thought").maxlength(); });
$(function() {
    $("div.error").click(function() { $(this).fadeOut('slow'); });
    $("textarea#thought").focus(function() { $(this).text(""); });
});

$(function() {
  $("#add_thought_form #submit_thought").click(function() {
    var thought = $("#add_thought_form #thought").val();
    
    if(thought.length < 3)
    {
      $(".error").html("For kort tanke.").fadeIn('slow');
      return false;
    }

    if(thought == default_val)
    {
      $(".error").html("Tanken virker kjent.").fadeIn('slow');
      return false;
    }

    var count = $("div.thought").size();

    var dataString = "thought="+thought+"&num="+count;
    $.ajax({
      type: "POST",
      url: "front/save",
      data: dataString,
      dataType: "json",
      success: function(data) {
        if(data["thought"])
        {
          $("#thoughts").prepend(data["thought"]);
          $("div.error").hide();
          $('#add_thought_form')[0].reset();
        }
        else
        {
          $(".error").html(data['error']).fadeIn('slow');
        }
      }
    });
    return false;
  });
}); 
