// File: /scripts/default.js
//
// Contains all site-wide JS helper functions.

// Simple function to reduce clutter later on
function g(id) { return document.getElementById(id); }

jQuery.fn.switchFade = function () {
    if (this.css("display") == "none") {
        this.fadeIn('slow');
    }
    else {
        this.fadeOut('slow');
    }
}

// Post the password via an AJAX request
function logIn() {
    $.ajax({
        type: "POST",
        url: "/ajax_listener.php",
        data: "action=login&password=" + $('#password').val(),
        success: function(responseText) {
            if (responseText && responseText != "--login failed--") {
                eval(responseText);
            }
            else {
                error("Error: The password you entered was invalid. Please try again.");
            }
        }
    });
}

function logOut() {
    $.ajax({
        type: "POST",
        url: "/ajax_listener.php",
        data: "action=logout",
        success: function(responseText) {
            if (responseText != "--logout failed--") {
                eval(responseText);
            }
        }
    });
}


// Fade in and out a simple error message
function error(message) {
    $('#error').html(message).fadeIn('slow');
    window.setTimeout("$('#error').fadeOut('slow');", 3000);
}

// Set the highlighted navigation item--quick and dirty.
function setNav(key) {
    $('#' + key + '_item').addClass("current");
}

// Prevent the "selected" dotted box from showing when you click a navigation link:
$(document).ready( function() {
    // this just blurs all A elements within the navigation as soon as you click.
    // it should not affect keyboard-based navigation for accessibility.
    $('#navitems a').click(function(){$(this).blur()});
});