/**
* This class represents a datasource which is used by jquery.suggest.
* The only "public" method a provider must implement is the query-function. 
* It must return an array of result-objects (see below)
*/
function YingizTypeAheadPartnerProvider() {
	
	/**
	* performs the actual query
	* The result is passed as an argument to the given callback function.
	* The callback-function enabled other Provider-implementations to return the results asynchronously.
	*/
	this.query = function(/*String*/query, /*function*/ callback) {
		var pos, result = [];
		var queryLc = query.toLowerCase();
		
		for (var i = 0; i < data.length; i++) {
			
			
			var prio = data[i] && (data[i].title.toLowerCase()).indexOf(queryLc);
			
			if (prio > -1) {
				result.push(new SearchResult(data[i].id, data[i].title, prio));
			}
			
		}
		
		result = sortSearchResults(result);
		var section = result.slice(0, maxResults);
		callback( section );
	};
	
	/**
	 *  represents a single result of a search query
	 */ 
	/*private inner class*/ function SearchResult(/*int*/ id, /*String*/ text, /*int*/ priority) {
		
		this.toString = function() {
			return text;
		};
		
		this.getPriority = function() {
			return priority;
		};
		
		this.getId = function() {
			return id;
		}; 
		
		this.toUrl = function() {
			
			var multiReplace = function(subject, patterns, substitutes) {
				var result = subject;
				for (var i = 0; i < patterns.length; i++) {
					result = result.replace(patterns[i], substitutes[i]);
				}
				return result;
			};
			
			/** taken from smarty_modifier_clean4filename.php */
			
		    // Alles in klein
		    var t = text.toLowerCase();
		    
		    // Ersetzt ä,ö,ü,ß durch ae,oe,ue,ss
		    t = multiReplace(t, [/ä/ig, /ö/ig, /ü/ig, /ß/ig, /€/ig], ['ae','oe','ue','ss', 'EUR']);
		    		
		    // Ersetzt alle nicht Buchstaben und Zahlen durch -
		    t = t.replace(/[\W]/g, "-");
		
		    // Ersetzt 1und mehr _ durch ein -
		    t = t.replace(/[_]+/g, "-");
		
		    // Ersetzt 1und mehr - durch ein -
		    t = t.replace(/[\-]+/g, "-");
		
		    // Entfernt - an Anfang und Ende
		    t = t.replace(/^[\-]+/, "");
		    t = t.replace(/[\-]+$/, "");			

			return escape(t) + '_' + that.getId() + '.html';
		};
		var that = this;
	}
	
	/**
	* converts the given result array to a simple string array
	*/
	function toStringArray(/*Array<SearchResult>*/ searchResults) {
		var result = [];
		
		for (var i = 0; i < searchResults.length; i++) {
			result.push(searchResults[i].toString());
		}
		return result;
	}
	
	/**
	* helper function for sorting result list by priority
	*/
	function sortSearchResults(/*Array<SearchResult>*/ result) {
		result.sort(function (first, second) {
			if (first.getPriority() == second.getPriority()) {
				return 0;
			}
			
			return first.getPriority() < second.getPriority() ? -1 : 1;
		});
		
		return result;
	}
	
	var data = TypeAheadPartnerData();
	var maxResults = 20;
}