
/* last updated: 18.05.2011 */



var AutoSuggestionSearch = Class.create(); 
			
AutoSuggestionSearch.prototype  = { 
	position:"",
	activeSuggestion:0,
	useInputValue:0,
	searchField:0,
	autoSuggestDiv: 0,
	self: this,
	inputLabel: this,
	runningRequests: new Array(),
	requestTunerUrl: "",
	initialize: function(id, position, allSearchResultsPath) { 

		 this.position = position;
	     this.id = id;  
	     this.searchField =  $("searchField"+id);
	     this.autoSuggestDiv =  $("autosuggestion"+id);
	     this.allSearchResultsPath = allSearchResultsPath;
	     
	     $("searchField"+id).observe("click", this.click.bindAsEventListener(this));
	     $("searchField"+id).observe("keyup", this.suggest.bindAsEventListener(this)); 
	     $("autosuggestionSubmit").observe("click", this.submit.bindAsEventListener(this));				     
	     $$("html")[0].observe("click", this.closeWindow.bindAsEventListener(this));
	     this.inputLabel =  this.searchField.value;
	     
	     Ajax.Request.prototype.abort = function() { 
		    this.transport.onreadystatechange = Prototype.emptyFunction();
		    this.transport.abort();
		    Ajax.activeRequestCount--;
		};
	     
	},
	closeWindow: function(event){ 
	
		var e = (event.target) ? event.target : event.srcElement;
		/*click event tritt auch bei return auf deshalb e.id != "autosuggestionSubmit"*/
	
		if (e.id != "searchField"+this.id && e.id != "autosuggestionSubmit") this.setSuggestionVisible(false);
	
	},
	
	submit: function(event){
		
		window.location = this.allSearchResultsPath+"?s="+this.searchField.value+this.getExternValues();
		
	},
	
	click: function(event){ 
		
		if (this.searchField.value == this.inputLabel) {
			this.searchField.value = "";
		} else {
			
			this.setSuggestionVisible(true);
			
		}
		this.activeSuggestion = 0;
	},	
	
	setSuggestionVisible: function(isVisible) {
	
		if (isVisible) {
			
			this.autoSuggestDiv.setStyle({display: 'block'});	
			$("searchForm").style.position="absolute";
			$("searchForm").style.zIndex="999";
			
		} else {
			this.autoSuggestDiv.setStyle({display: 'none'});	
			$("searchForm").style.position="static";
			$("searchForm").style.zIndex="0";
		}
		
	},
	
	suggest: function (event) { 
		
		
		this.setSuggestionVisible(true);
		
		if (event.keyCode==Event.KEY_DOWN) { 

			if (!this.activeSuggestion)  { 
				
				this.useInputValue =stripHTML( this.searchField.value);
				this.activeSuggestion = Element.down($("autosuggestResultContainer")); 

				
				
			} else  { 
				
				this.activeSuggestion.removeClassName("active");
				
				if (this.activeSuggestion.next()) {
					this.activeSuggestion = this.activeSuggestion.next();
				}
				
			}
			
			
			
			var isResult = ((this.activeSuggestion.className).indexOf("searchResult") != -1);
			var isAllResults = ((this.activeSuggestion.className).indexOf("asrContainer") != -1);
			
			if(isResult || isAllResults) this.activeSuggestion.addClassName("active"); 
			
		} else if (event.keyCode==Event.KEY_UP) {

			if (this.activeSuggestion && this.activeSuggestion.previous())  {

				this.activeSuggestion.removeClassName("active");
				this.activeSuggestion = this.activeSuggestion.previous();
				this.activeSuggestion.addClassName("active");

			} else if (this.activeSuggestion) {
			
			}

		} else if (event.keyCode==Event.KEY_RETURN) { 
		
			var isResult = false;
			var isAllResults = true;
			
			if(this.activeSuggestion != 0){
				var isResult = ((this.activeSuggestion.className).indexOf("searchResult") != -1);
				var isAllResults = ((this.activeSuggestion.className).indexOf("asrContainer") != -1);
			}
			
			if(isResult){
				window.location = this.activeSuggestion.href;
			} else {
				this.submit(event);
			}
		
	
			
		} else { 
			
			this.searchField.addClassName("loading");

			this.activeSuggestion = 0;
			
			this.abortRequests();
			
			var trim = this.searchField.value;
			trim = trim.replace(/^\s+|\s+$/g, '');
			
			
			var req = new Ajax.Request( this.requestTunerUrl+"?partialLoad=true&tar="+this.position,
			{
			method: 'post',
			parameters: "searchString="+trim+this.getExternValues(),
			onSuccess: function(transport){ 
			
				trim = this.searchField.value;
				trim = trim.replace(/^\s+|\s+$/g, '');
				
				// NEU:
				//
				var parameters = transport.request.options.parameters;
				var parameter_searchString = parameters.substr("searchString".length+1);

				if(parameter_searchString != trim+this.getExternValues()) return;				
				//
				
				// ALT:
				//
				//if(transport.request.options.parameters.searchString != trim+this.getExternValues().trim()) return;
				//
				

				
				
				
				this.searchField.removeClassName("loading");

				var response = transport.responseText;
				
			  	this.autoSuggestDiv.innerHTML = response;

			  	if (response) 	this.autoSuggestDiv.style.display= "block";
			  	else 			this.autoSuggestDiv.style.display= "none";
			  	
			  	

			  	var listItem = this.autoSuggestDiv.down();

				  	while (listItem) {
				  		listItem.observe("click", this.changeText.bindAsEventListener(this));
				  		listItem.style.cursor="pointer";
				  		listItem = listItem.next();
				  	}


			  	}.bind(this)

			});
			
			this.runningRequests.push(req);
			
		}

	},

	changeText: function (e) { 
		
		if (this.activeSuggestion)  {
			this.activeSuggestion.removeClassName("active");
		}
		this.activeSuggestion = Event.element(e);
		this.activeSuggestion.addClassName("active");
		this.useInputValue = stripHTML(this.searchField.value);
		this.autoSuggestDiv.style.display = "none";
		
	},
	
	getExternValues: function () {
		var ext = $$("input.autoexternField_"+this.id);		
		var add = "";
		for(var i = 0; i < ext.length; i++) add += ext[i].value + " "; 
		
		var deleteCount = 0;
		
		// strip whitespaces at the end
		for(var j = add.length-1; j >= 0; j--){
			if(add.substr(j,1) == " ") deleteCount++; else break;
		}
		
		if(add == " " || add == "") return "";
		
		
		
		return " "+add.substr(0,add.length-deleteCount);
	},
	
	abortRequests: function (){
		
		for(var i = 0; i < this.runningRequests.length; i++){
			var req = this.runningRequests[i];
			req.abort();
		}
		
		this.runningRequests = new Array();
		
	},
	
	setRequestTunerUrl: function(url){
		this.requestTunerUrl = url;
	}
				 
			
};
			
			
function stripHTML(oldString) {

   var newString = "";
   var inTag = false;
   for(var i = 0; i < oldString.length; i++) {
   
        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
              if(oldString.charAt(i+1)=="<")
              {
              		//dont do anything
	}
	else
	{
		inTag = false;
		i++;
	}
        }
   
        if(!inTag) newString += oldString.charAt(i);

   }

   return newString;
}			
