var comments_id = null;
var comments_type = null;
var comments_global = [];
var comments_per_page = 15;
var comments_pages;
var commentListObject = $('commentList');
var current_page = 0;

function doCommentEnabled(e) {
	$('comment_text').disabled = !e;
	$('comment_button').disabled = !e;
}

function doPostComment() {
	if (comments_type === null || comments_id === null) return;

	var text = String($('comment_text').value);
	text = text.trim();
	$('comment_text').value = text;
	
	doCommentEnabled(false);
	
	if (text.length < 0x10) {
		alert(locale._('COMM_LENGTH'));
		doCommentEnabled(true);
		return false;
	}
	
	var url = 'opajax.php?action=postcomment&type=' + comments_type + '&id=' + comments_id + '&text=' + escape($('comment_text').value);

	httpGetJson(url, function(data) {
		if (data) {
			updateComments();
			alert(locale._('COMM_SUCCESS'));
			$('comment_text').value = '';
		} else {
			alert(locale._('COMM_ERROR'));
		}
		doCommentEnabled(true);
	});
}

function htmlShowPages(page) {
	html = locale._('COMM_PAGES');

	html += '<ul class="commentsPageList">';
	
	for (var n = 0; n < comments_pages; n++) {
		if (n != page) {
			html += '<li><a href="javascript:showCommentsPage(' + n + ');">' + (n + 1) + '</a></li>';
		} else {
			html += '<li class="selected">' + (n + 1) + '</li>';
		}
	}
	
	html += '</ul>';

	return html;
}

function showCommentsPage(page) {
	if (page == undefined) page = current_page;
	var from = comments_per_page * page;
	var to = from + comments_per_page - 1;
	current_page = page;

	if (to >= comments_global.length) to = comments_global.length - 1;

	//alert(from + ', ' + to);

	var obj = commentListObject;
	var html = '';

	if (comments_global.length == 0) {
		html += '<p>' + locale._('COMM_EMPTY') + '</p>';
	} else {
		html += '<ul class="commentsList">';
		for (var n = from; n <= to; n++) {
			var comment = comments_global[n];
			//alert(comment); break;
			html += '<li class="commentEntry">';
			//html += '<div class="name">#' + (comments_global.length - n) + ' &middot; <strong>' + comment.name + '</strong> dice:</div>';
			html += '<div class="commentHeader"><span class="name">' + comment.name + '</span>';
			if (comment.ctime != 0) {
				html += ' <span class="date">(' + getDistTime(comment.ctime, comment.cctime) + ')</span>';
			}
			html += '</div>';
			html += '<div class="commentText">' + comment.text + '</div>';
			html += '</li>';
			//alert(comment);
			//document.body.comment = comment;
			//break;
		}
		html += '</ul>';

		html += '<div class="commentsHeader">';
		html += htmlShowPages(page);
		html += '</div>';
	}

	obj.innerHTML = html;
}

function updateComments(id, type) {
	if (id != undefined) comments_id = id;
	if (type != undefined) comments_type = type;
	httpGetJson('opajax.php?action=commentlist&type=' + comments_type + '&id=' + comments_id, function(comments) {
		comments_global = comments;
		comments_pages = Math.ceil(comments_global.length / comments_per_page);
		showCommentsPage(0);
	});
}