var ready = false;

// fired automatically when the youtube window is ready for api calls
function onYouTubePlayerReady() {
	ready = true;
}

// non-intensive loop to play video as soon as api is ready
function playVideo(video) {
	if(!ready) {
		var callback = function() {playVideo(video);}
		setTimeout(callback, 10); 
		return;
	}
	ready = false;
	video.playVideo();
}

// show the relevant title and video, and show the now playing label
function showVideo(panel, video) {
	$('.videobox'+panel+' .currentvideo .title'+video).fadeIn();
	$('.videobox'+panel+' .video span').hide();
	$('.videobox'+panel+' .video .video'+video).show();
	$('.videobox'+panel+' .morevideos .play').fadeIn();
	$('.videobox'+panel+' .morevideos .now_playing').fadeOut();
	$('.videobox'+panel+' .morevideos .thumb'+video+' .play').fadeOut();
	$('.videobox'+panel+' .morevideos .thumb'+video+' .now_playing').fadeIn();
	var videoobject = $('.videobox'+panel+' .video .video'+video+' embed').get(0);
	var callback = function() {playVideo(videoobject);}
	setTimeout(callback, 10); // can't play video till it's ready for api calls!
}


function resetVideos() {
	$('.videobox .currentvideo span').hide(); // hide titles
	$('.videobox .video span').hide(); // hide videos
	$('.videobox .morevideos .thumb .now_playing').hide(); // hide now playing images on thumbs
}

	
$(document).ready(function(){

	//$(document).pngFix(); // make our translucent buttons work in IE6
	
	if($('.videobox').length){
		$('.videobox').hide(); // hide video window(s)
		resetVideos();
	}
	$('body').prepend('<div id="shadow_mask"></div>');

	// clicked on a thumb in the main text - show video window and play clicked video
	$('.videopanel a').click(function() {
		var panel = '.'+$(this).parent().attr('class').substr(11); // we might have multiple sets on a page - this unique ID tells us which video window to work with
		$('#shadow_mask').fadeTo("fast", 0, function() {
			$('#shadow_mask').show();
			$('.videobox_container').show();
			$('select').hide();
			var top = document.documentElement.scrollTop + 30;
			var height = document.body.offsetHeight;
			$('.videobox_container').css({top:top});
			$('#shadow_mask').css({height:height});
			$('#shadow_mask').fadeTo("normal", 0.7);
		});
		$('.videobox'+panel).fadeIn();
		var video = $(this).attr('class').substr(5);
		showVideo(panel, video);
		return false;
	});
	
	// clicked a thumb from the video window
	$('.videobox .morevideos a').click(function() {
		var panel = '.'+$(this).parent().parent().parent().attr('class').substr(9);
		var video = $(this).attr('class').substr(5);
		$('.videobox'+panel+' .currentvideo span').fadeOut();
		showVideo(panel, video);
		return false;
	});
	
	// close video window
	$('.videobox a.close').click(function() {
		$('#shadow_mask').fadeOut();
		$('.videobox').fadeOut("normal", function() {
			resetVideos();
			$('.videobox_container').hide();
		});
		$('select').show();
		return false;
	});
	
	$('.videobox').click(function() {
		return false;
	});
	
	$('.videobox_container').click(function(e) {
		$('.videobox a.close').trigger('click');
	});
});