// Videos metadata
var flvs = {
	flashplayer03: {
		flvsrc: "/swfs/BS_Stay_Focused_20_English.flv",
		flvwidth: 720,
		flvheight: 480
	}
};

var video_playing = false;

function isIE6() {
	var major = parseInt(jQuery.browser.version.split('.')[0]);
	return jQuery.browser.msie && major <= 6;
}

// Return the video player object corresponding to the given id
function videoPlayer(id) {
	if (!id) id = "flashplayer";

	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[id]
	}
	else {
		return document[id]
	}
}

function playVideo(id) {
	try {
		// Hide all hi-res images
		$('.view img').hide();
		$('.arrowPlayer').hide();

		// Insert the player object in the DOM (if not already there)
		if ($('div#'+id)) {
			embedVideoPlayer(id, flvs[id]['flvsrc'], flvs[id]['flvwidth'], flvs[id]['flvheight']);
		}

		// Show the flash container
		$('.flashplayer').css('visibility', 'visible');

		// Waiting for the DOM to be ready, and play the video
		$(document).oneTime(2000, function() {
			var player = videoPlayer(id);
			player._play();
			video_playing = true;
		});
	} catch (e) {
	}
}

function pauseVideo(id) {
	try {
		var player = videoPlayer(id);
		if (player != undefined && player._pause) player._pause();
		video_playing = false;
	} catch (e) {
	}
}

function stopVideo(id) {
	try {
		// Hide the flash container
		$('.flashplayer').css('visibility', 'hidden');

		// Show all hi-res images
		$('.view img, .view .info').show();
		$('.arrowPlayer').show();

		// Waiting for the dom to be ready, and stop the video
		$(document).oneTime(500, function() {
			var player = videoPlayer(id);
			if (player != undefined && player._stop) player._stop();
			video_playing = false;

			// Replace the Flash object with a div
			if (isIE6()) {
				var view = $('#'+id).parent();
				var flash = $('#'+id);
				flash.remove();
				var img = $(view).children('img');
				$(img).after('<div id="'+id+'" class="flashplayer"><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>');
			}
		});
	} catch (e) {
	}
}

// Callback triggered by the Flash player
function _video_complete() {
	try {
		stopVideo('flashplayer03');
		slideshow_running = true; // see slideshow.js
	} catch (e) {
	}
}

function embedVideoPlayer(id, filename, videowidth, videoheight) {
	var swfwidth = 900;
	var swfheight = 384;
	var flash_version = "8.0.0";

	if (!id) id = "flashplayer";
	if (!videowidth) videowidth = swfwidth;
	if (!videoheight) videoheight = swfheight;

	// Scale the video to fill the maximum space
	videowidth = parseInt(videowidth) * swfheight / parseInt(videoheight);
	videoheight = swfheight;
	
	swfobject.embedSWF("/chrome/swfs/videoplayer.swf?1003021139", id, swfwidth, swfheight, flash_version, "/swfobject/expressInstall.swf", 
		{ 
			autoalign: "false",
			autoplay: "false",
			autoloop: "false",
			maintainratio: "false", // original video size (true) or stretch to videowidth/height (false)
			bgcolor: "606060",
			filename: filename, 
			swfwidth: swfwidth,
			swfheight: swfheight,
			videowidth: videowidth,
			videoheight: videoheight
		}, 
		{ 
			wmode: "transparent", 
			allowScriptAccess: "always"
		}, 
		{
			'class': "flashplayer"
		}
	);
}

