/*
Class: Aurora.FilterField
*/
Aurora.FilterField = new Class({
	
	Implements: Options,
	
	options: {
		itemsEl: document,
		itemsQuery: '.aurora-search-item',
		fieldQuery: '.aurora-search-field',
		onInit: null,
		itemsHaveAnyClass: false
	},
	
	initialize: function( target, options )
	{
		var $el = this.$el = $( target );
		
		this.setOptions( options );
		
		this.originalItemsQuery = this.options.itemsQuery;
		
		this._lastSearch = '';
		
		var itemsEl = this.itemsEl = this.options.itemsEl;
		
		if ( itemsEl != document )
			itemsEl = this.itemsEl = document.getElement( this.options.itemsEl );
		
		this.doSearch();
		
		if ( this.options.onInit )
			Aurora.doCallback( this.options.onInit );
	},
	
	setQuery: function( newQuery )
	{
		this.itemsEl.getElements( this.originalItemsQuery ).hide();
		
		if ( !newQuery )
			newQuery = this.originalItemsQuery;
		
		this.options.itemsQuery = newQuery;
		
		this.doSearch( this._lastSearch );
	},
	
	initSearchData: function()
	{
		var searchData = [];
		
		this.itemsEl.getElements( this.options.itemsQuery ).each( (function( $item ) {
			
			var terms = '';
			
			$item.getElements( this.options.fieldQuery ).each( function( $term ) {
				terms += $term.innerHTML + ' |||| ';
			});
			
			searchData.push({ terms: terms, $el: $item });
			
		}.bind( this )));
		
		this._searchData = searchData;
	},
	
	doSearch: function( searchString )
	{
		if ( !this._searchData )
			this.initSearchData();
		
		var searchData = this._searchData;
		
		
		// Store the last search
		this._lastSearch = searchString;
		
		
		// If the search string is blank and the query is the same as we started, show everything and return
		if ( ( !searchString || !$chk( searchString.trim() ) ) && ( this.originalItemsQuery == this.options.itemsQuery ) )
		{
			searchData.each( function( i ) {
				i.$el.show();
			});
			
			return;
		}
		
		// Clean up search string
		if ( searchString )
			searchString = searchString.trim().escapeRegExp();
		
		
		// Determine class for detection
		var queryClass = this.options.itemsQuery.replace( '.', '', 'g' );
		
		// Loop over the data and show and hide elements
		searchData.each( (function( i ) {
			
			if ( this.options.itemsHaveAnyClass )
			{
				// Seperate the items classes into an array
				var elClasses = i.$el.get( 'class' );
				
				if ( elClasses )
					elClasses = elClasses.split( ' ' );
				else
					elClasses = [];
				
				var queryClasses = queryClass.split( ' ' );
				
				if ( searchString )
				{
					i.$el.hide();
					
					if ( i.terms.test( searchString, "i" ) )
					{
						if ( !queryClasses || !queryClasses.length )
							i.$el.hide();
						else
						{
							i.$el.hide();
							
							queryClasses.each( function(qc) {
								
								elClasses.each( function(ec) {
								
									if ( ec == qc )
										i.$el.show();
								
								});
								
							});
						}
					}
				}
				else
				{
					if ( !queryClasses || !queryClasses.length )
						i.$el.hide();
					else
					{
						i.$el.hide();
						
						queryClasses.each( function(qc) {
							
							elClasses.each( function(ec) {
							
								if ( ec == qc )
									i.$el.show();
							
							});
							
						});
					}
				}
			}
			else
			{
				if ( searchString )
				{
					if ( i.terms.test( searchString, "i" ) && i.$el.hasClass( queryClass ) )
						i.$el.show();
					else
						i.$el.hide();
				}
				else
				{
					if ( i.$el.hasClass( queryClass ) )
						i.$el.show();
					else
						i.$el.hide();
				}
			}
			
		}.bind( this )));
	}
	
});
