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

var search = Class.create({	
	
	
	tgts: {},
	
	vars: {},
	
	/* ****************************************************************************************************************** */
	
	initialize: function() {
			
		document.observe('dom:loaded', function(e) {
			
			// Get our form vairables
			this.tgts.form 	= $$('form.headerSearch').first();
			this.tgts.field = $$('form.headerSearch input[type=text]').first();
			
			// When we focus on the input ...
			this.tgts.field.observe('focus', this.onFormFocus.bindAsEventListener(this));
			
			// When we blur on the input ...
			this.tgts.field.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[type=text]');
		
		// Start the Periodical Executor ...
		this.vars.pu = new PeriodicalExecuter(function() {	
		
			// If we need to change our search query ...		
			if ((i.value != this.vars.query)) {	
				
				// Get the fields value 
				this.vars.query = i.value;	
				
				// Construct an AJAX request to get the file information ...
		   		new Ajax.Request(site_url + 'ajax/search/' + this.vars.query, {
					onSuccess: function(response) {
						
						// If our HTML has changed
						if (response.responseText != this.vars.html) {
							
							// Remove the old table
							$$('#ajaxHeaderSearch').invoke('remove');						
							
							// Create a new table
							$$('body').first().insert({ bottom: response.responseText });
							
							$('ajaxHeaderSearch').style.left = (i.cumulativeOffset().left - 120) + 'px';
							$('ajaxHeaderSearch').style.top = (i.cumulativeOffset().top + 19) + 'px';						
							$('ajaxHeaderSearch').style.width = 300 + 'px';
							
							// Update our HTML
							this.vars.html = response.responseText;
						}
						
					}.bind(this)
		   		});				
			}
			
		}.bind(this), 0.3);
	}
	
	/* ****************************************************************************************************************** */
});