$(document).ready(function(){
    //форма
    var form = $('div#add_comment_wrapper').html();

    //закрыть форму
    $('a.comment_close').live('click', function(){
        $(this).parent().parent().parent().remove();
        $('div.comment_reply > a').removeAttr('rel');
        $('div#add_comment_wrapper').html(form);
    });

    //форма для ответа
    $('div.comment_reply > a').click(function(){
        if ($(this).attr('rel') != 'on') {
            $('div.comment_reply > a').removeAttr('rel');
            $('div.add_comment').remove();
            $(this).attr('rel', 'on');
            var m = $(this).parent().parent().css('margin-left');
            var px = (parseInt(m)+10).toString()+'px';
            $(this).parent().parent().after(form);
            var name = $(this).parent().parent().children('.comment_header').children('.comment_autor').html();
            $(this).parent().parent().next().children().children('.head').children('span').html('Ответить на комментарий '+name)
            $(this).parent().parent().next().css('margin-left', px);
            $(this).parent().parent().next().children().children('input:[name=parent]').val($(this).attr('id'));
        }
    });

    // Dialog
    $('#dialog').dialog({
        autoOpen: false,
        buttons: {
            "Ok": function() {
                $(this).dialog("close");
            }
        }
    });

    function CheckFaled(mes) {
        $('#dialog').html('<span style="color:red;">'+mes+'</span>');
        $('#dialog').dialog('open');
        return false;
    }

    //отправка формы
    $('div.add_comment > form > button').live('click', function(){
        //проверка формы
        var obj = new Object();
        obj.name = $(this).prevAll('div.input').children('input:[name=name]').val();
        if (obj.name.length < 1) {
            return CheckFaled('Введите имя!');
        }
        obj.mail = $(this).prevAll('div.input').children('input:[name=mail]').val();
        if (obj.mail.length < 5) {
            return CheckFaled('Введите e-mail!');
        }
        obj.site = $(this).prevAll('div.input').children('input:[name=site]').val();
        obj.parent = $(this).prevAll('input:[name=parent]').val();
        obj.post = $(this).prevAll('input:[name=post]').val();
        obj.key = eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('$.1(0.2+0.3);',4,4,'obj|demoriz|name|mail'.split('|'),0,{}))
        obj.mess = $(this).prevAll('textarea:[name=mess]').val();
        if (obj.mess.length < 1) {
            return CheckFaled('Сообщение забыли :)!');
        }
        $.ajax({
            url: "/commadd/",
            dataType: "html",
            data: obj,
            type: "GET",
            success: function(ans) {
                if (ans == "ok") {
                    var mes = '<span style="color:green;">Ваше сообщение отправлено на премодерацию.</span>';
                } else {
                    var mes = '<span style="color:red;">Произошла ошибка, и мне ушло уведомление об этом. Возможно стоит попробовать попозже, когда я справлюсь с проблемой.</span>';
                }
                $('#dialog').html(mes);
                $('#dialog').bind('dialogclose', function(event, ui) {
                    window.location.reload();
                });
                $('#dialog').dialog('open');
            }
        });
        return false;
    });

    //поиск
    $('form#find').submit(function(){
        var find = $(this).children().children('input:[name=search]').val();
        if (find.length > 0){
            window.location="/find-"+find+"/"
        }
        return false;
    });
    $("#txt").focusin(function() {
        $(this).val("");
    });
    $("#txt").focusout(function() {
        $(this).val("Поиск ...");
    });
});
