$(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(document.base_url+'ajax/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;
	});
	//moderator: add status
	$('#admin_post_status').bind('click', function(){
		var text = 'Status tidak berhasil dimasukkan.';
		$('.notebox').removeClass('notebox-warning').hide();
		if('Ketik status baru...' != $('#admin_status_content').val() && '' != $('#admin_status_content').val()){
			$(this).attr('disabled','disabled');
			$.post(document.base_url+'ajax/admin_status/',
				{ status_content: $('#admin_status_content').val(), is_ajax: true } ,
				function(data){
					if(!data){
						$('#admin_status_warning').addClass('notebox-warning').html(text).show();
					}else{
						text = 'Status telah ditambahkan.';
						$('.table-common a').removeClass('big');
						$('#plurk_list').after(data);
						$('#admin_status_warning').html(text).show();
						$('#admin_status_content').val('');
					}
				}
			);
			$(this).removeAttr('disabled');				
		}else{
			$('#admin_status_warning').addClass('notebox-warning').html(text).show();
		}
		return false;
	});
	//moderator: check all
	$('#select_all').click(function(){
		var c = this.checked;
		$('input[name=delete[]]').each(function(){
			this.checked = c;
		});
	});
});


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(document.base_url+'ajax/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(document.base_url+'ajax/comment_status_count/');		
		$('#plurk-status').load(document.base_url+'ajax/comment_status_count/');		
		$('#plurk_warning').empty().hide();
		$('#plurk_comment').siblings('.status-wait').hide();
}

function getRelatedNews() {
	if (document.bataviase_related_ajax_keywords) {
		$.post(document.base_url + "ajax/bataviase_related/", {title: document.bataviase_related_ajax_keywords}, function(json)
		{
			//console.log("tes");
			//var json = eval(data);
			var text = "";
			for(i=0; i < 3; i++) {
				text += "<li>";
				text += "<a href=\"" + json[i].url + "\" class=\"newstitle\">" + json[i].title + "</a>";
				text += "<br/><span class=\"small\">" + json[i].date + "</span>"
				text += "<br/><span class=\"small\">" + json[i].medianame + "</span>"
				text += "</li>";
			}
			
			$("#bataviaseRelated").append(text);
			$("#bataviaseRelated").parent().slideDown("fast");
		}, "json");
	}
};

function removeDisabled(){
	$('#plurk_save').removeAttr('disabled');
}

function removeDebateDisabled(){
	$('.response-comments-submit').removeAttr('disabled');
}

function getDebateResponseComments(responseId, responseCommentsDiv){
	var last_id = 0;
	$.getJSON(document.base_url + "debat/get_response_comments/" + responseId,  function(json){
		//$(responseCommentsDiv).children("dl").find("dd.wait").remove();
		text = "";
		for(i=0; i<json.length; i++) {
			text += "<dt>";
			text += "<img src=\"" + document.base_url +  "images/users/" + json[i].user_id + "-16.jpg\" alt=\"\" />";
			text += "<a href=\"" + document.base_url +  "profil/" + json[i].user_name + ".html\">" + json[i].user_penname + "</a>: ";
			text += "<span class=\"small\">(" + json[i].comment_datestring + ")</span></dt>";
			text += "<dd>" + json[i].comment_body + "</dd>";
			last_id = json[i].comment_id;
		}
		$(responseCommentsDiv).find("dd.wait").before(text);
		$(responseCommentsDiv).find("dd.wait").slideUp("slow");
		$(responseCommentsDiv).find("input.debate-respond-comment-id").val(last_id);
	});
}

function setDebateResponseComments(form){
	var debate_respond_comment_comment = form.debate_respond_comment_comment.value;
	var debate_respond_id = form.debate_respond_id.value;
	var last_debate_respond_comment_id = form.last_debate_respond_comment_id.value;
	var responseCommentsDiv = $(form).parent('dd').parent('dl').parent('div');
	$(responseCommentsDiv).find("dd.wait").show();
	
	//Check
	$(responseCommentsDiv).children("dl").find('dd.debate-ajax').empty().hide();
	var text = 'Komentar tidak berhasil dimasukkan. Minimal 5 karakter.';
	if('' != debate_respond_comment_comment && 0 != debate_respond_id){
	
	form.submit.disabled = true;
	$.post(document.base_url+'debat/set_response_comment/',
		{ 	debate_respond_comment_comment: debate_respond_comment_comment, 
			debate_respond_id: debate_respond_id,
			last_debate_respond_comment_id: last_debate_respond_comment_id } ,
		function(data){
			if(!data){
				setTimeout('removeDebateDisabled()', 100);
				$(responseCommentsDiv).find("dd.wait").hide();
				$(responseCommentsDiv).children("dl").find('dd.debate-ajax').html(text).show();
			}else{
				$.getJSON(document.base_url + "debat/get_response_comments/" + debate_respond_id + "/" + last_debate_respond_comment_id,  function(json){
					//$(responseCommentsDiv).children("dl").find("dd.wait").remove();
					for(i=0; i<json.length; i++) {
					//for(a=json.length; a>0; a--) {
						//i = a-1;
						text = "<dt>";
						text += "<img src=\"" + document.base_url +  "images/users/" + json[i].user_id + "-16.jpg\" alt=\"\" />";
						text += "<a href=\"" + document.base_url +  "profil/" + json[i].user_name + ".html\">" + json[i].user_penname + "</a>: ";
						text += "<span class=\"small\">(" + json[i].comment_datestring + ")</span></dt>";
						text += "<dd>" + json[i].comment_body + "</dd>";
						//$(responseCommentsDiv).children("dl").find('dd.wait').before(text);
						$(responseCommentsDiv).find("dd.wait").before(text);
						last_debate_respond_comment_id = json[i].comment_id;
					}
					$(responseCommentsDiv).find("dd.wait").hide();
					$(responseCommentsDiv).children("dl").find('dd.debate-ajax').hide();
					form.last_debate_respond_comment_id.value = last_debate_respond_comment_id;
				});
				form.debate_respond_comment_comment.value = '';
				setTimeout('removeDebateDisabled()', 2000);
			}
		}
	);
	
	}else{
		//Error Handling
		$(responseCommentsDiv).children("dl").find('dt.debate-ajax').html(text).show();
	}
	return false;
}

function getProfileComments(userName, lastCommentId, targetNode){
	var last_id = 0;
	$.getJSON(document.base_url + "ajax/get_profile_comments/" + userName + "/" + lastCommentId,  function(json){
		if(json.length > 0)
		{
			text = "";
			for(i=0; i<json.length; i++) {
				text += "<dt id=\"comment-" + json[i].comment_id + "\">";
				text += "<img src=\"" + document.base_url +  "images/users/" + json[i].user_id + "-32.jpg\" alt=\"\" />";
				text += "<a href=\"" + document.base_url +  "profil/" + json[i].user_name + ".html\" class=\"comment-author\">" + json[i].user_penname + "</a>";
				text += "<span class=\"small\">(" + json[i].comment_datestring + ")</span></dt>";
				text += "<dd><p>" + json[i].comment_body + "</p></dd>";
				last_id = json[i].comment_id;
			}
			$(targetNode).append(text);
			document.lastCommentId = last_id;
			$(".waitCommentLoad").hide();
			
			if(json.length < 25)
				$(".noCommentNotification").show();
			else
				$(".nextCommentsButton").show();
		}
		else
		{
			$(".waitCommentLoad").hide();
			$(".nextCommentsButton").remove();
			$(".noCommentNotification").show();
		}
	});
}

function getProfileMentions(userName, lastCommentId, targetNode){
	var last_id = 0;
	$.getJSON(document.base_url + "ajax/get_profile_mentions/" + userName + "/" + lastCommentId,  function(json){
		if(json.length > 0)
		{
			text = "";
			for(i=0; i<json.length; i++) {
				text += "<li id=\"comment-" + json[i].comment_id + "\">";
				text += "<h4><a href=\"" + document.base_url +  json[i].article_link + "\">" + json[i].article_title + "</a></h4>";
				text += "<p><span class=\"small\"><a href=\"" + document.base_url +  "profil/" + json[i].user_name + ".html\" class=\"comment-author\">" + json[i].user_penname + "</a> ";
				text += "(" + json[i].comment_datestring + ")</span></p>";
				text += "<blockquote><p>" + json[i].comment_body + "</p></blockquote>";
				last_id = json[i].comment_id;
			}
			$(targetNode).append(text);
			document.lastCommentId = last_id;
			$(".waitCommentLoad").hide();
			
			if(json.length < 20)
				$(".noCommentNotification").show();
			else
				$(".nextMentionsButton").show();
		}
		else
		{
			$(".waitCommentLoad").hide();
			$(".nextMentionsButton").remove();
			$(".noCommentNotification").show();
		}
	});
}


$(function(){
  $(".rateclick").bind('click', function(){
    var button = this.id.substr(1).split('v');
    var rateButton = $(this).parents(".ratingButton-active");
    var postvars = {};
    postvars.ajax = true;
    postvars.rating = button[0];
    postvars.value = button[1];
    //postvars.article = $(".ratingButton-active a.ratingButton").attr('id').substr(2);
    postvars.article = $(rateButton).attr('id').substr(2);
    clearTimeout(RATING_MENU_TIMER);
		$(".ratingButton div").appendTo("#rating-options");
    //$(".ratingButton-active").replaceWith('<p id="ar'+postvars.article+'"><img src="'+document.base_url+'images/ajax-rating.gif" style="float:right;" /></p>');
    
    $(rateButton).css("width", $(rateButton).width() + "px");
    $(rateButton).html("&nbsp;");
    $(rateButton).addClass("wait");
    $(rateButton).unbind();
    
    $.post(document.base_url+'ajax/rate_article/', postvars, function(data) {
      var id = $(data).attr('id');
      //$("#"+id).replaceWith(data);
      $(rateButton).html(data);
    	$(rateButton).css("width","auto");
	    $(rateButton).removeClass("wait");
	    $(rateButton).parent().addClass("ratingButtonVoted");
    });
  });
  $("body").append('<img src="'+document.base_url+'styles/images/ajax-rating.gif" style="display:none;" />');
});

