java.Collection = Class.create();
java.Collection.prototype = {
	initialize: function(){
		this.idCont = 0;
		this.ini = null;
		this.end = null;
		this.len = 0;
		this.current = null;
		this.xmlDoc = null;
	},
	addNodo : function(node){
	},
	newNodo : function(obj){
		var node = new java.Collection.Nodo();
		node.register = obj;
		node.id = this.idCont;
		this.idCont ++;
		this.len++;
		return node;	
	},
	add : function(obj){
		var node = this.newNodo(obj);
		if(this.ini==null){
			this.addStart(node);
		}else{
			this.addNodo(node);		
		}
	},
	addStart: function(node){
		this.ini = node; // O INICIO DA FILA RECEBE O NODO
		this.end = node; // 1° nodo O FIM DA FILA RECEBE NODO
	},
	seek : function(field,key){
		var aux = this.ini
		while(aux!= this.end ){
			if(aux.register[field]!=key){
				aux = aux.next;	
				this.current = aux;
			}else{
				break;
			}
		}
		if(aux==null){ 
			return null;
		}else{		
	
			if(aux.register[field]==key){ 
				this.current = aux;
				return aux.register;	
			}else{
				return null;
			}	
		}
	},
	seekAll : function(field,key){
		var result = new Array();
		var aux = this.ini
		var i=0
		do{
			if(aux.register[field]==key){
				result[i] = aux.register			
			}
			aux = aux.next;			
			i++;
		}while(aux!= null);
		return result;		
	},
	each: function(iterator) {
	    var aux = this.ini
		if(aux!=null){
			do{
				iterator(aux.register);
				aux = aux.next;
			}while(aux!= this.end);
			iterator(aux.register);
		}
	},
	clone:function(obj){
		obj = this;
		return obj;		
	},
	setCurrent:function(nodo){
		this.current = nodo;		
	},
	first:function(){
		this.current = this.ini
		return this.ini.register;	
	},
	last:function(){
		this.current = this.end
		return this.end.register;		
	},
	size:function(){	 
		return this.len;
	},
	remove : function(){
		var aux = this.ini;
		if(aux.next!=null){
			this.ini = aux.next;
			aux.next.prev = null;
			if(this.current = aux){
				this.current = aux.next;
			}			
		}else{
			this.ini = null;
			this.end = null;
			this.current = null;
		}		
		this.len--;
		var obj = aux.register;
		aux = null
		return obj;
	},
	loadXML: function(obj){
		var typeObj = typeof obj
		var classe = this;
		if( typeObj == "string"){
			new Ajax.Request(obj,{
							 method: 'get', 
							 onComplete: this.addXML.bind(this),
							 onFailure: function(){ alert('Não foi possível carregar o arquivo:' + ojb) }});		
		}else if(typeObj=="object"){
			this.xmlDoc = obj;
			this.createListXML();
		}
	},
	addXML:function (RequestXml){
		this.xmlDoc = RequestXml.responseXML;
		this.createListXML();
	},
	createListXML : function(){
		var xml = this.xmlDoc;
		this.xmlDoc = null;
		var regs = xml.getElementsByTagName("record");
		for (var i=0; i< regs.length;i++){
			var node = regs[i].childNodes;
			var obj = new Object();
			var obj = this.createObject(node);
			this.add(obj);			
		}	
	},
	createObject : function(node){	
		var obj = new Object();
		for( var i= 0; i < node.length; i++){
			var itemAtual = node[i];
			if(window.ActiveXObject){
				if(itemAtual!=null){
					obj[itemAtual.nodeName] = itemAtual.text;
				}else{
					obj[itemAtual.nodeName] = "";	
				}
			}else{
				
				if(itemAtual.firstChild!=null){				
					obj[itemAtual.nodeName] = itemAtual.firstChild.data
				}else{
					obj[itemAtual.nodeName] = "";
				}
			}
		}//endfor
		return obj;
	}
}
java.Collection.List = Class.create();
java.Collection.List.prototype =Object.extend(new java.Collection(),{
	initialize: function(xml){
		if(xml!=null){
			this.loadXML(xml);
			this.xmlDoc = xml;
		}
	},
	addNodo : function(nodo){
		var aux = this.end;
		aux.next = nodo;		
		nodo.prev = aux;
		this.end = nodo;
		this.current = nodo;
	},
	addNext : function(field,key,obj){
		this.seek(field,key);
		var nodo = this.newNodo(obj);
		if(this.current.next!=null){
			this.current.next.prev = nodo;	
		}else{
			this.end = nodo;	
		}
		nodo.next = this.current.next;
		nodo.prev = this.current;
		this.current.next = nodo
		this.current = nodo;
		this.len++;
	},
	next :function(){
		if(this.current.next!=null){
			this.current = this.current.next;	
			return true;
		}else{
			return false	
		}
	},
	prev : function(){
		if(this.current.prev!=null){
			this.current = this.current.prev;	
			return true;
		}else{
			return false	
		}
	},
	removeNodo : function(field,key){
		var nodos = new Array();
		nodos = this.seekAll(field,key);
		for(var i=0 ; i < nodos.length ; i++){
			var aux = nodos[i];
			if(aux.prev==null){  
				this.remove(); 
			}else if(aux.next==null){
				this.end = aux.prev;
				this.current = aux.prev;
				this.current.next = null;
				this.len--;
			}else{
				aux.next.prev = aux.prev
				aux.prev.next = aux.next;
				this.current = aux.next;
				this.len--;
			}
		}
		if(nodos.length!=0){
			if(nodos.length ==1){
				return nodos[0].register;
			}else{
				return nodos;
			}			
		}else{
			return false
		}
	},
	removeNodeObj:function(node){
		if(node==this.ini){
			this.remove();
		}else if(node==this.end){
			node.prev.next = null;
			this.end = node.prev;
		}else{
			node.prev.next = node.next;
			node.next.prev = node.prev;
		}
		node.next = null;
		node.prev = null;
		node.register = null;
		node=null;
	},
	fusionList : function(newList){
		if(this.ini==null && this.end==null ){// LISTA ATUAL ESTÁ VAZIA
			this.ini = newList.ini;
			this.current = newList.ini;
			this.end = newList.end;
		}else{
			newList.ini.prev = this.end;		
			this.end.next = newList.ini;
			this.current = newList.ini;
			this.end = newList.end;
		}
		this.idCont = newList.idCont;
		this.len = this.len + newList.len;
		newList.ini = null;
		newList.end = null;
		newList.current = null;
		newList = null;
	}
});
java.Collection.Stack = Class.create();
java.Collection.Stack.prototype =Object.extend(new java.Collection(),{
	initialize: function(){
	},
	addNodo : function(nodo){
		var aux = this.ini;
		aux.prev = nodo;		
		nodo.next = aux;
		this.ini = nodo;
		this.Node++;
	}
});
java.Collection.Queue = Class.create();
java.Collection.Queue.prototype =Object.extend(new java.Collection(),{
	initialize: function(){
	},
	addNodo : function(nodo){
		var aux = this.end;
		aux.next = nodo;		
		nodo.prev = aux;
		this.Node++;
		this.end = nodo;
	}	
});
java.Collection.CircleList = Class.create();
java.Collection.CircleList.prototype = Object.extend(new java.Collection.List(),{
	initialize: function(){
	},
	next :function(){
		if(this.current.next!=null){
			this.current = this.current.next;		
		}else{
			this.current = this.ini;
		}
	},
	prev : function(){
		if(this.current.prev!=null){
			this.current = this.current.prev;	
		}else{
			this.current = this.end			
		}
	}
});
java.Collection.Nodo = Class.create()
java.Collection.Nodo.prototype = {
	initialize: function(){
		this.id = 0;
		this.prev = null;
		this.next = null;
		this.register = null;
	}
}
