var RAF = (function($) {
    return {
        logout: function() {
            var url=HTTP_SERVER_BASE+'ajax/logout.php';
            $.post(url,RAF.Response);
        },
        loadEffects: function(form){
            $("#"+form+" input, #"+form+" textarea").each(function (type) {
                if(!$(this).val().blank())
                    $(this).prev("label").fadeTo('fast',0);

                $(this).focus(function () {
                    if($(this).val().blank())
                        $(this).prev("label").fadeTo('fast',0.45);
                });

                $(this).keypress(function () {
                    $(this).prev("label").fadeTo('fast',0);
                });

                $(this).blur(function () {
                    if($(this).val().blank()){
                        $(this).prev("label").fadeTo('fast',1);
                    }
                });
            });
        },
        login: function() {
            $('#login-form').submit(function(){
                var user=$('#username');
                var pass=$('#password');
                if(user.val().blank()){
                    alert('Please enter your username!');
                }
                else if(pass.val().blank()){
                    alert('Please enter your password!');
                } else {
                    var url=HTTP_SERVER_BASE+'ajax/login.php';
                    var pars='&username='+encodeURIComponent(user.val());
                    pars+='&password='+encodeURIComponent(pass.val());
                    $.post(url,pars,RAF.Response);
                }
            });
        },
        forgot: function(){
            $('#forgot-form').submit(function(){
                var email=$('#email');
                if(email.val().blank()){
                    alert('Please enter your email!');
                }
                else if(!email.val().validEmail()){
                    alert('Please enter a valid email address!');
                } else {
                    var url=HTTP_SERVER_BASE+'ajax/forgot.php';
                    var pars='&email='+encodeURIComponent(email.val());
                    $.post(url,pars,RAF.Response);
                }
            });
        },
        forgotShow: function(){
            $('#login-form').hide();
            $('#forgot-form').show();
            $('.response').html();
        },
        logout: function() {
            var url=HTTP_SERVER_BASE+'ajax/logout.php';
            $.post(url,RAF.Response);
        },
        showPopup: function(){
            //scroll to the top
            $('html, body').animate({
                scrollTop:0
            });
            //show the login poppup
            $("#pop").show(0, function(){
                $("#pop").fadeTo('slow', 1);
            });
            RAF.showOverlay();
        },
        loadLogin: function(){
            $('#pop').load(HTTP_SERVER_BASE+'ajax/login_html.php',function(){
                RAF.login();
                RAF.forgot();
                $("#login-form input,#forgot-form input").each(function (type) {
                    $(this).focus(function () {
                        if($(this).val().blank())
                            $(this).prev("label").fadeTo('fast',0.45);
                    });

                    $(this).keypress(function () {
                        $(this).prev("label").fadeTo('fast',0);
                    });

                    $(this).blur(function () {
                        if($(this).val().blank()){
                            $(this).prev("label").fadeTo('fast',1);
                        }
                    });
                });
                RAF.showPopup();
            });
        },
        showLogin: function() {
            $('#login-form').show();
            $('#forgot-form').hide();
        },
        hidePopup: function(action) {
            //0 to close when you click on the overlay and 1 at the call of this function
            switch(action){
                case 0:
                    $('#bgcover').click(function(){
                        $("#pop").fadeTo('slow', 0, function() {
                            $('#pop').css("display","none");
                        });
                        RAF.hideOverlay();
                    });
                    break;
                case 1:
                    $("#pop").fadeTo('slow', 0, function() {
                        $('#pop').css("display","none");
                    });
                    RAF.hideOverlay();
                    break;
            }
        },
        showOverlay: function() {
            //get height of the body and set it to the overlay
            var body=$("body").height();
            $("#bgcover").fadeTo('slow', 0.5);
            $("#bgcover").css('height', body);

        },
        hideOverlay: function() {
            $("#bgcover").fadeTo('slow', 0, function() {
                $('#bgcover').css("display","none");
            });
        },
        throwError:function (message){
            $('#error_msg').show('slow', function callback(){
                $('#error_msg').html(message);
            });
            $('#error_msg').mouseover(function(){
                $(this).hide('slow');
                $(this).html('');
            });
        },
        Response:function(data){
            try{
                var jsonObject = JSON.parse(data);
                var action         = jsonObject.action;
                var status         = jsonObject.status;
                switch(action){
                    case "send_email":
                        if(status.blank()){
                            RAF.hidePopup(1);
                        }else{
                            alert(status);
                        }
                        break;
                    case "login":
                        if(status==""){
                            $('html, body').animate({
                                scrollTop:0
                            });
                            location.reload(true);
                        }else{
                            alert(status);
                        }
                        break;
                    case "logout":
                        if(status==""){
                            $('html, body').animate({
                                scrollTop:0
                            });
                            window.location.href=HTTP_SERVER_BASE;
                        }else{
                            window.location.href=HTTP_SERVER_BASE;
                        }
                        break;
                    case "forgot":
                        if(status==""){
                            $('.response').html('We sent you a new password to your email address!');
                            $('#forgot-form')[0].reset();
                        }else{
                            alert(status);
                        }
                        break;
                }
            }catch(err){
                //alert(err);
                window.location.href=HTTP_SERVER_BASE;
            }
        },
        initMain: function() {
            $(document).ready(function(){
                RAF.hidePopup(0);
                try{
                    $("#pics_rez").load(HTTP_SERVER_BASE+'ajax/tellmezipcode.php');
                }catch(e){
                //error
                }
            });
        }

    };
// Pass in jQuery.
})(jQuery);
RAF.initMain();


