(function($) {
    $(function() {

        // START: LightBox Code
        function lightbox_close(){
            $('#lightbox-sign, .lightbox-blende').fadeOut(250, function () {
                $('#lightbox-sign').css({
                    top: '0px'
                })
            });
            $('body, html').removeClass('lightbox-body');
        }

        $('.lbox').click(function(){
            //clear all forms
            $('#lightbox-sign input').removeClass('error');
            $('#lightbox-sign div.error').remove();
            $('#lightbox-sign input[type = text]').val('');
            $('#lightbox-sign input[type = password]').val('');
            $('#lightbox-sign input[type = checkbox]').removeAttr('checked');

            $('body, html').addClass('lightbox-body');
            $('#lightbox-sign, .lightbox-blende').fadeIn(150);
            $('#lightbox-sign').animate({
                top: '50px'
            },350)
            
            $('.lightbox-blende, #ls-close').click(lightbox_close);
        });
        // END: LightBox Code

        
        //Login - register form
        //general functions
        function show_error_dial(){
            alert('Sorry, there is technical issue throw your request, please try later');
        }
        
        function before_send(formId){
            $('#'+formId+' img.process').show();
            $('#'+formId+' input[type = text]').attr('readonly', 'readonly');

            //hide errors
            $('#'+formId+' input').removeClass('error');
            $('#'+formId+' div.error').remove();
        }

        function success(response, formId, redirect_url){
            try{
                if(response == null){
                    throw 'Responce is undefined';
                }
                else{
                    if(response.result == null){
                        throw 'Result is undefined';
                    }
                    else{
                        if(response.result == 'failure'){
                            if(response.errors == null){
                                throw 'Errors list is undefined';
                            }
                            else{
                                $.each(response.errors, function(){
                                    if(this.message){
                                        $('#'+formId+' input[name = ' + this.name +']').addClass('error');
                                        $('<div class="error">'+this.message+'</div>').insertAfter('#'+formId+' input[name = ' + this.name +']');
                                    }
                                });
                            }
                        }
                        else{
                            if(response.result == 'success'){
                                lightbox_close();

                                if(redirect_url){
                                    $(location).attr('href', redirect_url)
                                }
                            }
                            else{
                                if(response.result == 'technical_issue'){
                                    throw 'Technical issue';
                                }
                                else{
                                    throw 'Undefined result answer';
                                }
                            }
                        }
                    }
                }
            }
            catch(exc){
                //alert(exc);
                show_error_dial();
            }
        }

        function complete(formId){
            $('#'+formId+' img.process').hide();
            $('#'+formId+' input[type = text]').removeAttr('readonly', 'readonly');
        }
        
        //register user
        var registerFormOptions = {};

        registerFormOptions.dataType = 'json';

        registerFormOptions.beforeSend = function(){
            $('#register_form input[name = register]').hide();
            before_send('register_form');
        };

        registerFormOptions.success = function(response){
            success(response, 'register_form', '/register');
        };
        registerFormOptions.error = show_error_dial;

        registerFormOptions.complete = function(){
            $('#register_form input[name = register]').show();
            complete('register_form');
        };

        $('#register_form').ajaxForm(registerFormOptions);

        //login user
        var loginFormOptions = {};

        loginFormOptions.dataType = 'json';

        loginFormOptions.beforeSend = function(){
            $('#login_form input[name = login]').hide();
            before_send('login_form');
        };

        loginFormOptions.success = function(response){
            success(response, 'login_form', $(location).attr('href'));
        };

        loginFormOptions.error = show_error_dial;

        loginFormOptions.complete = function(){
            $('#login_form input[name = login]').show();
            complete('login_form');
        };

        $('#login_form').ajaxForm(loginFormOptions);
    });
})(jQuery);
