
/*
 * Author: Vertex Logic, Inc
 * Copyright 2005, 2006, 2007 - All rights reserved
 *
 * Vertex Logic, Inc., California, USA 
 *
 * Vertex Logic grants you ("Licensee") a non-exclusive license to use
 * and modify this source and recompile it in
 * accordance with the terms of the Agreement under which this software is bought
 * and provided that (i) this copyright notice appear on all copies of the Software; 
 * (ii) Licensee does not sale the software as is or with modification without
 * the prior consent of Vertex Logic. (iii) Licensee agrees that it does not have any 
 * title or ownership of the Software.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * Vertex Logic will not be liable for any damages suffered 
 * by you as a result of using the Program. 
 * In no event will Vertex Logic be liable for any
 * special, indirect or consequential damages or lost profits even if
 * Vertex Logic have been advised of the possibility of their occurrence. 
 * Vertex Logic will not be liable for any third party claims against you.
 */
 Af.ElementCollection = Class.create();

Af.ElementCollection.prototype = {

	initialize : function(t) {
		 this.top = t;
		 this.list = new Array();
		 this.list.push(this.top);
		 this.buildList(this.top);
	},

	buildList : function(top) {
	   var cnodes = top.childNodes;
		 for (var i=0; i<cnodes.length; i++) {
		   this.list.push(cnodes[i]);
			 this.buildList(cnodes[i]);
		 }
	},

	getIterator : function() {
	   return new Af.ElementIterator(this.list);
	},
	
	getElements : function(tagName) {
	   var result = new Array();
		 var iter = this.getIterator();
		 while (iter.hasNext()) {
		   var e = iter.next();
			 if (e.tagName == tagName) {
			    result.push(e);
			 }
		 }
		 return result;
	},
	
	getElementsIndexedById : function(tagName) {
	   var result = new Object();
		 var iter = this.getIterator();
		 while (iter.hasNext()) {
		   var e = iter.next();
			 if (e.tagName == tagName) {
			    result[e.id] = e;
			 }
		 }
		 return result;
	},
	
	getFirstElement : function(tagName) {
		var iter = this.getIterator();
		while (iter.hasNext()) {
		   var e = iter.next();
			 if (e.tagName == tagName) {
			    return e;
			 }
		}
		return null;
	},
	
	getFirstElementById : function(id) {
		var iter = this.getIterator();
		while (iter.hasNext()) {
		   var e = iter.next();
			 if (e.id == id) {
			    return e;
			 }
		}
		return null;
	}
}


Af.ElementIterator = Class.create();

Af.ElementIterator.prototype = {
  initialize : function(list) {
		 this.list = list;
		 this.currentIndex = 0;
	},
	
	hasNext : function() {
	   return this.currentIndex < this.list.length;
	},
	
	next : function() {
	  if (this.hasNext()) {
		   return this.list[this.currentIndex++];
		}
		return null;
	}
}