var Search = Class.create();
Search.prototype = {
	initialize: function(parent) {
		this.parentElement = $(parent);
		this.name = "Bilaketaren emaitzak:";
		this.children = new Array();
		this.readonly = false;
		this.open = false;
		this.createSearch();
	},
	createSearch: function() {
		this.element = document.createElement('div');
		this.span =    document.createElement('span');
		this.link =    document.createElement('a');
		
		Element.addClassName(this.element, 'directory');
		Element.addClassName(this.element, 'search');
		Element.addClassName(this.span, 'search');
						
		// create a generic spinner
		this.spinner = document.createElement('img');
		this.spinner.src = spinnerIcon;
		this.spinner.style.display="none";
		Element.addClassName(this.spinner, 'spinner');
		
		// lets make the collapse expand indicator
		this.mark = document.createElement('img');
		this.mark.src = vcollapsed;
		Element.addClassName(this.mark, 'mark');
		
		this.link.href = "javascript:go();";
		this.link.innerHTML = this.name;
		this.display ? this.link.innerHTML = this.display : null;
		
		Element.addClassName(this.link, 'link');

		this.del = document.createElement('a');
		this.del.href = "javascript:go()";
		this.del.innerHTML = "itxi";
		Element.addClassName(this.del, 'del');


		// Events
		this.mark.onclick = this.openOrClose.bind(this);			
		this.span.ondblclick = this.openOrClose.bind(this);
		this.del.onclick = this.hide.bind(this);
		this.link.onselectstart = function() {return false; }
		
		this.span.appendChild(this.link);
		this.span.appendChild(this.mark);
		this.span.appendChild(this.del);
		this.span.appendChild(this.spinner);
		this.element.appendChild(this.span);
							
		this.element.id = "searchresults";
		this.element.object = this;
		this.element.style.display = "none";
		
		this.parentElement.appendChild(this.element);
	},
	
	openOrClose: function() {
		if(this.open) {
			this.element.style.height="20px"; 
			this.element.style.overflow = "hidden";
			this.mark.src = vcollapsed;
			this.open = false;
		}
		else {
			this.open = true;
			this.element.style.height="auto";
			this.mark.src = vexpanded;
		}
	},
	show: function() {
		//Effect.Appear(this.element.id);
		this.element.style.display = "block";
	},
	hide: function() {
		this.open=false;
		Effect.Fade(this.element.id);
		//this.element.style.display = "none";
	},
	start: function(string) {

		this.clearContents();		
		this.show();
		this.element.style.height="auto";
		this.mark.src = vexpanded;
		
		var term = $('searchbar').value || string;
		this.link.innerHTML = this.name + " <em>" + term + "</em>";
		this.spinner.style.display = "block";
		var params = $H({ relay: 'search', terms: term });
		//plusAjax();
		var ajax = new Ajax.Request(FC.URL, {
			onSuccess: this.start_handler.bind(this),
			method: 'post',
			parameters: params.toQueryString(),
			onFailusre: function() {showError(ER.ajax); }
		});
	},
	start_handler: function(response) {
		this.open = true;	
		this.spinner.style.display = "none";
		this.mark.src = vexpanded;
		var json_data = response.responseText;
		
		eval("var jsonObject = ("+json_data+")");
		for(var i=0; i < jsonObject.bindings.length; i++){
			var newFile = new File(jsonObject.bindings[i].id, jsonObject.bindings[i].name, jsonObject.bindings[i].flags, $('searchresults'), jsonObject.bindings[i].date);
			this.children.push(newFile);
		}
	},
	clearContents: function () {
		this.open = false;
		while(this.children.length > 0) {
			this.removeChild(this.children[0], 0);
		}
	},
	removeChild: function (child, index) {
		if(!index) {
			for(var i=0; i< this.children.length; i++) {
				if(this.children[i] == child){ var index = i; break; }
			}
		}
		this.children.splice(index, 1);
		// remove my droppable
		Element.remove(child.element);
		// and finally get rid of me
		delete child;	
	}

};


function monitorSearch(event) {
	alert(event);
	var charCode = (event.charCode) ? event.charCode : ((event.which) ? event.which : event.keyCode);
	switch(charCode) {
		case Event.KEY_RETURN:
			doSearch();
			break;
	}
}
