/*
 * new LiveProjectSearch('projectSearch', 'result_list', results, {listClass: 'textList', debug: false, fullSearch:true, choices: 1000, ignoreCase: true});
 */
LiveProjectSearch = Class.create(Autocompleter.Base, {
	initialize: function(element, update, array, options) {
		this.baseInitialize(element, update, options);
		this.options.array = array;

		this.options.onShow =     
			function(element, update){ 
				// Override default, do not move the div
				Effect.Appear(update,{duration:0.15});
			  };
		
		this.debug 		= (options.debug) ? options.debug : false;
		this.listClass 	= (options.listClass) ? options.listClass : false;		
		this.active 	= true;
		this.hasFocus 	= true;	// Force initial list
		this.initial 	= this.element.value;
		
		if (this.debug) console.log('START');
		this.updateChoices(this.getOutput(this.options.array));
	}

	, getUpdatedChoices: function() {
		if (this.debug)  console.log('- getUpdatedChoices');

		result = this.options.selector(this);

		this.updateChoices(this.getOutput(result));
	}

	, render: function() {
		if (this.debug) console.log('- render ('+this.entryCount+')');
	
		if(this.entryCount > 0) {
			this.show();
			this.active = true;
		} else {
			this.getOutput();
		}
	}
  
  	, setOptions: function(options) {
    	this.options = Object.extend({
			choices: 10,
			partialSearch: true,
			partialChars: 1,
			ignoreCase: true,
			fullSearch: false,
			selector: function(instance) {
				var ret       = []; // Beginning matches
				var partial   = []; // Inside matches
				var entry     = instance.getToken();
				var count     = 0;

				for (var i = 0; i < instance.options.array.length && ret.length < instance.options.choices ; i++) { 

					var elem = instance.options.array[i];
					var foundPos = instance.options.ignoreCase ? elem.toLowerCase().indexOf(entry.toLowerCase()) : elem.indexOf(entry);

					while (foundPos != -1) {

						if (foundPos == 0 && elem.length != entry.length) {
							
							// Found with match at beginning of word/line
							//if (this.debug) console.log(elem);

							ret.push("<strong>" + elem.substr(0, entry.length) + "</strong>" + elem.substr(entry.length));
							break;
						} else if (entry.length >= instance.options.partialChars && instance.options.partialSearch && foundPos != -1) {

							if (instance.options.fullSearch || /\s/.test(elem.substr(foundPos-1,1))) {
				
								// Found with match inside word
								//if (this.debug) console.log(elem);
								
								// This bolds the matched string. It's cool, but screws up html.
								//partial.push(elem.substr(0, foundPos) + "<strong>" + elem.substr(foundPos, entry.length) + "</strong>" + elem.substr(foundPos + entry.length));
								
								partial.push(elem);
								
								break;
							}
						}
				
					foundPos = instance.options.ignoreCase ? 
					  elem.toLowerCase().indexOf(entry.toLowerCase(), foundPos + 1) : 
					  elem.indexOf(entry, foundPos + 1);
					
					}
				}
				
				if (partial.length)
					ret = ret.concat(partial.slice(0, instance.options.choices - ret.length))
					
				return ret;
//				return "<ul>" + ret.join('') + "</ul>";
			}
		}, options || { });
		
	}


	, getOutput: function(matches) {
	
		var output 	= [];
		
		if (matches && matches.length >= 1) {

			var even 	= true;
			for (var i = 0; i < matches.length ; i++) {
			
				output.push('<li'
					+ ((even) ? ' class="zebra"' : '')
					+ '>'+matches[i]+'</li>');					
				even = !even;
			}			
			//return '<ul>' + output.join('') + '</ul>';
			
		} else {
			if (this.debug) console.log('* NO MATCH');
			output.push('<li class="no_results">There are no projects matching your search.</li>');
		}
		
		return '<ul'
			 + ((this.listClass) ? ' class="'+this.listClass+'"' : '')
			 + '>'
			 + output.join('') + '</ul>';
	
	}  

	, onObserverEvent: function() {
		this.changed 		= false;   
		this.tokenBounds 	= null;

		if (this.debug) console.log('- onObserverEvent');
		if (this.debug) console.log(this.getToken());

		if((this.getToken().length >= this.options.minChars) && (this.element.value != this.initial)) {
			this.getUpdatedChoices();
		} else {
			// this.active = false;
			if (this.debug) console.log('Show all');
			this.updateChoices(this.getOutput(this.options.array));
		}
		this.oldElementValue = this.element.value;
	}

	// Disabling extended methods

	, onHover: function(event) {
		// Nothing
	}
	, onClick: function() {
		// Nothing
	}
	, hide: function() {
		// Nothing
		if (this.debug) console.log('- hide');
	}
	, selectEntry: function() {
		// This gets called on Event.KEY_RETURN
		// Nothing
		if (this.debug) console.log('- selectEntry');
	}

});