
show_load_title = false;

function Comments(options)
{
	this.actions_path = "/common/comments/comments_actions.php";
	this.place = options.place;
	this.object_id = options.object_id;
	this.current_page = 0;
	this.plugins_callbacks = [];
	this.afterAddCallbacks = [];
	
	this.Init = function()
	{
		var obj = this;
		$("#answer-form .send-button").click(function(){
			obj.AddComment();
		});
		this.LightboxIt();
	};
	
	this.LightboxIt = function()
	{
		$("#comments-container .comments-list .comment_images a").lightBox({
			imageLoading: "/common/js/jquery/plugins/lightbox/images/lightbox-btn-loading.gif",
			imageBtnClose: "/common/js/jquery/plugins/lightbox/images/lightbox-btn-close.png",
			imageBtnPrev: "/common/js/jquery/plugins/lightbox/images/lightbox-btn-prev.png",
			imageBtnNext: "/common/js/jquery/plugins/lightbox/images/lightbox-btn-next.png",
			txtImage: "Изображение",
			txtOf: "из"
		});
	};
	
	this.GetPosts = function(page, get_last_page, callback)
	{
		var obj = this;
		$(".page_bar .throbber").show();
		$.ajax({
			url: this.actions_path,
			type: "GET",
			dataType: "json",
			data: {
				action: "getposts",
				get_last_page: get_last_page ? get_last_page : false,
				page: page ? page : 0,
				place: this.place,
				object_id: this.object_id
			},
			success: function(data) {
				$("#comments-container .comments-list").html(data.Html);
				if (obj.current_page != data.CurrentPage) {
					document.location.hash = "#st=" + data.CurrentPage;
					$.scrollTo("#top_pagebar");
				}
				obj.current_page = data.CurrentPage;
				$(".page_bar .throbber").hide();
				
				if (callback) callback();
				
				obj.LightboxIt();
			}
		});
	};
	
	this.AddComment = function() 
	{
		var text = $("#answer-form .text").val();
		if (text == "") {
			alert("Введите текст сообщения");
			return false;
		}
		$("#answer-form .send-button").attr("disabled", "disabled");
		$("#answer-form .text").attr("disabled", "disabled");
		$("#forum_send_info_img").show();
		$("#forum_send_info_text").html("Отправка сообщения");
		
		var plugins_data = [];
		for (var i=0;i<this.plugins_callbacks.length;i++) {
			plugins_data.push(this.plugins_callbacks[i]());
		}

		var obj = this;
		$.ajax({
			url: this.actions_path,
			type: "POST",
			dataType: "json",
			data: {
				action: "add_comment",
				hash: global_hash,
				text: text,
				place: this.place,
				object_id: this.object_id,
				anonymous: $("#answer-form .anonymous-checkbox").attr("checked") == "checked" ? "on" : "",
				nosmiles: $("#answer-form .smiles-checkbox").attr("checked") == "checked" ? "on" : "",
				plugins_data: plugins_data
			},
			success: function(data) {
				if (data.Message.Type == "info") {
					$("#forum_send_info_text").html("Обновление списка сообщений");
					obj.ShowResponseMessage(data.Message.Text, data.Message.Type);
					obj.GetPosts(0, true, function(){
						obj.EnableAnswerForm();						
						$("#answer-form .text").val("");
					});
					
					for (var i in obj.afterAddCallbacks) {
						obj.afterAddCallbacks[i]();
					}
					
				} else {
					obj.ShowResponseMessage(data.Message.Text, data.Message.Type);
					obj.EnableAnswerForm();
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				obj.ShowResponseMessage(textStatus, "error");				
				obj.EnableAnswerForm();
			}
		});
	};
	this.EnableAnswerForm = function()
	{
		$("#answer-form .text").removeAttr("disabled");
		$("#answer-form .send-button").removeAttr("disabled");
		
		$("#forum_send_info_img").hide();
		$("#forum_send_info_text").html("Добавить сообщение");
	};
	this.ShowResponseMessage = function(text, type)//type = error, info
	{
		$("#comments_info").html(text);
		$("#comments_info").show();
		$("#comments_info").removeClass().addClass(type + "_block");
	};
	
}

function SendPostByCtrlEnter(e)
{
	if (e.keyCode == 13 && e.ctrlKey)
		comments.AddComment();
}

	var Comments_ =
	{		
		actionsPath : "/common/actions/comments.php",
		
		Subscribe : function(commentsPlace, commentsPlaceNum)
		{
			var vars = "action=subscribe&place="+ commentsPlace +"&num=" + commentsPlaceNum;
			try {
				document.getElementById("ajax_send_info_img").style.display = "inline";
				sendXmlHttpRequest("post", this.actionsPath, vars, function(jsHttp) 
				{
					document.getElementById("ajax_send_info_img").style.display = "none";
					eval("var json = " + jsHttp.responseText);
					if (json.Message.Type == "info") 
					{
						if (json.StatusText == "subscribed")
							document.getElementById("comments_subscribe_link").innerHTML = "отписаться от комментариев";
						else if (json.StatusText = "ubsubscribed")
							document.getElementById("comments_subscribe_link").innerHTML = "подписаться на комментарии";
						else
							document.getElementById("comments_subscribe_link").innerHTML = "error";
					} else {
						alert(json.Message.Type + ":\n" + json.Message.Text);
					}
				});
			} catch (e) {
				alert(e);
			}
		},
		
		Rate : function(id, num)
		{
			var vars = "action=rate&commentid="+ id +"&num=" + num;
			try {
				sendXmlHttpRequest("post", this.actionsPath, vars, function(jsHttp) 
				{
					eval("var json = " + jsHttp.responseText);
					if (json.Message.Type == "info") 
					{						
						var str_rating = "";
						if (json["Rating"] > 0) 
							str_rating = "<span class=\"green_rating\">+"+json["Rating"]+"</span>";
						else if (json["Rating"] < 0) 
							str_rating = "<span class=\"red_rating\">"+json["Rating"]+"</span>";
						else
							str_rating = "<span class=\"gray_rating\">"+json["Rating"]+"</span>";
						document.getElementById("comment_rating_" + id).innerHTML = str_rating;
					} else {
						alert(json.Message.Type + ":\n" + json.Message.Text);
					}
				});
			} catch (e) {
				alert(e);
			}
		}
		
	}


	var curPage = 0;
	var commentsActions = "/common/actions/comments.php";
	
	function pasteQuote()
	{
		var answerTextField = document.getElementById("answerTextField");
		if (GK || OP)
			var quote = window.getSelection();
		else if (IE)
			var quote = document.selection.createRange().text;
		if (answerTextField.value == "") {
			answerTextField.value = "[quote]" + quote + "[/quote]\n";
		} else {
			InsertText("[quote]" + quote + "[/quote]\n", "");
		}
		answerTextField.focus();
	}
	
	function pasteNick(nick)
	{
		var answerTextField = document.getElementById("answerTextField");
		InsertText("[b]"+ nick +"[/b]\n", "");
		answerTextField.focus();
	}
	
	function InsertText(open, close)
	{
		var msgfield = document.getElementById("answerTextField");
		msgfield.focus();
		var ss = msgfield.selectionStart, st = msgfield.scrollTop, sh = msgfield.scrollHeight;
		if (!ss && document.selection) 
		{
			msgfield.caretPos = document.selection.createRange().duplicate();		
			var text = open;
			if (close != "")
				text += document.selection.createRange().text;
			text += close;
			msgfield.caretPos.text = text;
		}
		else if (ss || ss == '0') {
			var se = msgfield.selectionEnd;
			var text = msgfield.value.substring(0, ss) + open;
			if (close != '')
				text += msgfield.value.substring(ss, se);
			text += close + msgfield.value.substring(se, msgfield.value.length);
			msgfield.value = text;
			se = close.length ? se : ss;
			msgfield.selectionStart = se + open.length + close.length;
			msgfield.selectionEnd = se + open.length + close.length;
		}
		else
			msgfield.value += open + close;
		msgfield.scrollTop = st + msgfield.scrollHeight - sh;
		msgfield.focus();
	}
	
	//to functions.js
	function EnableSbmtBtn()
	{
		document.getElementById('sbmt_btn').disabled = false;
	}

	
