function switch_flash( img, id ) {
	remove_flash();
	generate_flash( img );
}

function remove_flash( ) {
	var objects = document.getElementsByTagName('object');
	for ( var i = objects.length - 1; i >= 0; --i ) {
		if ( objects[ i ].className == 'rte_embed' ) {
			generate_img( objects[ i ] );
		}
	}
}

function generate_img( swf ) {
	var img = document.createElement( 'img' );
	img.className = 'rte_embed';
	img.id        = swf.id;
	img.width     = swf.width;
	img.height    = swf.height;
	img.onclick   = function() { switch_flash( img, img.id.substring( 0, img.id.lastIndexOf( '.' ) ) ); };

	var params = swf.getElementsByTagName( 'param' );
	for ( var i = 0; i < params.length; ++i ) {
		if ( params[ i ].name == '_src' ) {
			img.src = params[ i ].value + '.jpeg';
			break;
		}
	}

	swf.parentNode.replaceChild( img, swf );
}

function generate_flash( img ) {
	var source = img.src.substring( 0, img.src.length - 5 );

	var width = img.width;
	var height = img.height;
	var id = source.substring( source.lastIndexOf( '/' ) + 1, source.lastIndexOf( '.' ) );

	var flashvars = {};
	var params = {};
	var attributes = {};

	flashvars.file           = source;
	flashvars.backcolor      = '0x003462';
	flashvars.frontcolor     = '0xFFFFFF';
	flashvars.lightcolor     = '0x33FFFF';
	flashvars.screencolor    = '0x003462';
	flashvars.shownavigation = 'true';
	flashvars.autostart      = 'true';
	flashvars.allowfullscreen= 'true';
	flashvars.image          = source + '.jpeg';
	flashvars.displayheight  = '455';
	//flashvars.displayheight  = parseFloat( height ) + 1;

	params.quality         = 'high';
	params.play            = 'false';
	params.loop            = 'false';
	params.wmode           = 'window';
	params.allowfullscreen = 'true';

	// This one is just so it can be easily retrieved by generate_img()
	params._src    = source;

	attributes.id         = 'swf_' + id;
	attributes.styleclass = 'rte_embed';

	swfobject.embedSWF(
		'http://oddblog.callisto.oddball.com/mediaplayer.swf',
		'swf_' + id,
		width,
		height,
		'6.0.40',
		null,
		flashvars,
		params,
		attributes
	);
}
