/* Copyright 2007 by Oliver Steele.
 * This work is licensed under the MIT License.
 * http://www.opensource.org/licenses/mit-license.php
 */


/*
 * Collections
 */

// From the Mozilla site:
Array.prototype.each = Array.prototype.each || Array.prototype.forEach || function(fn, thisObject) {
    var len = this.length;
    for (var i = 0 ; i < len; i++)
        if (typeof this[i] != 'undefined')
            fn.call(thisObject, this[i], i, this);
}

// From the Mozilla site:
Array.prototype.filter = Array.prototype.filter || function(fn, thisObject) {
    var len = this.length,
        results = [];
    for (var i = 0 ; i < len; i++)
        if (fn.call(thisObject, this[i], i, this))
            results.push(this[i]);
    return results;
}

Array.prototype.pluck = function() {
    return this[this.length-1];
}

Array.prototype.pluck = function(key) {
    var len = this.length,
        result = new Array(this.length);
    for (var i = 0; i < len; i++)
        result[i] = this[i][key];
    return result;
}

Array.prototype.shuffle = function() {
    // this works because |Co(Math.random)| >> |Dom(this)|
    return (this
            .map(function(item) {return [Math.random(), item]})
            .sort(function(a,b) {return a[0]-b[0]})
            .pluck(1));
}


/*
 * Functions
 */

function pluck(key) {
    return function(object) {
        return object[key];
    }
}

// Unlike the standard versions, this one ignores additional arguments.
Function.prototype.bind = Function.prototype.bind || function(thisObject) {
    var fn = this;
    return function() {
        fn.apply(thisObject, arguments);
    }
}


/*
 * Cookies
 */

// Uses jquery.cookie.js
jQuery.jsonCookie = function(name, value, options) {
    if (typeof value == 'undefined') {
        value = jQuery.cookie(name, value, options);
        return value && JSON.parse(value);
    } else {
        value = JSON.stringify(value);
        jQuery.cookie(name, value, options);
    }
}


/*
 * Options
 */

var kDefaultOptions = {
    feeds: {
        news:true,
        events:true,
        births:false,
        deaths:false
    },
    interval:10
};

