$(document).ready(function() {

	$("#status").ajaxForm({
		"dataType":     "json",
		"beforeSubmit": status_beforesubmit,
		"success":      status_success
	});

	$("a.deletestatus").click(status_delete);
	
	// Character counting
	var totalChars = 140;
	var warning = 50;			// How many characters left, change to warning color
	
	$('.blankable').each(function(){

		// Make expandable
		$(this).elastic();
	
		// The textbox isn't blank yet.
		var blank = false;
	
		$(this).focus(function() {
			
			// If the textbox isn't blank, clear it
			if(blank == false) {
				$(this).stop().animate({ color: '#ffffff' }, 250,function() {
					$(this).val("");
					$(this).css("color","#999");
					
					// Get the starting number of characters
					var length = $(this).val().length;
				
					// Update the display text
					$(this).parent().find('.counter').css("color","#fff");
					$(this).parent().find('.counter').html(totalChars-length+' characters left');
					$(this).parent().find('.counter').stop().animate({ color: '#999' }, 500);
				});
				//$(this).val("");
				
				// Only clears the first time you click on it
				blank = true;
			}
		
		});
		
		// Everytime a key is released
		$(this).keyup(function(){
		
			// Retrieve the new length
			var new_length = $(this).val().length;
			
			// Update the display text
			$(this).parent().find('.counter').html(totalChars-new_length+' characters left');
			
			// Change the color
			if(new_length == 0) {	
				$(this).parent().find('.counter').css('color','#999999');						// No characters typed
				$(this).parent().find('.shareSubmit').attr('disabled','disabled');
			} else if(new_length <= (totalChars - warning)) {									// 0 - warning characters typed
				$(this).parent().find('.counter').css('color','#999999');
				$(this).parent().find('.shareSubmit').removeAttr('disabled');
			} else if((new_length > (totalChars - warning)) && (new_length <= totalChars)) {		// warning - 140 characters typed
				$(this).parent().find('.counter').css('color','#990000');
				$(this).parent().find('.shareSubmit').removeAttr('disabled');
			} else if(new_length > totalChars) {											// 140+ characters typed
				$(this).parent().find('.counter').css('color','#ff0000');
				$(this).parent().find('.shareSubmit').attr('disabled','disabled');
			}
			
		});
	});

});


function status_delete() {

	var url    = $(this).attr("href");
	var snd    = {"delete": $(this).attr("title")};
	var update = $(this).parent().parent().parent();

	$.post(url, snd, function(ret) {
		update.fadeOut(300,function(){
			update.remove();
		});
	});

	return false;

}


function status_beforesubmit(d,f,o) {
	if (d[1].value == "") {
		$(f).children("input:text").addClass("error")
		return false;
	}
	$(f).children("input:text").removeClass("error")
}

function status_success(data, status, xhr, f){

	var thingy = checkForLink(data.name);

	// Prepare the new post
	var post = "\
		<div class=\"photo\">\
			<div class=\"photothumb\">\
				<a href=\"/organizations/" + data.organisation_id + "/\"><img alt=\"" + data.organisation_name + "\" src=\"" + data.organisation_logo + "\"></a>\
			</div>\
				<a class=\"follow\" href=\"/organizations/" + data.organisation_id + "/follow/\">Follow</a>\
		</div>\
		<div class=\"dialog\">\
			<h4><strong><a href=\"/organizations/" + data.organisation_id + "/\">" + data.organisation_name + "</a></strong></h4> \
			" + thingy + "\
			<p class=\"CommentMenu\">\
				<em>Just now</em> by <a href=\"/accounts/" + data.user_id + "/\">" + data.user_name + "</a> &bull; \
				<a href=\"#\">Comment</a> &bull; \
				<a href=\"/accounts/like/u/" + data.id + "\">Like</a> &bull; \
				<a href=\"/feeds/status/\" title=\"" + data.id + "\" class=\"deletestatus\">Delete</a>\
			</p>\
		</div>\
	";
	$("#stream .posts").prepend("<div></div>").children("div").eq(0).hide().addClass("post").append(post).fadeIn(900);
	$("#status textarea.blankable").val("");
	$("#status .counter").html('140 characters left');
	$("#status .counter").css('color','#999999');						// No characters typed
	$("#status .shareSubmit").attr('disabled','disabled');
	
	// Update the display text
	//$("#status .counter").html(totalChars+' characters left');

	$("a.deletestatus").click(status_delete);

}

