$(document).ready(function(){

	/* This is the back button friendly tab switcher */
	var speakContainers = $('.switcher > .speaker');
  speakContainers.hide().filter(':first').show();
  
  $(window).bind('hashchange', function () {
    hash = window.location.hash || '#dabramowicz';
    
    speakContainers.hide();
    speakContainers.filter(hash).show();
    $('.bullpen a').removeClass('active');
    $('a[hash='+hash+']').addClass('active');
  });
  
  /* 37 is prev, 39 is next */
	$("#speakers_page").keyup(function(e){
		key = e.keyCode ? e.keyCode : e.which;
		
		index = $('.bullpen a').index($('a.active'));
		total = $('.closer').length - 1;
		
		if (index > 0 && index < total) {
			$prev = $('.bullpen a:eq('+(index - 1)+')');
			$next = $('.bullpen a:eq('+(index + 1)+')');
		} else {
			if (index == 0) {
				$prev = $('.bullpen a:eq('+total+')');
				$next = $('.bullpen a:eq('+(index + 1)+')');
			} else if (index == total) {
				$prev = $('.bullpen a:eq('+(index - 1)+')');
				$next = $('.bullpen a:eq(0)');
			}
		}
		
		$prev_hash = $prev.attr("href");
		$next_hash = $next.attr("href");
		
		if (key == 37 || key == 39) {
			if (key == 37) {
				$('a[hash='+$prev_hash+']').click();
			} else if (key == 39) {
				$('a[hash='+$next_hash+']').click();
			}
		}
	});
  
  $(window).trigger("hashchange");
	
});
