/**
 * CDB v0.20: Copyright (c) 2008 Christopher Barlow. All rights reserved. (http://www.cbarlow.com/)
 * 
 * Variables
 * -------------------
 *     $.chrome
 *     $.ff
 *     $.ie
 *     $.opera
 *     $.safari
 * 
 * Array Prototypes
 * -------------------
 *     [].avg		average value of array
 *     [].contains	see PHP's in_array()
 *     [].each		array foreach using [].each(function(i){});
 *     [].find		get index of first occurence of x
 *     [].findLast	get index of last occurence of x
 *     [].first		first element of array
 *     [].has		see .contains()
 *     [].implode	see .join()
 *     [].indexOf	see .find()
 *     [].keys		get all keys of array
 *     [].last		last element of array
 *     [].max		highest value in array
 *     [].min		lowest value in array[].sum
 *     [].random	get random element from array
 *     [].randomize	get randomized copy of array
 *     [].reverse	reverse the order of elements without preserving keys
 *     [].sum		sum of array elements
 *     [].unique	get array of unique elements
 * 
 * String Prototypes
 * -------------------
 *     ''.contains	substring exists within string
 *     ''.explode	see .split()
 *     ''.has		see .contains()
 *     ''.lower		see .toLowerCase()
 *     ''.ltrim		PHP's ltrim()
 *     ''.repeat	repeat string x times
 *     ''.reverse	reverse string
 *     ''.rtrim		PHP's rtrim()
 *     ''.trim		PHP's trim()
 *     ''.ucfirst	PHP's ucfirst()
 *     ''.upper		see .toUpperCase()                          
 */

//
// VARIABLES
//
window.chrome	= (navigator.userAgent.indexOf('Chrome/') >= 0);
window.ff		= (navigator.userAgent.indexOf('Firefox/') >= 0);
window.ie		= (navigator.userAgent.indexOf('MSIE') >= 0);
window.ie6		= (navigator.userAgent.indexOf('MSIE 6') >= 0 || navigator.userAgent.indexOf('MSIE 5') >= 0);
window.opera	= (window.opera != undefined ? window.opera : null);
window.safari	= (navigator.userAgent.indexOf('Safari/') >= 0);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//
// ARRAY PROTOTYPES
//
Array.prototype.avg = function() {
	return this.sum() / this.length;
}
Array.prototype.contains = function(x) {
	for (i = 0; i < this.length; i++) {
		if (this[i] == x) {
			return true;
		}
	}
	return false;
}
Array.prototype.each = function(fn) {
	var expando = (new Date()).getTime();
	for (i = 0; i < this.length; i++) {
		obj = [this[i]];
		key = 'cdb' + expando;
		obj[key] = fn;
		obj[key](i);
	}
}
Array.prototype.find = function(val) {
	for (i = 0; i < this.length; i++) {
		if (this[i] == val) {
			return i;
		}
	}
	return false;
}
Array.prototype.findLast = function(val) {
	for (i = this.length - 1; i >= 0; i--) {
		if (this[i] == val) {
			return i;
		}
	}
	return false;
}
Array.prototype.first = function() {
	for (i in this) {
		return this[i];
	}
}
Array.prototype.has = Array.prototype.contains;
Array.prototype.implode = Array.prototype.join;
Array.prototype.indexOf = Array.prototype.find;
Array.prototype.keys = function() {
	keys = [];
	for (i in this) {
		keys.push(i);
	}
	return keys;
}
Array.prototype.last = function() {
	for (i in this) {}
	return this[i];
}
Array.prototype.max = function() {
	var result;
	for (i = 0; i < this.length; i++) {
		if (!result || this[i] > result) {
			result = this[i];
		}
	}
	return result;
}
Array.prototype.min = function() {
	var result;
	for (i = 0; i < this.length; i++) {
		if (!result || this[i] < result) {
			result = this[i];
		}
	}
	return result;
}
Array.prototype.random = function() {
	i = Math.round(Math.random() * (this.length - 1));
	return this[i];
}
Array.prototype.randomize = function() {
	a = this;
	a.sort(function(){
		return 0.5 - Math.random();
	});
	return a;
}
//Array.prototype.reverse = function() {
//	var result;
//	for (i = this.length-1; i >= 0; i--) {
//		result.push(this[i]);
//	}
//	return result;
//}
Array.prototype.sum = function() {
	result = 0;
	for (i = 0; i < this.length; i++) {
		result += this[i];
	}
	return result;
}
Array.prototype.unique = function() {
	a = this;
	keys = {};
	unique = [];
	a.each(function(i){
		keys[a[i]] = i;
	});
	for (i in keys) {
		unique.push(a[keys[i]]);
	}
	unique.sort();
	return unique;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//
// STRING PROTOTYPES
//
String.prototype.contains = function(str)
{
	return (this.indexOf(str) >= 0);
}
String.prototype.explode = String.prototype.split;
String.prototype.has = String.prototype.contains;
String.prototype.lower = function()
{
	return this.toLowerCase();
}
String.prototype.ltrim = function()
{
	return this.replace(/^[\n\r\s\t]+/, '');
}
String.prototype.repeat = function(count)
{
	if (!count || count < 2) {
		count = 1;
	}
	for (i = 1, result = ''; i <= count; i++) {
		result += this;
	}
	return result;
}
String.prototype.reverse = function()
{
	s = '';
	for (i = this.length - 1; i >= 0; i--) {
		s += this.charAt(i);
	}
	return s;
}
String.prototype.rtrim = function()
{
	return this.replace(/[\n\r\s\t]+$/, '');
}
String.prototype.trim = function()
{
	return this.ltrim().rtrim();
}
String.prototype.ucfirst = function()
{
	return this.charAt(0).toUpperCase() + this.substr(1);
}
String.prototype.upper = function()
{
	return this.toUpperCase();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//
// NUMBER PROTOTYPES
//
//Number.prototype.format = function() {
//	i = r = '';
//	n = this + '';
//	n.reverse().replace(/\d\d\d/g, function(a, b) {
//		r += a + ',';
//		i = b;
//	});
//	r += n.reverse().substr(i + 3);
//	return r.reverse();
//}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//
// OBJECT PROTOTYPES
//
/*Object.prototype.each = function(fn) {
	var expando = (new Date()).getTime();
	for (i in this) {
		if (i === 'each') {
			continue;
		}
		obj = [this[i]];
		key = 'cdb' + expando;
		obj[key] = fn;
		obj[key](i);
	}
}*/

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//
// Other Functions
//

//var byId = window.byId = function(id) {
var byId = window.byId = function(id)
{
	return (typeof id == 'string') ? document.getElementById(id) : id;
}

var objX = function(obj)
{
	if (typeof obj == 'string') obj = byId(obj);
	return (obj.offsetParent && obj.tagName != 'BODY') ?
		(obj.offsetLeft + objX(obj.offsetParent)) : 0;
}
var objY = function(obj) {
	if (typeof obj == 'string') obj = byId(obj);
	return (obj.offsetParent && obj.tagName != 'BODY') ?
		(obj.offsetTop + objY(obj.offsetParent)) : 0;
}
