$(document).ready(function(){
	var inside = $('body').children();
	$('.eggs').click(function(){
		$(inside).addClass('class1');
		$('body').addClass($(this).attr('name'));
		return false;
	});
	
	$('body').click(function(){
		$('body').removeClass($(this).attr('name'));
		$(inside).removeClass('class1');
	});
})

jQuery.preloadImages = function() {
	jQuery.each (arguments,function (e) {
		jQuery("<img>").attr("src", this);
	});
}

function commentArea()
{
	$('#comment-area').toggle();
}

function commentSubmit(id_article)
{
	//var oFCKeditor = FCKeditorAPI.GetInstance($('#textarea').attr('name')) ;
	//user_text = oFCKeditor.GetXHTML( true );
	
	user_text = $('#imvesstext').val();
	user_title = $('#user_title').val();
	user_email = $('#user_email').val();
	choosen_user_pic = $('#choosen-user-pic').attr("src");
	
	
	$.post("/articles-post-comment", 
	{
		ajax: '1',
		id_article: id_article,
		text: user_text,
		user_title: user_title,
		user_email: user_email,
		choosen_user_pic: choosen_user_pic
	 },
		function(data){
			data = data.split('|||');
			status = data[0];
			com_page = data[1];
			data = data[2];
			if(status == 0)
			{
				$('#errors_out').html(data);
				$('#errors_out').show();
			}
			else if(status == 1)
			{
				getComments(id_article, com_page);
				
				//$('#no-comment').hide();
				
				//$('#comments-list').append(data);
				//$('#comment-area').toggle();
				//$('#imvesstext').val('');
				//oFCKeditor.SetData('');
			}
     	});
}

function getComments(id_article, page)
{
	$.post("/get-comments", 
	{
		ajax: '1',
		id_article: id_article,
		page: page
	 },
		function(data){
			
			$('#comment_here').html(data);
     	});
}

function rateArticle(id_article, vote)
{
	$.get("/rate-article", 
	{
		ajax: '1',
		id: id_article,
		vote: vote
	 },
		function(data){
			data = data.split('|');
			if(data[0] == 0)
			{
				alert(data[1]);
				return false;
			}
			if(data[0] == 1)
			{
				$('.like-yes').html(data[1]);
				alert(data[2]);
				return false;
			}
     	});
}

function replayToComment(id)
{
	comment = $('#comment_'+id).html();
	//text = "<p class=\"comment_reply\">(.*)</p>"
	comment = comment.replace(/<p class(.*)p>/i, "");
	alert(comment);
	$('#imvesstext').val('{цитата}'+comment+'{/цитата}\n');
	$('#imvesstext').focus();
}

function ComplaintComment(id)
{
	$.get("/complaint-comment", 
	{
		ajax: '1',
		id: id
	 },
		function(data){
			$('#complaint_'+id).html(data);
     	});
}

function ShowLoginForm()
{
	$('#form_auth_block').toggle();
}


function sendPoll()
{
	poll = $("#poll input:checked").val();
	if(poll == undefined)
	{
		alert('Вы уже голосовали!');
		return false;
	}
	$.post("/poll-send", { id: poll},
	function(data){
		PollGetResult(0);
		alert(data);
	});
}

function PollGetResult(id)
{
	$.post("/poll-get-result", { id: id},
	function(data){
	  $('#poll').html(data);
	});
}

function showChatBlock()
{
	$('#mini-chat-say').toggle();
}

function sendMessageToChat()
{
	author = $('#chat_author').val();
	text = $('#chat_textarea').val();
    if(text == '')
    {
        alert('Введите текст сообщения!');
        return false;
    }
	$.post("/chat-send", { text: text, author: author},
	function(data){
        n_data = data;
        data = data.split('||');
        if(data[0] == 0)
        {
            alert(data[1]);
        }
        else
        {
            $('#chat-wrap').html(n_data);
        }
	});
}

function sendMessageToChatArchive()
{
	author = $('#chat_author_2').val();
	text = $('#chat_textarea_2').val();
	$.post("/chat-send", { text: text, author: author},
	function(data){
		window.location.href = '/chat-all';
	});
}

function chooseUserPic()
{
	$('#user-pic-choose').toggle();
}

function showLinkFriendForm(id)
{
	$.post("/show-link-friend-form", { id: id},
	function(data){
		$('.link_friend').after(data);
		$('#link_textarea').val(location.href + " \r");
		$('#link_textarea').focus();
	});
}

function sendLinkFriend(id)
{
	alert(id);return false;
	author = $('#link_author').val();
	to = $('#link_to').val();
	text = $('#link_textarea').val();
	$.post("/show-link-friend-form?send=1", { id: id, text: text, author: author, to: to},
	function(data){
		data = data.split('|');
		if(data[0] == 1)
		{
			$('#link-say').remove();
		}	
		alert(data[1]);
	});
}

function sendSubscribe()
{
	email = $('#idsubscrb').val();
	$.post("/send-subscribe", { email: email},
	function(data){
		data = data.split('|');
		alert(data[1]);
		if(data[0] == 1)
		{
			window.location.href = location.href;
		}
	});
}

function deleteComment(id)
{
	$.post("/delete-comment", { id: id},
	function(data){
		if(data == 1)
		{
			$('#onecom_'+id).remove();
		}
	});
}

function openContestWindow(id)
{
	if(id == '')
	{
		return false;
	}
	$('#opencontestwindow').remove();
	$('body').append('<div id="opencontestwindow"></div>');
	$.post("/contest-one", { id: id},
	function(data){
		$('#opencontestwindow').html(data);
		$('#opencontestwindow').show();
	});
}
