/* Copied from ClickCaster, DO NOT MODIFY! */

// create_player.js - Automagic DOM object generator for media players
// (C) 2006 Tony Arcieri, ClickCaster, Inc.
// All rights reserved

// media_by_mimetype - MIME type to media player mappings
// Player types:
//	video	- OS native video player (i.e. WMP on Windows, Quicktime on OS X)
//	wmp 	- Windows Media Player
//	qt		- Quicktime
//	flv	- Flash Video Player
var media_by_mimetype = {
	'video/mpeg' 										: 'video',
	'video/x-mpeg' 									: 'video',
	'video/msvideo'									: 'wmp',
	'video/x-msvideo'								: 'wmp',
	'video/avi'											: 'wmp',
	'video/x-avi'										: 'wmp',
	'video/ms-asf'									: 'wmp',
	'video/x-ms-asf'								: 'wmp',
	'video/ms-wmv'									: 'wmp',
	'video/x-ms-wmv' 								: 'wmp',
	'video/quicktime'								: 'qt',
	'video/x-quicktime'							: 'qt',
	'video/mov'											: 'qt',
	'video/x-mov'										: 'qt',
	'video/m4v'											: 'qt',
	'video/x-m4v'										: 'qt',
	'video/mp4'											: 'qt',
	'video/x-mp4'										: 'qt',
	'audio/m4a'											: 'qt', 
	'audio/x-m4a'										: 'qt',
	'audio/m4b'											: 'qt', 
	'audio/x-m4b'										: 'qt',
	'video/x-shockwave-flash'				: 'flv',
	'video/flv'											: 'flv',
	'video/x-flv'										: 'flv',
	'audio/mpeg'										: 'mp3', 
	'audio/x-mpeg'									: 'mp3', 
	'audio/mp3'											: 'mp3', 
	'audio/x-mp3'										: 'mp3', 
	'audio/mpeg3'										: 'mp3', 
	'audio/x-mpeg3'									: 'mp3', 
	'audio/mpg'											: 'mp3', 
	'audio/x-mpg'										: 'mp3', 
	'x-audio/mp3'										: 'mp3'
};

// mimetype_by_extension - File extension to MIME type mappings
var mimetype_by_extension = {
	'mpg'				: 'video/mpeg',
	'mpeg'			: 'video/mpeg',
	'mpe'				: 'video/mpeg',
	'mp2'				: 'video/mpeg',
	'mpv2'			: 'video/mpeg',
	'avi'				: 'video/x-msvideo',
	'asf'				: 'video/x-ms-asf',
	'wmv'				: 'video/x-ms-wmv',
	'mov'				: 'video/quicktime',
	'm4v'				: 'video/x-m4v',
	'mp4'				: 'video/x-mp4',
	'flv'				: 'video/x-flv',
	'mp3'				: 'audio/mpeg',
	'm4a'				: 'audio/x-m4a',
	'm4b'				: 'audio/x-m4b'
};

// Return the file extension at the end of the path contained in a URL
function extract_file_extension_from_url(url)
{
	// Strip possible arguments from the URL
	path = url.split('?').shift();
	
	// Look for the last '.' delimited entity
	return path.split('.').pop();
}

// identify_player - Return the appropriate player for a url/mimetype pair
function identify_player(url, mimetype)
{
  // Check for YouTube URLs
  if(url.substr(7, 12) == 'youtube.com/') return 'youtube';

	// Experience shows file extensions are a more reliable identifier than
	// MIME types, so attempt identification by extension first
	extension = extract_file_extension_from_url(url);
	if(extension) {
		extension_type = mimetype_by_extension[extension];
		if(extension_type)
			return media_by_mimetype[extension_type];
	}
		
	// If the extension cannot be identified, fall back on the MIME type
	return media_by_mimetype[mimetype];
}

function create_player_embed(params)
{
	var markup = '<embed '
	
	for(name in params)
		markup += name + '="' + params[name] + '" ';
		
	return markup + '/>';
}

function create_generic_player(url, mimetype, width, height)
{
	var width 	= (width == null) 	? "320" : width;
	var height 	= (height == null) 	? "240" : height;
	
	return create_player_embed({
		'src'			:	url,
		'type'		:	mimetype, 
		'width'		: width, 
		'height'	: height});
}

function create_wmplayer(url, width, height, autostart)
{
	var width 	= (width == null) 	? "320" : width;
	var height 	= (height == null) 	? "285" : height;
	
	// Generate object tag for IE
	if(window.ActiveXObject)
		return '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" ' +
			'width="' + width + '" height="' + height + '">\n' +
			'<param name="URL" value="' + url + '">\n' +
			'<param name="autoStart" value="' + autostart + '">\n' +
			'<param name="uiMode" value="full">\n' +
			'</object>'
	
	return create_player_embed({
		'src'					:	url,
		'type'				: 'application/x-mplayer2',
		'pluginspage' : 'http://www.microsoft.com/Windows/MediaPlayer',
		'width'				: width,
		'height'			: height,
		'uiMode'			: 'full',
		'autoStart'		: (autostart == true || autostart == 'true') ? 1 : 0
	});
}

function create_quicktime_player(url, width, height, autostart)
{
	var width 	= (width == null) 	? "320" : width;
	var height 	= (height == null) 	? "256" : height;

	return create_player_embed({
		'width'			: width,
		'height'		: height,
		'src'				: url,
		'scale'			: 'aspect',
		'controller': true,
		'bgcolor'		: 'white',
		'cache'			: 'true',
		'autoplay'	: autostart,
		'type'			: 'video/quicktime'
	});
}

function create_flv_player(url, width, height, autostart)
{
	var width 	= (width == null) 	? "403" : width;
	var height 	= (height == null) 	? "329" : height;
	
	return create_player_embed({
		'width'			: width,      
		'height'		: height,     
		'src'				: '/plugin_assets/clickcaster_engine/players/video.swf?file=' + url + '&bgcolor=000000&&autostart=' + autostart,
		'quality'		: 'high',     
		'type'			: 'application/x-shockwave-flash',
		'allowFullScreen' : 'true'
	});
}

function create_youtube_player(url, width, height, autostart)
{
	var width 	= (width == null) 	? "320" : width;
	var height 	= (height == null) 	? "240" : height;
	
  if(url.substr(url.length - 4, 4) == '.swf')
    url = url.substr(0, url.length - 4);

	if(autostart == true || autostart == 'true') url += '&autoplay=1'
	
	return create_player_embed({
		'src'		: url, 
		'type'	: 'application/x-shockwave-flash', 
		'width'	: width, 
		'height': height
	});
}

function create_native_player(url, mimetype, width, height, autostart)
{
	if(navigator.appVersion.indexOf('Win') != -1)
		return create_wmplayer(url, width, height, autostart);
		
	if(navigator.appVersion.indexOf('Macintosh') != -1)
		return create_quicktime_player(url, width, height, autostart);
		
	return create_generic_player(url, mimetype, width, height);
}

function create_player_markup(url, mimetype, width, height, autostart, force)
{
	// Set defaults for unspecified arguments
	var autostart   = (autostart == null) ? true : autostart;
	var force       = (force == null)     ? true : force;

	// Attempt to identify the player given the url and mimetype
	var player = identify_player(url, mimetype);
	
	// Create the appropriate player for the given media type
	switch(player) {
		case 'mp3':
		case 'video':
			return create_native_player(url, mimetype, width, height, autostart);
		case 'wmp':
			return create_wmplayer(url, width, height, autostart);
		case 'qt':
			return create_quicktime_player(url, width, height, autostart);
		case 'flv':
			return create_flv_player(url, width, height, autostart);
		case 'youtube':
			return create_youtube_player(url, width, height, autostart);
	}
	
	if(force)
		return create_generic_player(url, mimetype, width, height);
		
	return null;
}

// create_player - Return a DOM element representing the player
// Arguments:
//   id 				- Element ID into which the player is inserted
//	 url 				- Resource to generate a player for (required)
//	 mimetype 	- MIME type of the player if known (otherwise null)
//	 width			- Width of the player
//	 height			- Height of the player
//	 autostart	- Automatically begin playing video
//	 force			- Return a generic embed tag for the given MIME type if the format cannot be identified
//				  	(if this paramater is null or omitted, then the function will return null for unknown types)
function create_player(id, url, mimetype, width, height, autostart, force)
{
	var page_element = document.getElementById(id);
	var player_code = create_player_markup(url, mimetype, width, height, autostart);
	
	if(!player_code) return false;
	
	page_element.innerHTML = player_code;
	return true;
}

