SearchControl = Class.create();
SearchControl.serviceURL = "/resource/application/service/search.php";
SearchControl.prototype = {

	initialize: function(searchUsage, inputElement, displayElement, searchingGraphicElement, exitGraphicElement, displayAllByDefault, autoClearText, searchImportantOnly) {
  	this.searchUsage = searchUsage;
  	this.inputElement = $(inputElement);
  	this.displayElement = $(displayElement);
  	this.searchingGraphicElement = $(searchingGraphicElement);
  	this.exitGraphicElement = $(exitGraphicElement);
  	this.displayAllByDefault = displayAllByDefault;
  	this.autoClearText = autoClearText;
  	this.liRows = new Array();
  	this.searchImportantOnly = searchImportantOnly;
  	
    this.displayAllByDefault = displayAllByDefault;
    
    Event.observe(this.inputElement, "keyup", this.onKeyUp.bindAsEventListener(this));
    Event.observe(this.inputElement, "keydown", this.onKeyDown.bindAsEventListener(this));
    this.inputElement.onblur = this.onBlurEvent.bindAsEventListener(this);
    this.exitGraphicElement.onclick = this.onExitClickEvent.bindAsEventListener(this);
    
    if (this.displayAllByDefault) {
  		if (this.ul == undefined) {
  			this.createOutputTable();
  		}    
    	this.searchForData();
    }
  },
  
  createOutputTable: function() {
  	var divElement = document.createElement("div");
		Element.extend(divElement);
		this.displayElement.appendChild(divElement);
		
		this.ul = document.createElement("ul");
		Element.extend(this.ul);
		divElement.appendChild(this.ul);		
  },
  
	searchForData: function(category) {
		if (this.ul == undefined) {
			this.createOutputTable();
		}	
	
		var search = "";
		if (this.inputElement.value != this.autoClearText) {
			search = this.inputElement.value;
		} else {
			search = "";
		}
	
  	new Ajax.Request(SearchControl.serviceURL, {
  		method: "get",
			parameters: {
				Action: "SearchData",
				Usage: this.searchUsage,
				Search: search,
				Important: this.searchImportantOnly,
				Category: category
			},
  		onSuccess: this.onSearchData.bind(this)
  	});  	
  },
  
  displaySearchResults: function() {
  	this.liRows.each(this.removeSearchResult.bind(this));
  	this.liRows = [];
		$H(this.results).each(this.displayArtist.bind(this));
  },
  
  displayArtist: function(artist) {
  	var li = new Element("li");
  	li.onclick = (function() {	 		
	 		window.location="/artist/" + artist[1].FolderName;		
	 		this.displayElement.hide();
	 		return false;
		}).bind(this);
  	this.liRows.push(li);
		this.ul.appendChild(li);
		
  	var artistNameAnchor = new Element("a", { "href": "/artist/" + artist[1].FolderName } );
  	li.appendChild(artistNameAnchor);
  	
  	var cellText = document.createTextNode(artist[1].Name);
  	artistNameAnchor.appendChild(cellText);
  	
  	/*var li = document.createElement("li");
  	Element.extend(li);
  	li.onclick = (function() {	 		
	 		window.location="/artist/" + artist[1].FolderName;		
	 		this.displayElement.hide();
	 		return false;
		}).bind(this);
  	this.liRows.push(li);
		this.ul.appendChild(li);
		
  	var artistNameAnchor = document.createElement("a");
  	artistNameAnchor.setAttribute("href", "/artist/" + artist[1].FolderName);
  	Element.extend(artistNameAnchor);
  	li.appendChild(artistNameAnchor);
  	
  	var cellText = document.createTextNode(artist[1].Name);
  	artistNameAnchor.appendChild(cellText);*/
  	
  },
  
  removeSearchResult: function(searchResult) {
  	this.ul.removeChild(searchResult);
  },
  
  displayNoResultsMessage: function() {
  	this.liRows.each(this.removeSearchResult.bind(this));
  	this.liRows = [];
  	
  	var li = new Element("li", { "id": "no-search-results" }).update("No results match your criteria.");		
  	this.liRows.push(li);
		this.ul.appendChild(li);
  	
  },
	
	/**
	 * Events
	 */
	onSearchData: function(transport) {
		this.displayElement.style.display = "block";
	 	var response = transport.responseText.evalJSON();
	 	if (response.success) {
		 	this.results = response.data;
		 	this.displaySearchResults();
	 	} else {
	 		this.displayNoResultsMessage().bind(this);
	 	}
	 	if (this.searchingGraphicElement != undefined) {
		 	this.searchingGraphicElement.style.display = "none";
		}
	},
	
	onKeyDown: function(event) { 	
  	if (event.keyCode == 13) {  
  		Event.stop(event);		
  		return false;
  	}
  	//if (event.keyCode == 40) {
  	//	if (!$("no-search-results")) {
  	//		var increment = 1;
  	//		var nextElement = 0;
  	//		$("artist-choice").select("li").each(function(element) {
  	//			element.id = "search-result-" + increment;
  	//			element.removeClassName("highlighted");
  	//			increment++;
  	//		})
 				
 		//		console.debug(increment);
  	//		document.getElementById("search-result-1").addClassName("highlighted");
  	//		document.getElementById("search-result-1").focus();
  	//		return false;
  	//		if (event.keyCode == 13) {
  	//			redirect("/artist/" + artist[1].FolderName);
  	//		}
  	//		var person = $("artist-choice").getElementsByClassName("highlighted");
 		//	}
  	//}
	},
	 
  onKeyUp: function(event) { 
  	//if (event.keyCode == 40) {
		//	return false;
		//}
  	if (this.inputElement.value.length >= 3) {
		 	if (this.searchingGraphicElement != undefined) {
			 	this.searchingGraphicElement.style.display = "block";
			}
  		this.searchForData();
  	} else {
	    if (this.displayAllByDefault) {
		  	this.searchForData();
		  } else {
		  	this.displayElement.style.display = "none";
		  }
  	}
  },
  
  onBlurEvent: function(event) {
 		if ($("artist-choice").down("li").innerHTML == "No results match your criteria.") {
			this.displayElement.style.display = "none";
			this.searchingGraphicElement.style.display = "none";
			this.inputElement.value = this.autoClearText
		}
  },
  
  onExitClickEvent: function(event) {
		this.displayElement.style.display = "none";
		this.searchingGraphicElement.style.display = "none";
		this.inputElement.value = this.autoClearText
  }
}
