// Array Extensions  v1.0
// http://www.dithered.com/javascript/array/index.html
// code by Chris Nott (chris@dithered.com)


// Join two arrays
function _Array_concat(secondArray) {
	var firstArray = this.copy();
	for (var i = 0; i < secondArray.length; i++) {
		firstArray[firstArray.length] = secondArray[i];
	}
	return firstArray;
}


// Copy an array
function _Array_copy() {
	var copy = new Array();
	for (var i = 0; i < this.length; i++) {
		copy[i] = this[i];
	}
	return copy;
}


// Remove the last element of an array and return it
function _Array_pop() {
	var lastItem = this[this.length - 1];
	this.length--;
	return lastItem;
}


// Add an element to the end of an array
function _Array_push() {
	var currentLength = this.length;
	for (var i = 0; i < arguments.length; i++) {
		this[currentLength + i] = arguments[i];
	}
	return arguments[arguments.length - 1];
}


// Remove the first element of an array and return it
function _Array_shift() {
	var firstItem = this[0];
	for (var i = 0; i < this.length - 1; i++) {
		this[i] = this[i + 1];
	}
	this.length--;
	return firstItem;
}


// Copy several elements of an array and return them
function _Array_slice(start, end) {
	var temp;
	
	if (end == null || end == '') end = this.length;
	
	// negative arguments measure from the end of the array
	else if (end < 0) end = this.length + end;
	if (start < 0) start = this.length + start;
	
	// swap limits if they are backwards
	if (end < start) {
		temp  = end;
		end   = start;
		start = temp;
	}
	
	// copy elements from array to a new array and return the new array
	var newArray = new Array();
	for (var i = 0; i < end - start; i++) {
		newArray[i] = this[start + i];
	}
	return newArray;
}


// Splice out and / or replace several elements of an array and return any deleted elements
function _Array_splice(start, deleteCount) {
	if (deleteCount == null || deleteCount == '') deleteCount = this.length - start;
	
	// create a temporary copy of the array
	var tempArray = this.copy();
	
	// Copy new elements into array (over-writing old entries)
	for (var i = start; i < start + arguments.length - 2; i++) {
		this[i] = arguments[i - start + 2];
	}
	
	// Copy old entries after the end of the splice back into array and return
	for (var i = start + arguments.length - 2; i < this.length - deleteCount + arguments.length - 2; i++) {
		this[i] = tempArray[i + deleteCount - arguments.length + 2];
	}
	this.length = this.length - deleteCount + (arguments.length - 2);
	return tempArray.slice(start, start + deleteCount);
}


// Add an element to the beginning of an array
function _Array_unshift(the_item) {
	for (loop = this.length-1 ; loop >= 0; loop--) {
		this[loop+1] = this[loop];
	}
	this[0] = the_item;
	return this.length;
}


// For those browsers that don't define these Array methods, make functions Array instance methods
var agent = navigator.userAgent.toLowerCase(); 

if (Array && !((agent.indexOf('mozilla')!=-1) && (agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1) && (agent.indexOf('opera')==-1) && (agent.indexOf('webtv')==-1) && parseInt(navigator.appVersion) > 4)) {
	Array.prototype.pop     = _Array_pop;
	Array.prototype.push    = _Array_push;
	Array.prototype.shift   = _Array_shift;
	Array.prototype.splice  = _Array_splice;
	Array.prototype.unshift = _Array_unshift;
	
	if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) < 4) {
		Array.prototype.concat = _Array_concat;
		Array.prototype.slice  = _Array_slice;
	}
}
Array.prototype.copy = _Array_copy;
