// super duper speedy search filter!!
// working with jQuery quicksearch this script toggles to visual state
// of the buttons within 3 categories and a show all button
// then populates the quicksearch field with the appropriate tag to trigger the search
// Paul Evans, PEDesign, Oct 2009

jQuery(document).ready(function () {
	// activate quicksearch
	jQuery('.searchtags').quicksearch({
		position: 'before',
		attached: '#sh_container',
		labelText: '',
		hideElement: 'parent',
		formId:'filterer',
		loaderImg:'',
		labelText: '',
		onAfter: function() {checkResults(jQuery('#freetext').val())},
		randomElement: 'freetext'
	});
	// set the click function for the buttons
	jQuery('.filter').click(function() {
		if (!jQuery(this).is('.selected') ) {
			jQuery(this).addClass('selected');
			jQuery(this).siblings().removeClass('selected');
			var thisTag = jQuery(this).attr('tag');
			updateSearchField(thisTag,1);
			jQuery(this).siblings().each(function() {
				removeTag = jQuery(this).attr('tag');
				updateSearchField(removeTag,0);
			});
		} else {
			jQuery(this).removeClass('selected');
			var thisTag = jQuery(this).attr('tag');
			updateSearchField(thisTag,0);
		}
	});
});

// function to populate the quicksearch field based on the button clicked
// passes 'tag' which is the text clicked
// passes 'type' which specifies whether to add(1) or delete(2) it from the field
function updateSearchField(tag,type) {
	if (tag == 'all') {
		var newFilters = '';
		jQuery('#search-filters .filter').removeClass('selected');
		jQuery('#all-option').addClass('selected');
	} else {
		jQuery('#all-option').removeClass('selected');
		// get the current search string from the field
		var currentFilters = jQuery('#freetext').val();
		// check if we are adding(1) or removing(2) from type parameter
		if (type) {
			// add the new text plus a space to the search string, from tag parameter
			var newFilters = currentFilters+tag+' ';
		} else {
			var re = new RegExp(tag+' ', 'gi');
			var newFilters = currentFilters.replace(re,'');
		}
	}
	jQuery('#freetext').val(newFilters);
	jQuery('#freetext').keydown();
	if (newFilters == '') {
		jQuery('#all-option').addClass('selected');	
	}
}

function checkResults(term) {
	var results = 0;
	if ( term != "" ) {
		term = term.slice(0, -1);
		var aTerm = term.split(' ');
		jQuery('#search-feedback').hide();
		var matches = 0;
		
		jQuery('.searchtags').each( function() {
			var tags = jQuery(this).html();
			var termFound = 0;
			for ( i=0; i < aTerm.length; i++ ) {
				if (tags.indexOf(aTerm[i]) > -1) {
					termFound = termFound+1;
				}
			}
			if (termFound == aTerm.length) {
				results = 1;
				matches = matches+1;
			}
		});
		if (results == 1) {
			jQuery('#search-feedback').html('' + matches + ' Produkte gefunden.');
		} else {
			jQuery('#search-feedback').html('Entschuldigung, wir haben keine passenden Produkte gefunden.');
		}
	} else {
		jQuery('#search-feedback').html('Dir werden alle Produkte angezeigt.');
	}
	jQuery('#search-feedback').fadeIn();
}	   

