// Our show album IDs as variables to quickly reference
var showLabels=["Mics and Lights","Behind the Screen","The Full Court Press","The Biweekly Show","49 News","What in the Hall?!"];
var showSelect=["mnl","bts","fcp","biweekly","news","with"];
var albums=["138221","140198","131662","131661","133861","138251"];

// Tell Vimeo what function to call
var vimeoAPIv2 = 'http://vimeo.com/api/v2/';
var vimeoAPIoEmbed = 'http://vimeo.com/api/oembed.json';
var path;

$(document).ready(function() {
	$.post(
		"wordpress_ajax.php",
		{ action : "get_template_directory_uri" },
		function(data) {
			path = data.path;
			frontPage();
		}, "json"
	);
});

function frontPage() {
	var first = true;
	//for each show put the first one on the front page
	$.each(albums, function(albumsIndex,albumID) {
		$.getJSON(vimeoAPIv2 + 'album/' + albumID + '/videos.json?callback=?', function(data){
			setupGallery(albumsIndex,data);
			if(first) {
				$.getJSON(vimeoAPIoEmbed + '?url=' + data[0].url + '?width=640&callback=?', function(data){
					$('#video').html(unescape(data.html));
				});
				first = false;
			}
		});
	});
}

function getVideo(url) {
	$.getJSON(vimeoAPIoEmbed + '?url=' + url + '?width=640&callback=?', function(data){
		$('#video').html(unescape(data.html));
	});
}

function setupGallery(index,videos) {
	$('#'+showSelect[index]).append('<a href="' + videos[0].url + '"><img src="' + path + '/images/' + showSelect[index] + '.png" class="thumb-logo"><img src="' + videos[0].thumbnail_large + '" class="thumb-img" /></img></a>');

	// Switch to the video when a thumbnail is clicked
	$('#thumbs a').click(function(event) {
		event.preventDefault();
		var url = this.href;
		$('#video').hide('fast',loadVideo(url));
		$('#load').remove();
		$('#videoWrap').append('<span id="load">LOADING...</span>');
		$('#load').fadeIn('normal');
		function loadVideo(url) {
			$.getJSON(vimeoAPIoEmbed + '?url=' + url + '?width=640&callback=?', function(data){
				$('#video').html(unescape(data.html));
				showNewVideo();
			});
		}
		function showNewVideo() {
			$('#video').show('slow',hideLoader());
		}
		return false;
	});
}

function hideLoader() {
	$('#load').fadeOut('normal');
}
