var Presentation = {
	
	container: {},
	
	init: function() {
		// add a class the presentation wrapper to mark scrollable availability (affects CSS)
		Presentation.container = $("#presentation").addClass("scrollable");		
		
		
		// prep the slides
		Presentation.initSlides();
		
		
		// prep the thumbs
		Presentation.initThumbs();
		
		
		// remove the focus [effect] from clicked navigation links (for later navigating via keyboard, etc…)
		$("#slidesMenu, .slideThumbs").delegate("a", "click", function(event) {
			$(this).blur();
		});
	},
	
	initSlides: function() {
		// set a height for the wrapper based on the tallest slide
		var slidesHeight = 0;
		
		$(".slide", "#slides").each(function() {
			// check the height to see if it's the tallest (so far)
			var height = $(this).height();
		
			if (height > slidesHeight) {
				slidesHeight = height;
			}
			
			// remove the slides' IDs to stop the menu links from jumping to the slides ID target by default
			$(this).attr("id", "");
		});
		
		Presentation.container.height(slidesHeight);
		
		// initialize the scroller
		Presentation.container
			.scrollable({
				// loop continuously between first & last slides (doesn't work well with our photo switching; flickers)
				circular: false,
				
				// use mousewheel to scroll between slides
				mousewheel: false
			})
			.navigator({
				// jQuery selection to be used as navigator
				navi: "#slidesMenu",
		
				// select A tags inside the navigator to work as items (not direct children)
				naviItem: 'a',
				
				// make browser's back button work (! freaks out IETester)
				history: true
			});
	},
	
	initThumbs: function() {
		// add a click handler for the slides' thumbs
		$(".slide .thumbs").delegate("a", "click", function(event) {
			event.preventDefault();
			
			var link = $(this);
			
			// see if same thumb is being clicked
			if (link.hasClass("active")) {
				return;
			}
			
			// locate the parent slide's main photo
			var photo = link.closest(".slide").find(".slidePhoto");
			
			// swap out the old and new photos
			photo
				// create a load handler
				.load(function(){
					// fade in the photo
					$(this).fadeTo('slow', 1);
					
					// clear the load handler
					$(this).unbind('load');
				})
				
				// fade out the current image
				.fadeTo('fast', 0, function() {
					// load the new image
					$(this).attr('src', link.attr("href"));
				});
			
			// mark the thumb as active
			link.closest(".thumbs").find(".active").removeClass("active")
			link.addClass("active");
		});
		
		
		// init scrolling
		$(".slideThumbs").scrollable({
			keyboard: false,
			next: ".nextThumbs",
			prev: ".prevThumbs"
		});
	}
};

Presentation.init();
