var smiles = new Array();
smiles[0] = new Array(':)', 'smile.gif');
smiles[1] = new Array(';-)', 'aq.gif');
smiles[2] = new Array(':(', 'ac.gif');
smiles[3] = new Array(':-P', 'ae.gif');
smiles[4] = new Array('[:-}', 'ar.gif');
smiles[5] = new Array(':-D', 'ag.gif');
smiles[6] = new Array('>:o', 'am.gif');

function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}

function smilesReplace(comment) {
	for (var j = 0; j < smiles.length; j++) 
		comment = str_replace(smiles[j][0], '<img style="vertical-align: middle" src="/img/smiles/' + smiles[j][1] + '"/>', comment);
	return comment;
}


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

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 drawCommentsPagerC(member, competition, page, pages)
{
	
	if(page > pages)
	{
		page = pages;
	}

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

	if(pages > 1)
	{
		var content = "<div class=\"pager\">";
		//content += "<table><tr>";
		
		if(page != 1)
		{
			content += "<div class=\"prev_page\"><a href=\"\" onclick=\"this.blur(); return getCommentsC('" + member + "', '" + competition + "', " + (page - 1) + ", " + pages + ");\">&#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 getCommentsC('" + member + "', '" + competition + "', this.value, " + pages + ");\" />\n";
		content += "из " + pages + "</div>\n";

		if(page != pages)
		{
			content += "<div class=\"next_page\"><a href=\"\" onclick=\"this.blur(); return getCommentsC('" + member + "', '" + competition + "', " + (page + 1) + ", " + pages + ");\">&#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 drawCommentsC(obj, member, competition, page, pages)
{
	$("#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=\"user_mini\"><a href=\"#\" onclick=\"this.blur(); return delCommentC(" + obj[i].comment_id + ", '" + member + "', '" + competition + "', " + page + ", " + pages + ");\">Удалить</a></div>\n";
		}
		
		this_div += "</td>\n";
		this_div += "</tr></table>\n";
		this_div += "</div>\n";

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

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

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)
{
	$("#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";
		if(obj[i].pay=='Y'){
		this_div += "<div class=\"comment_title\"><a class=\""+obj[i].sex+"\" href=\"/users/" + obj[i].name + "\">" + obj[i].name + "</a>&nbsp; " + obj[i].date + "<span class=\"plus\">" + obj[i].comment + "</span></div>\n";
		}else if (obj[i].vote=='Y'){
			stars = '&nbsp;';
			star_comment = str_replace('+', '', obj[i].comment); 
			for (var j = 0; j < star_comment; j++)
				stars += '<img src="/img/vote_star.gif" />';
			//alert(stars);
			this_div += "<div class=\"comment_title\"><a class=\""+obj[i].sex+"\" href=\"/users/" + obj[i].name + "\">" + obj[i].name + "</a>&nbsp; " + obj[i].date + stars + "</div>\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 + "</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=\"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 getCommentsC(member, competition, page, pages)
{
	$("#comments").html("<div style=\"padding-left:15px;\">" + wait_message + "</div>");
    var data="act=show&member=" + member + "&competition=" + dream + "&page=" + page;
  
	$.ajax({
		type: "POST",
		url: "/jx/c_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 + ";");
				
				drawCommentsC(this_comments, member, competition, page, pages);
			}
		}
	});

	drawCommentsPagerC(member, dream, page, pages);

	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 addCommentC()
{	
	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/c_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("");
									  
						getCommentsC(member, competition, 1, response.pages);
					
				}
				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 delCommentC(comment_id, member, competition, page, pages)
{
	$("#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/c_comments.php",
				data: "act=delete&id=" + comment_id + "&competition=" + competition ,
				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)
						{
							
							getCommentsC(member, competition, page, response.pages);
						}
						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";
		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].pay=='Y'){
		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=\"/dreams/" + obj[i].dream_id + "\">" + obj[i].dream_title + "</a><span class=\"plus\">" + obj[i].comment + "</span></div>\n";
		}else{
		if(obj[i].type=='мечта'){
			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=\"/dreams/" + obj[i].dream_id + "\">" + obj[i].dream_title + "</a></div>\n";
		}else if(obj[i].type=='подарок'){
			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=\"/gifts/" + obj[i].dream_id + "\">" + obj[i].dream_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=show&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;
}

