// Video variables 
var videoPlayerWidth = 560;
var videoPlayerHeight = 340;
var videoPlayerMinVersion = "8";
var videoParams = { allowScriptAccess: "always", allowFullScreen: "true", wmode: "transparent" };
var videoAttributes = {};
var isFlash = true;
var autoPlay = false;
var timeMagazineVideo = {
	videoID: "avM3Aor7Ptg",
	videoPath: "http://www.getonthetablet.com/videos/time.mp4",
	videoName: "time-magazine-video-inner",
	videoElement: "#time-magazine-video-inner",
	videoPoster: "images/video-landing-ipad.jpg"
};
var sportsIllustratedVideo = {
	videoID: "ntyXvLnxyXk",
	videoPath: "http://www.getonthetablet.com/videos/si4.mp4",
	videoName: "sports-illustrated-video-inner",
	videoElement: "#sports-illustrated-video-inner",
	videoPoster: ""
};

$(document).ready(function() {
	// Check PC or Mac for font
	if(navigator.appVersion.indexOf("Win") != -1) {
		$("#modules p").css("font-size", "10px");
	}
	
	// Determine user agent 
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		// Play HTML5
		isFlash = false;
		$("#video-player img").remove();
		$(timeMagazineVideo.videoElement).replaceWith($("<video controls poster='" + timeMagazineVideo.videoPoster + "' width=" + videoPlayerWidth + " height=" + videoPlayerHeight + " src='" + timeMagazineVideo.videoPath + "'>Your browser does not support the video tag.</video>"));
		$(sportsIllustratedVideo.videoElement).replaceWith($("<video controls poster='" + sportsIllustratedVideo.videoPoster + "' width=" + videoPlayerWidth + " height=" + videoPlayerHeight + " src='" + sportsIllustratedVideo.videoPath + "'>Your browser does not support the video tag.</video>"));
	} else {
		// Play flash
		swfobject.embedSWF("http://www.youtube.com/v/" + timeMagazineVideo.videoID + "?enablejsapi=1&playerapiid=" + timeMagazineVideo.videoName + "&hl=en_US&fs=1&hd=1", timeMagazineVideo.videoName, videoPlayerWidth, videoPlayerHeight, videoPlayerMinVersion, null, null, videoParams, videoAttributes);
		swfobject.embedSWF("http://www.youtube.com/v/" + sportsIllustratedVideo.videoID + "?enablejsapi=1&playerapiid=" + sportsIllustratedVideo.videoName + "&hl=en_US&fs=1&hd=1", sportsIllustratedVideo.videoName, videoPlayerWidth, videoPlayerHeight, videoPlayerMinVersion, null, null, videoParams, videoAttributes);
	}
	
	// Tabs
	var tabs = $(".video ul li");
	var videoPlayer = $("#video-player");
	var videoContainers = $(videoPlayer).find("div.video-container");
	
	var videos;
	if(!isFlash) {
		videos = $(videoPlayer).find("video");
	}
	
	tabs.find("a").each(function(index, element) {
		var i = index;		
		var self = $(element);
		var parent = self.parent();
		
		$(this).click(function() {
			// Check if you clicked on already active element.  [0] is the DOM element.
			if(parent[0] != $(".video ul li.active")[0]) {
				var hasLanding = $(videoContainers[i]).find("img");
				hasLanding.length == 0 ? autoPlay = true : autoPlay = false;
				
				// Toggle classes
				$(tabs).removeClass("active");
				parent.addClass("active");	
				
				$(videoContainers).removeClass("active");
				$(videoContainers[i]).addClass("active");
								
				// Unhide landing images on all other tabs
				$(videoContainers).each(function(index) {
					if(index != i) {
						$(this).find("img").addClass("visible");
					}
				});			
				
				if(!isFlash) {
					// Pause all videos
					$(videos).each(function() {
						this.pause();
					});
					
					// If it doesn't have a landing image, play the video
					var landing = $(videos[i]).siblings("img");
					if(landing.length == 0) {
						videos[i].play();
					}
				}
			} 
			
			return false;
		});
	});
	
	$(videoContainers).each(function() {
		$(this).find("img").click(function() {
			$(this).removeClass("visible");
			if(isFlash) {
				$(this).siblings("object")[0].playVideo();
			} else {
				$(this).siblings("video")[0].play();
			}
		});
	});
});

function onYouTubePlayerReady(pid) {
	if(autoPlay) {
		var player = document.getElementById(unescape(pid));
		player.playVideo();
	}
}
