$(document).ready(function() {
        $("div.rightside #usa_birth_date").mask("99/99/9999");
        $("div.rightside #cit_postal_code").mask("99999");
        $('div.rightside #usa_mobile_phone').mask('9999999999');
    
        $('div.sex input[type=radio]').uniform();
        
        // Top User Login overlay
        $('#user-panel_login').click(function()
        {
            var click_callback = function(e)
            { 
                if( !$(e.target).is('#user-panel_login-popup') && !$(e.target).parents('#user-panel_login-popup').length )
                {
                    $(document).unbind('click', click_callback);
                    $('#user-panel_login-popup').fadeOut(); 
                }
            };
                            
            if( $('#user-panel_login-popup').css('display') == 'none' )
            {
                $(document).bind('click', click_callback);
                $('#user-panel_login-popup').fadeIn();
            }
            else
            {
                $(document).unbind('click', click_callback);
                $('#user-panel_login-popup').fadeOut();
            }
       
            return false;
        });
        
        $('#user-panel_login-popup button[type="submit"],#form_login-popup button[type="submit"]').live('click', function()
        {
            var form = $(this).parents('form:first');
        
            // Post form
            $.post('/user/login/pop-up', form.serialize(), function(data)
            {
                // Remove old errors messages
                form.find('div.errors').remove();
                form.find('.error_login-popup').html('');

                // Error occured ?
                if( data.error )
                {
                    if( typeof(data.messages) != 'undefined' )
                    {
                        for(var target in data.messages)
                        {
                            var msg = new Array;
                            for( var k in data.messages[target] )
                                msg.push(data.messages[target][k]);
						
                            form.find('[name="'+target+'"]').after('<div class="errors"><ul class="errors"><li>'+msg.join('</li><li>')+'</li></ul></div>');
                        }
                    }

                    if( typeof(data.message) != 'undefined' )
                    {
                        form.find('.error_login-popup').html(data.message);
                    }
                }
                // On success, redirect to URL
                else
                {
                    window.location.href = data.redirect;
                }
            }, 'json');

            return false;
        });
    
        FB.init({
            appId:'128316417257238', 
            cookie:true, 
            status:true, 
            xfbml:true
        });

        // Callback on login button
        $("body").delegate("a.compte-facebook", "click", function(e){
            var root = $(this).parents('#facebox').length > 0 ? '#facebox' : ''; // on s'adapte en cas d'ouverture en popup
            FB.login(function(response) {
                if (response.authResponse) {
                    FB.api('/me', function(response) {
                        if (typeof(response.id) != 'undefined') $('#usa_facebookid', root).val(response.id);
                        if (typeof(response.first_name) != 'undefined') $('#usa_first_name', root).val(response.first_name);
                        if (typeof(response.last_name) != 'undefined') $('#usa_last_name', root).val(response.last_name);
                        if (typeof(response.email) != 'undefined') $('#usa_email', root).val(response.email);
                    });
                }
            }, {
                scope: 'email'
            });
            e.preventDefault(); 
        });    

        /**
     * Signin in 3 steps
     *  - Create user disabled
     *  - Send SMS code       
     *  - Activate account    
     */
        $('form.inscription button[type="button"]').live('click', function()
        {
            var form = $(this).parents('form:first');
        
            // Post form
            $.post('/user/inscription/signin', form.serialize(), function(data)
            {
                // Remove old errors messages
                form.find('div.errors').remove();
                form.find('.error_signin').html('');

                // Error occured ?
                if( data.error )
                {
                    if( typeof(data.messages) != 'undefined' )
                    {
                        for(var target in data.messages)
                        {
                            var msg = new Array;
                            for( var k in data.messages[target] )
                                msg.push(data.messages[target][k]);

                            form.find('[name="'+target+'"]').after('<div class="errors"><ul class="errors"><li>'+msg.join('</li><li>')+'</li></ul></div>');
                        }
                    }

                    if( typeof(data.message) != 'undefined' )
                    {
                        form.find('.error_signin').html(data.message);
                    }
                }
                // On success, show popin to confirm mobile phone
                else
                {
                    $.facebox(data.content);
                }
            }, 'json');

            return false;
        });

        // SMS code signin step
        $('#submit_send-code').live('click', function()
        {
            // Post form
            $.post('/user/inscription/send-code', $('#form_send-code').serialize(), function(data)
            {
                // Remove old errors messages
                $('#form_send-code').find('div.errors').remove();
                $('#error_send-code').html('');

                // Error occured ?
                if( data.error )
                {
                    if( typeof(data.messages) != 'undefined' )
                    {
                        for(var target in data.messages)
                        {
                            var msg = new Array;
                            for(var k in data.messages[target] )
                                msg.push(data.messages[target][k]);

                            $('#form_send-code #'+target).after('<div class="errors"><ul class="errors"><li>'+msg.join('</li><li>')+'</li></ul></div>');
                        }
                    }
                    if( typeof(data.message) != 'undefined' )
                    {
                        $('#error_send-code').html(data.message);
                    }
                }
                // On success, show popin to input activation code
                else
                {
                    var popin = $('#facebox');
                    if( popin.length )
                        popin.find('div.content').html(data.content);
                    else
                        $.facebox(data.content);
                }
            }, 'json');

            return false;
        });

        // Activate step signin
        $('#submit_activate-me').live('click', function()
        {
            // Post form
            $.post('/user/inscription/activate-me', $('#form_activate-me').serialize(), function(data)
            {
                // Remove old errors messages
                $('#form_activate-me').find('div.errors').remove();
                $('#error_activate-me').html('');

                // Error occured ?
                if( data.error )
                {
                    if( typeof(data.messages) != 'undefined' )
                    {
                        for(var target in data.messages)
                        {
                            var msg = new Array;
                            for(var k in data.messages[target])
                                msg.push(data.messages[target][k]);

                            $('#'+target).after('<div class="errors"><ul class="errors"><li>'+msg.join('</li><li>')+'</li></ul></div>');
                        }
                    }
                    if( typeof(data.message) != 'undefined' )
                    {
                        $('#error_activate-me').html(data.message);
                    }
                }
                // On success, reload page
                else
                {
                    // Redirect ?
                    if( typeof(data.redirect) != 'undefined' )
                        window.location.href = data.redirect;
                    else
                        window.location.reload();
                }
            }, 'json');

            return false;
        });


        // User Inscription Mode
        $('div.list-mode').live('click', function()
        {
            var value = $(this).attr('rel');
            $('#uim_mode').val(value);

            if( value == 3 )
            {
                $('#code_vip').show();
                $('#free_desc').hide();
                $(this).addClass('cadre-vip-select');
                $('div.cadre-free-select').removeClass('cadre-free-select');
            }
            if( value == 1)
            {
                $('#code_vip').hide();
                $('#free_desc').show(); 
                
                $(this).addClass('cadre-free-select');
                $('div.cadre-vip-select').removeClass('cadre-vip-select');
            }
        
            $('#partner_desc').hide(); 
            $('.cadre-offre').removeClass('cadre-offre-select');
        });

        $('div.cadre-offre').live('click', function()
        { 
            $('#uim_mode').val($(this).attr('rel')); 
            $(this).addClass('cadre-offre-select');
            $('#code_vip,#free_desc').hide(); 
            $('div.cadre-free-select').removeClass('cadre-free-select');
            $('div.cadre-vip-select').removeClass('cadre-vip-select');
            $('#partner_desc').show(); 
        });
    
        // Display description pass code on blur
        $('#uim_code').live('change', function()
        {
            $.post('/user/inscription/accessdescription', {
                'code':$(this).val()
            }, function(data)

            {
                    if( typeof(data) != 'undefined' && data )
                        $('#uim_code_desc').html(data.acc_description);
                    else
                        $('#uim_code_desc').html('Code incorrect');
            
                },'json');
        
        });
    
        $("#forgot").live('click',function() 
        {
            $.get("/user/login/forgot/",
                function(data){
                    $.facebox.settings.opacity = 0.9;
                    $.facebox(data);
                    $("#facebox").css("top", ( $(window).height() - $("#facebox").height() ) / 2+$(window).scrollTop() + "px");
                    $("#facebox").css("left", ( $(window).width() - $("#facebox").width() ) / 2+$(window).scrollLeft() + "px");
                });
        });
    
    
        $('label[for]').live('click', function()
        {
            $(this).parents('form').find('[name="'+ $(this).attr('for') +'"]').focus();
            return false;
        });
    
    });
