var post_count=10;

function showCommentBlock(postId){
	document.getElementById("hidden_comments_" + postId).style.display = "block";
	document.getElementById("view_all_comments_" + postId).style.display = "none";
}

function showComments(postId) {
	   //document.getElementById("hidden_comments_" + postId).style.display = "block";
	   $("#hidden_comments_" + postId).show("slow");
	   document.getElementById('comment_count_text_' + postId).style.display = "none";
}

function toggleCategoryCheckboxes(button){
	catList = document.getElementById('categories_checkboxes');
	if(catList.style.display == "none" || catList.style.display == ""){
		catList.style.display = "block";
	}else{
		catList.style.display = "none";
	}
}


function addComment(message_input, postId){

	comments = document.getElementById('comments_' + postId);
	comment_count = document.getElementById('comment_count_' + postId);
	comment_count_text = document.getElementById('comment_count_text_' + postId);

	var s = function addCommentSuccess(response) {
		
		var errorSign = "ERROR:";
		if(response.substring(0, errorSign.length) === errorSign){
			var message = response.substring(errorSign.length);
			alert(message);
			return;
		}
		
		var newComment = document.createElement("div");
		newComment.style.display = "none";
		newComment.id = "comment_style";
		newComment.innerHTML = response;

		$('#comments_' + postId).append(newComment);
		$(newComment).show("slow");

		if (comment_count != null) {
			var commentCount = parseInt(comment_count.innerHTML) + 1;
			comment_count.innerHTML = commentCount;
		}
		message_input.value = "";
	}
	var url = "saveComment.do";
	var method = "POST";
	var params = "post_id=" + postId + "&content=" + message_input.value;
		
	sendAjaxRequest(url, method, params, s);
}

function add_post() {
	//content = document.getElementById('new_posts');

	post_title = document.getElementById('post_title');
	post_text = document.getElementById('post_text');
	post_tags = document.getElementById('tags_input');
	category_select = document.getElementById('category_select');
	categories = "";
	for ( var i = 0; i < category_select.options.length; i++) {
		if (category_select.options[i].selected) {
			categories += category_select.options[i].value + " ";
		}
	}

	categories_cb_block = document.getElementById('categories_checkboxes');

	var s = function addPostSuccess(response) {
		var errorSign = "ERROR:";
		if(response.substring(0, errorSign.length) === errorSign){
			var message = response.substring(errorSign.length);
			$('#loadingScreen').hide("slow");
			showMessageDialog({title: "Ooops...", message: message});
			//alert(message);
			return;
		}
		
		var newPost = document.createElement("div");
		newPost.innerHTML = response;
		var ppp = newPost.children[0];

		$('#posts').prepend(ppp);
		$(ppp).css("display", "none");
		$(ppp).show("slow");

		post_title.value = "";
		post_text.value = "";
		post_tags.value = "";
		$('#loadingScreen').hide("slow");
	}

	var url = "savePost.do";
	var method = "POST";
	var params = "post_title=" + post_title.value + "&post_text="
			+ post_text.value + "&post_tags=" + post_tags.value
			+ "&categories=" + categories;
	
	$('#loadingScreen').show("slow");
	sendAjaxRequest(url, method, params, s);
	
} 

function sendForgotPasswordRequest(){
	   var s = function success(response){
		   var obj = eval("(" + response + ")");
		   if(obj.status == "success"){
				showLoginForm();
				alert("Reminder message sent");
	   		}else if(obj.status == "fail"){
	   			//Internal error
	   			showLoginForm();
	   			alert("Some how couldn't send password reminder. Please contact admin.");
	   		}
	   }
	   
	   var params = "userName=" + document.getElementById("userName").value;
	   var url = "sendPasswordReminder.do";
	   var method = "POST";	

	   sendAjaxRequest(url, method, params, s);
}

function register(){
	var s = function success(response){
		   var obj = eval("(" + response + ")");
		   if(obj.status == "success"){
				showLoginForm();
				alert("Check your mail for your login password");
	   		}else if(obj.status == "fail"){
	   			//Internal error
	   			showLoginForm();
	   			alert(obj.message);
	   		}
	   }
	   
	   var params = "email=" + document.getElementById("register_email").value;
	   var url = "register.do";
	   var method = "POST";	

	   sendAjaxRequest(url, method, params, s);
} 

function showAddCommentField(id){
	var s = function checkAuthorisedSuccess(response){
		var obj = eval("(" + response + ")");
		   if(obj.status == "success"){
			   document.getElementById("add_comment_text_" + id).style.display="none";
			   document.getElementById("comment_avatar_" + id).innerHTML = "<img src=\"" + obj.userAvatarPath + "\" width=\"32px\"\>";
			   $("#add_comment_" + id).show("slow");
			}else if(obj.status == "fail"){
				//not authorised
				alert("Please authorise");
			}
	}
	var url = "isauthorised.do";
	var method = "POST";
	
	sendAjaxRequest(url, method, "", s);

}

function removePost(id){
	var s = function postRemovedSuccess(response){
		var obj = eval("(" + response + ")");
		   if(obj.status == "success"){
			   $("#post_" + id).hide("slow");
			}else if(obj.status == "fail"){
				alert(obj.message);
			}
	}
	var url = "deletePost.do";
	var method = "POST";
	var params = "postId=" + id;
	
	sendAjaxRequest(url, method, params, s);
}

function deleteComment(id){
	var s = function commentRemovedSuccess(response){
		var obj = eval("(" + response + ")");
		   if(obj.status == "success"){
			   $("#comment_" + id).hide("slow");
			}else if(obj.status == "fail"){
				alert(obj.message);
			}
	}
	var url = "deleteComment.do";
	var method = "POST";
	var params = "commentId=" + id;
	
	sendAjaxRequest(url, method, params, s);
}

function ratePost(id, how){
	var s = function success(response){
		var obj = eval("(" + response + ")");
		   if(obj.status == "success"){
			   //$("#comment_" + id).hide("slow");
			   //alert("updated");
			   var sumObj = document.getElementById("rating_sum_" + id);
			   var sumVal = parseInt(sumObj.innerHTML);
			   if(how == 'up'){
				   sumVal += 1;
			   }else if(how == 'down'){
				   sumVal -= 1;
			   }
			   sumObj.innerHTML = sumVal;
			   
			   var countObj = document.getElementById("rating_count_" + id);
			   var countVal = parseInt(countObj.innerHTML) + 1;
			   countObj.innerHTML = countVal;
			   
			}else if(obj.status == "fail"){
				alert(obj.message);
			}
	}
	var url = "ratePost.do";
	var method = "POST";
	var params = "postId=" + id + "&how=" + how;
	
	sendAjaxRequest(url, method, params, s);
}


function getMorePosts(from, count){
	var s = function success(response){
		var newPosts = document.createElement("div");
		newPosts.innerHTML = response;
		var ppp = newPosts.children[0];

		$('#posts').append(ppp);
		$(ppp).css("display", "none");
		$(ppp).show("slow");
	}
	var url = "getMorePosts.do";
	var method = "GET";
	var params = "?from=" + post_count + "&count=" + count;
	post_count += 10;
	
	sendAjaxRequest(url + params, method, null, s);
}
