$(document).ready(function() {

    // Create modal login dialog
    $('#login a').overlay({
        mask: {
            color: '#fff',
            loadSpeed: 200,
            opacity: 0.9
        },

        closeOnClick: false
    });

    // Add ajax-event for the login evaluation on submit
    $('#login-form button').click(function() {
        $.ajax({
            type: "POST",
            url: "/?s=ajax",
            data: {
                'mode': 'handleLogin',
                'p_userid' : $('#login-form input[name=p_userid]').val(),
                'p_password' : $('#login-form input[name=p_password]').val()
            },
            async: false,
            success: function(result) {
                if (result == 'true') {
                    // close the overlay
                    $('#login-form').fadeOut('slow');
                    location.reload();
                } else {
                    // Wrong login, throw error message
                    $('#login-form-error-msg').hide().empty().append(result).fadeIn('slow');
                }
            }
        });
    });

    // Add ajax-event for logout
    $('#logout a').click(function() {
        $.ajax({
            type: "POST",
            url: "/?s=ajax",
            data: {
                'mode': 'handleLogin',
                'logout' : 'true'
            },
            async: false,
            success: function(result) {
                if (result == 'true') location.reload();
                else alert(result);
            }
        });
    });
});
