 var speed = 200;
$(document).ready(function(){
    // Высота верхнего ряда формы, нужня для того, чтобы сворачивать обёртку при скрытии формы
    FORM_HEIGHT = $('#comment-form-body').height()-1;
    $('#comment-form-body').css({                
        'height': FORM_HEIGHT,
        'overflow': 'hidden'
    });
    // После того, как мы зафиксировали размеры обёртки, можно показать скрытые ряды таблицы
    $('#comment-form-body tr:hidden').css({'display': ''});
    
    if ($.browser.msie) {
         speed = 0;
     }
     $("#s-comment-form").click(function(){
         if ($(this).hasClass("current-switch")){
             $(this).removeClass("current-switch");
             $('#comment-form-body').animate({'height': FORM_HEIGHT}, 'fast');
             $('#fake-strut').css({'height': '1em'});

         } else {
             $(this).addClass("current-switch");
             $('#comment-form-body').animate({'height': $('#comment-form-body').attr('scrollHeight')}, 'fast');
             $('#fake-strut').css({'height': '0em'});
         }
     });
    $("#comment-submit").click(submitComment);
    $('#comment-form-solid').append('<input type="hidden" name="ajax" value="comment-form" />');
    $('#comment-form-solid').attr('target', 'add-comment');

    $('#add-comment').load(function(){
        data = $('#add-comment').contents().find('html').html();  
        $("#comment-submit").click(submitComment);
        $("label").css("color", "");
        $("form textarea").attr("style", "");
        if (/--ERROR--/.test(data))
        {
            $("#comment-submit").attr({disabled: ""});
            $("#comment-submit").val('Добавить');
            $("#comment-submit").removeClass('onair');
            if (/--ERROR-AUTHOR--/.test(data))
            {
                $("#author-name").css('width', $("#author-name").width());
                $("#author-name").effect('shake', {times: 2, distance: 10}, 70);
            }
            if (/--ERROR-EMAIL--/.test(data))
            {
                $("#author-email").css('width', $("#author-email").width());
                $("#author-email").effect('shake', {times: 2, distance: 10}, 70);
            }
            if (/--ERROR-PASSWORD--/.test(data))
            {
                $('#comment-password').slideDown();
                $("#password").css('width', $("#password").width());
                $("#password").effect('shake', {times: 2, distance: 10}, 70);
            }
            if (/--ERROR-COMMENT--/.test(data))
            {
                $("#comment-text").effect('shake', {times: 2, distance: 10}, 70); 
            }
            
        }
        else
        {
            $("#comment-submit").attr({disabled: ""});
            $("#comment-submit").val('Добавить');
            $("#comment-submit").removeClass('onair');
            $("#s-comment-form").removeClass("current-switch");
            div = $('<div style="display: none;"></div>');
            div.html(data);
            $('#new-comment-here').append(div);
            div.slideDown(speed);
            $("#comment-form").slideUp(speed);
            $('#comment-text').val('');
            $('a.MultiFile-remove').click();
            $('#fake-strut').css({'height': '1em'});
            $('#comment-form-body').animate({'height': FORM_HEIGHT}, 'fast');
        }
    });
    setInterval(checkForm, 500);
    /**
    * Если есть куки с именем и адресом почты, то подставляем их 
    */
    if ($.cookie('b_name') && (name = $.cookie('b_name').replace('+', ' ')))
    {
        $('#author-name').val(name);
    }
    if ($.cookie('b_name') && (email = $.cookie('b_email').replace('+', ' ')))
    {
        $('#author-email').val(email);
    }
});

function submitComment()
{
    $("#comment-submit").unbind('click', submitComment);
    $("#comment-submit").val('Добавляется...');
    $("#comment-submit").addClass('onair');
    $("#comment-form-solid").submit();
    $("#comment-submit").attr({disabled: "disabled"});
    return false;
}

function checkForm()
{
    user = false;
    try {
        user = DESIGN_USER;    
    } catch (e){}
    pass = true;
    if (!$("#author-name").val()) pass = false;
    if (!$("#author-email").val()) pass = false;
    if (!$("#comment-text").val()) pass = false;
    if ( !user && /artgorbunov\.ru/i.test($("#author-email").val()) && $('#comment-password').is(':hidden'))
    {
        $('#comment-password').slideDown();    
        $('#comment-password-label').slideDown();    
    }
    else if (!/artgorbunov\.ru/i.test($("#author-email").val()))  
    {
        $('#comment-password').slideUp();     
        $('#comment-password-label').slideUp();    
    }   
    if (!$("#comment-submit").hasClass('onair'))
    {
        if (pass) 
        {
            $("#comment-submit").removeAttr('disabled');    
        }
        else
        {
            $("#comment-submit").attr({disabled: "disabled"});
        }
    }
}


