/***/
window.addEvent('domready', function() {
/***/
var search = new Class({
	options: {
		input : $('S_keywords'),
		php : 'search.php',
		div : $('results2'),
		ul : $('resultList'),
		form: $('searchForm'),
		minWord : '2',
		delayTime : '500',
		divHeight : 320,
		noresult : 'No results found',
		button : $('searchGo'),
		overClass : 'hover',
		notOverClass : 'nothover',
		show : new Fx.Style($('results2'),'height',{transition:Fx.Transitions.Expo.easeIn,duration:400})
	},
	initialize: function(options){
		this.setOptions(options);
		this.actual = -1;
		this.time = 0;
		this.mouse = 0;		
		var othis = this;
		
		this.options.form.addEvent('submit',function(e){var event = new Event(e).stop();});
		this.options.button.addEvent('mousedown',function(e){othis.sendForm(othis);});
		this.options.input.addEvent('keyup',function(e){
			var event = new Event(e).stop();
			
			var all = othis.options.ul.getElements('li');
			
			if(event.alt == true){}
			if(event.shift==false || event.control==false || event.alt==false || event.meta==false){
				switch(event.key){
					case 'down':
						othis.mouse = 0;
						if(othis.actual == -1){
							if(all[0].innerHTML != othis.options.noresult){
								all[0].fireEvent('mouseenter');
							}
						}
						else if(othis.actual < all.length - 1){
							var next = othis.actual +1;
							all[othis.actual].fireEvent('mouseleave');
							all[next].fireEvent('mouseenter');
						}
					break;
					case 'up':
						othis.mouse = 0;
						if( !(othis.actual == -1 || othis.actual == 0) && othis.actual < all.length){
							all[othis.actual].fireEvent('mouseleave');
							all[othis.actual - 1].fireEvent('mouseenter');
							//alert(othis.caretPos(othis));
							othis.setCaretPos(this.value.length);
						} else othis.setCaretPos(this.value.length);
					break;
					case 'enter':
						if(othis.mouse == 1) othis.actual = -1;
						if(othis.actual != -1)all[othis.actual].fireEvent('click');
						else othis.sendForm(othis);
					break;
					case 'esc':
						othis.mouse = 0;
						othis.options.show.start(othis.options.divHeight,0);
					break;
					case 'left': /*do nothing*/break;
					case 'right':/*do nothing*/break;
					default:
						othis.mouse = 0;
						$clear(othis.time);
						if(this.getValue().length >= othis.options.minWord) othis.time = othis.search.delay(othis.options.delayTime,othis);
						else if(this.getValue().length == 0 && othis.options.div.getStyle('height') != '0px') othis.options.show.start(othis.options.divHeight,0);
					break;
				}
			}
			
			
		});
		
	},
	search: function(){
		var othis = this;
		new Ajax(this.options.php,{
			method: 'get',
			update: othis.options.ul,
			data: {
				'search' : othis.options.input.getValue()
			},
			onComplete: function(){
				othis.options.show.start(0,othis.options.divHeight);
					var all = othis.options.ul.getElements('li');
					all.each(function(element,index){
						if(index == 0) element.setStyle('border-top','3px solid #000');
						if(index == all.length -1) element.setStyle('border-bottom','3px solid #000');
						element.addEvents({
						'click': function(){
							var val = othis.stripHTML(this.innerHTML);
							if(val != othis.options.noresult) othis.options.input.value = val;
							othis.sendForm(othis);
						},
						'mouseenter': function(){
							this.removeClass(othis.options.notOverClass);
							this.addClass(othis.options.overClass);
							othis.mouse = 1;
							othis.actual = index;
						},
						'mouseleave': function(){
							this.removeClass(othis.options.overClass);
							this.addClass(othis.options.notOverClass);
						}
					});
				});
				othis.actual = -1;
			}
		}).request();
	},
	sendForm: function(){
		if(this.options.input.getValue().length >= 2){
			this.options.form.removeEvents('submit');
			this.options.form.submit();
		} 
		else {}
	},
	stripHTML : function(data){
		var matchTag = /<(?:.|\s)*?>/g;
		var d = data.replace(matchTag, "");
		return d;
	},
	setCaretPos: function(iCaretPos){
		// IE Support
		if (document.selection) {
			// Set focus on the element
			this.options.input.focus ();
			// Create empty selection range
			var oSel = document.selection.createRange ();
			// Move selection start and end to 0 position
			oSel.moveStart ('character', -oField.value.length);
			// Move selection start and end to desired position
			oSel.moveStart ('character', iCaretPos);
			oSel.moveEnd ('character', 0);
			oSel.select ();
		}
		// Firefox support
		else if (this.options.input.selectionStart || this.options.input.selectionStart == '0') {
			this.options.input.selectionStart = iCaretPos;
			this.options.input.selectionEnd = iCaretPos;
			this.options.input.focus ();
		}
	}
});
search.implement(new Options);
if(!window.ie6) var bb = new search({});
	
/***/	
},'javascript');/**end of domready load*/
/***/