/**
 * This file must be included to add functionality to arrays.
 * 
 * @version 2009-12-14
 * @author <a href="mailto:r.tennapel@griponservice.nl?SUBJECT=array.js">R. ten Napel, ing.</a>
 **/

/**
 * Add value(s) to the array (may pass more then one argument).
 * 
 * @param value			The value to add to the array.
 * @param ...			More values to add.
 * 
 * @return				The new length of the array.
 **/
Array.prototype.add = function(value) {
	for (var i = 0; i < arguments.length; i++) {
		this[this.length] = arguments[i];
	}
	
	return this.length;
};

/**
 * Remove value(s) from the array (may pass more then one argument).
 * 
 * @param value			The value to add to the array.
 * @param ...			More values to remove.
 * 
 * @return				This array.
 **/
Array.prototype.remove = function(value) {
	for (var i = 0; i < arguments.length; i++) {
		for (j = 0; j < this.length; j++) {
			if (this[j] == arguments[i]) {
				for (var k = j; k < this.length - 1; k++) {
					this[k] = this[k + 1];
				}
				
				this.length--;
			}
		}
	}
	
	return this;
};

/**
 * Remove value(s) from the array by index (may pass more the one argument).
 * 
 * @param index			The value index.
 * @param ...			More value indexes.
 * 
 * @return				This array.
 **/
Array.prototype.removeByIndex = function(index) {
	for (var i = 0; i < arguments.length; i++) {
		for (j = 0; j < this.length; j++) {
			if (j == arguments[i]) {
				for (var k = j; k < this.length - 1; k++) {
					this[k] = this[k + 1];
				}
				
				this.length--;
			}
		}
	}
	
	return this;
};

/**
 * Clears the array (sets the length to 0).
 * 
 * @return				This array.
 **/
Array.prototype.clear = function() {
	this.length = 0;
	
	return this;
};

/**
 * Checks if the array is empty.
 * 
 * @return				If the array is empty.
 **/
Array.prototype.isEmpty = function() {
	return this.length == 0;
};

/**
 * Merge the given array with the current one (may pass more then one argument).
 * 
 * @param array			The array(s) to merge.
 * @param ...			More arrays to merge.
 * 
 * @return				This array.
 **/
Array.prototype.merge = function(array) {
	var p = this.length;
	
	// Iterate through all arguments:
	for (i = 0; i < arguments.length; i++) {
		this.length += arguments[i].length;
		
		// Iterate thourgh all argument arguments:
		for (j = 0; j < arguments[i].length; j++) {
			this[p] = arguments[i][j];
			p++;
		}
	}
	
	return this;
};

/**
 * Return the index of a value.
 * 
 * @param value			The value to find.
 * 
 * @return				The index of the value to find.
 **/
Array.prototype.indexOf = function(value) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == value) {
			return i;
		}
	}
	
	return -1;
};

/**
 * Checks if the array contains a (set of) value(s).
 * 
 * @param value			The value to check.
 * @param ...			More values to check.
 * 
 * @return				True if all values exist or false if at least 1 doesn't exist.
 **/
Array.prototype.contains = function(value) {
	// Iterate through all arguments:
	for (i = 0; i < arguments.length; i++) {
		if (this.indexOf(arguments[i]) == -1) {
			return false;
		}
	}
	
	return true;
};

/**
 * Shuffle the array.
 * 
 * @return				This array.
 **/
Array.prototype.shuffle = function() {
	for (var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
	
	return this;
};

/**
 * Implode an array by means of the given glue.
 * 
 * @param glue			The string to put between each element (If not given, no glue is used).
 * 
 * @return				The glued array.
 **/
Array.prototype.implode = function(glue) {
	var text = "";
	
	// Ensure the glue:
	if (glue == null) {
		glue = "";
	}
	
	// Glue each array element:
	for (i = 0; i < this.length; i++) {
		// Add the glue:
		if (i > 0) {
			text += glue;
		}
		
		// Add the element:
		text += this[i];
	}
	
	return text;
};
