/**
 * This script contains embed functions for common plugins. This scripts are complety free to use for any purpose.
 */

function Media(src, width, height) {
	this.src = src;
	this.width = width;
	this.height = height;
}

function getMediaCode(mimetype, src, width, height) {
	var media = new Media(src, width, height);
	media['wmode'] = 'opaque';
	var embedCode = '';
	var noPluginCode = '<div class="media" height="' + height + '" width="' + width +'">';
			
	// ********************************************************
	// To support more mimetypes, extend the following switch:
	// ********************************************************
	switch(mimetype) {
		case 'application/x-shockwave-flash':
			if(canDetectPlugins() && detectFlash() || !canDetectPlugins()) {
				// Embed code
				embedCode = getFlashCode(media);
			} else {
				embedCode = noPluginCode + 'No flash plug-in detected, download the plugin <a href="http://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" target="_new">here</a>.</div>';
			}
			break;
                case 'Failed to determine type': // annoying mmbase
		        if(canDetectPlugins() && detectFlash() || !canDetectPlugins()) {
			        // Embed code
			        embedCode = getFlashCode(media);
                        } else {															                                embedCode = noPluginCode + 'No flash plug-in detected, download the plugin <a href="http://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" target="_new">here</a>.</div>';
                        }
                        break;
																										
																					    case 'video/quicktime':
			if(canDetectPlugins() && detectQuickTime() || !canDetectPlugins()) {
				embedCode = getQuickTimeCode(media);
			} else {
				embedCode = noPluginCode + 'No quicktime plug-in detected, download the plugin <a href="http://www.apple.com/quicktime/download/" target="_new">here</a>.</div>';
			}
			break;
		default:
			alert('Unsupported media mimetype: ' + mimetype + '. (change embed_media.js)');								
	}
	return embedCode;
}

function getFlashCode(p) {
	return getEmbedCode(
		'D27CDB6E-AE6D-11cf-96B8-444553540000',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'application/x-shockwave-flash',
		p
	);
}

function getShockWaveCode(p) {
	return getEmbedCode(
	'166B1BCA-3F9C-11CF-8075-444553540000',
	'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0',
	'application/x-director',
		p
	);
}

function getQuickTimeCode(p) {
	return getEmbedCode(
		'02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
		'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0',
		'video/quicktime',
		p
	);
}

function getRealMediaCode(p) {
	return getEmbedCode(
		'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'audio/x-pn-realaudio-plugin',
		p
	);
}

function getWindowsMediaCode(p) {
	p.url = p.src;
	return getEmbedCode(
		'6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
		'application/x-mplayer2',
		p
	);
}

function getEmbedCode(cls, cb, mt, p) {
	var h = '', n;

	h += '<div class="media">';
	h += '<object classid="clsid:' + cls + '" codebase="' + cb + '" ';
	h += typeof(p.id) != "undefined" ? 'id="' + p.id + '"' : '';
	h += typeof(p.name) != "undefined" ? 'name="' + p.name + '"' : '';
	h += typeof(p.width) != "undefined" ? 'width="' + p.width + '" ' : '';
	h += typeof(p.height) != "undefined" ? 'height="' + p.height + '"' : '';
	h += typeof(p.align) != "undefined" ? 'align="' + p.align + '"' : '';
	h += '>';

	for (n in p)
		h += '<param name="' + n + '" value="' + p[n] + '">';

	h += '<embed type="' + mt + '" ';

	for (n in p)
		h += n + '="' + p[n] + '" ';

	h += '></embed></object></div>';

	return h;
}

