$(document).ready(function() {
	var content = $("#content-bg");
	var aboutBox = $("#aboutBox");
	var contactBox = $("#contactBox");
	var feedbackBox = $("#feedbackBox");
	var noticeBox = $("#noticeBox");
	
	var firstImage = $("#firstImage");
	var secondImage = $("#secondImage");
	var imageLabel = $("#slideshowLabel");
	
	var shots = [
	             { src: "shot3-search.png", 				label:"Search page" },
	             { src: "shot4-searchresults.png", 			label:"Search results" },
	             { src: "shot1-movie.png", 					label:"Movie page" },
	             { src: "shot1b-poster.png", 				label:"Movie poster" },
	             { src: "shot5-dnascore.png", 				label:"DNA Score details" },
	             { src: "shot2-best.png", 					label:"Best movies Top 100" },
	             { src: "shot6b-twitteropinion.png", 		label:"Movie on Twitter" },
	             { src: "shot7-tweet.png", 					label:"A single movie tweet" },
	             { src: "shot8-retweet.png", 				label:"Retweet screen" },
	             { src: "shot10-tweetmovie.png", 			label:"Tweet a movie screen" },
	             { src: "shot9-images.png", 				label:"More movie images" },
	             { src: "shot4b-awards.png",				label:"Movie awards and nominations" },
	             { src: "shot5a-reviews.png", 				label:"Rotten Tomatoes reviews" },
	             { src: "shot5b-review.png", 				label:"A review" },
	             { src: "shot5c-videos.png", 				label:"Movie trailers and videos" },
	             { src: "shot6a-actress.png", 				label:"Actress page" },
	             { src: "shot11-search-black.png", 			label:"Search page (Black theme)" },
	             { src: "shot12-favorites-black.png", 		label:"Favorite page (Black theme)" },
	             { src: "shot13-actor-black.png", 			label:"Actor page (Black theme)" },
	             { src: "shot14-best-black.png", 			label:"Best mvoies (Black theme)" },
	             { src: "shot14-popular-black.png", 		label:"Popular movies (Black theme)" },
	             { src: "shot15-tweet-black.png", 			label:"Tweet page (Black theme)" },
	             { src: "shot16-movie-pink.png", 			label:"Movie page (Pink theme)" },
	             { src: "shot17-actor-pink.png", 			label:"Actor page (Pink theme)" },
	             { src: "shot19-dnascore.png", 				label:"DNA Score (Pink theme)" },
	             { src: "shot20-twitteropinion-pink.png", 	label:"Movie on Twitter (Pink theme)" },
	             { src: "shot21-images-pink.png", 			label:"More images (Pink theme)" }
	];
	
	var preload = [
		"feedbackLabel.png",
		"feedbackLabelHover.png"
	];
	
	var currentShot = 0;
	var currentImage = firstImage;
	
	imageLabel.html(shots[0].label + " (" + (1) + "/" + shots.length + ")");
	
	// Preload screenshots.
	for( var i = 0; i < shots.length; ++i ) {
		var shot = new Image();
		shot.src = "img/" + shots[i].src;
	}
	
	// Preload other images.
	for( var i = 0; i < preload.length; ++i ) {
		var img = new Image();
		img.src = "img/" + preload[i];
	}
	
	// Make email images clickable.
	$(".mailLink").each(function() {
		$(this).css("cursor", "pointer");
		$(this).click(sendMail);
	});
	
	// Make external links open in new window.
	$("a.external").attr("target", "_blank");
	
	function sendMail() {
		var m = ['l','n','.','s','t','c','e','j','o','r','p','8','@','a','n','d','e','i','v','o','m'];
		window.location.href = "mailto:" + m.reverse().join('') + "?subject=MovieDNA Feedback";
		return false;
	}
	
	function toggleBox(box) {
		if (box == contactBox) {
			aboutBox.hide();
			feedbackBox.hide();
			noticeBox.hide();
			
		} else if (box == aboutBox) {
			contactBox.hide();
			feedbackBox.hide();
			noticeBox.hide();
			
		} else if (box == feedbackBox) {
			contactBox.hide();
			aboutBox.hide();
			noticeBox.hide();
			
		} else if (box == noticeBox) {
			contactBox.hide();
			aboutBox.hide();
			feedbackBox.hide();
		}
		
		box.slideToggle("fast");
	}
		
	function showImage(nextShot) {
		var nextImage = currentImage == firstImage ? secondImage : firstImage;
		nextImage.attr("src", "img/" + shots[nextShot].src);
				
		nextImage.fadeIn();
		currentImage.fadeOut();
		imageLabel.html(shots[nextShot].label + " (" + (nextShot+1) + "/" + shots.length + ")");	
		
		currentShot = nextShot;
		currentImage = nextImage;
	}
	
	function showPreviousImage() {
		var nextShot = --currentShot;
		if (nextShot < 0) {
			nextShot = shots.length - 1;
		}
		showImage(nextShot);
		return false;
	}
	
	function showNextImage() {
		var nextShot = ++currentShot;
		if (nextShot >= shots.length) {
			nextShot = 0;
		}
		showImage(nextShot);
		return false;
	}
	
	function showDownloadInfo() {
		alert("This link will be enabled as soon as Apple has reviewed MovieDNA and released it into the App Store!");
		return false;
	}
	
	$("#aboutLink").click(function() { toggleBox(aboutBox); return false; });
	$("#aboutLink2").click(function() { toggleBox(aboutBox); return false; });
	aboutBox.click(function(e) { if (e.target.tagName.toLowerCase() != "a") { toggleBox(aboutBox); } });

	$("#contactLink").click(function() { toggleBox(contactBox); return false; });
	contactBox.click(function(e) { if (e.target.tagName.toLowerCase() != "a") { toggleBox(contactBox); } });
	
	$("#feedbackLink").click(function() { toggleBox(feedbackBox); return false; });
	feedbackBox.click(function(e) { if (e.target.tagName.toLowerCase() != "a") { toggleBox(feedbackBox); } });
	
	$("#newsLink").click(function() { toggleBox(noticeBox); return false; });
	$("#newsLink2").click(function() { toggleBox(noticeBox); return false; });
	noticeBox.click(function(e) { if (e.target.tagName.toLowerCase() != "a") { toggleBox(noticeBox); } });

	$("#slideLeft").click(showPreviousImage);
	$("#slideRight").click(showNextImage);
	
	$("#firstImage").click(showNextImage);
	$("#secondImage").click(showNextImage);
});

