
jQuery(document).ready(function ()
{
	var currentSlide = 0;
	
	function fb_isphoneagent()
	{
		try
		{
			return ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)));
		}
		catch (e) 
		{ 
			return false; 
		}
	}
	
	function fb_animate_to(elem, x)
	{
		if (elem.style.webkitTransform !== undefined)
		{
			/* Safari and Chrome */
			elem.style.webkitTransform = "translate3d(" + x.toString() + "px, 0, 0)";
		}
		else
		{
			/* All other browsers */
			jQuery(elem).animate({ left: x }, { duration: 550 });
		}
	}
	
	function fb_update_slider(index)
	{
		if (index == currentSlide)
		{
			return;
		}

		var elem = jQuery("#slider-inner").get(0);
	
		var slideWidth = 1024;
		
		fb_animate_to(elem, index * -slideWidth);
		
		/* Parallax the bg-corner */
		var bgelem = jQuery("#bg-corner").get(0);
		fb_animate_to(bgelem, index * -400);
		
		/* Slide out the logo if we're not slide 0, fade it in if we are. */
		var logoelem = jQuery("#logo").get(0);
		fb_animate_to(logoelem, index * -250 + 20);
		
		var captions = jQuery("div.slide-caption");
		jQuery(captions[currentSlide]).fadeOut();
		
		jQuery(captions[index]).delay(550).queue(function (next)
		{
			if (currentSlide === 0)
			{
				jQuery("#content-caption").removeClass("secondpage");
			}
			else
			{
				jQuery("#content-caption").addClass("secondpage");
			}
			next();
		}).fadeIn();

		jQuery("#main-slide-nav ul li a.selected").removeClass("selected");
		jQuery(jQuery("#main-slide-nav ul li a")[index]).addClass("selected");

		if (index == 4)
		{
			jQuery("#content-email").fadeIn();
		}
		else
		{
			jQuery("#content-email").fadeOut();
		}
		
		currentSlide = index;
	}
	
	jQuery("div.slide-caption").hide();
	jQuery(jQuery("div.slide-caption")[currentSlide]).show();
	jQuery("#content-email").hide();
	
	jQuery("#commentForm").submit(function ()
	{
		// TODO: Validate the email address, but the api validates anyway.
		jQuery.post("/api-emailupdate", { email: jQuery("#email").val() },
			function (data, textStatus, request)
			{
				try
				{
					if (data.indexOf("thankyou") != -1)
					{
						jQuery("#commentForm").replaceWith("<p class='thankyou'>Thank you.</p>");
					}
				}
				catch (e)
				{
					jQuery("#commentForm").replaceWith("<p class='thankyou'>Sorry, an error occurred, please try again.</p>");
				}
			});
		return false;
	});
	
	if (!fb_isphoneagent())
	{
		/* Desktop version allows sliding around. */

		jQuery("#nextslide").click(function ()
		{
			fb_update_slider((currentSlide + 1) % 5);
			return false;
		});
		
		jQuery("#slider").click(function ()
		{
			fb_update_slider((currentSlide + 1) % 5);
			return false;
		});
	
		jQuery('#main-slide-nav ul li a').each(function (navIndex, elem)
		{
			jQuery(elem).click(function(e)
			{
				fb_update_slider(navIndex);
				return false;
			});
		});
	
		jQuery("#learnmore").fancybox({ width: 640, height: 385 });
	}
	else
	{
		/* Mobile version links directly to YouTube. */
		jQuery("#learnmore").attr("href", "http://www.youtube.com/v/v2vpvEDS00o");
	}
	
	if (top.location != self.location) 
	{
		top.location = self.location.href;
	}
});


