//<![CDATA[
/* 
 Copyright Information
 This code copyright 2007, Masala, Inc. 
 This code contains proprietary information which is a trade secret of Masala, Inc. 
 and also is protected as an unpublished work under applicable copyright law. 
 Recipients are to retain this code in confidence and are not permitted to use or 
 make copies thereof other than as permitted in a written agreemnet with Masala, Inc.
*/

//Class: ArrayList
//Description: 
//Implemented: 9-7-2007 Hasham Malik
//Updated: 9-7-2007 Hasham Malik

function ArrayList(){
	//constructor of ArrayList class 
	this.aList = [];
}

ArrayList.prototype.count = function (){
	//the count method of ArrayList class
	return this.aList.length;
}

ArrayList.prototype.getAt = function (index){
	//the getAt method of ArrayList class
	if (index > -1 && index < this.aList.length){
		return this.aList[index];
	}else{
		throw 'Out of Bounds';
		//return undefined;
	}	
}

ArrayList.prototype.add = function (object) {
	//the add method implementation
	this.aList.push(object);
}

ArrayList.prototype.clear = function () {
	//the clear method of ArrayList class
	this.aList = [];
}

ArrayList.prototype.removeAt = function (index) {
	//the removeAt method for ArrayList class
	var listCount = this.aList.length;
	if(listCount > 0 && index > -1 && index < this.aList.length){
		switch(index){
			case 0:
				this.aList.shift();
				break;
			case listCount - 1:
				this.aList.pop();
				break;
			default:
				var head = this.aList.slice(0, index);
				var tail = this.aList.slice(index + 1);
				this.aList = head.concat(tail);
				break;
		}
	}	
}

ArrayList.prototype.insert = function (object, index){
	//the insert method implemented for ArrayList class
	var listCount = this.aList.length;
	var returnValue = -1;
	
	if(index > -1 && index < listCount){
		switch(index){
			case 0:
				this.aList.unshift(object);
				returnValue = 0;
				break;
			case listCount:
				this.aList.push(object);
				returnValue = listCount;
				break;
			default:
				var head = this.aList.slice(0, index - 1);
				var tail = this.aList.slice(index);
				this.aList = this.aList.concat(tail.unshift(object));
				returnValue = index;
				break;
		}
	}
	return returnValue;
}

ArrayList.prototype.indexOf = function (object, startIndex){
	//the indexOf method implemented for ArrayList class
	var listCount = this.aList.length;
	var returnValue = -1;
	if(startIndex > -1 && startIndex < listCount){
		var i = startIndex;
		while(i < listCount){
			if(this.aList[i] == object){
				returnValue = i;
				break;
			}
			i++;
		}
	}
	return returnValue;
}

ArrayList.prototype.lastIndexOf = function (object, startIndex){
	//the lastIndexOf method implemented for ArrayList class
	var listCount = this.aList.length;
	var returnValue = -1;
	if(startIndex > -1 && startIndex < listCount){
		var i = listCount - 1;
		while(i >= startIndex){
			if(this.aList[i] == object){
				returnValue = i;
				break;
			}
			i--;
		}
	}
	return returnValue;
}

//Class: Observer
//Description: 
//Implemented: 9-7-2007 Hasham Malik
//Updated: 9-7-2007 Hasham Malik

function Observer(){
	//the constructor for Observer class
}

Observer.prototype.update = function (){
	//the update method stub for Observer class		
	return;
}

//Class: Subject
//Description: 
//Implemented: 9-7-2007 Hasham Malik
//Updated: 9-7-2007 Hasham Malik

function Subject(){
	//the constructor method for Subject class
	
}

Subject.prototype.notify = function (context){

	try
	{
		//the notify method implementation for Subject class
		var observerCount = this.observers.count();
		for(var i = 0; i < observerCount; i++){
			this.observers.getAt(i).update(context);
		}
	}catch(ex)
	{
		log_exceptions(" Method:- Subject.prototype.notify " + ex.message);
	}
}

Subject.prototype.addObserver = function (observer){
	//the addObserver method implementation for Subject class
	if(!observer.update){
		throw 'Wrong parameter';
	} else {
		this.observers.add(observer);
	}
}

Subject.prototype.removeObserver = function (observer){
	//the removeObserver method implementation for Subject class
	if(!observer.update){
		throw 'Wrong parameter';
	} else {
		this.observers.removeAt(this.observers.indexOf(observer, 0));
	}
}





