jQuery(document).ready(function() {
    jQuery('.inscription-action').click(function(){
        top.location = '/fr/inscription';
    });
        
    jQuery('.logoff-action').click(function(){
        var bnt = jQuery(this);
        disable(bnt,true);
        
        bnt.parent().find('.ajax-loader').show();
        jQuery.post('/web/connexion/singout/', {}, function (data) {
            if (data.success) {
                top.location.reload(true);
            }
        }, 'json');
    });
        
    jQuery('.cancel-action').click(function(){
        jQuery('#lostpassword-box').hide();
        jQuery('#connexion-box').show();
    });
        
    jQuery('.lost-action').click(function(){
        jQuery('#lostpassword-box').show();
        jQuery('#connexion-box').hide();
    });
        
    jQuery('.send-action').click(function(){
        var box = jQuery('#lostpassword-box');
            
        var bnt = jQuery(this);
        var email = box.find('#email').val();
        
        box.find('.alert').hide();
        
        if( email != ''){
                
            box.find('.ajax-loader').show();
            disable(bnt,true);
            jQuery.post('/web/connexion/lost/', {
                email: email
            }, function (data) {
                box.find('.ajax-loader').hide();
                disable(bnt,false);
                if (data.success) {
                    box.find('.info').html(data.message);
                    box.find('.info').fadeIn();
                    window.setTimeout(function () {
                        jQuery('.cancel-action').click();
                    },5000);
                } else {
                    box.find('.ajax-loader').hide();
                    box.find('.alert').html(data.message);
                    box.find('.alert').fadeIn('fast');
                }
            }, 'json');
        }
    });
        
    jQuery('.login-action').click(function(){
        var box = jQuery('#connexion-box');
            
        box.find('.alert').hide();
        var user = box.find('#user').val();
        var password = box.find('#password').val();
            
        if( user != '' && password != ''){
                
            var bnt = jQuery(this);
            disable(bnt,true);
            box.find('.ajax-loader').show();
                
            jQuery.post('/web/connexion/singin/', {
                user: user,
                password: password
            }, function (data) {
                if (data.success) {
                    top.location.reload(true);
                } else {
                    disable(bnt,false);
                    box.find('.ajax-loader').hide();
                    box.find('.alert').fadeIn('fast');
                }
            }, 'json');
        }
                        
    });
        
    function disable(item , value ){
        if( value ){
            item.attr('disabled', 'disabled');
            item.fadeTo('fast', 0.5);
        }else{
            item.removeAttr('disabled');
            item.fadeTo('fast', 1);
        }
    }
});
