$(document).ready(function(){
	// setup ajax defaults
	$.ajaxSetup({
		data: 'ajax_frontpage_list.php'
	});
	// load unfiltered results initially
	$('#results').load('ajax_frontpage_list.php');
	$('body#home #photos').load('ajax_frontpage_photos.php');

	// toggle style when label is clicked, and add value to query array
	$("#matchmaker>li>ul>li>a").click(function() {
		if($(this).hasClass('selected')) { 
			$(this).children('img').remove();
			$(this).removeClass('selected');
			makeMatch();
			return false;
		}	
		else { 
			$(this).addClass('selected');
			$(this).append("<img src='gfx/nav-label-close.png' alt='close' class='close' title='verwijder uit selectie' />");
			makeMatch();
			return false;
		}
	});
	// toggle button style for all labels when category button clicked, and add value to query array
	$("#matchmaker>li").click(function() {
		if($(this).hasClass('selected')) { 
			$(this).removeClass('selected');
			$(this).contents().find('img').remove();
			$(this).contents().find('a.selected').removeClass('selected');
			makeMatch();
			return false;
		}
		else {
			$(this).addClass('selected');
			$(this).children('a').append("<img src='gfx/nav-question-close.gif' alt='close' class='close' title='verwijder uit selectie' />");
			$(this).contents().find('a').addClass('selected');
			$(this).contents().find('a.selected').append("<img src='gfx/nav-label-close.png' alt='close' class='close' title='verwijder uit selectie' />");
			makeMatch();
			return false;
		}
	});
});

var showAllMatches = false;
function makeMatch() {	
	// $("#photos li").slideUp(200);
	// define and empty selection arrays	
	var audience = [];
	var location = [];
	var activity = [];
	// fill selection arrays
	$('#audience a.selected').each(function (i) {
		audience.push($(this).attr('name'));
	});
	$('#location a.selected').each(function (i) {
		location.push($(this).attr('name'));
	});
	$('#activity a.selected').each(function (i) {
		activity.push($(this).attr('name'));
	});

	var ajaxcall = 'ajax_frontpage_list.php?audience='+audience+'&location='+location+'&category='+activity;
	if (showAllMatches){
		var ajaxcall = 'ajax_frontpage_list.php?audience='+audience+'&location='+location+'&category='+activity+'&all=all';
	}

	var ajaxphotocall = 'ajax_frontpage_photos.php?audience='+audience+'&location='+location+'&category='+activity;
	if (showAllMatches){
		var ajaxphotocall = 'ajax_frontpage_photos.php?audience='+audience+'&location='+location+'&category='+activity+'&all=all';
	}


	// Slide up now.
	$("#results").slideUp(500);
	$("#photos").slideUp(500);

	// ... but only slide down when the ajax load finished
	$('#results').load(ajaxcall, function(){$("#results").slideDown(200);});
	$("#photos").load(ajaxphotocall, function(){$("#photos").slideDown(200);});
}

// Show/hide all results.
function toggleShowAllMatches() {
	if (showAllMatches === false) {
		showAllMatches = true;
	} else {
		showAllMatches = false;
	}
	makeMatch();
}
