/* !JQUERY STUFF */

$(document).ready(function() {
	// Filter presentations, speakers.
	$('#event-item-filter input').change(function(){
			update_filtered_content($(this).val(), $(this).attr('checked'));
		});
	
	// Search presentations, speakers.
	$('#event-item-search #search_speakers-title').keyup(function(event){
			update_searched_content($(this).val());
		});
	if($('#search_speakers-title').val() && $('#search_speakers-title').val() != 'start typing...') {
		update_searched_content($('#event-item-search #search_speakers-title').val());
	}
	
	// Make the entire event block clickable.
	$('.upcoming-event').css('cursor', 'pointer').click(function(){
		window.location = $(this).children().children().filter('a').attr('href');
	});
	$('.upcoming-event').hover(
		function(){
			$('#'+$(this).attr('id')+' .upcoming-event-link2').animate({right:'68px'}, 150);
		},
		function(){
			$('#'+$(this).attr('id')+' .upcoming-event-link2').animate({right:'73px'}, 150);
		}
	);

	$(window).load(function(){
		if($('#feature-img').length == 1) {
			// Load and fade in the feature image.
			var img = new Image();
			img.src = $('#feature-img .feature-img-url').text();
			if(!img.complete) { // IE apparently doesn't do .complete
				jQuery(img).bind('error load onreadystatechange', show_feature);
			} else {
				show_feature();
			}
		}
	});
	
});



/* !FUNCTIONS */


function show_feature() {
	$('#feature-img').css({'background':'url('+$('#feature-img .feature-img-url').text()+') no-repeat center top'}).height($(document).height()-20).fadeIn('slow', function(){
		$('#feature-img-credit-wrapper').fadeIn('slow');
	});
}

function update_searched_content(what) {
	var filter_items = $('.filtered-content .event-item');
	var hidden = 0;
	jQuery.each(filter_items, function(){
		var title = $('#' + $(this).attr('id') + ' .event-item-title').text().toLowerCase();
		if(title.indexOf(what.toLowerCase()) != -1) {
			$(this).show('normal');
		} else {
			$(this).hide('normal');
			hidden++;
		}
	});
	$('.filtered-content .message').remove();
	if(hidden == filter_items.length) {
		$('.filtered-content').prepend('<div class="message">No results for <strong>'+what+'</strong></div>');
	}
}

function update_filtered_content(what, show) {
	var filter_items = $('.filtered-content .event-item');
	var hidden = 0;
	jQuery.each(filter_items, function(){
		if($(this).hasClass(what) == true && show == false) {
			$(this).hide('normal');
			hidden++;
		} else if($(this).hasClass(what) == true && show == true) {
			$(this).show('normal');
		}
	});
}

