
var resetisset = '';

Livesearch = Class.create();

Livesearch.prototype = {
	initialize: function(father, attachitem, target, url, pars, searchform, loaditem, searchtext, resetbutton) {
		this.father = father;
		this.attachitem = attachitem;
		this.target = target;
		this.url = url;
		this.pars = pars;
		this.searchform = searchform;
		this.loaditem = loaditem;
		this.searchtext = searchtext;
		this.resetbutton = resetbutton;
		this.t = null;  // Init timeout variable

		var buttonvalue = 'go';
		$(father).innerHTML = '<input type="text" id="s" name="s" class="livesearch" autocomplete="off" value="'+searchtext+'" /><div src="http://kai.thesubmachine.com/blog/wp-content/themes/k2_3column/images/transparent.gif" id="reset" alt="Reset" title="Reset"><input type="submit" id="searchsubmit" value="'+buttonvalue+'" />';

		// Style the searchform for livesearch
		var inputs = $(searchform).getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++) {
			var input = inputs.item(i);
			if (input.type == 'submit')
				input.style.display = "none";
		}
		Effect.Fade(resetbutton, { duration: .1, from: 1.0, to: 0.3 })
		Event.observe(attachitem, 'focus', function() { if ($(attachitem).value == searchtext) $(attachitem).setAttribute('value', '') });
		Event.observe(attachitem, 'blur', function() { if ($(attachitem).value == '') $(attachitem).setAttribute('value', searchtext) });

		// Bind the keys to the input
		Event.observe(attachitem, 'keyup', this.readyLivesearch.bindAsEventListener(this));
	},

	readyLivesearch: function(event) {
		var code = event.keyCode;
		if (code == Event.KEY_ESC || ((code == Event.KEY_DELETE || code == Event.KEY_BACKSPACE) && $F(this.attachitem) == '')) {
			this.resetLivesearch();
		} else if (code != Event.KEY_LEFT && code != Event.KEY_RIGHT && code != Event.KEY_DOWN && code != Event.KEY_UP && code != Event.KEY_RETURN) {
			if (this.t) { clearTimeout(this.t) };
	        this.t = setTimeout(this.doLivesearch.bind(this), 400);
		}
	},

    doLivesearch: function() {
		new Ajax.Updater(
			this.target,
			this.url,
			{
				method: 'get',
				evalScripts: true,
				parameters: this.pars + $F(this.attachitem)
				//onLoading: Effect.Appear(this.loaditem, {duration: .2})
		});
		Event.observe(this.resetbutton, 'click', this.resetLivesearch.bindAsEventListener(this));
		$(this.resetbutton).style.cursor = 'pointer';

		if (resetisset != '1') {
			Effect.Appear(this.resetbutton, { duration: .1, from: 0.3, to: 1.0 });
		}
		resetisset = '1';
	},

	resetLivesearch: function() {
		$(this.attachitem).value = '';
		//$(this.target).innerHTML = null;
		new Ajax.Updater(
			this.target,
			this.url,
			{
				method: 'get',
				evalScripts: true,
				parameters: this.pars + $F(this.attachitem)
		});
		Event.stop(this.resetbutton);
		$(this.resetbutton).style.cursor = 'default';
		if (resetisset != '0') {
			Effect.Fade(this.resetbutton, { duration: .1, from: 1.0, to: 0.3 });
		}
		resetisset = '0';
	}
}

Event.observe(window, "load", function() { new Livesearch('searchform', 's', 'primary', 'http://kai.thesubmachine.com/blog/wp-content/themes/k2_3column/rollingarchive.php', '&s=', 'searchform', 'rollload', 'Type and Wait to Search', 'reset'); } , false);