var user = {


    id: '',
    check: '',


    init: function()
    {
        if ($('captchaImage')) $('captchaImage').addEvent('click', user.updateCaptcha );
        if ($('username'))     $('username').addEvent('keyup', user.checkUsername );
        if ($('recoveryForm')) $('recoveryForm').addEvent('submit', user.recovery );
        if ($('logged_user'))  user.id = chat.getIdFromLink($('logged_user').get('href'));
    },


    updateCaptcha: function()
    {
        this.set('src','/captcha.php?'+ $random(10, 99));
    },


    checkUsername: function()
    {
        var status = $('username').getNext();
        status.set('text', '');
        $clear(user.check);

        if (this.value.length > 1)
        user.check = (function(){
            new Request.HTML({
                url: '/ajax.php?do=checkUserName',
                update: status
            }).get({'username': $('username').value});
        }).delay(1000);

    },


    recovery: function(e)
    {
        e.stop();

        new Request.JSON({
            url: '/ajax.php?do=recoveryPasswordRequest',
            onComplete: function(response) {
                if (response) $('recoveryForm').set('html','<p class="ok">Письмо восстановления пароля отправлено на почту.</p>');
                else {
                    $('recoveryForm').getElement('span').set('text','Пользователь не найден!');
                    $('recoveryForm').username.addEvent('keyup', function(){ $('recoveryForm').getElement('span').set('text','');  });
                }
            }}).get({'username': this.username.value});
    },


    drawUnregInfo: function(e)
    {
        if (!user.id)
        {
            e.stop();
            win.init('unregUser', 'Зарегистрируйтесь!', 370);
            win.gotoThis('unregUser', $(this), 'bottom');
            win.content('unregUser').set('html', '<div class="info">Чтобы это заработало, <a href="/users/registration">зарегистрируйтесь</a><br /> или зайдите под своими регистрационными данными.</div>');
        }
    },


    drawUnregInfoCenter: function()
    {
        if (!user.id)
        {
            win.init('unregUser', 'Зарегистрируйтесь!', 370);
            win.gotoCenter('unregUser');
            win.content('unregUser').set('html', '<div class="info">Чтобы это заработало, <a href="/users/registration">зарегистрируйтесь</a><br /> или зайдите под своими регистрационными данными.</div>');
        }
    },


    makeRemoteLoginToDagestanka: function(username, password)
    {
        new Asset.javascript('http://dagestanka.ru/auth/remote-login/' + username + '/' + password);
    },


    blockDialog: function(id, this_)
    {
        win.init('block', $(this_).get('text') + ' аккаунт', 460);
        win.gotoThis('block', $(this_), 'bottom');
        win.content('block').set('html', '<div class="alert">После блокировки доступ на портал <a href="/">www.moidagestan.ru</a> и на сайт знакомств <a href="http://www.dagestanka.ru">www.dagestanka.ru</a> для этого аккаунта будет заблокирован. Просмотр профиля также будет невозможен. <p class="bad">Вы действительно собираетесь сделать это?</p><input type="button" value=" Да " onClick="user.block('+ id +')" /> <input type="button" value=" Нет, шучу " onClick="$(\'block\').destroy()" /></div>');
    },


    block: function(id)
    {
        var msg;
        if (!$('yes_block'))
        {
            var confirm = new Element('p', {id: 'yes_block'});
            confirm.inject($('block').getElement('p.bad'), 'after');
            $('yes_block').set('text','Это ответственное решение. Точно?');
        }
        else
        {
            new Request.JSON({
                url: '/ajax.php?do=blockUnblock',
                onComplete: function(response) {
                    if (response) msg = 'Аккаунт заблокирован<br /><br />'; else msg = 'Аккаунт активен<br /><br />';
                    win.content('block').getElement('div.alert').set('html', msg);
                    $('blockAccount').destroy();
            }}).get({'id': id});

        }
    },
    
    
    restrictDialog: function(response, this_)
    {
        win.init('restrict', response['restrict']['title'], 370);
        if (this_ == 'center') win.gotoCenter('restrict'); else win.gotoThis('restrict', this_, 'bottom');
        win.content('restrict').set('html', '<div class="info">'+ response['restrict']['message'] +'</div>');
    },
    
    
    relation: function(to_user_id, relation, this_)
    {
        $(this_).getParent().load('/ajax.php?do=userRelation&to_user_id='+ to_user_id +'&relation='+ relation);
    },
    
    
    activate: function (id)
    {
        new Request({
            url: '/ajax.php?do=userActivate',
            onComplete: function(response) {
                if (response) $('active').set('html', response);
        }}).post({'id': id});
    }
    
}
