/*
 * Display the video in a nice neat box on the screen
 * instead of in a popup window.
 */


$(document).ready(function() {


	// If "Watch Video" or "Close" was clicked, toggle the video.
	$("#watchVideo, #closeVideo").click(function(e) {
		e.preventDefault();
		
		// Try to stop the video before closing.
		try {
			togglePlay();
		} catch (e) {
			// IE gives an error with the YouTube API.
			//alert("Error: " + e.name + "\n" + e.message);
			//
			// We could destroy the video <object> if need be.
			// This is not ideal as one can not open up the
			// video again unless they refresh the page.
			//$("#localedgeYoutubeVideo").remove();
			//
			// Note: Perhaps we could use ajax?

		}

		$("#videoHolder, #videoHolderBackground").slideToggle(500);

	});

	// For debug purposes.
	$("#play").click(function(e) {
		e.preventDefault();
		document.getElementById('localedgeYoutubeVideo').playVideo();
	});
	$("#stop").click(function(e) {
		e.preventDefault();
		document.getElementById('localedgeYoutubeVideo').pauseVideo();
	});

});


// This function will stop the video before closing it.
// It would be ideal to have it play the video when it opens,
// but that doesn't seem to work.
function togglePlay () {

	// If video is visible then it's about to be hidden. Stop the video.
	if ( $("#videoHolder").is(':visible') ) {
		document.getElementById('localedgeYoutubeVideo').pauseVideo();
	}
	
}
