/*-----------------------------------------------------
 * Utility Functions for Javascript and jQuery
 *  * Pushingkarma MAIN
 *  * Useful Functions
 *  * Javascript Prototypes
 *-----------------------------------------------------*/

var DOMAIN = "http://"+document.domain;
var DEFAULT_GRAVATAR = DOMAIN+"/staticfiles/img/gravatar/pompom.png";

/*-----------------------------------------------------
 *  Pushingkarma MAIN
 *-----------------------------------------------------*/
$(function() {
    $('#login').toggleLogin();
    $('a.slide').slideLink();
    $('a.headerlink').slideLink();
    $('#content pre').prettyPrint();
    $('#content p code').prettyPrint();
    $('#content li code').prettyPrint();
});


/*-----------------------------------------------------
 *  Useful Functions
 *-----------------------------------------------------*/

// Given an email address, return it's gravatar URL.
getGravatarUrl = function(email, size) {
    if (size == undefined) { size = 30; }
    var gravatarUrl = "http://www.gravatar.com/avatar.php";
    gravatarUrl += "?size=" + size;
    gravatarUrl += "&gravatar_id=" + $.md5(email);
    gravatarUrl += "&default=" + DEFAULT_GRAVATAR;
    return gravatarUrl;
};

// Calls prettyprint on all the specified elements
$.fn.prettyPrint = function() {
    if ($(this).length) {
        $(this).addClass('prettyprint');
        prettyPrint();
    }
};

// Applies an animation to inpage anchor links
$.fn.slideLink = function() {
    $(this).click(function() {
        var anchor = $(this).attr('href');
        $('html,body').animate({scrollTop: $(anchor).offset().top}, function() {
            return true;
        });
    });
};

// Open and close the Login form
$.fn.toggleLogin = function() {
    $('#login').click(function() {
        if ($('#loginform').is(':visible')) {
            $('#loginform').slideUp('fast', function() {
                $('#navigation').removeClass('login-visible');
            });
        } else {
            $('#navigation').addClass('login-visible');
            $('#loginform').slideDown('fast');
        }
    });
}


/*-----------------------------------------------------
 *  Javascript Prototypes
 *-----------------------------------------------------*/
Array.prototype.indexof  = function(i) { for (j in this) { if (this[j] == i) { return j; }} return -1; }
Array.prototype.contains = function(search) { for (var i=0; i<this.length; i++) { if (this[i] == search) return true; } return false; }
String.prototype.trim       = function()  { return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "")); }
String.prototype.startsWith = function(s) { return (this.match("^"+s)==s); }
String.prototype.endsWith   = function(s) { return (this.match(s+"$")==s); }
String.prototype.escapeHTML = function()  { return(this.replace(/&/g,'&amp;').replace(/>/g,'&gt;').replace(/</g,'&lt;').replace(/"/g,'&quot;')); }

