/* !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_modal_window(href, title) {
	//alert('title:'+title+', href:'+href);
	if(title == undefined) {
		title = '';
	}
	// ThickBox seems to add pixels, at least using the iframe, so enter the target dimensions and let just let the math do it's thing.
	var w = 504;
	var h = 284;
	//tb_show(title, href+'?KeepThis=true&TB_iframe=true&height='+(h - 12)+'&width='+(w - 29));
	$('#modal_content').remove();
	$('<div id="modal_content"><object width="504" height="284"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="'+href+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=e62727&amp;fullscreen=1" /><embed src="'+href+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=e62727&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="504" height="284"></embed></object></div>').appendTo('body');
	tb_show(title, '#TB_inline?inlineId=modal_content&height='+(h + 5)+'&width='+(w + 0));
}

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('fast');
	});
}

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');
		}
	});
}

/* !COOKIES */
function set_cookie(name, value, days) {
	if(days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function read_cookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function erase_cookie(name) {
	createCookie(name, "", -1);
}


/* !SIFR */
var AvenirBook = {src:'/_assets/flash/fonts/AvenirBook.swf'};
var AvenirLight = {src:'/_assets/flash/fonts/AvenirLight.swf'};

sIFR.fromLocal = true;
sIFR.activate(AvenirBook);
sIFR.activate(AvenirLight);

sIFR.replace(AvenirLight, {
	selector: '#content h2'
	,css: [
		'.sIFR-root { color: #303030; background-color: #ffffff; font-size: 28px; letter-spacing: -.75; leading: -8; }',
		'a { color: #303030; background-color: #ffffff; font-size: 28px; letter-spacing: -.75; text-decoration: none; }',
		'a:hover { color: #303030; background-color: #ffffff; font-size: 28px; letter-spacing: -.75; text-decoration: underline; }',
	]
	,wmode: 'transparent'
	,tuneHeight: -4
});

sIFR.replace(AvenirBook, {
	selector: '#sidebar h2'
	,css: [
		'.sIFR-root { color: #ffffff; background-color: #000000; font-size: 25px; letter-spacing: -.75; leading: -6; }',
	]
	,wmode: 'transparent'
	,tuneHeight: -4
});

sIFR.replace(AvenirLight, {
	selector: '.shadow-box h1'
	,css: [
		'.sIFR-root { color: #ffffff; background-color: #424040; font-size: 31px; letter-spacing: -1; leading: -8; }',
	]
	,wmode: 'transparent'
	,tuneHeight: -4
});

sIFR.replace(AvenirLight, {
	selector: '#content h1'
	,css: [
		'.sIFR-root { color: #303030; background-color: #424040; font-size: 31px; letter-spacing: -1; leading: -8; }',
	]
	,wmode: 'transparent'
	,tuneHeight: -4
});

sIFR.replace(AvenirLight, {
	selector: '#footer h1'
	,css: [
		'.sIFR-root { color: #7b7b7b; background-color: #ffffff; font-size: 17px; leading: -4; }',
	]
	,wmode: 'transparent'
	,tuneHeight: -4
});