var handySearch = new Class({

	Implements: [Events, Options],
	
	options: {
        url: 'results.php',
        method: 'get',
		delay: 300
	},

	initialize: function(input, resultsBox, searchWrapper, btnSearch, options) {
		this.setOptions(options);
		this.input = $(input);
		this.results = $(resultsBox);
		this.wrapper = $(searchWrapper);
		this.btnSearch = $(btnSearch);
		this.input.set('value', 'Search');
		this.defaultVal = 'Search';
		window.overResults = false;
		var self = this;
		self.liveRequest = false;
		keyHash = { 'down': true, 'up': true, 'enter': true, 'left': true, 'right': true, 'space': true };
		t = 0;

		this.request = new Request({
			url: this.options.url,
			method: this.options.method,
			onCancel: function() {
				self.liveRequest = false;
			},
			onRequest: function() {
				self.liveRequest = true;
				self.input.addClass('active');
			},
			onSuccess: function(responseText, responseXML) {
				self.liveRequest = false;
				this.input.removeClass('active');
				if (this.input.get('value').length >= 2){
					this.results.set('html', responseText);
					$$(this.wrapper,this.results).addClass('active');
				}
			}.bind(this)
		});

		this.input.addEvents({
			focus: this.focus.bind(this),
			blur: this.blur.bind(this),
			keydown: this.keydown.bind(this),
			keyup: this.keyup.bind(this)
		});

		this.results.addEvents({
			mouseenter: function() { return window.overResults = true },
			mouseleave: function() { return window.overResults = false }
		});
	
		this.btnSearch.addEvents({
			'click': this.button.bind(this)
		});

	},

	focus: function() {
		if(this.box().total > 0 && this.input.get('value').length >= 2) $$(this.results,this.wrapper).addClass('active');
		this.setValue();
	},

	blur: function() {
		if(!window.overResults) ((this.box().active) ? $$(this.results,this.wrapper,this.box().active) : $$(this.results,this.wrapper)).removeClass('active');
		this.setValue();
	},

	button: function() {
		if (this.input.get('value') != this.defaultVal) {
			top.location = this.options.url + this.input.get('value');
		} else {
			this.input.highlight('#579ED7');
		}
	},

	keydown: function(event) {
		switch(event.key) {
			case 'right':
			case 'left': if (this.box().active != null) this.box().active.removeClass('active'); break;
			case 'up':
			case 'down': if (this.box().els != '') this.select(event.key); break;
			case 'enter': //if (this.input.get('value') != '') top.location = (!this.box().active) ? this.options.url + this.input.get('value') : this.box().active.get('href'); break;			
				if (this.input.get('value') != '') {
					if (!this.box().active) {
						top.location = this.options.url + this.input.get('value');
					} else {
						top.location = this.box().active.get('href');
					}
				}
				break;
				
			
			default: return;
		}
	},

	keyup: function(event) {
		val = this.input.get('value');
		if (!keyHash[event.key] && val.length >= 2) {
			t = $clear(t);
			t = this.getResults.delay(this.options.delay, this);
		} else if (val.length < 2) {

			$$(this.results,this.wrapper,this.input).removeClass('active');
			if(self.liveRequest) this.request.cancel();
			this.results.empty();
		}
	},

	setValue: function() {
		val = this.input.get('value');
		value = (val == '') ? this.defaultVal : (val == this.defaultVal) ? '' : val;
		this.input.set('value', value);
	},

	getResults: function() {
		this.request.send({url: this.options.url + val + "&context=html_ajax"});
	},

	select: function(direction) {
		way = (direction == 'up') ? -1 : 1;
		oldIndex = this.box().index;		
		if(oldIndex + way >= this.box().total) {
			newIndex = 0;
		} else {
			if(oldIndex == 0 && way == -1) {
				newIndex = this.box().total - 1;
			} else if(oldIndex == -1 && way == -1){
				newIndex = this.box().total - 1;
			} else {
				newIndex = oldIndex + way;
			}
		}
		if(oldIndex != -1) this.box().els[oldIndex].removeClass('active');
		this.box().els[newIndex].addClass('active');
	},

	box: function() {
		var active = this.results.getElement('a.active');
		var els = this.results.getElements('a');
		var index = els.indexOf(active);
		var total = els.length;
		var obj = {active: active, els: els, index: index, total: total};
		return obj;
	}

});