$(document).ready(function() {
	
	var terms = [];
	var all = $('.listings').html();
	var $filters = $('#filter a').not($('#recent a'));
	$filters.prepend('<img src="/wp-content/themes/prestige/images/x.png">');
	
	$filters.click(function() {
		terms = [];
		var search = '';
		$(this).toggleClass('selected');
		$filters.filter('.selected').each(function(i){
			terms.push($(this).text().split('/')[0].replace(/\s$/, ''));
		});
		$.each(terms, function(i, val) {
			search += (search == '' ? '' : '+') + val.replace(/\s-\s|\s/g, '-').toLowerCase();
		});
		if(terms.length > 0) {
			$.get('/?tag=' + search, function(data) {
				$('.listings').html(data);
			});	
		} else {
			$('.listings').html(all);
		}
		return false;
	});
	
	$('.home .feature .links a').click(function() {
		$('.selected').removeClass('selected');
		$(this).addClass('selected');
		$('#photo ul').animate({ left: -($(this).index() * 621) }, 500);
		$('.feature .info ul').animate({ left: -($(this).index() * 240) }, 500);
		$('.home .feature .next').attr('href', parseInt($(this).index()) + 1);
		return false;
	});
	
	$('.home .feature .next').click(function() {
		$('#photo ul').animate({ left: -($(this).attr('href') * 621) }, 500);
		$('.feature .info ul').animate({ left: -($(this).attr('href') * 240) }, 500);
		var next = (parseInt($(this).attr('href')) === 2) ? 0 : parseInt($(this).attr('href')) + 1;
		$(this).attr('href', next);
		$('.selected').removeClass('selected');
		$('.home .feature .links a').eq(parseInt($(this).attr('href')) - 1).addClass('selected');
		return false;
	});

});