$(document).ready(function(){
						   
	$('#inputField').bind("blur focus keydown keypress keyup", function(){recount();});
	$('input.submitButton').attr('disabled','disabled');
	
	$('#tweetForm').submit(function(e){
	
		tweet();
		e.preventDefault();
	
	});
	$('#moreTweet').click(function(){
		getMoreTweets();						   
        }); 
	getMoreTweets();
});


function recount()
{
	var maxlen=140;
	var current = maxlen-$('#inputField').val().length;
	$('.counter').html(current);
	
		
	if(current<0 || current==maxlen)
	{
		$('.counter').css('color','#D40D12');
		$('input.submitButton').attr('disabled','disabled').addClass('inact');
	}
	else
		$('input.submitButton').removeAttr('disabled').removeClass('inact');

	
	if(current<10)
		$('.counter').css('color','#D40D12');
	
	else if(current<20)
		$('.counter').css('color','#5C0002');

	else
		$('.counter').css('color','#cccccc');
	
}



function tweet()
{
	var submitData = $('#tweetForm').serialize();
	
	$('.counter').html('<img src="discussion/ajax_load.gif" width="16" height="16" style="padding:12px" alt="loading" />');
	
	$.ajax({
		type: "POST",
		url: "discussion/submit.php",
		data: submitData,
		dataType: "html",
		success: function(msg){
			
			if(parseInt(msg)!=0)
			{
				$('ul.statuses li:first-child').before(msg);
				$("ul.statuses:empty").append(msg);
				
				$('#lastTweet').html($('#inputField').val());
				
				$('#inputField').val('');
				recount();
			}
		}
		
	});

}


function getMoreTweets()
{
	var submitData = "lastId=" + $('#lastId').val() + "&domid=" + $('#domid').val();//$('#tweetForm').serialize();
	$('#moreTweet').html('<img src="discussion/ajax_load.gif" width="16" height="16" style="padding:12px" alt="loading" />');
	//$('.counter').html('<img src="discussion/ajax_load.gif" width="16" height="16" style="padding:12px" alt="loading" />');
	
	$.ajax({
		type: "POST",
		url: "discussion/more.php",
		data: submitData,
		dataType: "html",
		success: function(msg){
			
			if(parseInt(msg)!=0)
			{
				if(msg=="%%NO-MORE%%"){
					$("#moreTweet").css("display", "none");
				} else {
					$('ul.statuses li:last-child').after(msg);
					$("ul.statuses:empty").append(msg);
					$('#moreTweet').html('more');
					$('#lastId').val(parseInt($('#lastId').val())+parseInt($('#maxTweets').val()));
					//If we reached the last tweet, hide [more]
					if(parseInt($('#lastId').val())>=parseInt($('#totalTweets').val())){
						$("#moreTweet").css("display", "none");
					}
					
				}
				
			}  
		}
		
	});

}