var video = document.createElement("video");
var videoTagSupport = !!video.canPlayType; // note double negation
var sides = [
	"01Baguette",
	"02Rolls",
	"03Rolls_Multiple",
	"04Rolls_Multiple_Above",
	"05Corn",
	"06MashedPotatoes",
	"07Corn_Bucket",
	"08GreenBeans",
	"09Peas_Carrots",
	"10FruitSalad",
	"11TrishAppear",
	"12TrishPeck",
	"13TrishArms",
	"14Pie",
	"15Salad",
	"16Bored01",
	"17Bored02",
	"18Bored03",
	"19Spaghetti",
	"20Gravy",
	"21Back"
];
var behindTheScenes = [
	"23BTS_Corn",
	"24BTS_Green Beans",
	"25BTS_Fruit Salad",
	"26BTS_Spaghetti",
	"27BTS_01",
	"28BTS_02",
	"29BTS_03",
	"30BTS_04",
	"31BTS_05",
	"32BTS_06",
	"33BTS_07",
	"34BTS_08",
	"35BTS_09",
	"36BTS_10",
	"37BTS_11"
]
var egg = ["22All"];

function load(video) {
	replaceVideo(video, false);
}
function loadLoop() {
	replaceVideo('00IntroLoop', true);
}
function loadRandom(list) {
	var index = Math.floor(Math.random() * list.length);
	load(list[index]);
	return list[index];
}
function replaceVideo(video, loop) {
	$("#video-area").empty();
	if (videoTagSupport && (!$.browser.mozilla || ($.browser.mozilla && parseInt($.browser.version) > 3))) {
		var html = '<video id="video" class="video-js" width="550" height="462" ' + ((loop) ? 'loop="true" onended="this.play()"' : '') + ' preload="auto" autoplay nocontrols>';
		if ($.browser.webkit) {
			html += '<source id="mp4" src="videos/mp4/' + video + '.mp4" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />';
		} else {
			html += '<source id="mp4" src="videos/mp4/' + video + '.mp4" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />';
			html += '<source id="webm" src="videos/webm/' + video + '.webm" type=\'video/webm; codecs="vp8, vorbis"\' />';
			html += '<source id="ogv" src="videos/ogv/' + video + '.ogv" type=\'video/ogg; codecs="theora, vorbis"\' />';
		}
		html += '</video>';
		$("#video-area").html(html);
		var player = VideoJS.setup('video');
		if (loop) {
			player.addVideoListener('ended', function () { player.play(); });
		} else {
			player.addVideoListener('ended', loadLoop);
		}
	} else {
		$f("video-area",
			{
				"src": "http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf",
				"height": 462,
				"width": 550,
				"wmode": "transparent"
			},
			{
				"clip": {
					"url": "videos/flv/" + video + ".flv",
					"autoPlay": true,
					"autoBuffering": true,
					"onBeforePause": function() {
						return false;
					},
					"onBeforeFinish": function () {
						if (loop) {
							this.seek(0);
						} else {
							loadLoop();
						}
						return false;
					}
				},
				"plugins":{
					"controls": null
				}
			}
		);
	}
}
function googleTrack(label, video) {
	// _trackEvent(category, action, opt_label, opt_value, opt_noninteraction)
	_gaq.push(['_trackEvent', 'Video', label, video]);
}

$(document).ready(function() {
	
	$("#add-side a").click(function () {
		var v = loadRandom(sides);
		googleTrack('Add a Side', v);
		return false;
	});
	$("#behind a").click(function () {
		var v = loadRandom(behindTheScenes);
		googleTrack('Behind the Scenes', v);
		return false;
	});
	$("#egg").click(function () {
		var v = loadRandom(egg);
		googleTrack('Easter Egg', v);
		return false;
	});
});

$(document).ready(loadLoop);

