$package("js.lang");

/**
 * Construct js.lang.Array.inherits from Object.
 * @class js.lang.Array is the String class.  
 *
 * @see Object Object is the base class for this
 */
js.lang.Array = Array;

js.lang.Array.prototype.addItem = function(obj){
	this[this.length] = obj;
}
/**
 * Remove the item form the array
 * @param {} val 
 */ 
js.lang.Array.prototype.removeItem = function(val) {
	var retCount = 0;
	for(var i = 0; i < this.length; i++) {
		if(this[i] == val) {
			for(var j = i; j < this.length - 1; j++) {
				this[j] = this[j + 1];
			}
			this.length = this.length - 1;
			retCount++;
		}
	}
	return retCount;
}
/**
 * Remove the item form the array as index
 * @param {int} index 
 */ 
js.lang.Array.prototype.removeItemAt = function(index) {
	if(index < this.length){
		for(var i = index; i < this.length - 1; i++) {
			this[i] = this[i + 1];
		}
		this.length = this.length - 1;
	}
}

js.lang.Array.prototype.clear = function() {
  this.length = 0;
}

js.lang.Array.prototype.contains = function(obj){
	return this.indexOf(obj, 0) >= 0;
}

js.lang.Array.prototype.indexOf = function(obj, index){
	var i;
	if (!index) index = 0;
	if (obj == null) {
	  for (i = index; i < this.length; i++)
			if (this[i] == null) return i;
	} else {
		for (i = index; i < this.length; i++)
			if (obj == this[i]) return i;
	}
	return -1;
}
js.lang.Array.prototype.getItem = function(idx){
	return this[idx];
}
js.lang.Array.prototype.getFirstItem = function(){
	if (this.length == 0) throw "NoSuchItemException";
	return this[0];
}
js.lang.Array.prototype.getLastItem = function(){
	if (this.length == 0) throw "NoSuchItemException";
	return this[this.length - 1];
}

js.lang.Array.prototype.setItemAt = function(obj, index) {
	if (index == null) {
		alert("index is undefined!");
		throw "index is undefined!";
	}
	if (this.length < index) {
		alert("this.length < " + index);
		throw "this.length < " + index;
	}
	this[index] = obj;
}
js.lang.Array.prototype.insertItem = function(obj, index){
	if (index > this.length) throw "ArrayIndexOutOfBoundsException: " + index + " > " + this.length;
	if(this.length>0)
		this.arrayCopy(this, index, this, index + 1, this.length - index);
	this[index] = obj;
}

js.lang.Array.prototype.arrayCopy = function(srcArray, srcIndex, dstArray, dstIndex, copyLength){
	if ((srcIndex + copyLength) > srcArray.length){
		 alert("(srcIndex + copyLength) > srcArray.length");
		 throw "(srcIndex + copyLength) > srcArray.length";
	}
	if (dstIndex > dstArray.length) {
		alert("dstIndex > dstArray.length");
		throw "dstIndex > dstArray.length";
	}	
	if ((dstArray.length - dstIndex) < copyLength) {
		dstArray.length = dstIndex + copyLength;
	}

	var i;
	var tempArray = new Array();
	for (i = 0; i < copyLength; i++ ){
		tempArray.addItem(srcArray[srcIndex + i]);
	}
	for (i = 0; i < copyLength; i++ ){
		dstArray.setItemAt(tempArray[i], dstIndex + i);
	}
}
