function drawCommentsPager(type, item_id, page, pages) {

	if(page > pages) {
		page = pages;
	}

	$("#comments_pager1").hide();
	$("#comments_pager2").hide();

	if(pages > 1) {
		var content = "<div class=\"comments_pager cfx\">";
		content += "<table><tr>";

		if(page != 1) {
			content += "<td class=\"f10\"><a href=\"\" onclick=\"this.blur(); return getComments('" + type + "', '" + item_id + "', " + (page - 1) + ", " + pages + ");\">&larr;</a></td>\n";
		} else {
			content += "<td class=\"f10\">&larr;</td>\n";
		}

		content += "<td>" + lang.common.page + ":</td>\n";
		content += "<td><div class=\"input_bg\"><input type=\"text\" style=\"width:20px;\" id=\"comments_pages_page\" value=\"" + page + "\" onkeypress=\"if(event.keyCode == 13) return getComments('" + type + "', '" + item_id + "', this.value, " + pages + ");\" /></div></td>\n";
		content += "<td>" + lang.common.page_of + " " + pages + "</td>\n";

		if(page != pages) {
			content += "<td class=\"f10\"><a href=\"\" onclick=\"this.blur(); return getComments('" + type + "', '" + item_id + "', " + (page + 1) + ", " + pages + ");\">&rarr;</a></td>\n";
		} else {
			content += "<td class=\"f10\">&rarr;</td>\n";
		}

		content += "</tr></table>\n";
		content += "</div>\n";

		$("#comments_pager1").html(content);
		$("#comments_pager2").html("<div class=\"line\"><span></span></div>\n\n" + content);
		$("#comments_pager1").fadeIn(200);
		$("#comments_pager2").fadeIn(200);
	}
}

function drawComments(obj, type, item_id, page, pages) {

	$("#comments").hide();
	$("#comments").empty();

	var a = 0;
	for(var i in obj) {
		a++;

		var this_div = "";

		if(a > 1) {
			this_div += "<div class=\"line\"><span></span></div>\n\n";
		}

		if(obj[i].url_name) {
			var user_link = "/" + language + "/users/" + obj[i].url_name + "/";
		} else {
			var user_link = "/" + language + "/users/" + obj[i].user_id + "/";
		}

		this_div += "<a name=\"comment" + obj[i].comment_id + "\"></a>\n";
		this_div += "<div id=\"comment" + obj[i].comment_id + "\">\n";
		this_div += "<table class=\"comments_tbl\"><tr>\n";
		this_div += "<td>\n";
		this_div += "<div class=\"cfx\">\n";
		this_div += "<div class=\"upic\"><a href=\"" + user_link + "\"><img src=\"" + obj[i].ava + "\" alt=\"\" /></a>";
		if(obj[i].online == "Y") {
			this_div += "<span>online</span>";
		}
		this_div += "</div>\n";
		this_div += "<div class=\"text\">\n";

		this_div += "<a href=\"" + user_link + "\" class=\"name\">" + obj[i].name + "</a><br />\n";
		this_div += "<span class=\"dates\">" + obj[i].date + "</span>\n";
		this_div += "<div>\n";
		if(obj[i].mediatype == 1) {
			this_div += "<span class=\"graffiti\"><a href=\"" + obj[i].gr_big + "\" rel='lightbox'><img src=\"" + obj[i].gr + "\" alt=\"\" /></a></span>\n";
		} else {
			this_div += "<span>" + obj[i].comment + "</span>\n";
		}
		this_div += "</div>\n";
		if(obj[i].can_delete) {
			this_div += "<div class=\"links\"><a href=\"#\" onclick=\"this.blur(); return confirma('" + lang.comments.sure + "', delComment, [" + obj[i].comment_id + ", " + type + ", " + item_id + ", " + page + ", " + pages + "]);\">" + lang.common.del + "</a></div>\n";
		}

		this_div += "</div>\n";
		this_div += "</div>\n";
		this_div += "</td>\n";
		this_div += "</tr></table>\n";
		this_div += "</div>\n";

		$("#comments").append(this_div);
	}

	$("#comments").fadeIn(200);

	if(!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
		jQuery(function($) {
			$("a[rel^='lightbox']").slimbox({ }, null, function(el) {
				return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
			});
		});
	}
}

function getComments(type, item_id, page, pages) {

	$("#comments").html("<div>" + wait_message + "</div>");

	$.ajax({
		type: "POST",
		url: "/" + language + "/engine/comments.php",
		data: "action=get&type=" + type + "&item_id=" + item_id + "&page=" + page,
		success: function(msg) {
			if(msg == "error") {
				$("#comments").html("<div>" + lang.common.error + ".</div>");
			} else {
				if(!msg) msg = "{}";
				eval("var this_comments = " + msg + ";");
				drawComments(this_comments, type, item_id, page, pages);
			}
		}
	});

	drawCommentsPager(type, item_id, page, pages);

	return false;
}

function addComment() {

	if(!$("#commentText").val()) { $("#commentText").focus(); return false; }

	$("#comments").html("<div>" + wait_message + "</div>");

	var data = "action=add&" + $("#comments_add_form").serialize();

	$.ajax({
		type: "POST",
		url: "/" + language + "/engine/comments.php",
		data: data,
		success: function(msg) {
			if(msg == "error") {
				$("#comments").html("<div>" + lang.comments.not_added + "</div>");
			} else {
				if(!msg) msg = "{}";
				eval("var response = " + msg + ";");
				if(response.pages) {
					$("#commentText").val("");
					if(type == 3 || type == 4 || type == 7 || type == 10) {
						getComments(type, item_id, response.pages, response.pages);
					} else {
						getComments(type, item_id, 1, response.pages);
					}
					/*getComments(type, item_id, 1, response.pages);*/
				} else {
					$("#comments").html("<div>" + lang.comments.not_added + "</div>");
				}
			}
		}
	});

	return false;
}

function delComment(comment_id, type, item_id, page, pages) {

	var comment_content = $("#comment" + comment_id).html();
	$("#comment" + comment_id).html(wait_message);
	$.ajax({
		type: "POST",
		url: "/" + language + "/engine/comments.php",
		data: "action=delete&comment_id=" + comment_id,
		success: function(msg) {
			if(msg == "error") {
				$("#comment" + comment_id).html(comment_content);
				$("#comment" + comment_id).prepend("<span>" + lang.comments.not_deleted + "</span><br />");
			} else {
				if(!msg) msg = "{}";
				eval("var response = " + msg + ";");
				if(response.pages) {
					getComments(type, item_id, page, response.pages);
				} else {
					$("#comments").html("<div>" + lang.comments.no_comments + "</div>");
				}
			}
		}
	});
}
