var SearchResultsWindow = newClass(WbsPopwindow, {
    constructor: function (config) {
		if (!config) config = {};
		config.width = 230;
		this.constructor.prototype.constructor.call(this, config);	
	},
	
	render: function() {
		if (this.rendered)
			return false;
		this.rendered = true;
	},
	
	getWindowCls: function() {
		return "wbs-popwindow search-form";
	}
});



SearchResult = newClass (null, {
	constructor: function(item) {
		this.title = item.highlight;
		this.url = item.url;
		this.prefix = item.prefix;
	}
});

SearchForm = newClass (null, {
	constructor: function(formElemId) {
		this.formElem = $(formElemId);
		
		this.input = this.formElem.getChildrenTagByClassName("input", "search-str-input")[0];
		
		this.defaultValue = this.input.value;
		
		addEvent( this.input, "keyup", this.search.bind(this));
		addEvent( this.input, "focus", this.focused.bind(this));
		addEvent( this.input, "blur", this.onBlur.bind(this));
		
		this.searchResultsDiv = null;
	},
	
	focused: function(e) {
		if (this.input.value == this.defaultValue) 
			this.input.value = "";
	},
	
	onBlur: function(e) {
		if (this.input.value == "")  {
			this.input.value = this.defaultValue;
			addClass (this.formElem, "default-state");
		}
	},
	
	search: function(e) {	
		var searchStr = this.input.value;
		removeClass (this.formElem, "default-state");
		
		if (!this.popwindow)
			this.popwindow = new SearchResultsWindow();
		
		if (searchStr.length < 3)
			return;
		
		if (!this.searchResultsDiv) {
		 	//this.searchResultsDiv = createDiv("search-result-div");
			//this.formElem.appendChild(this.searchResultsDiv);
			this.searchResultsDiv = this.popwindow.getInnerElem();
		}
		
		this.startSearch(e);
	},
	
	startSearch: function(e) {
		//clearNode(this.searchResultsDiv);
		//this.searchResultsDiv.addClass('loading');
		
		var params = {str: this.input.value};
		
		//this.popwindow.show(e, this.formElem);
		
		Net.post({
			url: "/pages/ajax/form-search.php", 
			vars: params, 
			onsuccess: function(response) {
				var result = json(response); 
				if(!result.success) { 
					showErrors(result);
					return false;
				}
				this.data = new Array();
				var jsonData = result.data;
				this.result = result;
				for (var i = 0; i < jsonData.length; i++) {
					this.data.push(new SearchResult(jsonData[i]));
				}
				this.changed(e);
			}.bind(this), 
			onerror: showAjaxError
		});
	},
	
	changed: function(e) {
		this.searchResultsDiv.removeClass('loading');
		
		var list = createElem("ul");
		
		clearNode(this.searchResultsDiv);
		
		if (this.data.length > 1)
			this.popwindow.show(e, this.formElem);
		else 
			this.popwindow.hide();
		
		
		for (var i = 0; i < this.data.length && i < 5; i++) {
			var item = this.data[i];
			var li = createElem("li");
			var link = createElem("a");
			
			var prefix = createElem("div", "prefix");
			prefix.setText(item.prefix);
			li.appendChild(prefix);
			
			link.href = item.url;
			link.innerHTML = item.title;
			li.appendChild(link);
			
			list.appendChild(li);
		}
		
		this.searchResultsDiv.appendChild(list);
	}
});
