/*
	Copyright (c) 2009, Scott & White Hospital All Rights Reserved.
 */

dojo.provide("sw._base.array");

/**
 * isArray
 * @return a boolean value telling whether the first argument is an Array object. 
 */
isArray = function() {
	if (typeof arguments[0] == 'object') {  
		var criterion = arguments[0].constructor.toString().match(/array/i); 
		return (criterion != null);  
	}
	return false;
}
/*	Array prototypes *****************************************************************************/
/**
 * inArray()
 * Determine if a value is in an array. 
 * Returns true or false
 */
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
/**
 * The indexOf array method will search an array until it matches your search critera. 
 * It will then return the index where the item was found. It will match only one 
 * item and the match must be exact. Returns -1 if not found.
 * This is prototyped to allow Internet Explorer and older browsers to use this array method.
 */
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(elt /*, from*/) {
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		from = (from < 0)
		       ? Math.ceil(from)
		       : Math.floor(from);
		if (from < 0) from += len;
		for (; from < len; from++) {
		    if (from in this && this[from] === elt) return from;
		}
		return -1;
	};
}
if (!Array.prototype.lastIndexOf) {
	Array.prototype.lastIndexOf = function(elt /*, from*/) {
		var len = this.length;
		var from = Number(arguments[1]);
		if (isNaN(from)) {
			from = len - 1;
		}else{
			from = (from < 0)
					? Math.ceil(from)
					: Math.floor(from);
			if(from < 0) from += len;
			else if(from >= len) from = len - 1;
		}
		for (; from > -1; from--) {
			if (from in this && this[from] === elt) return from;
		}
		return -1;
	};
}
/**
 * forEach array method
 */
if (!Array.prototype.forEach) {
	Array.prototype.forEach = function(func /*, thisp*/) {
		var len = this.length;
		if (typeof func != "function") throw new TypeError();
		var thisp = arguments[1];
		for (var i = 0; i < len; i++) {
			if (i in this) { 
				func.call(thisp, this[i], i, this);
			}
		}
	};
}