// Wrapper class for ajax based search
function SearchEngine(mn,url,liT){
	this.json = new JsonRequest(mn + '.json', new Function('data',mn + '.display(data)')); // ajax/json request handler object
	this.list = new ListBuilder(liT,'sr'); // list builder object
	this.url = url; // ajax request url
	this.mn = mn; // my name
};

// must be called onload with ref to form and content container div
SearchEngine.prototype.init = function(f,c,i,iT){
	this.b = document.getElementById(this.mn + '_searchButton'); // search button
	this.th = document.getElementById(this.mn + '_throbber'); // throbber element
	this.obt = this.b.value; // original search button text
	
	this.i = i; // info div
	this.iT = iT; // info div template
	
	this.list.init(c); // initialise list with reference to list container
	ghost_addEvent(f,'submit',new Function('e','return '+this.mn+'.formSubmit(e,this.searchString.value);'));
};

// this function is attached to form's onsubmit handler
SearchEngine.prototype.formSubmit = function(e,text){
	this.s();
	var r = this.json.makeRequest(this.url + "?searchString=" + text);
	if(r && e && e.preventDefault) e.preventDefault();
	return !r;
};

// fill the content container- called onreadystatechange
SearchEngine.prototype.display = function(data){
	this.list.clear();
	
	this.i.innerHTML = this.list.parseText(this.iT,0,data.info);
	
	for (i in data.results)
		this.list.createLi(0,i,data.results[i]);
	this.e();
};

// called at start of search
SearchEngine.prototype.s = function(){
	this.b.disabled = true;
	this.b.value = 'Searching...';
	this.th.style.display = 'inline';
};
// called at end of search
SearchEngine.prototype.e = function(){
	this.b.disabled = false;
	this.b.value = this.obt;
	this.th.style.display = 'none';
};


