/*

Searcher.

*/

if (typeof(debug) == "undefined")
{
	debug = false;
}

function Searcher()
{
	// doont tuts
	this.busy = false;
	this.timeoutid = null;
	
	// number of times to check before timing out
	this.iterations = 6;
	// time between check intervals
	this.timebetween = 5000;
}

Object.extend(Searcher.prototype,
{
	searchStarter: function()
	{
		// you should probably disable some submit button here
		if (debug) alert("No function has been bound to searchStarter");
	},
	
	searchVerifier: function()
	{
		if (debug) alert("No function has been bound to searchVerifier");
	},
	
	searchFinisher: function()
	{
		// you should probably re-enable some submit button here or redirect
		// the browser somewhere...
		if (debug) alert("No function has been bound to searchFinisher");
	},
	
	searchTimeout: function()
	{
		// you should probably do some things like provide a timeout error
		// message here...
		if (debug) alert("No function has been bound to searchTimeout");
	},
	
	searchReset: function()
	{
		// you should probably re-enable some submit button here
		if (debug) alert("No function has been bound to searchReset");
	},
	
	startSearch: function()
	{
		if (this.busy)
		{
			if (debug) alert("Search has not finished yet");
			return false;
		}

		this.busy = true;
		this.searchStarter();

		if (this.iterations > 0 && this.busy)
		{
			var thisObject = this;
			var timeoutFunction = function() { thisObject.verifySearch(thisObject.iterations) };
			this.timeoutid = setTimeout(timeoutFunction, this.timebetween);
		} else
		{
			// iets fout gegaan wsl.
			this.resetSearch();
		}
		
		return true;
	},
	
	verifySearch: function(iteration)
	{
		this.searchVerifier();
		
		iteration--;
		var thisObject = this;
		var timeoutFunction = function() { thisObject.verifySearch(iteration)} ;
		
		if (iteration > 0 && this.busy)
		{
			this.timeoutid = setTimeout(timeoutFunction, this.timebetween);
		} else
		{
			this.timeoutSearch();
		}
	},
	
	finishSearch: function()
	{
		this.searchFinisher();
		this.wrapupSearch();
	},
	
	timeoutSearch: function()
	{
		this.searchTimeout();
		this.wrapupSearch();
	},
	
	resetSearch: function()
	{
		this.searchReset();
		this.wrapupSearch();
	},
	
	wrapupSearch: function()
	{
		this.busy = false;
		
		if (this.timeoutid != null)
		{
			clearTimeout(this.timeoutid);
			this.timeoutid = null;
		}
	}
});