/**
*	@package		Generic CMS, (GCMS)
*	@subpackage		Javascript
*	@author			Ben Sekulowicz-Barclay
*	@copyright		Copyright 2008, Outside Line.
*	@version		9.01
*
************************************************************************************************************************ **/

var gcms_search = Class.create({	
	
	vars: { format: 'admin', html: '', pu: '', query: '' },
	
	/* ****************************************************************************************************************** */
	
	initialize: function(format) {
		
		this.vars.format = (format != null)? format: this.vars.format;
		
		document.observe('dom:loaded', function(e) {			
			
			// If we have no form, don't do a thing ...
			if ((!$('formSearch')) || (!$$('form#formSearch input[name=query]').first())) { return; }
			
			// Prevent the form from submitting ...
			$('formSearch').onsubmit = function() { return false; }
			
			// Reset the form ...
			$$('form#formSearch input[name=query]').first().value = '';
			
			// When we focus on the input ...
			$$('form#formSearch input[name=query]').first().observe('focus', this.onFormFocus.bindAsEventListener(this));
			
			// When we blur on the input ...
			$$('form#formSearch input[name=query]').first().observe('blur', this.onFormBlur.bindAsEventListener(this));
												
		}.bindAsEventListener(this));
	},
	
	/* ****************************************************************************************************************** */
	
	onFormBlur: function(e) {
		this.vars.pu.stop();
	},	
		
	/* ****************************************************************************************************************** */
	
	onFormFocus: function(e) {		
		// Get the field
		var i = Event.element(e, 'input');
		
		// Get the fields value 
		this.vars.query = i.value;
		
		// Start the Periodical Executor ...
		this.vars.pu = new PeriodicalExecuter(function() {
			
			if ((i.value != this.vars.query)) {
				
				// Add loading class to the input
				i.up().down('label').addClassName('searchLoading');
				
				// Update our stored value ...
				this.vars.query = i.value;
				
				// Construct an AJAX request to get the file information ...
		   		new Ajax.Request(site_url + this.vars.format + '/ajax/getSearchResults', {
					parameters: { query: i.value },
					onSuccess: function(response) {
						
						// If our HTML has changed
						if (response.responseText != this.vars.html) {
							
							// Remove the old table
							$$('div.content div.a table.tableSearch').invoke('remove');
							
							// Create a new table
							$$('form#formSearch').first().insert({ after: response.responseText });
							
							// Update our HTML
							this.vars.html = response.responseText;
						}
						
					}.bind(this)
		   		});			
			} else {
				// Add loading class to the input
				i.up().down('label').removeClassName('searchLoading');
			}			
			
		}.bind(this), 0.3);
	}
	
	/* ****************************************************************************************************************** */
});
