/** -- Functions -------------------------
 * 
 * -- DOM functions
 * elm (x)                    
 * 
 * 
 * -- String Methods          
 * htmlents()                 
 * 
 * 
 * -- Array Methods           
 * max()                      
 * min()                      
 * indexOf()                  
 * filter(fun, [thisp])       
 * 
 * 
 * 
**/

function elm (x) {
	return document.getElementById(x);
}

// not sure this is the /right/ right way to extend JS, but that's for another time to find out.
String.prototype.htmlents = function() {
	var trans = {'&':'&amp;', '"':'&quot;', '\'':'&#039;', '<':'&lt;', '>':'&gt;'};
	var arr = this.split('');
	for (var i=0;i<arr.length;i++) {
		if (trans.hasOwnProperty(arr[i])) {
			arr[i] = trans[arr[i]];
		}
	}
	return arr.join('');
};


// based on Resig's, http://ejohn.org/blog/fast-javascript-maxmin/
// .max()
Array.prototype.max = function(){
	var narray=[];
	for (i=0;i<this.length;i++) {
		if (! isNaN(this[i])) narray.push(this[i]);
	}
	return Math.max.apply(Math, narray);
};

// .min()
Array.prototype.min = function(){
	var narray=[];
	for (i=0;i<this.length;i++) {
		if (! isNaN(this[i])) narray.push(this[i]);
	}
	return Math.min.apply(Math, narray);
};

// .indexOf(obj)
if (! Array.indexOf) {
	Array.prototype.indexOf = function(obj) {
		for (var i=0; i<this.length; i++) {
			if (this[i]==obj) {
				return i;
			}
		}
		return -1;
	};
};

// hacked from mozilla reference example,
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter
if (! Array.prototype.filter) {
	Array.prototype.filter = function(fun /*, thisp*/) {
		var len = this.length >>> 0;
		if (typeof fun != "function") throw new TypeError();
		var res = [];
		var thisp = arguments[1];
		for (var i = 0; i < len; i++) {
			if (i in this) {
				var val = this[i]; // in case fun mutates this
				if (fun.call(thisp, val, i, this)) res.push(val);
			}
		}
		return res;
	};
}

