//= require <jquery-1.3.2.min>
//= require <jquery.jeditable-1.7.1>
//= require <jquery.form-2.28>
//= require <json2>

// $(document).ajaxSend(function(event, request, settings) {
//   if (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() {
  $('.update-profile').editable('/profile', {
    method : "PUT",
    indicator : 'Saving...',
    tooltip : "Click to edit profile name",
    style : 'inherit',
    id : 'profile',
    name : 'field',
    submitdata : { authenticity_token : AUTH_TOKEN }
  });

  // $('#haiku_poem').focus();

  $('#haiku-submit').click(function() {
    $('#haiku_poem').addClass("haiku-input-spinner")
  });

  $('.new_haiku').ajaxForm({ 
    dataType: 'json',
    // beforeSubmit: ga_log,
    success: processJson
  });

  function processJson(data) {
    $('#haiku_poem').removeClass('haiku-input-spinner')

    $('#haiku-form-errors').html('');
    if (data.st == "invalid") {
      jQuery.each(eval(data.msg), function(index, item){
        $('#haiku-form-errors').append(item + '<br/>');
      });
    }
    if (data.st == "ok") {
      $('#haiku-form-errors').html("Thank you for writing such a beautiful haiku. Compose another.");
      var new_div = jQuery(data.msg);
      new_div.css("position", "relative");
      new_div.css("top", -155);
      jQuery(".haiku-list").prepend(new_div);
      jQuery("#haiku_poem").val('');
      new_div.animate({'top': 0,}, 600);
    }
  }
  
  function ga_log(formData, jqForm, options) { 
    var queryString = "";
    jQuery.each(formData, function(i, data) {
      if (data.name == "haiku[poem]") {
        queryString += data.value
      }
    });
    urchinTracker('/haiku/'+queryString);
    return true; 
  }
  
  $('.new_user').ajaxForm({
    dataType: 'json',
    success: processNewUserForm
  })
  
  function resetNote(div_id) {
    jQuery(div_id + "_note").hide();
    jQuery(div_id).css("border-bottom", "1px solid black");
    if (div_id == "#user_password") {
      jQuery("#user_password_confirmation").css("border-bottom", "1px solid black");
    }
  }
  
  function processNewUserForm(data) {
    resetNote("#user_fullname");
    resetNote("#user_haimei");
    resetNote("#user_password");
    resetNote("#user_email");
    resetNote("#user_cc_license");
    jQuery("#haiku_poem_note").hide();

    if (data.st == "invalid") {
      if (data.errors["user"]) {
        user_errors = data.errors["user"]
        jQuery.each(user_errors, function(i, arr) {
          k = arr[0];
          v = arr[1];
          note_div = "#user_"+k+"_note"
          note = jQuery(note_div);
          note.show();
          if (note_div != "#user_cc_license_note") {
            note.find(".haiku-sticky-note").find(".haiku-stick-note-text").html(k + " " + v);
          }
          jQuery("#user_"+k).css("border-bottom", "2px solid red");
          if ("#user_"+k == "#user_password") {
            jQuery("#user_password_confirmation").css("border-bottom", "2px solid red");
          }
        });
      }
      if (data.errors["haiku"]) {
        jQuery("#haiku_poem_note").show();
      }
    } else {
      jQuery(".haiku-sign-up").hide();
      jQuery(".haiku-sign-up-confirmation").show();
      username = jQuery("#user_haimei").val();
      jQuery("#new-vanity-url").html("<a href='/" + username + "/'>http://haikuist.com/"+ username +"</a>");
    }
    
  }
  
});

//  onclick="clickclear(this, 'Type your message')" onblur="clickrecall(this,'Type your message')

function clearOnClick(this_field, default_text) {
  if (this_field.value == default_text) {
    this_field.value = "";
  }
}

function resetOnBlur(this_field, default_text) {
  if (this_field.value == "") {
    this_field.value = default_text;
  }
}
