/* 
Better-Than-Live AJAX (WordPress-Ready) Search v2

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/2007/06/27/better-than-live-ajax-wordpress-ready-search-version-2/
*/

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

var Live = function() {

	var form	= 'search'; // id of your search form
	var input	= 'search_input'; // id of your search input field
	var results	= 'searchresults'; // id of your results div/paragraph/whatever
	var x		= 'closeresults'; // id of the button/link to close the results
	var submit	= 'searchsubmit'; // id of the submit button
	var sText	= 'Search'; // Default value for your search input field. This is for the automatic clearing of the field onClick.
	var iUrl	= 'http://press.princeton.edu/blog/wp-content/themes/PUP/images/loader.gif'; // url of indicator image
	var file	= 'http://press.princeton.edu/blog/index.php'; // url of the backend file doing the actual searching. If using WordPress, there's a 99% chance you should this 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});
					}
};
}();