var timeoutID = null;
var unsubscribe_text = "<div class='buttons'><input type='button' value='Да' class='button-allow' /><input type='button' value='Нет' class='button-regret' /></div>";
var subscribe_text = "Уведомления об обновлениях в данном элементе будут приходить на ваш личный ящик сообщений" + unsubscribe_text;
var unsubscribe_text = "Уведомления об обновлениях в данном элементе не будут приходить на ваш личный ящик сообщений" + unsubscribe_text;


$(document).ready(function(){
	$(".articles-comments a").mouseover(function(){
		var offset = $(this).offset();
		var name = $(this).attr('name');

		if(typeof timeoutID == "number") {
			clearTimeout(timeoutID);
		}

		$(this).parent().css('background-color', '#fff');
		
		$("#article-window").fadeOut('normal', function(){
			var obj = $(this);
			
			$.get("/requests/articles/getCommentInfo.php", { id: name.substr(4, name.length) }, function(data){
				$("#article-window-content").html(data);
				obj.css( {top:offset.top-28+'px', left:offset.left-447+'px'} ).fadeIn('normal');
			});
		});
	}).mouseout(function(){
		$(this).parent().css('background-color', 'transparent');
		timeoutID = setTimeout("hideArticleCommentsBlock()", 1000);
	});

	$("#article-window").hover(
		function(){
			if(typeof timeoutID == "number") {
				clearTimeout(timeoutID);
			}
		},
		function(){
			timeoutID = setTimeout('hideArticleCommentsBlock()', 1000);
		}
	);

	$(".add-comment").click(function(){
		var offset = $(this).offset();
		var id = $(this).attr('id');
		var aid = $(this).attr('href');

		id = id.substr(7, id.length);
		aid = aid.substr(1, aid.length);

		$.get("/requests/articles/getCommentsForm.php", { aid: aid, id: id }, function(data){
			$("#article-add-window-content").html(data);
			$("#article-add-window").css( {top:offset.top-25+'px', left:offset.left-460+'px'} ).fadeIn('normal');
			
			$("#acomment").keyup(function(){
				if(this.value.length > 500){
					this.value = this.value.substring(0, 500);
				}

				$("#aleft").text(500 - this.value.length);
			});

			$("#form-nick").focus(function(){
				this.value = $.trim(this.value);
				if(this.value == 'Ваше имя'){
					this.value = '';
				}
			}).blur(function(){
				this.value = $.trim(this.value);
				if(this.value == ''){
					this.value = 'Ваше имя';
				}
			});
		});
	});

	$(".articles-blocks tr").hover(
		function(){
			$(this).css('background-color', '#eaeaea');
			$(".comment-gradient", this).attr('src', '/res/grey_gradient.png');
		},
		function(){
			$(this).css('background-color', '#ffffff');
			$(".comment-gradient", this).attr('src', '/res/white_gradient.png');
		}
	);

	$(".subscribe").click(function(){
		var lnk = $(this).attr("href");

		openGWindow(subscribe_text, 0);
		
		$(".button-allow").click(function(){
			location.href = lnk;
		});

		$(".button-regret").click(function(){
			closeGWindow();
		});

		return false;
	});

	$(".unsubscribe").click(function(){
		var lnk = $(this).attr("href");

		openGWindow(unsubscribe_text, 0);

		$(".button-allow").click(function(){
			location.href = lnk;
		});

		$(".button-regret").click(function(){
			closeGWindow();
		});
		
		return false;
	});

	$("#bform-nick").focus(function(){
		this.value = $.trim(this.value);
		if(this.value == 'Ваше имя'){
			this.value = '';
		}
	}).blur(function(){
		this.value = $.trim(this.value);
		if(this.value == ''){
			this.value = 'Ваше имя';
		}
	});
});

var hideArticleCommentsBlock = function(){
	$("#article-window").fadeOut("normal");
}

var hideArticleAddCommentsBlock = function(){
	$("#article-add-window").fadeOut("normal");

	return false;
}

var sendArticleComment = function(){
	var obj = $("#article-add-window form").get(0);
	var params = {};

	with(obj)
	{
		$.post("/requests/articles/sendCommentsInfo.php", { article_id: obj.article_id.value, block_id: obj.block_id.value, user_id: obj.user_id.value, nick: obj.nick.value, comment: comment.value, code: obj.code.value }, function(data){
			if('fail' == data){
				$(".error-console").addClass('error').html('Заполните правильно все поля');
			} else {
				$(".error-console").removeClass('error').html('');
				hideArticleAddCommentsBlock();
				location.reload(true);
			}
		});
	}
}

var sendBOTArticleComment = function(){
	var obj = $("#articlebot-add-window form").get(0);
	var params = {};

	with(obj)
	{
		var editor_obj = document.getElementById('comment_rEdit').contentDocument || document.frames['comment_rEdit'].document;
		var editor_content = editor_obj.body.innerHTML;

		$.post("/requests/articles/sendCommentsInfo.php", { article_id: obj.article_id.value, block_id: obj.block_id.value, user_id: obj.user_id.value, nick: obj.nick.value, comment: editor_content, code: obj.code.value }, function(data){
			if('fail' == data){
				$(".error-console").addClass('error').html('Заполните правильно все поля');
			} else {
				$(".error-console").removeClass('error').html('');
				hideArticleAddCommentsBlock();
				location.reload(true);
			}
		});
	}
}