$(document).ready(function(){
	
	/* Sup-pages */
	
	// First, add support for bookmarkable sup-pages. Determine content ID from location hash
	var hash = window.location.hash.substring(1);
	if (hash && $('div.right#' + hash).length > 0) {
		// Remove class from all <li> tags
		$('div.left ul.menu li').removeClass("current");
		
		// Set class for current menu item
		$("div.left ul.menu li a[href='#"+ hash +"']").parent().addClass("current");
		
		// Correctly set class for content div
		$('div.right.show').removeClass("show");
		$('div.right#' + hash).addClass("show");
		
	};
	
	// Hide all content divs except the current one
	$('div.right').hide();
	$('div.right.show').show();
	
	$('div.left ul.menu a').click(function() {
		var parentItem = $(this).parent().get(0);
		// Only run this if we're not clicking the current section
		if (parentItem.className != "current") {
			// Remove class from all <li> tags
			$('div.left ul.menu li').removeClass("current");
			
			// Set current <li> as class "current"
			$(parentItem).addClass("current");
			
			// Determine id of div we should display
			var divID = $(this).attr("href").substring(1);
			
			// Change classes of content divs
			$('div.content div.right').removeClass("show");
			$('div.content div.right#' + divID).addClass("show");
			
			// Hide screenshot if we're in Overview
			if (divID == 'overview') {
				$('ul.screenshots a').removeClass('current');
				$('#screenshot').fadeOut("fast");
			}

			// Hide all content divs
			$('div.content div.right:visible').fadeOut("fast", function() {
				// Show appropriate content
				$('div.content div.right.show').fadeIn("normal");
				
				// Show QuickTime Loop if we're in Overview
				if (divID == 'overview') {
					$('.quicktimeLoop').fadeIn("normal");
				}
			});
		}
	});
	
	/* QuickTime Loop */
	$('.quicktimeLoop').media({
		width: 257,
		height: 385,
		bgcolor: '#000000',
		autoplay: true,
		params: { loop: true, controller: false },
		caption: false
	});
	
	/* Product Screenshots */
	
	// Create empty image within div.iphone
	$('div.iphone.movie').append('<img id="screenshot" src="" alt="" />');
	$('div.iphone.movie #screenshot').hide();

	// Attach actions to thumbnails
	$('ul.screenshots li a').click(function() {
		var link = $(this);
		
		// Remove "current" class from other thumbnails and add it to this one
		$('ul.screenshots li a').removeClass('current');
		$(this).addClass('current');
		
		// Hide QuickTime loop
		$('.quicktimeLoop:visible').hide();
		
		// Fade out current image
		$('#screenshot').fadeOut("fast", function() {
			// Fade in the correct screenshot
			$('#screenshot').attr('src', link.attr('href')).fadeIn("normal");
		});
		return false;
	});
	
});