var flash = {
	installed: false,
	version: 0
};
var is_IE = true;

function detect_flash() {
	var type;
	var mt;
	var v;
	var axo;

	// not_IE
	if(navigator.plugins && navigator.plugins.length > 0) {
		type = 'application/x-shockwave-flash';
		mt = navigator.mimeTypes;
		is_IE = false;
		if(mt && mt[type] && mt[type].enabledPlugin && mt[type].enabledPlugin.description) {
			flash.installed = true;
			v = mt[type].enabledPlugin.description.match(/Shockwave Flash (\d+)/);
			if(v) {
				flash.version = v[1];
			}
		}
	}
	// IE
	else if(navigator.appVersion.indexOf("Mac")==-1 && window.execScript) {
		try {
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			flash.version = axo.GetVariable("$version").match(/\w+ (\d+),\d+,\d+,\d+/)[1];
			flash.installed = true;
		}
		catch(err) {
			// flash > 9 not installed
		}
	}
}

//<![CDATA[
function write_no_flash() {
	var ob;
	var txt;

	// redetect flash incase they have just installed it
	detect_flash();
	if(flash.installed && flash.version >= flash.version_req) {
		write_flash();
		set_video_box_width();
		return;
	}
	if(is_IE) {
		ob = '\
			<object \
				type="application/x-shockwave-flash"\
				codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"\
				width="1" height="1">\
			<\/object>\
		';
	}
	else {
		ob = '\
			<object \
				type="application/x-shockwave-flash"\
				width="1" height="1">\
			<\/object>\
		';
	}
	if(flash.version < flash.version_req && flash.version != 0) {
		txt = '\
			<p>\
			You need to upgrade Flash to view this content.\
			You may need to restart your browser after updating.\
			<\/p>\
			<p><a href="http://www.adobe.com/go/getflashplayer">Download Flash</a><\/p>\
		';
		ob = '';
	}
	else {
		txt = '\
			<p>\
			You need to install flash to view this content.\
			<\/p>\
			<p><a href="http://www.adobe.com/go/getflashplayer">Download Flash</a><\/p>\
		';
	}
	
	document.getElementById('object').innerHTML = txt + ob;
	if (document.getElementById('video-box')) {
		document.getElementById('video-box').style.width = '500px';
	}
}
//]]>

/**
 * Displays the video
 */
function show_video()
{
	// Set the width and height of the greyed out area to be that of the page div dimensions.
	// An extra 4px for borders left and right.
	document.getElementById('video').style.width  = document.getElementById('page').clientWidth + 4 + 'px';
	document.getElementById('video').style.height = document.getElementById('page').clientHeight + 'px';
	document.getElementById('bg').style.width     = document.getElementById('page').clientWidth + 4 + 'px';
	document.getElementById('bg').style.height    = document.getElementById('page').clientHeight + 'px';
	
	// Move to the top of the page where the video will be displayed.
	window.scrollTo(0,0);
	
	if(!flash.installed || flash.version < flash.version_req) {
		write_no_flash();
	} else {
		write_flash();
		set_video_box_width();
	}
	
	// Display the video.
	document.getElementById('video').style.display = 'block';
}

/**
 * Displays an inline video
 */
function show_inline_video()
{	
	detect_flash();
	
	if(!flash.installed || flash.version < flash.version_req) {
		write_no_flash();
	} else {
		write_flash();
	}
}

/**
 * Hides the video
 */
function hide_video()
{
	// Hide the video.
	document.getElementById('video').style.display = 'none';
	
	// Remove the contents of the object element.
	// This prevents the video from playing while hidden after being closed.
	document.getElementById('object').innerHTML = '';
}

/**
 * Used to set the width of the video box to be that of the size of the object 
 * being embedded into the page.
 */
function set_video_box_width()
{
	document.getElementById('video-box').style.width = document.getElementById('flash-object').width + 'px';
}
