var searchInputs = new Array();

function Search(id, input, object, callback, overItemMessage){
	this.id					= id;
	this.object 			= object;
	this.index 				= searchInputs.length;
	this.callback			= callback;
	this.overItemMessage	= overItemMessage;
	this.advancedSearch 	= false;
	this.highlighted		= -1;
	this.listcount				= 0;
	
	searchInputs[this.index] = this;
	this.assignToInput(input);
}
{	
	Search.prototype.getElement = function(pos)
	{
		if (this.container)
		{
			var list = this.container.getElementsByTagName('li');
			
			if (list && list[pos])
				return list[pos];
		}
		return false;
	}
	
	
	Search.prototype.move = function(offset)
	{
		if (this.highlighted >= 0)
			removeElementClass(this.getElement(this.highlighted), 'highlight');
		
		this.highlighted += offset;
		
		if (this.highlighted >= this.listcount)
			this.highlighted = - 1;else
			
		if (this.highlighted < - 1)
			this.highlighted = this.listcount - 1;
		
		if (this.highlighted >=0)
		{
			addElementClass(this.getElement(this.highlighted), 'highlight');
		}
	}
	
	Search.prototype.assignToInput = function(input)
	{
		if (!input)
			return;
			
		this.input = input;
		this.input.search = this;
		this.input.searchid = this.id;
		this.input.keydown = true;
		
		if (!this.input.getValue)
		{
			this.input.getValue = InputObject.prototype.getValue;
			this.input.setValue = InputObject.prototype.setValue;
		}
		
		xb.addEvent(this.input,'keydown', function (e)
		{
			e = e || window.event;
			var charCode = (e.keyCode) ? e.keyCode : e.charCode;
			
			this.keydown = false;
			
			if (charCode == 40)
			{
				this.search.move(1);
			}
			else if (charCode == 38)
			{
				this.search.move(-1);
			}
			else 
				this.keydown = true;
		});
		
		this.input.onkeypress = function (e)
		{
			e = e || window.event;
			var charCode = (e.keyCode) ? e.keyCode : e.charCode;
			
			if (charCode == 13 && this.search.highlighted >= 0)
			{
				setTimeout('searchInputs[' + this.search.index + '].hide();', 1);
				
				var link = this.search.getElement(this.search.highlighted).getElementsByTagName('a');
				
				if (link)
					if (link[0].onclick)
					{
						if (link[0].onclick() && link[0].href != 'javascript:void(0)')
							window.location = link[0].href;
					}
					else
					if (link[0].href != 'javascript:void(0)')
					{
						window.location = link[0].href;
					}
					
				return false;
			}
			
			if (this.keydown)
			{
				if (this.timer)
					clearTimeout(this.timer);
				
				this.timer = setTimeout('searchInputs[' + this.search.index + '].getSuggestions();', 100);
			}
			else
				return false;
		};
		
		xb.addEvent(this.input,'blur', function (e)
		{
			if (this.search.hide)
			{
				setTimeout('searchInputs[' + this.search.index + '].hide();', 500);
			}
		}, true);
		
		xb.addEvent(this.input,'focus', function (e)
		{
			if (this.search.display)
				this.search.display.style.display = 'block';
		});
	}
	
	Search.prototype.toggleSearch = function(state)
	{
		var prevState = this.advancedSearch;
		
		if (typeof(state) != 'undefined')
			this.advancedSearch = state;else
			this.advancedSearch = !this.advancedSearch;
		
		if (prevState != this.advancedSearch)
		{
			if (this.advancedSearch)
			{
				animate('advanced_frame', 'style.height', '0px', '350px', 0.5, EaseInAndOut);
				
				animate('quick_frame', 'opacity', 100, 0, 0.5, EaseInAndOut);
				animate('quick_frame', 'style.height', '70px', '0px', 0.5, EaseInAndOut);
			}
			else
			{
				animate('advanced_frame', 'style.height', '350px', '0px', 0.5, EaseInAndOut);
				
				animate('quick_frame', 'opacity', 0, 100, 0.5, EaseInAndOut);
				animate('quick_frame', 'style.height', '0px', '70px', 0.5, EaseInAndOut);
			}
		}
	}
	
	Search.prototype.getSuggestions = function(xhr)
	{
		if (!xhr)
		{
			
			if (this.xhr)
			{
				this.xhr.clear();
			}
			
			if (this.input.getValue())
			{
				this.xhr = sendAjaxRequest('?section=search', 'class=' + this.object + '&' + this.input.name + '=' + this.input.getValue(), this, 'getSuggestions', 50);
			}
			else
			{
				this.hide();
			}
		}
		else
		{
			var xml 		= xhr.responseXML;
			if (xml)
			{
				var count = 0;
					if (xml.getElementsByTagName('count')[0] && xml.getElementsByTagName('count')[0].childNodes[0])
					var count = xml.getElementsByTagName('count')[0].childNodes[0].nodeValue;
				
				this.total = count;
				
				var guessedWord = '';
					var guessed = xml.getElementsByTagName('guessed')[0];
					
					if (guessed && guessed.childNodes.length)
					{
						guessedWord =  guessed.firstChild.nodeValue;
					}
				
				/*if (callback && callback.getSuggestions)
					callback.getSuggestions(this, xml, count, guessedWord);*/
				
				this.displaySuggestions(xhr, count, guessedWord);
			}
			else
			{
				this.hide();
			}
		}
		//xhrtimer = setTimeout('xhrObject = sendAjaxRequest(\'?\', \'who='+who+'&where='+where+'&search=true&ajax=true\', \'ajaxResponse\');', 50);
	}
	
	Search.prototype.displaySuggestions = function(xhr, count, guessedWord)
	{
		if (!this.box)
		{
			this.box = document.createElement('div');
			this.box.className = 'suggestionsbox';
			
			this.container = document.createElement('ul');
				this.container.className = 'items list';
				this.container.setAttribute('editable', 'true');
				makeEditable(this.container);
				this.container.setEditable(false);
			
			this.box.appendChild(this.container);
			
			document.body.appendChild(this.box);
		}
		
		this.highlighted = -1;
		
		if (this.container.clearElements)
		this.container.clearElements();
		
		if (count || guessedWord)
		{
			var bounds = getBounds(this.input);
			bounds.top += bounds.height;
			bounds.height = '';
			
			setBounds(this.box, bounds);
			this.box.style.display = 'block';
			
			this.container.appendElements(xhr.responseXML, 'display', xhr);
			
			var links = this.container.getElementsByTagName('a');
				
			for (var i = 0; i < links.length; i++)
			{
				links[i].callback 		= this.callback;
				links[i].searchid		= this.id;
				links[i].searchindex	= this.index;
				links[i].onmousedown 	= null;
				links[i].onmouseup 		= null;
				links[i].onclick		= function (e)
				{
					searchInputs[this.searchindex].hide();
					return true;
				}
				
				if (this.callback && this.container.childNodes)
				{
					links[i].href		= 'javascript:void(0)';
					links[i].onclick	= function (e)
					{
						e = e || window.event;
						searchInputs[this.searchindex].hide();
						return this.callback.onItemClicked(e, this.searchid, this.parentNode);
					};
				}
			}
			
			this.listcount = 0;
			
			for (var i = 0; i < this.container.childNodes.length; i++)
			if (this.container.childNodes[i].nodeType == 1)
			{
				var item = this.container.childNodes[i];
				if (this.overItemMessage)
					item.setAttribute('title', this.overItemMessage);
				
				inherit(item, InputObject);
				this.listcount ++;
				
			}
			
			
			if (guessedWord)
			{
				var b 	= document.createElement('b');
					b.appendChild(document.createTextNode(guessedWord));
					
				var p	= document.createElement('p');
					p.appendChild(document.createTextNode('"'));
					p.appendChild(b);
					p.appendChild(document.createTextNode('"?'));
					
				var a	= document.createElement('a');
					a.appendChild(p);
					a.input 		= this.input;
					a.className		= 'guessedword';
					a.href			='javascript:void(0)';
					a.guessedWord	= guessedWord;
					a.onmousedown	= function(){this.input.setValue(this.guessedWord);this.input.search.getSuggestions();this.input.focus();};
				
				var li 	= document.createElement('li');
					li.appendChild(a);
				
				this.container.insertBefore(li, this.container.firstChild);
			}
			
			if (count)
			{
				var div = document.createElement('div');
					div.className = 'count';
					div.appendChild(document.createTextNode('Total: ' + count));
					
				this.container.appendChild(div);
			}
		}
		else
		{
			this.hide();
		}
	}
	
	Search.prototype.hide = function()
	{
		if (this.box)
			this.box.style.display = 'none';
	}

	Search.prototype.checkSubmitedFields = function()
	{
		var form = document.getElementById('searchform');
		var fields = form.getElementsByTagName('*');
		
		for (var i = 0; i < fields.length; i++)
		{
			if (fields[i].type != 'submit' && fields[i].type != 'button' &fields[i].name != 'section' && 
				(fields[i].is_empty 
				|| (this.advancedSearch && (fields[i].name == 'who' || fields[i].name == 'where'))
				|| (!this.advancedSearch && fields[i].name != 'who' && fields[i].name != 'where')
				)
			)
			{
				fields[i].value = '';
			}
		}
		return true;
	}
}


var search = new Search();