/*
	cbs_live_search v0.4
	by Christophe Beyls (http://www.digitalia.be) - MIT-style licence
	powered by Mootools
*/

var CbsLiveSearch = new Class({

	initialize: function(textbox, results, default_value, noresults, options, postbody) {
		this.default_value = default_value;
		this.noresults = '<p>'+noresults+'</p>';
		this.postbody = postbody;
		this.textbox = $(textbox);
		this.webkit = options.k && navigator.vendor && navigator.vendor.test('Apple Computer') && (navigator.productSub.toInt() >= 20020000);
		
		if(options.r && !this.webkit)
			this.resetimage = new Element('img').setProperty('src', options.r).addClassName('livesearch_resetimage').setStyles({cursor: 'pointer', visibility: 'hidden'}).injectInside(this.textbox.parentNode).addEvent('click', function() {
				this.textbox.value = '';
				this.check();
				this.textbox.focus();
				
			}.bind(this));
		if(options.w) this.waitimage = new Element('img').setProperty('src', options.w).addClassName('livesearch_waitimage').setStyle('visibility', 'hidden').injectInside(this.textbox.parentNode);
		
		this.results = new Element('div').setProperty('id', results).setStyle('display', 'none').injectAfter(this.textbox.form);
		this.clone = this.results.clone().setStyles({visibility: 'hidden', position: 'absolute', display: '', height: ''}).injectBefore(this.results);
		
		this.resizeFx = this.results.setStyles({display: '', overflow: 'hidden'}).effect('height', { duration: 500, wait: false }).hide();
		this.fadeFx = this.results.effect('opacity', { duration: 500, wait: false }).hide();
		this.visible = false;
		
		if(this.webkit) {
			$(this.textbox.form).addClassName('livesearch_webkit');	
			this.textbox.setProperties({type: 'search', autosave: this.textbox.form.action, results: '5', placeholder: this.default_value});
		}
		else this.textbox.setProperties({autocomplete: 'off', value: this.default_value}).addClassName('livesearch_inactive');
		this.textbox.addEvent('focus', this.onFocus.bindAsEventListener(this)).addEvent('blur', this.onBlur.bindAsEventListener(this));
	},

	onFocus: function() {
		if(!this.webkit && (this.textbox.value == this.default_value))
			this.textbox.removeClass('livesearch_inactive').value = '';
		this.oldValue = this.textbox.value;
		this.checkTimer = this.check.periodical(1000, this);
	},

	onBlur: function() {
		$clear(this.checkTimer);
		this.check();
		if(!this.webkit && (this.textbox.value == ''))
			this.textbox.addClass('livesearch_inactive').value = this.default_value;
	},

	hideResults: function() {
		if(!this.visible) return;
		this.visible = false;
		this.fadeFx.goTo(0);
		this.resizeFx.goTo(0);
	},

	check: function() {
		if(this.textbox.value == this.oldValue) return;
		$clear(this.showTimer);
		this.abort();
		this.oldValue = this.textbox.value;
		if(this.resetimage) this.resetimage.style.visibility = this.oldValue ? '' : 'hidden';
		if((this.postbody.q = this.oldValue.trim()).length < 2) return this.hideResults();
		if(this.waitimage) this.waitimage.style.visibility = '';
		this.ajax = new Ajax(window.location.href, {
			method: 'post',
			postBody: this.postbody,
			onComplete: this.onRequestComplete.bind(this),
			onStateChange: function() {
				try { if((this.ajax.transport.readyState != 4) || (this.ajax.transport.status == 200)) return; } catch(e) {}
				this.abort();
				this.hideResults();
			}.bind(this)
		}).request();
	},

	abort: function() {
		if(this.ajax) {
			this.ajax.transport.abort();
			this.ajax = null;
			if(this.waitimage) this.waitimage.style.visibility = 'hidden';
		}
	},

	onRequestComplete: function(response) {
		this.ajax = null;
		if(this.waitimage) this.waitimage.style.visibility = 'hidden';
		this.results.setHTML((response == '<none />') ? this.noresults : response);
		this.clone.setHTML(this.results.innerHTML);
		this.showTimer = this.show.delay(50);
	},

	show: function() {
		if(!this.visible) {
			this.visible = true;
			this.fadeFx.goTo(1);
		}
		this.resizeFx.goTo(this.clone.clientHeight);
	}
});

