//-----------------------------------------------------------------------------
// videoplayer.js
// (c) 2010 Colorize, Apache license (http://www.colorize.nl/code_license.txt)
//-----------------------------------------------------------------------------

/**
 * Embeds a video player in the element with the specified ID. 
 * @param elementID ID of the element in which to place the video player.
 * @param url Path to the SWF file.
 * @param width Width of the video player in pixels.
 * @param height Height of the video player in pixels.
 * @param flashvars Parameters to pass to the video player. This can either be 
 *        a query string or an object with key/value pairs.
 */
function createVideoPlayer(elementID, url, width, height, flashvars) {

	var flashVersion = getFlashVersion();
	
	if ((flashVersion == -1) || (flashVersion < 9)) {
		// Flash is not installed or outdated. Leave the old contents of the
		// target element to display a message where to get Flash.
		return;
	}
	
	var targetElement = document.getElementById(elementID);
	var randomID = 'videoplayer' + new Date().getTime();
	flash(targetElement, randomID, url, width, height, flashvars);
}

/**
 * Embeds a HTML 5 video player in the element with the specified ID. Because
 * HTML 5 is not yet finished, supported codecs are different for each browser,
 * and not all browsers support it this method is experimental. Also, not all
 * features of the Flash video player are supported yet.
 */
function createHTML5VideoPlayer(elementID, width, height, vars) {

	var videoURL = vars['_video'];
	var screenshotURL = vars['_screenshot'];
	var autoplay = (vars['_autoplay'] != 'false');
	var controls = (vars['_controls'] != 'false');
	
	var embed = '<video width="' + width + '" height="' + height + '"';
	if ((screenshotURL != null) && (screenshotURL.length > 0)) {
		embed += ' poster="' + screenshotURL + '"';
	}
	if (autoplay) {
		embed += ' autoplay="autoplay"';
	}
	if (controls) {
		embed += ' controls="controls"';
	}
	embed += '>';
	embed += '<source type="video/mp4" src="' + videoURL + '" />';
	embed += '</video>';
	
	document.getElementById(elementID).innerHTML = embed;
}

/**
 * Embeds a Flash movie at the specified location in the page.
 * @param div Element in which the Flash movie will be placed.
 * @param id The ID that will be given to the inserted object.
 * @param url Path to the SWF file.
 * @param width Width of the SWF in pixels.
 * @param height Height of the SWF in pixels.
 * @param flashvars Parameters to pass to Flash. This can either be a query
 *        string or an object with key/value pairs.
 */
function flash(div, id, url, width, height, flashvars) {

	var flashvarsString = '';
	if (flashvars.length) {
		flashvarsString = flashvars;
	} else { 
		for (var key in flashvars) {
			if (flashvarsString.length > 0) {
				flashvarsString += '&';
			}
			flashvarsString += key + '=' + flashvars[key];
		}
	}

	var embed = '<object type="application/x-shockwave-flash" id="' + id + '" data="'
			+ url + '" width="' + width + '" height="' + height + '">';
	embed += '    <param name="movie" value="' + url + '" />';
	embed += '    <param name="allowScriptAccess" value="always" />';
	embed += '    <param name="allowFullscreen" value="true" />';
	embed += '    <param name="flashvars" value="' + flashvarsString + '" />'
	embed += '</object>';
	
	div.innerHTML = embed;	
}

/**
 * Returns the version of Flash Player that is installed in the browser. The
 * returned value is the major version number as an integer. If no version of 
 * Flash Player could be detected this method returns -1. 
 */
function getFlashVersion() {

	// Detection for Firefox, Safari and Chrome
	
	if ((navigator.plugins != null) && (navigator.plugins.length > 0)) { 
		if (navigator.plugins['Shockwave Flash']) {
			var description = navigator.plugins['Shockwave Flash'].description;
			return parseInt(description.split(' ')[2].split('.')[0]);
		} else if (navigator.plugins['Shockwave Flash 2.0']) {
			var description = navigator.plugins['Shockwave Flash 2.0'].description;
			return parseInt(description.split(' ')[2].split('.')[0]);
		}
	}
	
	// Detection for Internet Explorer
	
	try {
		var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
		return parseInt(ax.GetVariable('$version').split(' ')[1].split(',')[0]);
	} catch (e) {	
	}
	
	try {
		var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
		return parseInt(ax.GetVariable('$version').split(' ')[1].split(',')[0]);
	} catch (e) {
	}
	
	// Flash is not installed or browser is not supported
	return -1;
}
