/*
	SearchInterface.js
	
	Site Lve Search Class
	
*/

var LiveSearch = new Class({
	/*
		Options to set rendering locations.
	*/
	options: {
		SearchForm:		'search',						// ID of search form
		SearchPage:		'/inc/inc_live_search.cfm',	// Page to send AJAX serach request to
		SearchInput:		'id_search_submit',				// Search Keyword Input
		SearchWrap:		'live_search_wrap',				// Wrapper for live search dropdown
		SearchResults:		'live_search_results',			// Container for search results
		SelectablesClass:	'.selectable'					// Class for all selectable items
	},
	
	initialize: function(options) {
		
		this.setOptions(options);

		this.SearchForm	=  $( this.options.SearchForm );
		this.SearchPage	=     this.options.SearchPage;
		this.SearchInput	=  $( this.options.SearchInput );
		this.SearchWrap	=  $( this.options.SearchWrap );
		this.SearchResults	=  $( this.options.SearchResults );
		this.SelectablesAll	= $$( this.options.SelectablesClass );
		
		this.SelectablesCurrent = 0;
				
		this.InterfaceSetup();
		
	},

	LoadContent: function( sAttributes, oContainer, postFunction ) {
		
		if( postFunction != null )
			new Ajax( this.SearchPage + sAttributes, { method: 'get', update: oContainer, onComplete: postFunction } ).request();
		else
			new Ajax( this.SearchPage + sAttributes, { method: 'get', update: oContainer } ).request();
			
	},

	KeyUp: function( event ) {
	
		var theKeywords = this.theSearchInput.value;
		this.SelectablesAll	= $$( this.options.SelectablesClass );
		
		if( theKeywords.length > 0 ) { // /////////////////////////////////////////////////////////////////////// CORE //
			
			if( event.key == 'down' ) { // Down
				
				if( this.SelectablesCurrent < this.SelectablesAll.length )
					this.SelectablesCurrent = this.SelectablesCurrent + 1;
					
				this.HighlightActive( 'opt_' + this.SelectablesCurrent );
				
			} else if( event.key == 'up' ) { // Up
				
				if( this.SelectablesCurrent > 0 ) {
					
					this.SelectablesCurrent = this.SelectablesCurrent - 1;
					
					if( this.SelectablesCurrent == 0 )
						this.theSearchInput.focus();
				}
				
				this.HighlightActive( 'opt_' + this.SelectablesCurrent );

			} else if( event.key == 'enter' ) { // Enter/Return
				
				if( this.SelectablesCurrent > 0)
					location.href = $$( '#opt_' + this.SelectablesCurrent + ' a' )[0].href;
				else
					this.ValidateSearch();
			
			} else {
				
				var Attributes = '?AJAXRequest=LiveSearch&Keywords=' + theKeywords;
				new Ajax( this.theSearchPage + Attributes, { method: 'get', update: this.theResultContainer } ).request();
				this.SelectablesCurrent = 0;
				this.theSearchInput.fireEvent( 'showResults', 1500 );
				
			}
			
		} else {
			
			if( theKeywords.length != 0 ) {
				this.theSearchWrap.setStyles ({
					'display':	'block',
					'visibility':	'visible'
				});
			}
			
		}
	},
	
	OnFocus: function() {
		if( this.theSearchInput.value.contains( 'Search Our Site:' ) ) this.theSearchInput.value = '';
	},

	OnBlur: function() {
		if( this.theSearchInput.value == '' ) this.theSearchInput.value = 'Search Our Site:';
	},

	ShowSearchResults: function() {
		if( this.theSearchInput.value != 'Search Our Site:' && this.theSearchInput.value != '' ) {
			if( $( 'viewing_ie6' ) ) {
				// account for stupid ie6
				var theCoordinates = this.theSearchInput.getCoordinates();
				this.theSearchWrap.setStyle( 'top',  ( theCoordinates.top + theCoordinates.height + 1 ) );
				this.theSearchWrap.setStyle( 'left', ( theCoordinates.left - 37 ) );
			}
			this.theSearchWrap.setStyles ({
				'display':	'block',
				'visibility':	'visible'
			});
		}
	},
	
	MouseIn: function() {
		this.theSearchWrap.setStyles({
			'display':	'block',
			'visibility':	'visible'
		});
	},
	
	MouseOut: function() {
		this.theSearchWrap.setStyles({
			'display':	'none',
			'visibility':	'hidden'
		});
		this.SelectablesCurrent = 0;
	},

	HighlightActive: function( _id ) {
		
		$$( '#live_search_results li.active' ).removeClass( 'active' );
		
		if( $( _id ) )
			$( _id ).addClass( 'active' );
			
	},
	
	ClickOpen: function() {
		if( this.theSearchForm.Keywords.value != "" && this.theSearchForm.Keywords.value != "Search Our Site:" ) {
			this.theSearchWrap.setStyles({
				'display':	'block',
				'visibility':	'visible'
			});
		}
	},

 	ValidateSearch: function() {
		
		if( this.theSearchForm.Keywords.value == "" || this.theSearchForm.Keywords.value == "Search Our Site:" ){
			
			this.theSearchForm.Keywords.value = "";
			this.theSearchForm.Keywords.focus();
			
		} else {
			
			this.theSearchForm.onsubmit = "";
			this.theSearchForm.submit();
			
		}
	},

	InterfaceSetup: function() {

		this.theSearchForm		=  $( this.options.SearchForm );
		this.theSearchPage		=     this.options.SearchPage;
		this.theSearchWrap		=  $( this.options.SearchWrap );
		this.theResultContainer	=  $( this.options.SearchResults );
		this.theSearchInput		=  $( this.options.SearchInput );
		
		this.SelectablesAll		= $$( this.options.SelectablesClass );
		this.SelectablesCurrent	=     0;

		this.theSearchInput.addEvents({

			'keyup': this.KeyUp.bindWithEvent( this ),
			
			'focus': this.OnFocus.bindWithEvent( this ),
			
			'blur': this.OnBlur.bindWithEvent( this ),
			
			'showResults': this.ShowSearchResults.bind( this ),
			
			'click': this.ShowSearchResults.bind( this ),
			
			'mouseleave': this.MouseOut.bind( this )
			
		});
		
		this.theSearchWrap.addEvent( 'mouseenter', this.MouseIn.bindWithEvent( this ) );
		
		this.theSearchWrap.addEvent( 'mouseleave', this.MouseOut.bindWithEvent( this ) );
		
	}

});


// /////////////////////////////////////////////////////////////////////////////// // OTHER FUNCTIONS //


function ValidateSearch( x ) {
	if( x.Keywords.value == "" || x.Keywords.value == "Search Our Site:" ){
		x.Keywords.value = "";
		x.Keywords.focus();
		return false;
	} else {
		return true;
	}
}


// /////////////////////////////////////////////////////////////////////////////// // IMPLMENTATION //


LiveSearch.implement( new Options );

// DOMReady
window.addEvent( 'domready', function(){
	var theLiveSearch = new LiveSearch();
});
