$(function() {
    $('#loginPanel').simple_box({
        overlay : $('#login_overlay')
    });
    
    $('a.login').click(function() {
        $('#loginPanel').simple_box('show').find('a[target="login"]').click();

        return false;
    });

    if(!navigator.userAgent.match(/iP(hone|od)/i)) {
        $(document).scroll(function() {
            $('#loginPanel').css({
                left    : Math.round($('body').width() / 2 - $('#loginPanel').width() / 2) + 'px',
                top     : ($(window).height() / 2 - $('#loginPanel').height() / 2) + $(window).scrollTop()
            });
        }).trigger('scroll');
    }
    
    $('#login').submit(function() {
        var post_data = {
            phone_number    : $('[name="phone_number"]', this).val(),
            pin             : $('[name="pin"]', this).val(),
            remember_me     : $('[name="remember_me"]', this).attr('checked') ? '1' : '0'
        };
        
        $.post('/phones/authenticate', post_data, function(response) {
            if(response.error) {
                $('#loginPanel .invalid_login').fancy_show();
                
                return setTimeout(function() {
                    $('#loginPanel .invalid_login').fancy_hide();
                }, 3000);
            }
            
            if(response.security_question) {
                $('#login').hide();
                $('#security_question').template(response).show();
            } else {
                if(typeof doc_click != 'undefined')
                    doc_click = true;

                if(typeof forced_login == 'boolean' && forced_login)
                    window.location.reload();
                else
                    window.location.assign(response.redirect);
            }
        }, 'json');
        
        return false;
    });
    
    $('#security_question').submit(function() {
        var post_data = {
            phone_number    : $('#login [name="phone_number"]').val(),
            pin             : $('#login [name="pin"]').val(),
            remember_me     : $('#login [name="remember_me"]').attr('checked') ? '1' : '0',
            security_answer : $('[name="security_answer"]', this).val()
        };
                
        $.post('/phones/authenticate', post_data, function(response) {
            if(response.security_question || response.error) {
                $('#loginPanel .invalid_login').fancy_show();
                
                return setTimeout(function() {
                    $('#loginPanel .invalid_login').fancy_hide();
                }, 3000);
            }

            if(typeof doc_click != 'undefined')
                doc_click = true;

            if(typeof forced_login == 'boolean' && forced_login)
                window.location.reload();
            else
                window.location.assign(response.redirect);
        }, 'json');
        
        return false;
    });
    
    $('#remind_pin').submit(function() {
        $.post('/user/remind-pin', {
            phone_number    : $('[name="phone_number"]', this).val()
        }, function(response) {
            $('#forgotPassword a[target="login"]').click();
            
            $('#loginPanel .pin_sent').fancy_show();
            
            return setTimeout(function() {
                $('#loginPanel .pin_sent').fancy_hide();
            }, 3000);
        }, 'json');
        
        return false;
    });
    
    $('#forgotPassword a').click(function() {
/*         $('#loginBody form').toggle(); */
        $('#' + $(this).attr('target')).show().siblings('form').hide();
        
        $(this).hide().siblings().show();
        
        return false;
    });
    
    if(window.location.hash == '#login_required')
        $('#loginPanel').simple_box('show');
});

