var Skillopedia_CommentsManager = new function()
{
    var QuoteForm = 0;

    this.switchCommentForm = function(open)
    {
        section = document.getElementById('comment-form_top');

        if ( open )
        {
            section.style.display = 'block';
        }
        else
        {
            section.style.display = 'none';
        }
    }

    this.openQuoteForm = function(id)
    {
    	if ( this.QuoteForm )
    	{
    	    past = document.getElementById('quote-form_'+this.QuoteForm);

    	    if ( past ) past.style.display = 'none';
    	}

    	document.getElementById('quote-form_'+id).style.display = 'block';

    	this.QuoteForm = id;
    }

    this.closeQuoteForm = function()
    {
    	if ( this.QuoteForm )
    	{
    		document.getElementById('quote-form_'+this.QuoteForm).style.display = 'none';
    	}

    	this.QuoteForm = null;
    }

    this.goToPage = function(thread, number, move)
    {
        JsHttpRequest.query(
            '_parts/comments/list.php',
            {
                'id': thread,
                'page': number
            },
            function(result, data)
            {
                document.getElementById('comments').innerHTML = data;

                location.href = '#'+move;
            },
            true
        );
    }

    this.quote = function(id, thread, level)
    {
        this.closeQuoteForm();

        block = document.getElementById('response-'+id);
        data  = document.getElementById('quote-'+id).value;

        if ( !data )
        {
            alert('Похоже комментарий пуст :\\');

            return;
        }
        
        document.getElementById('comment-reply-'+id).style.display = 'none';
        document.getElementById('comment-reply-wait-'+id).style.display = 'block';


        JsHttpRequest.query(
            '_parts/comments/post.php',
            {
                'id': thread,
                'parent': id,
                'value': data
            },
            function(result, data)
            {
                if ( !result )
                {
                    document.getElementById('comment-reply-'+id).style.display = 'block';
                    document.getElementById('comment-reply-wait-'+id).style.display = 'none';
                    
                    alert('Комментарий почему-то не добавился :(');
                    return;
                }

                JsHttpRequest.query(
                    '_parts/comments/entry.php',
                    {
                        'id': result,
                        'thread': thread,
                        'level': level+1,
                        'new': 1
                    },
                    function(foo, data)
                    {
                        block.style.display = 'block';
                        block.innerHTML += data;

                        location.href = '#response-'+id;
                        
                        document.getElementById('comment-reply-'+id).style.display = 'block';
                        document.getElementById('comment-reply-wait-'+id).style.display = 'none';
                    },
                    false
                );
            },
            true
        );
    }

    this.comment = function(thread, section)
    {
        if ( !User.ID )
        {
            alert('Пожалуйста, зарегистрируйтесь или войдите, чтобы оставлять комментарии');

            return;
        }

        data = document.getElementById(section+'-value');

        if ( !data )
        {
            return;
        }

        data = data.value;

        if ( !data )
        {
            alert('Похоже комментарий пуст :\\');

            return;
        }
        
        document.getElementById('comment-reply-common').style.display = 'none';
        document.getElementById('comment-reply-wait-common').style.display = 'block';

        JsHttpRequest.query(
            '_parts/comments/post.php',
            {
                'id': thread,
                'value': data
            },
            function(result, data)
            {
                if ( !result )
                {
                    alert('Комментарий почему-то не добавился :(');
                    return;
                }

                Skillopedia_CommentsManager.goToPage(thread, 'last', 'comment-'+result);
            },
            true
        );

    }

    this.vote = function(id, vote)
    {
        if ( !User.ID )
        {
            alert('Пожалуйста, зарегистрируйтесь или войдите, чтобы оценивать комментарии');

            return;
        }

        JsHttpRequest.query(
            '_parts/comments/vote.php',
            {
                'id': id,
                'vote': vote
            },
            function(result, data)
            {
                if (result === false)
                {
                    alert(data);

                    return;
                }
                
                comment = document.getElementById('comment-rating-'+id);

                comment.innerHTML = result;
                
                if ( result < 0 )
                {
                    comment.style.color = 'red';
                }
                else if ( result == 0 )
                {
                    comment.style.color = 'gray';
                }
                else comment.style.color = '#33cc00';
            },
            true
        );
    }

    this.highlight = function(field, message)
    {
        field.style.border = '1px solid red';
        field.focus();
   
        if ( message )
        {
            alert(message);
        }
    }

    this.normalize = function(field)
    {
        field.style.border = '1px solid gray';
    }

	this.register = function(id)
	{
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
		var email = document.getElementById('reg-email-'+id);
		var password1 = document.getElementById('reg-password1-'+id);
		var password2 = document.getElementById('reg-password2-'+id);
		var agree     = document.getElementById('reg-agree-'+id);
		
		this.normalize(email);
		this.normalize(password1);
		this.normalize(password2);
		this.normalize(agree);
		
		if ( !agree.checked )
		{
		    this.highlight(agree, 'Необходимо подтвердить соглашение пользователя.');
		    
		    return;
		}
	
		if ( !email.value || !filter.test(email.value) )
		{
			this.highlight(email, 'Неверный адрес email.');
			
			return;
		}
		if ( !password1.value || password1.value != password2.value )
		{
			this.highlight(password2);
			this.highlight(password1, 'Пароли не совпадают.');
			
			return;
		}
	
	    JsHttpRequest.query(
	        Skillopedia_RPC+'/user_exists.php',
	        {
	            'login': email.value
	        },
	        function(result, data)
	        {
	            if ( result )
	            {
	            	Skillopedia_Register.highlight(email, 'Человек с этим email уже зарегистрирован. <a href="password.php">Выслать пароль?</a>');
	            }
	            else
	            {
            	    /*JsHttpRequest.query(
            	        Skillopedia_RPC+'/captcha.php',
            	        {
            	            'code': captcha.value
            	        },
            	        function(result, data)
            	        {
            	            if ( !result )
            	            {
            	            	Skillopedia_Register.highlight(captcha, 'Неверный код');
            	            }
            	            else
            	            {*/
            	                document.getElementById('comment-register-'+id).submit();
            	            /*}
            	        },
            	        true
            	    );*/
	            }
	        },
	        true
	    );
	}
}

