jQuery(document).ready(function() {

var commCount = 0;
var commPerPage = 3;
var pageCount = 0;

function getCommentsNav() {
	$.post( 
		"/control/jsp/comments/list_page_comments.jsp", 
		{ act: "getCount" },
		function(data){
			commCount = parseInt(data) ;
			pageCount = (commCount-(commCount % commPerPage)) / commPerPage ;
			if ((commCount % commPerPage)>0) { pageCount = pageCount + 1; }
			if (commCount>0) {
				for ( var i = 1; i <= pageCount; i++ )
	   				$("#links").append("<a href='#'>" + i + "</a> ");
			}
			$('#links').find('a').click(function(e) {
				$('#links').find('a').each(function(i) {
					$(this).css("text-decoration","underline");
				});
				e.preventDefault();
				$(this).css("text-decoration","none");
				getData(commPerPage*($(this).text()-1))
			});
			getData(0);
		}
	);
}

function sendComment() {
	$.post( 
		"/control/jsp/proccess_comment.jsp", 
		{ 
			redirect_page: $("#redirect_page").val(),
			page_uuid: $("#page_uuid").val(),
			page_path: $("#page_path").val(),
			photo_id: $("#photo_id").val(),
			com_text: $("#com_text").val()
		},
		function(data){
			$('#links').find('a').remove();
			getCommentsNav() ;
		}
	);
}

function getData(i) {
	$.post( 
		"/control/jsp/comments/list_page_comments.jsp", 
		{ act: "getData", pageNum: i },
		function(data){
			$("#result").html(
				data
			);

			$("#send").click(function(e) {
				e.preventDefault();
				sendComment();
			});
		},
		"html"
	);   
}

getCommentsNav() ;

});	