jQuery(document).ready(function() {

  // AJAX SEND MESSAGE WINDOW --------------------------------------------------

  // create send message popup window
	$("#send_message_window").draggable();

  // open send message window
	$(".send_message_link:not(.disabled)").click(function(event) {

    // position window relative to mouse click
    $("#send_message_window").css("top", event.pageY-150).css("left", event.pageX-100);

    // prepopulate information
    $("#send_message_subject").children("input").attr("value","");
    $("#send_message_body").children("textarea").attr("value","");
    $("#send_message_error").text("");
	  $("#send_message_button").removeAttr("disabled");

    var url = $(this).attr("href");
    var query_string = url.split("?")[1];
    var recipient_name = query_string.split("&")[0].split("=")[1];
    var hash_key = query_string.split("&")[1].split("=")[1];   
    $("#recipient_name").text(recipient_name);
    $("#recipient_id").val(hash_key);

    // show window
		$("#send_message_window").fadeIn("fast");
    event.preventDefault();
	});

  // submit AJAX form when user clicks send
	$("#send_message_button").click(function() {
    // disable button immediately
	  $(this).attr("disabled","disabled");
	
    // initialize arguments to pass to server
    var hash_key = $("#recipient_id").val();
    var action = "Send Message";
    var subject = $("#send_message_subject").children("input").attr("value");
    var body = $("#send_message_body").children("textarea").attr("value");

    // ajax call to server to send message
    $.post("/ajax_send_message", { id: hash_key, action: action, subject: subject, body: body}, function(xml) {

      // convert xml to json (for IE)
      var json = $.xml2json(xml);
      var error = json.error_message;

      // show error message
      if(error.length > 0) {
        $("#send_message_error").css("color","#ff0000").text(error).fadeIn("fast");
        $("#send_message_button").removeAttr("disabled");
      // show success message, fade window, disable link
      } else {
        $("#send_message_error").css("color","green").css("font-weight","bold").text("Your message has been sent!").fadeIn("fast");
      	$("#send_message_window").animate({opacity: 1.0}, 1500).fadeOut("slow");
      }
    })
  });
	
	// hide window when user clicks cancel
	$("#cancel_send_message").click(function(event) {
		$("#send_message_window").fadeOut("slow");
    event.preventDefault();
	});
	
  // AJAX FOLLOW PERSON LINK ---------------------------------------------------

	$(".follow_link").click(function(event) {

    // prevent default behavior    
    event.preventDefault();

    // grab id of link
    var link_id = $(this).attr("id");

    var url = $(this).attr("href");
    var query_string = url.split("?")[1];
    var hash_key = query_string.split("&")[0].split("=")[1];
	
    // initialize arguments to pass to server
    if ($(this).text() == "Follow This Person") {
      var action = "Follow User";
    } else if ($(this).text() == "Stop Following") {
      var action = "Unfollow User";
    }

    // ajax call to server to send message
    $.post("/ajax_follow_user", { id: hash_key, action: action, link_id: link_id}, function(xml) {

      // convert xml to json (for IE)
      var json = $.xml2json(xml);

      // show confirmation message
      $("#"+link_id).css("color","green").css("font-weight","bold").css("text-decoration","none").text("Saved!");

      // wait and then change link text
      $(this).delay(2500,function(){
        
        $("#"+link_id).css("color","#0463ff").css("font-weight","normal");
        
        if (action == "Follow User") {
          $("#"+link_id).text("Stop following");
        } else if (action == "Unfollow User") {
          $("#"+link_id).text("Follow This Person");          
        }
      });
    })
  });

  // delay function  
  jQuery.fn.delay = function(time,func){
  	this.each(function(){
  		setTimeout(func,time);
  	});
  	return this;
  };
});