jQuery(function($) {	
	
	
	/*
	 *	Klick auf Kategorie und Sprecher abfangen und entsprechende Produkte anzeigen
	 */
	$('div.categories .category, #right #submenu li')
		.hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		})
		.click(function() {
			if($(this).is('.active')) {
				$(this).removeClass('active');
			}
			else {
				$(this).addClass('active');
			}
			filterProducts();
			return false;
		})
	;
	
	filterProducts();

});


function filterProducts() {
	$('li', 'ul.records').removeClass('inactive').addClass('active');
	
	$activeCategories = $('div.categories .category.active');
	$activeSpeaker = $('#submenu li.active', '#right');
	
	if($activeCategories.length == 0 && $activeSpeaker.length == 0) {
		$('li', 'ul.records').removeClass('active').addClass('inactive');
	}
	else {	
		$('.category.active', 'div.categories').each(function() {		
			category_id = $(this).attr('rel');
			$('li:hasNotCategory('+category_id+')', 'ul.records').addClass('inactive').removeClass('active');
		});
		
		$('#submenu li.active', '#right').each(function() {
			speaker_id = $(this).find('span').attr('rel');
			$('li:hasNotSpeaker('+speaker_id+')', 'ul.records').addClass('inactive').removeClass('active');
		});
	}
	
	if($activeCategories.length == 0 && $activeSpeaker.length == 0) {
		$('ul.records li img').animate({opacity: 1}, 400);
	}
	else {
		$('ul.records li.active img').animate({opacity: 1}, 400);
		$('ul.records li.inactive img').animate({opacity: 0.5}, 400);
	}
}


jQuery.extend(jQuery.expr[':'], {
	hasNotCategory: function(a, b, c) {
		return $('.categories[rel*='+c[3]+',]', a).length == 0 ? true : false;
	},
	hasNotSpeaker: function(a, b, c) {
		return $('.speaker[rel*='+c[3]+']', a).length == 0 ? true : false;
	}
});