//Requires jQuery 1.2.3 as Q
//
//filter: what elements are to have their children filtered
//filtride: only in childs of filters, used to filter away the filtrate
//filtrate: the data that is filtered
/*
<select id="id_1" filter="id_2 id_3">
	<option filtride="101 301">one</option>
	<option filtride="102 302">two</option>
	<option filtride="103 303">three</option>
</select>

<select id="id_2" filter="id_3">
	<option filtrate="101" filtride="301">one</option>
	<option filtrate="102" filtride="302">two</option>
	<option filtrate="103" filtride="303">three</option>
</select>

<div id="id_3">
	<div filtrate="301">one</div>
	<div filtrate="302">two</div>
	<div filtrate="303">three</div>
</div>
*/

// logging function for debugging
// use log(params) instead of alert() or console.log()

if (typeof console == "object"){
	log = console.log;
}
else {
	log = function() {};
}

//Called when a filter is changed
filter = function(eventObject){
	//Get the information associated with the changed select menu
	var id = this.id;
	var filters = Q(this).attr('filter').split(' ');
	var filtrides = Q(this)//the DOM element - select
		.children(':selected')//the selected option element of the select 
		.attr('filtride')//space delimited filtrides
		.split(' ');

	//Hide everything if no selection is made
	//Show results otherwise!
	var showContacts = false;
	var officeselect=document.getElementById("filteroffices");
	var departmentselect=document.getElementById("filterdepartments");
	var nameselect=document.getElementById("filternames");
	if(officeselect.selectedIndex>0 || departmentselect.selectedIndex>0 || nameselect.selectedIndex>0) {
		showContacts=true;
	}
	if (showContacts) {
		Q('#search_results').show();
	} else {
		Q('#search_results').hide();
	}
		
	/*if (Q('#filters option:selected').text() == '') {
		Q('#search_results').hide();
	}
	else {
		Q('#search_results').show();
	}*/
	
	//A name has been selected
	//Cancel office and department choices
	//Hide #offices
	//Show #contacts
	if (id == "filternames") {
		Q('#offices').hide();
		Q('#contacts').show();
		Q('#filteroffices').val('');
		Q('#filterdepartments').val('');
		Q('.office').removeClass('filteroffices_filtered');
		Q('.office').removeClass('filterdepartments_filtered');
		Q('.contact').removeClass('filteroffices_filtered');
		Q('.contact').removeClass('filterdepartments_filtered');
	
	//Either an office or department has been selected	
	//Cancel name selection
	//Hide #contacts
	//Show #offices
	} else if (id == "filteroffices" || id == "filterdepartments") {
		Q('#contacts').hide();
		Q('#offices').show();
		Q('#filternames').val('');
		Q('.office').removeClass('filternames_filtered');
		Q('.contact').removeClass('filternames_filtered');
	}
	
	Q.each(filters, function(i, filter){
		var target = Q(filter);
		Q.each(filtrides, function(j, filtride){
			//log('filtride = ' + filtride);
			if (filtride=="?all?") {
				//log('removing tag: ' + id + '_filtered from all things filtred by ' +id);
				target.children('[filtrate]').removeClass(id + '_filtered');
				target.val('');
			} else if (filtride=="?none?") {
				target.children('[filtrate]').addClass(id + '_filtered');
				target.val('');
			} else {
				target.children('[filtrate~=' + filtride + ']').removeClass(id + '_filtered');
				target.children('[filtrate]:not([filtrate~=' + filtride + '])').addClass(id + '_filtered');
				target.val('');
			}
		})
	})
	
	//If selecting by department, filter out offices that have no contacts
	//within the selected department
	/*if (id == "filterdepartments") {
		Q.each(Q('.office'), function(i, office){
			var thisoffice = Q(office);
			Q.each(filtrides, function(j, selecteddept){
				if (selecteddept!='' && thisoffice.find('.contact[filtrate~=' + selecteddept + ']').length < 1) {
					thisoffice.addClass('filterdepartments_filtered');
				} else {
					thisoffice.removeClass('filterdepartments_filtered');
				}
			})
		})
	}*/
	
	//Show the "no results" message if there are no matches, otherwise hide it
	if(Q(".office:not('.filteroffices_filtered'):not('.filterdepartments_filtered'):not('.filternames_filtered')").length > 0) {
		Q('#no_results').hide();
	} else {
		Q('#no_results').show();
	}
}