$(function(){
	plurk();
	//member: saving head status comment
	$('#plurk_save').bind('click', function(){
		var text = 'Komentar tidak berhasil dimasukkan. Minimal 5 karakter. Maksimal 140 karakter.';
		var postInfo = $('#plurk_comment').siblings("form").find(".post-info");
		
		$("#plurk_status_comment_text").attr("disabled", "disabled");
		$(postInfo).children("span").addClass("none");
		$(postInfo).addClass("comment-sending");
		if('Mari berkomentar di sini...' != $('#plurk_status_comment_text').val() && '' != $('#plurk_status_comment_text').val()){
			$(this).attr('disabled','disabled');
			$.post(url+'groupyajax/comment_status/',
				{ status_comment_text: $('#plurk_status_comment_text').val() } ,
				function(data){
					plurk();
					if(!data){
						$('#plurk_warning').html(text).show();
						$(postInfo).children("span").removeClass("none");
						setTimeout('removeDisabled()', 100);
					}else{
						$('#plurk_status_comment_text').val('');
						$(postInfo).children("span").empty();
						setTimeout('removeDisabled()', 1000);
					}
				}
			);
			
		}else{
			$('#plurk_warning').html(text).show();
		}
		return false;
	});
});


function plurk(){
	$('#plurk_comment')
		.ajaxStart(function(){
			//$('#plurk_comment').find('span').remove().append('<span id="plurk_load">loading </span>');
			//$('#plurk_comment').find('div.status_wait').remove().append('<span id="plurk_load">loading </span>');
		})
		.load(url+'groupyajax/status/', function(){
				$("#plurk_comment").slideDown();
				document.getElementById('plurk_comment').scrollTop = $("#plurk_comment ul").height();
				$('#plurk_comment').siblings("form").find(".post-info").removeClass("comment-sending");
				$("#plurk_status_comment_text").removeAttr("disabled");
			})
		.ajaxStop(function(){
			//$('#plurk_comment').find('span').remove().focus();
			$('#plurk_comment').find('div.status_wait').remove()
		});
		//$('.plurk_total').load(url+'ajax/comment_status_count/');		
		$('#plurk-status').load(url+'groupyajax/comment_status_count/');		
		$('#plurk_warning').empty().hide();
		$('#plurk_comment').siblings('.status-wait').hide();

}

function removeDisabled(){
	$('#plurk_save').removeAttr('disabled');
}

var POL_STATUS_IS_OPEN = 0;

$("document").ready(function() {

	/* STATUSBOX */
	$(".status-body").click(function(){
		if (POL_STATUS_IS_OPEN == 0)
		{
			$("#screen-blocker").css("display", "block");
			$(".status-info").slideDown('fast', function(){
				$(".status-body .excerpt").css('display', 'none');
				$(".status-body .text").css('display', 'block');
			});
			POL_STATUS_IS_OPEN = 1;
			$('#plurk_warning').empty().hide();
		}
		else
		{
			$("#screen-blocker").css("display", "none");
			$(".status-info").slideUp('fast', function(){
				$(".status-body .excerpt").css('display', 'block');
				$(".status-body .text").css('display', 'none');
			});
			POL_STATUS_IS_OPEN = 0;
			plurk();
		}
		return false;
	});
	
	$("#slide-status-toggle").click(function(){
		if (POL_STATUS_IS_OPEN == 0)
		{
			$(".status-info").slideDown('fast', function(){
				$(".status-body .excerpt").css('display', 'none');
				$(".status-body .text").css('display', 'block');
			});
			POL_STATUS_IS_OPEN = 1;
		}
		else
		{
			$(".status-info").slideUp('fast', function(){
				$(".status-body .excerpt").css('display', 'block');
				$(".status-body .text").css('display', 'none');
			});
			POL_STATUS_IS_OPEN = 0;
		}
		return false;
	});
	
	$("#screen-blocker").click(function(){
		$(".status-info").slideUp('fast', function(){
			$(".status-body .excerpt").css('display', 'block');
			$(".status-body .text").css('display', 'none');
		});
		POL_STATUS_IS_OPEN = 0;
		$(this).css("display", "none");
	});
	
	// Indicator to display how many characters left //
	$("#plurk_status_comment_text").keypress(function(){
		var numChars = ($(this).attr("value")).length;
		
		if (numChars > 82) {
			$(this).css("height", "6em");
		}
		
		if(numChars > 140) {
			$(this).parent().find(".post-info span").html( "Kelebihan " + (numChars - 140) + " huruf");
			$(this).parent().find(".post-info span").addClass("post-info-alert");
			$(this).parent().children("#plurk_save").attr("disabled", "disabled");
		}
		else {
			if ((140 - numChars) == 0)
				$(this).parent().find(".post-info span").html( "Sudah pas 140 karakter");
			else
				$(this).parent().find(".post-info span").html( "Masih muat " + (140 - numChars) + " huruf lagi");

			$(this).parent().children("#plurk_save").removeAttr("disabled");
			$(this).parent().find(".post-info span").removeClass("post-info-alert");
		}
		
	});
	/* END STATUSBOX */
	
});