
(function($) {
	
	$.fn.statusbox = function(options) {
		
		var opts = $.extend({}, $.fn.statusbox.defaults, options);
		
		return this.each(function() {
			
			var $this = $(this);
			var $inputField = $this.find(".statusinput");
			var $updateButton = $this.find(".statusupdate");
			
			if ($inputField.val().length == 0)
				$inputField.val(opts.placeholder);
			
			$inputField.focus(function(e){
				$inputField.val("");
				$updateButton.slideDown("fast");
			});
			
			$inputField.blur(function(e){
				if ($.trim($inputField.val().length) == 0) {
					$inputField.val(opts.placeholder);
//					$updateButton.slideUp("fast");
				}
			});
			
			$updateButton.click(function(){
				console.log($inputField.val() + "?! Det här går ju inte!");
				if ($.trim($inputField.val()).length > 0) {
					$updateButton.attr("disabled", "true");
					$.ajax({
						type: "post",
						url: opts.ajaxUrl,
						data: { action: 'post', status: $inputField.val() },
						success: function(data) {
							$inputField.val(opts.placeholder);
							$updateButton.attr("disabled", "false");
							$updateButton.slideUp("fast");
						},
						error: function(data) {
							// show error message
							console.log(data);
							$updateButton.attr("disabled", "false");
						}
					});
				}
				else {
//					$this.val(opts.placeholder);
//					$updateButton.slideUp();
				}
			});
		});
	}
	
	$.fn.statusbox.defaults = {
		placeholder:	"Contact me when...",
		ajaxUrl:		"/ajax/status/"
	};
	
	$(document).ready(function() {
		$(".statusbox").statusbox();
	});
	
})(jQuery);