$(document).ready(function(){

	$("#tweet").focus().setCursorPosition(0);
	redcheck();
	$("#tweets li:last").addClass("last");

	$("#tweets .avatar").hover(function() {
		$(this).children(".avatar_big").stop().animate({opacity: 1.0}, 500).fadeIn();
	},function(){
		$(this).children(".avatar_big").stop().fadeOut();
	});

	$("#tweets li").hover(function() {
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});

	$("#tweet").keyup(function() {
		redcheck();
	})

});

function redcheck() {
	var chars = 140 - $("#tweet").val().length;
	$("#chars").html(chars);
	if (chars < 0) {
		$("#chars").addClass("red");
	} else {
		$("#chars").removeClass("red");
	}
};

// http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area
new function($) {
  $.fn.setCursorPosition = function(pos) {
    if ($(this).get(0).setSelectionRange) {
      $(this).get(0).setSelectionRange(pos, pos);
    } else if ($(this).get(0).createTextRange) {
      var range = $(this).get(0).createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
    }
  }
}(jQuery);
