// ###########################
// ############" START Comment

$('#openCloseAddComment').live('click', function(event) {
	event.preventDefault();
	
	var itemBox = $(this).closest('#itemBox');
	
	if ($('#domAddCommentBox', itemBox).css('display') == 'none') {
		$('#domAddCommentBox', itemBox).css('display','block');
		enableAddCommentBox(itemBox);
	}
	else {
		$('#domAddCommentBox', itemBox).css('display','none');
	}
});

$('#showAllComments').live('click', function(event) {
	event.preventDefault();
	var itemBox = $(this).closest('#itemBox');
	var item_id = itemBox.attr('class');
	
	var nb_comments = ybComment_getCommentCountFromDom(item_id);
	var criteria = {"resourceid":item_id, "commentCount":nb_comments};
	ybComment_displayComments(criteria);
});

$('.showAllCommentsPagination').live('click', function(event) {
	event.preventDefault();
	var itemBox = $(this).closest('#itemBox');
	var item_id = itemBox.attr('class');
	var url = $(this).attr('id');
	var criteria = {"resourceid":item_id};
	ybComment_displayComments(criteria, url);
});

$('#hideAllComments').live('click', function(event) {
	event.preventDefault();
	var itemBox = $(this).closest('#itemBox');
	var item_id = itemBox.attr('class');
	var nb_comments = ybComment_getCommentCountFromDom(item_id);
	var criteria = {"resourceid":item_id, "collapse":1, "commentCount":nb_comments};
	ybComment_displayComments(criteria);
});

function ybComment_getCommentCountFromDom(item_id) {
	var commentCountDom = 'commentCount_' + item_id;
	var commentCount = $('body').data(commentCountDom);
	if(commentCount==null || commentCount=='undefined') {
		var commentCount = $('#comments_'+item_id).attr("count");
	}
	return commentCount;
}

function ybComment_decrementCommentCount(item_id) {
	var commentCount = ybComment_getCommentCountFromDom(item_id);
	commentCount--;
	var commentCountDom = 'commentCount_' + item_id;
	$('body').data(commentCountDom, commentCount);
}

function ybComment_incrementCommentCount(item_id) {
	var commentCount = ybComment_getCommentCountFromDom(item_id);
	commentCount++;
	var commentCountDom = 'commentCount_' + item_id;
	$('body').data(commentCountDom, commentCount);
}

function ybComment_displayComments(criteria, url) {
	criteria.dom = 'comments_'+criteria.resourceid;
	criteria.ftype=11;
	var dom = criteria.dom;
	if(url===undefined) url='';
	criteria = JSON.stringify(criteria);
	
	$('#'+dom).append(' '+loading_img);
	
	$.ajax({
	  type: 'POST',
	  url: App.ajaxurl + 'process.php?q=facebookView',
	  data: 'criteria=' + criteria + '&url=' + url,
	  success: function(msg){
	  	if(msg=='') $('#'+dom).css('padding-top','0px');
	  	$('#'+dom).html(msg);
	  	$('.prettyDate').prettyDate();
	  }
	});
}

function enableAddCommentBox(itemBox) {
	var domComment = $('#domAddComment', itemBox);
	var domPicture = $('#domAddCommentPicture', itemBox);
	var domSubmit = $('#domAddCommentSubmit', itemBox);
	
	if(domComment.val()=='' || domComment.val()=='Write a comment...') {
		domComment.val('');
		domComment.css('color','black');
		domComment.css('height','50px');
		domComment.focus();
		domPicture.css('display','block');
		domPicture.css('width','2px');
		domPicture.css('padding-right','8px');
		domSubmit.css('display','block');
	}
}

function hideAddCommentBox(itemBox) {
	var domAddCommentBox = $('#domAddCommentBox', itemBox);
	var domComment = $('#domAddComment', itemBox);
	var domSubmit = $('#domAddCommentSubmit', itemBox);
	var domSubmitBtn = $('#domAddCommentSubmitBtn', itemBox);
	
	domAddCommentBox.css('display','none');
	domComment.removeAttr('disabled');
	domSubmitBtn.removeAttr('disabled');
	domComment.val('');
	domSubmitBtn.removeAttr('disabled');
}

$('#addCommentCancel').live('click', function(event) {
	event.preventDefault();
	var itemBox = $(this).closest('#itemBox');
	hideAddCommentBox(itemBox);
});

$('#domAddCommentSubmitBtn').live('click', function(event) {
	event.preventDefault();
	var itemBox = $(this).closest('#itemBox');
	var post_id = itemBox.attr('class');
	var comment = $('#domAddComment', itemBox).val();
	fb_addComment(post_id,comment,itemBox);
});

// #########################
// ############" END Comment

function fb_addComment(post_id,comment,itemBox) {
	var domAddComment = $('#domAddComment', itemBox);
	var domAddCommentSubmitBtn = $('#domAddCommentSubmitBtn', itemBox);
	domAddComment.attr('disabled','disabled');
	domAddCommentSubmitBtn.attr('disabled','disabled');
	
	var criteria = {"ftype":"12", "post_id":post_id, "comment":comment};
	$.ajax({
	  type: 'POST',
	  url: App.ajaxurl + 'process.php?q=facebookListener',
	  data: 'criteria=' + JSON.stringify(criteria),
	  success: function(msg){
	  	appendCommentToCommentsList(post_id,comment,itemBox);
  		ybComment_incrementCommentCount(post_id);
	  }
	});	
}

function appendCommentToCommentsList(post_id,comment,itemBox) {
	var criteria = {"ftype":"13", "comment":comment};
	$.ajax({
	  type: 'POST',
	  url: App.ajaxurl + 'process.php?q=facebookListener',
	  data: 'criteria=' + JSON.stringify(criteria),
	  success: function(msg){
	  	$('#comments_'+post_id).prepend(msg);
		hideAddCommentBox(itemBox);
		$('.prettyDate').prettyDate();
	  }
	});
}

