var requiredFlashVersion = 5;
var class_id = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
var codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0";
var defaultOptions = new Array(
	'allowScriptAccess', 'sameDomain',
	'wmode', 'transparent',
	'quality', 'high',
	'menu', 'false'
);


function activateFlash(swf_url, width, height, flashVars, options) {
	// Step 1:
	//  a) Supply a SWF URL argument.
	//  b) Supply an array of SWF URLs.
	//  c) Supply an array of 1 SWF URL and 1 Image URL.
	//  d) Supply an array of arrays of SWF URLs & Image URLs.
	//  z) ALSO NEED TO SUPPLY REGULAR LINK URLS FOR THE ALT IMAGE + OTHER

	// Step 2:
	//  Arguments: Figure out how to cleanly supply params, including flashvars. ALSO supply additional arguments such as class, id, codebase, etc...

	// Other Steps/Options:
	//  Test for plugin, also find alternative to document.write
	//  Allow predefined values for height, width, ID, parameters, flashvars, etc..  NOTE: Predefined values should be overwritable by arguments.

	// Future enhancements: We support randomizing the SWF files, but all SWFs must have the same dimensions and options. Fix this.

	// Parse Object Location value
	var ObjLocation = new Array ();
	if (swf_url && swf_url.constructor == Array) { // Array
		if (swf_url[0] && swf_url[0].constructor == Array) { // 2D Array
			if (swf_url.length == 3) {
				// We make sure all sub arrays are of equal length, and throw out any extraneous elements
				//  NOTE: TODO: Technically, not every Flash file needs a static alternative, but we are forcing that here.
				var shortest = swf_url[0].length;
				for (var i = 1; i < swf_url.length; i++) { shortest = (swf_url[i].length < shortest) ? swf_url[i].length : shortest; }
				for (var i = 0; i < swf_url.length; i++) {
					if (!ObjLocation[i]) { ObjLocation[i] = new Array(); }
					for (var j = 0; j < shortest; j++) { ObjLocation[i][j] = swf_url[i][j]; }
				}
			} else {
				// 2D array of improper length, return just the first element of the supplied array.
				ObjLocation[0][0] = swf_url[0][0];
			}
		} else if (swf_url.length == 1) { // Special case array with only 1 element
			// TODO:
			//  Currently this only handles type: swf_url[0] = "some_url.swf".
			//  Also need to handle type: swf_url[0][0] = "some_url.swf".
			ObjLocation[0] = swf_url;
		} else { // 1D Array
			// Determine if its an array of SWF urls, or a set of SWF + Image URL + URL
			var extension = ".swf";
			if(swf_url[1].length != swf_url[1].lastIndexOf(extension) + extension.length) { // SWF, JPG/GIF, URL
				ObjLocation[0] = new Array(swf_url[0]);
				ObjLocation[1] = new Array(swf_url[1]);
				ObjLocation[2] = new Array(swf_url[2]);
			} else { // SWF only
				ObjLocation[0] = swf_url;
			}
		}
	} else { // String
		ObjLocation[0] = new Array(swf_url);
	}
	swf_url = null;

	// Default ID, if more than one <object> tag, add a number to the default for each ID
	var id = "flashObject";
	var objs = document.getElementsByTagName('object');
	if (objs && objs.length) { id += objs.length; }

	// Determine if plugin detection is necessary and, if so, go for it
	var plugin = flashVersion();

	// Output the Flash or Alt gif
	// If only SWFs were supplied, don't bother with plugin detection
	if (ObjLocation.length == 1 || (plugin && plugin[0] >= requiredFlashVersion)) {
		var r = Math.floor(Math.random()*ObjLocation[0].length);
		document.write('<object classid="'+class_id+'" codebase="'+codebase+'" width="'+width+'" height="'+height+'" id="'+id+'" align="middle">');
		document.write('<param name="movie" value="' + ObjLocation[0][r] + '" />');
		document.write('<param name="flashVars" value="' + flashVars + '"/>');
		var str = "";
		if (options) {
			var opts = options.split('&');
			// Parse the parameteres
			for (var i = 0; i < opts.length; i++) {
				var n = opts[i].substring(0,opts[i].indexOf('='));
				var v = opts[i].substring(n.length+1);
				document.write('<param name="' + n + '" value="' + v + '"/>');
				str += n + '="' + v + '" '; // For the embed tag
			}
		}
		if (defaultOptions) {
			for (var i = 0; i < defaultOptions.length; i+=2) {
				if (str.indexOf(defaultOptions[i]) < 0) { // if
					document.write('<param name="' + defaultOptions[i] + '" value="' + defaultOptions[i+1] + '"/>');
					str += defaultOptions[i] + '="' + defaultOptions[i+1] + '" ';
				}
			}
		}
		document.write('<embed src="' + ObjLocation[0][r] + '" width="'+width+'" height="'+height+'" name="video_player" align="middle" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="' + flashVars + '" ' + str + '/>');
		document.write('</object>');
	} else if (!(navigator.appName && navigator.appName.indexOf("Netscape")>=0 && navigator.appVersion.indexOf("2.")>=0)){
		var r = Math.floor(Math.random()*ObjLocation[1].length);
		if (ObjLocation[2][r]) {
			document.write('<a href="' + ObjLocation[2][r] + '"><img src="' + ObjLocation[1][r] + '" alt="" border="0" /></a>');
		} else {
			document.write('<img src="' + ObjLocation[1][r] + '" alt="" border="0" />');
		}
	}
//	alert(document.getElementsByTagName('object')[0].innerHTML);
}

function flashVersion(v) {
	// Returns an array:
	//  0: Major version number
	//  1: Minor version number
	//  2: Revision
	if (!v) { v = requiredFlashVersion; }
	var version = new Array(0,0,0);
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swfPlugin = navigator.plugins["Shockwave Flash 2.0"] ? "Shockwave Flash 2.0" : "Shockwave Flash";
			// Example description: Shockwave Flash 8.0 r22
			var holder = navigator.plugins[swfPlugin].description.split(' ');
			version[0] = holder[2].substring(0,holder[2].indexOf('.'));
			version[1] = holder[2].substring(holder[2].indexOf('.')+1);
			version[2] = holder[3] ? holder[3] : holder[4];
			version[2] = version[2].substring(version[2].indexOf('r')+1);
		}
	} else {
		document.write('<script type="text/vbscript">' + "\n");
		document.write('Function flashVersionVB(v)' + "\n");
		document.write('	on error resume next' + "\n");
		document.write('	Dim swControl, swVersion' + "\n");
		document.write('	swVersion = 0' + "\n");
		document.write('	set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(v))' + "\n");
		document.write('	if (IsObject(swControl)) then' + "\n");
		document.write('		swVersion = swControl.GetVariable("$version")' + "\n");
		document.write('	end if' + "\n");
		document.write('	flashVersionVB = swVersion' + "\n");
		document.write('End Function' + "\n");
		document.write('</script>' + "\n");
		// Example description: WIN 7,0,19,0
		var holder = "";
		try { holder = flashVersionVB(v).split(','); }
		catch(e) { /* e should be a Reference Error, if not defined */ }
		if (holder) {
			version[0] = holder[0].substring(holder[0].lastIndexOf(' ')+1);
			version[1] = holder[1];
			version[2] = holder[2];
		}
	}
	return version;
}

