/**
 * @author Matthew Foster
 * @date	January 2nd 2007
 */

	var HighLight = Class.create();
	
	
	
	HighLight.prototype = {
				
				initialize : function(searchTerm, options){
					
					this.searchTerm = searchTerm;
					this.regEx = new RegExp(searchTerm, "i");
					
					this.options = {
										src 		   : document,
										onFoundElement : this.foundElement.bind(this),
										onClear        : this.clear.bind(this),
										onTransform    : this.transformText.bind(this),
										tag            : "*"
										
									}
					
					
					Object.extend(this.options, options || {});
				
					this.eleArr = [];				
					
				},
				
				start : function(){
					
					this.collection = $A($(this.options.src).getElementsBySelector(this.options.tag));
					
					this.collection.each(this.parseElement.bind(this));							
				
				},
				
				parseElement : function(ele){
					var arr = $A(ele.childNodes);
					
					var refinedArr = arr.findAll(function(ele){
													return (ele.nodeType == 3);										
												});
					refinedArr.each(this.parseText.bind(this, ele));
					
				
				
				},
				
				parseText : function(parent, text){
					
					if(text.nodeValue.search(this.regEx) == -1){
						
						return false;
						
					}
				
					if(parent.nodeName.toLowerCase() == "script")
						return false;
					
					
					
					this.options.onFoundElement(parent, text, this);
					
					
				},
				foundElement : function(parent, text, instance){
									
					var newText = document.createElement("span");
					newText.innerHTML = text.nodeValue.gsub(this.regEx, this.options.onTransform);
					
					parent.replaceChild(newText, text);
					this.eleArr.push({ parent : parent, newNode : newText, oldNode : text});
					
					
				},
				transformText : function(text){
					
					return "<b>"+text+"</b>";
				
				},
				clearAll : function(){
					
					this.eleArr.each(this.options.onClear);	
					this.eleArr = [];
					this.collection = [];
					
				},
				
				clear : function(obj){
					
					obj.parent.replaceChild(obj.oldNode, obj.newNode);
					
					
				}
 
			}

