/*
This script is copyright (c) 2006 Elliot Swan under the
Creative Commons Attribution-ShareAlike 2.5 license:
http://creativecommons.org/licenses/by-sa/2.5/

More information on this script can be found at:
http://www.elliotswan.com/2006/07/05/better-than-live/
*/

Event.observe(window, 'load', initSearch, false);
function initSearch() {
	Live.init();
}

var Live = function() {

	var form	= 'search';
	var input	= 'search_input';
	var results	= 'searchresults';
	var x		= 'closeresults';
	var submit	= 'searchsubmit';
	var sText	= 'Better-than-live search.'
	var iUrl	= 'http://www.elliotswan.com/accidental/wp-content/themes/worn/images/indicator.gif'; // url of indicator image
	var file	= '/index.php'; // url of the backend file doing the actual searching. If using WordPress, leave as-is. 

return {

		init	:	function() {
					 Event.observe(form, 'submit', this.search, false);
					 Event.observe(input, 'click', this.clear, false);
					  $(form).onsubmit = function() {return false;};
					 
					 Event.observe(x, 'click', this.close, false);
					  $(x).onclick = function() {return false;};
		},

		browser	:	function() {
		// Safari can't handle the fading on the form submit, so we have to check for it in order to send it some different styles
					 if (navigator.userAgent.toLowerCase() == 'safari') {return true;}
		},

		clear	:	function() {
					 if($F(input) == sText) {$(input).value = ''; }
					 $(input).onblur = function() { if($F(input) == '') {$(input).value = sText; }}
		},

		close	:	function() {
					 Effect.SwitchOff(results, {queue: {position: 'end', scope: 'close'}});
					 Effect.Fade(x, {duration: .5, queue: {position: 'front', scope: 'close'}});
		},

		active	:	function() {
					 if(this.Browser == true) {
					 var indicator = document.createElement('img');
						indicator.src = iUrl;
						indicator.id = 'indicator-safari';
						indicator.style.display = 'none';
					 $(form).appendChild(indicator);
					 Effect.Appear('indicator', {duration: .5, queue: {position: 'end', scope: 'active'}});
					}
					
					else {
						var indicator = document.createElement('img');
						indicator.src = iUrl;
						indicator.id = 'indicator';
						indicator.style.display = 'none';
					 $(form).appendChild(indicator);
					 Effect.Fade(submit, {duration: .5, queue: {position:'front', scope: 'active'}});
					 Effect.Appear('indicator', {duration: .5, queue: {position: 'end', scope: 'active'}});
					}			 
		},

		complete:	function() {
					 Effect.Fade('indicator', {duration: .5, queue: {position:'end', scope: 'active'}});
		 			 Effect.BlindDown(results, {queue: {position: 'end', scope: 'active'}});
					 if(this.Browser != true) {Effect.Appear(submit, {duration: .5, queue: {position: 'end', scope: 'active'}});}
					 Effect.Appear(x, {duration: .5, queue: {position: 'end', scope: 'active'}});
		},

		search	:	function() {
					 if($(results).style.display != 'none') {
						Effect.Fade(x, {duration: .5, queue: {position: 'front', scope: 'active'}});
						Effect.SwitchOff(results, {queue: {position: 'front', scope: 'active'}});
					 }
					 
					 Live.active();
					 var url = file;
					 var pars = 's='+$F(input);
					 var target = results;
					 var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars, onComplete: Live.complete, evalScripts: true});
					}
};
}();