
// ..................................................................

function getCommentColor (currColor) {
	var color = '';
	if (currColor == 'green') color = 'style="color: green;"';
	if (currColor == 'blue') color = 'style="color: blue;"';
	if (currColor == 'bluebold') color = 'style="color: blue; font-weight: bold;"';
	return color;
}





function drawCommentsPager(member, dream, page, pages, type)
{
	
	if(page > pages)
	{
		page = pages;
	}

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

	if(pages > 1)
	{
		var content = "<div class=\"pager\">";
		//content += "<table><tr>";
		
		if(page != pages)
		{
			content += "<div class=\"prev_page\"><a href=\"\" onclick=\"this.blur(); return getComments('" + member + "', '" + dream + "', " + (page + 1) + ", " + pages + ", " + type + ");\">&#8592;</a></div>\n";
		}
		else
		{
			content += "<div class=\"prev_page\">&#8592;</div>\n";
		}
		
		content += "<div class=\"pages\" style=\"text-align:center; float:left; width:246px;\">Страница:\n";
		content += "<input type=\"text\" id=\"comments_pages_page\" value=\"" + page + "\" onkeypress=\"if(event.keyCode == 13) return getComments('" + member + "', '" + dream + "', this.value, " + pages + ", " + type +");\" />\n";
		content += "из " + pages + "</div>\n";
                if(page != 1)
		{
			content += "<div class=\"next_page\"><a href=\"\" onclick=\"this.blur(); return getComments('" + member + "', '" + dream + "', " + (page - 1) + ", " + pages + ", " + type + ");\">&#8594;</a></div>\n";
		}
		else
		{
			content += "<div class=\"next_page\">&#8594;</div>\n";
		}
		
		
		content += "<div class=\"clr\"></div>\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, member, dream, page, pages, type)
{
	$("#comments").hide();
	$("#comments").empty();

	var a = 0;
	for(var i in obj)
	{
		a++;
		var this_div = "";
		
		var color = getCommentColor(obj[i].color);		
		
		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=\"comment_table\" cellspacing=\"0\"><tr>\n";
		this_div += "<td>\n";
		
		//this_div += "<div class=\"comment_title\"><a class=\""+obj[i].sex+"\" href=\"/users/" + obj[i].name + "\">" + obj[i].name + "</a>&nbsp; " + obj[i].date + "</div>\n";
		//this_div += "<p>\n";
		//this_div += "<span " + color + ">" + smilesReplace(obj[i].comment) + "</span>\n";
		//this_div += "</p>\n";
	
		if(obj[i].can_delete)
		{
		this_div += "<div class=\"comment_title\"><a class=\""+obj[i].sex+"\" href=\"/users/" + obj[i].name + "\"><b>" + obj[i].name + "</b></a>&nbsp; " + obj[i].date + "&nbsp;&nbsp;&nbsp;<a href=\"#\" onclick=\"this.blur(); return delComment(" + obj[i].comment_id + ", '" + member + "', '" + dream + "', " + page + ", " + pages + ");\">удалить</a> </div> \n";
		this_div += "<p>\n";
		this_div += "<span  class=\"my_message\">" + smilesReplace(obj[i].comment) + "</span>\n";
		this_div += "</p>\n";
		
		
		}else{
		
		this_div += "<div class=\"comment_title\"><a class=\""+obj[i].sex+"\" href=\"/users/" + obj[i].name + "\">" + obj[i].name + "</a>&nbsp; " + obj[i].date + "&nbsp;&nbsp;&nbsp;<a href=\"\" onclick=\"insertSmile('comment_text', '<b>to "+obj[i].name+":</b> '); return false;\">ответить</a> </div>\n";
		this_div += "<p>\n";
		this_div += "<span  >" + smilesReplace(obj[i].comment) + "</span>\n";
		this_div += "</p>\n";		
		}
			
		//if(obj[i].can_delete)
		//{
			//this_div += "<div class=\"user_mini\"><a href=\"#\" onclick=\"this.blur(); return delComment(" + obj[i].comment_id + ", '" + member + "', '" + dream + "', " + page + ", " + pages + ", " + type +");\">Удалить</a></div>\n";
		//}
		
		this_div += "</td>\n";
		this_div += "</tr></table>\n";
		this_div += "</div>\n";

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

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

// ..................................................................

function getComments(member, dream, page, pages, type)
{
	$("#comments").html("<div style=\"padding-left:15px;\">" + wait_message + "</div>");
    var data="act=show&member=" + member + "&dream=" + dream + "&page=" + page +"&type=" + type;
   
	$.ajax({
		type: "POST",
		url: "/jx/comments.php",
		data: data,
		success: function(msg)
		{ 
		
			if(msg == "error")
			{
				$("#comments").html("<div style=\"padding-left:15px;\">Ошибка.</div>");
			}
			else
			{  
				if(!msg) msg = "{}";
				eval("var this_comments = " + msg + ";");
				
				drawComments(this_comments, member, dream, page, pages, type);
			}
		}
	});

	drawCommentsPager(member, dream, page, pages, type);

	return false;
}


// ..................................................................

function addComment()
{
	if(!$("#comment_text").val()) { $("#comment_text").focus(); return false; }

	$("#comments").html("<div style=\"padding-left:15px;\">" + wait_message + "</div>");
	
	var data = "act=save&" + $("#comments_add_form").serialize();
   
	$.ajax({
		type: "POST",
		url: "/jx/comments.php",
		data: data,
		success: function(msg)
		{  
		
			if(msg == "error")
			{
				$("#comments").html("<div style=\"padding-left:15px;\">Ошибка. Комментарий не был добавлен.</div>");
			}
			else
			{
				if(!msg) msg = "{}";
				eval("var response = " + msg + ";");
				
				if(response.pages)
				{
					$("#comment_text").val("");
								  
						getComments(member, dream, 1, response.pages, type);
					
				}
				else
				{
					$("#comments").html("<div style=\"padding-left:15px;\">Ошибка. Комментарий не был добавлен.</div>");
				}
			}
		}
	});

	return false;
}


// ..................................................................

function delComment(comment_id, member, dream, page, pages, type)
{
	$("#confirm").jqmShow()
	.find("div.jqmConfirmMsg").html("Вы уверены, что хотите удалить <br>комментарий?")
	.end()
	.find("div.modalButtons").html("").html("<div class='yes'>Да</div><div class='no'>Отмена</div>")
	.end()
	.find("div.yes")
		.click(function(){
			$("#confirm").jqmHide();
			var comment_content = $("#comment" + comment_id).html();
			$("#comment" + comment_id).html("<div style=\"padding-left:15px;\">" + wait_message + "</div>");
			$.ajax({
				type: "POST",
				url: "/jx/comments.php",
				data: "act=delete&id=" + comment_id + "&dream=" + dream +"&type="+ type,
				success: function(msg)
				{
				
					if(msg == "error")
					{ 
						$("#comment" + comment_id).html(comment_content);
						$("#comment" + comment_id).prepend("<span>Ошибка. Комментарий не был удален.</span><br />");
					}
					else
					{
						if(!msg) msg = "{}";
						eval("var response = " + msg + ";");
						
						if(response.pages)
						{
							
							getComments(member, dream, page, response.pages, type);
						}
						else
						{
							$("#comments").html("<div style=\"padding-left:15px;\">Пока нет комментариев.</div>");
						}
					}
				}
			});
		})
	.end()
	.find("div.no")
		.click(function(){
			$("#confirm").jqmHide();
		});

	return false;
}



// ..................................................................


function drawBroadcastPager(page, pages)
{
	
	if(page > pages)
	{
		page = pages;
	}

	$("#Broadcast_pager").hide();
	if(pages > 1)
	{
		var content = "";
	
		if(page!=1)
		content += "<div class=\"efir_more2\"><a href=\"\" onclick=\"this.blur(); return getBroadcast( " + (page - 1) + ", " + pages + ");\">Назад</a></div>\n";
		if(page != pages)
		content += "<div class=\"efir_more\"><a href=\"\" onclick=\"this.blur(); return getBroadcast( " + (page + 1) + ", " + pages + ");\">Дальше</a></div>\n";
		content += "<div class='clr'></div>\n";
				
		$("#Broadcast_pager").html(content);
		$("#Broadcast_pager").fadeIn(200);
		
	}
}

// ..................................................................

function drawBroadcast(obj, page, pages)
{
	$("#Broadcast").hide();
	$("#Broadcast").empty();

	var a = 0;
	for(var i in obj)
	{
		a++;
		
		var this_div = "";
		
		var color = getCommentColor(obj[i].color);
	
		this_div += "<a name=\"Broadcast" + obj[i].comment_id + "\"></a>\n";
		this_div += "<div id=\"Broadcast" + obj[i].comment_id + "\">\n";
		this_div += "<table cellspacing=\"0\"><tr>\n";
		this_div += "<td>\n";

		if(obj[i].type==0){
			this_div += "<div class=\"efir_user\"><a class=\""+obj[i].sex+"\" href=\"/users/" + obj[i].name + "\">" + obj[i].name + "</a>&nbsp;&#8594;&nbsp;<a class=dream href=\"/game/" + obj[i].game_id + "\">" + obj[i].game_title + "</a></div>\n";
		}else if(obj[i].type==1){
			this_div += "<div class=\"efir_user\"><a class=\""+obj[i].sex+"\" href=\"/users/" + obj[i].name + "\">" + obj[i].name + "</a>&nbsp;&#8594;&nbsp;<a class=dream href=\"/video/" + obj[i].game_id + "\">" + obj[i].game_title + "</a></div>\n";
		}
		this_div += "\n";
		this_div += "<span " + color + ">" + smilesReplace(obj[i].comment) + "</span>\n";
		this_div += "\n";

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

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

	$("#Broadcast").fadeIn(200);
}

function getBroadcast(page, pages)
{
	$("#Broadcast").html("<div style=\"padding-left:15px;\">" + wait_message + "</div>");
    var data="act=broad&count=3&page=" + page;
  
	$.ajax({
		type: "POST",
		url: "/jx/comments.php",
		data: data,
		success: function(msg)
		{ 
			if(msg == "error")
			{
				$("#Broadcast").html("<div style=\"padding-left:15px;\">Ошибка.</div>");
			}
			else
			{  
				if(!msg) msg = "{}";
				eval("var this_comments = " + msg + ";");
				
				drawBroadcast(this_comments, page, pages);
			}
		}
	});

	drawBroadcastPager(page, pages);

	return false;
}

