var content = [
		"http://movies.apple.com/media/us/science/2009/apple-science-manchester-us-20090611_480x272.mov",
		"http://movies.apple.com/media/us/science/2009/profiles/apple_science_cell-signaling-technology-us-20090121_480x272.mov",
		"http://movies.apple.com/media/us/science/2009/profiles/apple_science_mark-williams-us-20090209_480x272.mov",
		"http://movies.apple.com/media/us/science/2009/profiles/apple_science_dr-edward-zaragosa-us-20090121_480x272.mov",
		"http://movies.apple.com/media/us/science/2008/profiles/apple_science_flicek-us-20081209_480x272.mov",
		"http://movies.apple.com/media/us/science/2008/profiles/apple_science_dr-graham-kent-us-20080815_480x272.mov",
		"http://movies.apple.com/media/us/science/2008/profiles/apple_science_dr-gary-spiegel-us-20080529_480x272.mov",
		"http://movies.apple.com/media/us/science/2008/profiles/apple_science_gazzaley-us-20081030_480x272.mov",
		"http://movies.apple.com/media/us/science/2008/profiles/apple_science_dr-robert-lue-us-20080815_480x272.mov"
];

var ads = [
		"http://movies.apple.com/media/us/ipad/2010/ads/apple-ipad-ad-what_is_ipad-us-20100512_480x272.mov",
		"http://movies.apple.com/media/us/ipad/2010/ads/apple-ipad-ad-meet_ipad-us-20100403_480x272.mov",
		"http://movies.apple.com/media/us/mac/macbook/2009/ads/apple-macbook-ad-green_battery-us-20090106_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2008/ads/cromag/apple_iphone3g_ad_cromag-us-20080821_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2010/ads/apple-iphone3gs-ad-family_man-us-20100420_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2010/ads/apple-iphone3gs-ad-dog_lover-us-20100419_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2010/ads/apple-iphone3gs-ad-backpacker-us-20100419_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-pass_test_pass_time-us-20090921_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-multi_people-us-20091123_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-copy_and_paste-us-20090706_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3g-ad-student-us-20090406_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3g-ad-fix-us-20090126_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-break_in-us-20090615_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-share-us-20090812_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-voice_control-us-20090630_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-nature_lovers-us-20090921_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3g-ad-read-us-20090125_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3g-ad-itchy-us-20090406_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-skateboard-us-20090701_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-wheres_the_movie-us-20091123_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3g-ad-office-us-20090406_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-dine_in_dine_out-us-20090921_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-travel-us-20090803_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-song-us-20091116_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-avid-us-20090820_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3g-ad-check-us-20090127_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-gift-us-20091116_480x272.mov",
		"http://movies.apple.com/media/us/iphone/2009/ads/apple-iphone3gs-ad-12_apps_of_christmas-us-20091216_480x272.mov",
		"http://movies.apple.com/media/us/ipoditunes/touch/2009/ads/apple-ipodtouch-more_fun-us-20090216_480x272.mov"
];

var video = document.getElementById('video');
var adDurationTxt = document.getElementById('ad_duration');
var adBug = document.getElementById('ad_bug');
var isAdPlaying = false;
var isAdDurationSticky = false;
var adDurationStickyTime = 9; // In seconds; Time at which the "time remaining" element stays on the screen.
var hasAnythingBeenPlayedYet = false;

// Setup event listeners so we can react to different HTML Media Element events
// Full list of media element event listeners at:
// http://developer.apple.com/safari/library/documentation/AudioVideo/Reference/HTMLMediaElementClassReference/HTMLMediaElement/HTMLMediaElement.html

video.addEventListener('ended', function (event) { 
	if (isAdPlaying) {
		loadContent(this, content);
		setIsAdPlaying(false);
	} else {
		loadAd(this, ads);
		setIsAdPlaying(true);
	}
 }, false);
video.addEventListener('mouseover', function (event) { showTimeRemaining(); }, false);
video.addEventListener('mouseout', function (event) { hideTimeRemaining(); }, false);
// There aren't any hover events on the iOS devices, so let's have a single finger tap behave like a mouse hover.
video.addEventListener('mousedown', function (event) { (adDurationTxt.style.opacity > 0) ? hideTimeRemaining() : showTimeRemaining(); }, false);

video.addEventListener('timeupdate', function (event) { updateAdTimeRemaining(this, adDurationTxt); }, false);

function loadContent (video, content) {
	setIsAdPlaying(false);
	//Randomly pick a piece of content from our array.
	video.src=content[Math.floor(Math.random()*content.length)];
	video.load();
	if (hasAnythingBeenPlayedYet) {
		video.play();
	}
	video.controls = true;
}

function loadAd (video, ads) {
	hasAnythingBeenPlayedYet = true;
	//Randomly pick an advertisment from our array.
	video.src=ads[Math.floor(Math.random()*ads.length)];
	video.load();
	video.play();
	// Disable controls -- advertisers really seem to like this.
	video.controls = false;
	setIsAdPlaying(true);
}

function updateAdTimeRemaining (video, remainingTimeElement) {
	var secondsRemaining = video.duration - video.currentTime;
	remainingTimeElement.innerHTML = convertStoHMS(secondsRemaining);
}

function showTimeRemaining () {
	if (isAdPlaying) {
		adDurationTxt.style.opacity = 0.5;
	}
}

function hideTimeRemaining () {
	if ((isAdPlaying) && (!isAdDurationSticky)) {
		adDurationTxt.style.opacity = 0.0;
	}
}

function convertStoHMS (secs) {
	var t = new Date(1970,0,1);
	t.setSeconds(secs);
	var HMS = t.toTimeString().substr(0,8);
	
	var hours   = parseInt(t.toTimeString().substr(0,2), 10);
	var minutes = parseInt(t.toTimeString().substr(3,2), 10);
	var seconds = parseInt(t.toTimeString().substr(6,2), 10);
	
	
	// Humanize time.
	var string;
	if (hours === 0) {
		if (minutes !== 0) {
			if (seconds > 40) {
				string = "Less than " + (parseInt(minutes, 10) + 1) + " minutes remaining..."; 
			} else if (seconds > 25) {
				string = "About " + minutes.toString().replace(/^0/,"") + " and a half minutes remaining..."; 
			} else if (seconds >= 0) {
				string = "Around " + minutes.toString().replace(/^0/,"") + " minutes remaining..."; 
			}
		} else {
			if (seconds > 40) {
				string = "Less than a minute remaining..."; 
			} else if (seconds >= adDurationStickyTime.toString()) {
				string = "About half minute remaining..."; 
			} else if (seconds >= 2) {
				showTimeRemaining();
				string = seconds.toString().replace(/^0/,"") + " seconds remaining...";
				isAdDurationSticky = true;
			} else if (seconds >= 1) {
				hideTimeRemaining();
				isAdDurationSticky = false;
				string = seconds.toString().replace(/^0/,"") + " seconds remaining..."; 
			} else {
				hideTimeRemaining();
				string = "";
			}
		}
	}
	return string;
}

function setIsAdPlaying (bool) {
	isAdPlaying = bool;
	
	if (bool) {
		setTimeout ( function () { adBug.style.webkitTransitionDuration = "0.8s"; adBug.style.opacity = 0.5; }, 2000 );
	} else {
		setTimeout ( function () { adBug.style.webkitTransitionDuration = "0.2s"; adBug.style.opacity = 0.0; }, 0 );
	}
}

window.onload = function () {
	loadContent(video, content);
}
