function set_page(id, page)
{
    var myHTMLRequest = new Request({
        url: '/?comments=1&id='+id+'&comments_page='+page,
        method: 'get',
        onRequest: function()
        {            
            $('comments_container').setStyle('opacity', 0.4);
        },
        onSuccess: function(html_resp)
        {
            $('comments_container').set('html', html_resp);
            $$('a.comments_page').each(function(item,index, array){                
                item.removeProperty('href');
            });
            $$('div.commentator').each(function(item,index, array){
                item.set('html', '<a class="commentator2quote">'+array[index].innerHTML+'</a>');
            });
            $$('a.commentator2quote').each(function(item,index, array){
                item.addEvent('click', function(){
                    $('comment').value = 'Ответ '+item.innerHTML+' ('+item.getParent().getNext('div').innerHTML+'):\n'+$('comment').value;
                });
            });
            $('comments_container').setStyle('opacity', 1);
        }
    }).send();
}

window.addEvent('domready', function(){
    var formFx = new Fx.Slide('form_slider', {link: 'cancel'}).hide();
    //alert($$('a.comments_page')[0].innerHTML);
    $$('a.comments_page').each(function(item,index, array){
        item.removeProperty('href');
    });
    
    $$('div.commentator').each(function(item,index, array){
        item.set('html', '<a class="commentator2quote">'+array[index].innerHTML+'</a>');
    });
    
    $$('a.commentator2quote').each(function(item,index, array){
        item.addEvent('click', function(){
            $('comment').value = 'Ответ '+item.innerHTML+' ('+item.getParent().getNext('div').innerHTML+'):\n'+$('comment').value;
        });
    });
    
    $('submit_button').set('html', '<button id="comment_submit">Добавить комментарий</button>');
    $('direct').destroy();
    $('add_comment').removeProperties('action', 'method');
    
    $('comment_submit').addEvent('click', function(e){
        e.stop();
        //alert(location.href.substr(location.href.));
        var validateNick = $('nick').value;
        var validateTxt = $('comment').value;
        if(validateNick.test('[><\\/\\\\;]') || validateTxt.test('[><\\/\\\\;]')) alert('Поле "Имя" и/или текст комментария содержат недопустимые символы.\nУдалите, пожалуйста, следующие символы: <, >, ;, \\ и /');
        else if(validateNick.length < 1 || validateTxt.length < 4) alert('Поле "Имя" не заполнено и/или текст комментария слишком короткий');
        else
        {
            var myRequest = new Request({
                url: '/?comments=1',
                link: 'ignore',
                onRequest: function()
                {
                    $('input_fields').setStyle('opacity', 0.4);
                },
                onFailure: function()
                {
                    var img_rnd = $('captimg').src.indexOf('?');
                    var rnd_new = Math.random().toString();
                    rnd_new = rnd_new.substr(rnd_new.indexOf('.')+1,10);
                    $('captimg').src = $('captimg').src.substr(0, img_rnd+1)+rnd_new;
                    $('captanswr').value = '';
                    $('input_fields').setStyle('opacity', 1);
                    // creating, then hiding, then assigning text, then toggling in, then sliding out after delay...
                    // don't try to repeat this at home! ;)
                    var myFx1 = new Fx.Slide('submit_answr', {mode: 'horizontal'}).hide();
                    $('submit_answr').set('html', 'Произошла ошибка! Попробуйте еще раз.');
                    myFx1.toggle().chain(function(){this.slideOut.delay(3000, this)});
                },
                onSuccess: function()
                {
                    set_page($('news_id').value, 0);
                    $('add_comment').reset();
                    var img_rnd = $('captimg').src.indexOf('?');
                    var rnd_new = Math.random().toString();
                    rnd_new = rnd_new.substr(rnd_new.indexOf('.')+1,10);
                    $('captimg').src = $('captimg').src.substr(0, img_rnd+1)+rnd_new;
                    $('captanswr').value = '';
                    $('input_fields').setStyle('opacity', 1);
                    // creating, then hiding, then assigning text, then toggling in, then sliding out after delay...
                    // don't try to repeat this at home! ;)
                    var myFx1 = new Fx.Slide('submit_answr', {mode: 'horizontal'});
                    myFx1.hide();
                    $('submit_answr').set('html', 'Комментарий добавлен!');
                    myFx1.toggle().chain(function(){this.slideOut.delay(3000, this)});                    
                }
            }).post($('add_comment'));
        }
    });
    
    $('toggle_form').addEvent('click', function(e)
     {
        e.stop();
        if (!formFx.open && $('toggle_form').innerHTML != "<b>Свернуть форму</b>")
        {
           $('toggle_form').set('html', "<b>Свернуть форму</b>");
           formFx.toggle();
        }
        else
        {
           $('toggle_form').set('html', "<b>Обсудить новость</b>");
           formFx.toggle();
        }        
    });
});