/*
	unFocusFlashPlayerInfo, version 1.0b1 (beta) (2005/07/28)
	Copyright: 2005, Kevin Newman (http://www.unfocus.com/)
	License: http://creativecommons.org/licenses/LGPL/2.1/
*/
if (typeof unFocus == 'undefined') var unFocus = {};
if (!unFocus.Flash) unFocus.Flash = {};

if (!unFocus.Flash.Player) new function() {
	unFocus.Flash.Player = this;
	
	// private vars
	var _installed = false;
	var _beta = false;
	var _majorVersion = 8;
	var _minorVersion = 0;
	var _betaVersion = 0;
	
	var _version;
	
	// detection work
	if (navigator.plugins && navigator.plugins.length > 0) {
		_version = (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description;
		/* if (_version) {
			_installed = true;
			if (/Shockwave Flash/.test(_version)) {
			//alert(_version);
				//alert(_version.match(/Shockwave \d/)[0]);
				_majorVersion = _version.match(/Shockwave Flash (\d\.\d)/)[1];				
				if (/r\d+/.test(_version))
					_minorVersion = _version.match(/r(\d+)/)[1];
				else if (/[bd]\d+/.test(_version)) { // I'm not sure what other letters would be here, but I've encountered b and d so far
					_beta = true;
					_betaVersion = _version.match(/[bd](\d+)/)[1];
				}
				if(_majorVersion = 0) { 
					_majorVersion = 8;
					_minorVersion = 0;
					_betaVersion = 0;
					}
			} else _majorVersion = 1;
		} */
	} else if (typeof ActiveXObject != 'undefined') {
		// src: Player.vbs
		document.write('<scr'+'ipt type="text/vbscript"\>\nFunction unFocusDetectActiveX(C)\n',
			'On Error Resume Next\nunFocusDetectActiveX=IsObject(CreateObject(C))\n',
			'End Function\nFunction unFocusDetectFlashVersion()\nOn Error Resume Next\n',
			'Set C=CreateObject("ShockwaveFlash.ShockwaveFlash.4")\n',
			'unFocusDetectFlashVersion=C.GetVariable("$',
			'version")\nEnd Function\n</scr'+'ipt\>');
		_version = unFocusDetectFlashVersion();
		if (_version) {
			_version = _version.split(',');
			_majorVersion = _version[0].match(/\d+/)+'.'+_version[1];
			_minorVersion = _version[2];
			_betaVersion = _version[3];
			if (_version[3]) _beta = true;
		} else if (unFocusDetectActiveX('ShockwaveFlash.ShockwaveFlash.4'))
			_majorVersion = 4;
		else if (unFocusDetectActiveX('ShockwaveFlash.ShockwaveFlash.3'))
			_majorVersion = 3;
		else if (unFocusDetectActiveX('ShockwaveFlash.ShockwaveFlash')) 
			// Tested on Windows 95 with flash player 2 - using the ".2" at the end doesn't work, but this does ;-)
			_majorVersion = 2;
		if (_majorVersion) _installed = true;
	} else if (/WebTV/.test(navigator.userAgent)) { // WebTV
		_version = navigator.userAgent.match(/WebTV\/(\d\.\d)/)[1];
		if (_version > 2.5) _majorVersion = 4;
		else if (_version == 2.5) _majorVersion = 3;
		else _majorVersion = 2;
	}
	
	// public/priveleged Getters
	this.isInstalled = function() {
		return _installed;
	};
	this.isBeta = function() {
		return _beta;
	};
	this.getMajorVersion = function() {
		return _majorVersion;
	};
	this.getMinorVersion = function() {
		return _minorVersion;
	};
	this.getBetaVersion = function() {
		return _betaVersion;
	};
	this.getVersion = function() {
		return parseFloat(_majorVersion + '.' + _minorVersion);
	};
};


unFocus.Flash.HTML = function() {
	this._properties = {};
	this._params = {};
	this._flashVars = '';
	this._majorVersion = 0;
	this._minorVersion = 0;
	this._betaVersion = 0;
	this._src = '';
};
// setters check against valid values
// ref http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_12701
unFocus.Flash.HTML.prototype.SetSrc = function($src) {
	this._src = $src;
};
unFocus.Flash.HTML.prototype.SetWidth = function($width) {
	this._properties.width = $width;
};
unFocus.Flash.HTML.prototype.SetHeight = function($height) {
	this._properties.height = $height;
};
unFocus.Flash.HTML.prototype.SetId = function($id) {
	this._properties.id = $id;
};
unFocus.Flash.HTML.prototype.SetName = function($name) {
	this._properties.name = $name;
};

unFocus.Flash.HTML.prototype.SetMovie = function($src) {
	this.SetSrc($src);
};
unFocus.Flash.HTML.prototype.SetSwliveconnect = function($swliveconnect) {
	if (typeof $swliveconnect == 'boolean')
		this._params.swliveconnect = $swliveconnect;
	else
		throw new Error('InvalidValue', 'Valid Values for swliveconnect: true, false');
};
unFocus.Flash.HTML.prototype.SetPlay = function($play) {
	if (typeof $loop == 'boolean')
		this._params.play= $play;
	else
		throw new Error('InvalidValue', 'Valid Values for play: true, false');
};
unFocus.Flash.HTML.prototype.SetLoop = function($loop) {
	if (typeof $loop == 'boolean')
		this._params.loop= $loop;
	else
		throw new Error('InvalidValue', 'Valid Values for loop: true, false');
};
unFocus.Flash.HTML.prototype.SetMenu = function($menu) {
	if (typeof $menu == 'boolean')
		this._params.menu= $menu;
	else
		throw new Error('InvalidValue', 'Valid Values for menu: true, false');
};
unFocus.Flash.HTML.prototype.SetQuality = function($quality) {
	switch ($quality) {
		case 'low':
		case 'medium':
		case 'high':
		case 'autolow':
		case 'autohigh':
		case 'best':
			this._params.quality = $quality; /* low, medium, high, autolow, autohigh, best */
			break;
		default:
			throw new Error('InvalidValue', 'Valid Values for quality: low, medium, high, autolow, autohigh, best');
	}
};
unFocus.Flash.HTML.prototype.SetScale = function($scale) {
	switch ($scale) {
		case 'showall':
		case 'noborder':
		case 'exactfit':
		case 'noscale':
			this._params.scale = $scale; /* showall, noborder, exactfit, noscale (missing from the documentation) */
			break;
		default:
			throw new Error('InvalidValue', 'Valid Values for scale: showall, noborder, exactfit, noscale');
	}
};
unFocus.Flash.HTML.prototype.SetAlign = function($align) { // :NOTE: There is a conflict here - you can set align on the html element, as well as for the movie. Hhow does this sort out for embed?
	switch ($align) {
		case 'l':
		case 't':
		case 'r':
		case 'b':
			this._params.align = $align; /* l, t, r, b (defaults to center, which isn't in the list) */
			break;
		default:
			throw new Error('InvalidValue', 'Valid Values for align: l, t, r, b');
	}
};
unFocus.Flash.HTML.prototype.SetSalign = function($salign) {
	switch ($salign) {
		case 'l':
		case 't':
		case 'r':
		case 'b':
		case 'tl':
		case 'tr':
		case 'bl':
		case 'br':
			this._params.salign = $salign;
			break;
		default:
			throw new Error('InvalidValue', 'Valid Values for salign: l, t, r, b, tl, tr, bl, br');
	}
};
unFocus.Flash.HTML.prototype.SetWmode = function($wmode) {
	switch($wmode) {
		case 'window':
		case 'opaque':
		case 'transparent':
			this._params.wmode = $wmode;
			break;
		default:
			throw new Error('InvalidValue', 'Valid Values for wmode: window, opaque, transparent');
	}
};
unFocus.Flash.HTML.prototype.SetBgcolor = function($bgcolor) {
	// /^#[\dA-Fa-f]{6}$/
	if (/^#[\dA-F]{6}$/.test($bgcolor))
		this._params.bgcolor = $bgcolor; /* #RRGGBB, hexadecimal RGB value */
	else
		throw new Error('InvalidValue', 'Valid Values for bgcolor: a valid html color hex value (#0099FF)');
};
unFocus.Flash.HTML.prototype.SetBase = function($base) {
	this._params.base = $base;
};
unFocus.Flash.HTML.prototype.SetFlashvars = function($flashvars) {
	this._flashVars = $flashvars;
};



// http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_16494
unFocus.Flash.HTML.prototype.SetAllowscriptaccess = function($allowscriptaccess) {
	switch ($allowscriptaccess) {
		case 'never':
		case 'always':
			this._params.allowscriptaccess = $allowscriptaccess;
			break;
		default:
			throw new Error('InvalidValue', 'Valid Values for allowscriptaccess: never, always');
	}
};

// for version stuffs
unFocus.Flash.HTML.prototype.SetMinorVersion = function($minorVersion) {
	this._minorVersion = $minorVersion;
};
unFocus.Flash.HTML.prototype.SetMajorVersion = function($majorVersion) {
	this._majorVersion = $majorVersion;
};
unFocus.Flash.HTML.prototype.SetBetaVersion = function($betaVersion) {
	this._betaVersion = $betaVersion;
};

/**
 * Generates the platform specific HTML for the flash movie
 *
 * @return String containing the platform specific HTML for the flash movie
 */
unFocus.Flash.HTML.prototype.getHTML = function() {
	// initialize local variables
	var $key, /*$src = this._src, */$html = '',
		$ActiveX = (typeof ActiveXObject != 'undefined' && window.print && !/Opera/.test(navigator.userAgent)),
		$flashvars = 'unFocusFlashMovieId=' + this._properties.id + '&unFocusHistoryUpdate=' + location.hash.substring(1);
	
	if (this._flashVars) $flashvars += '&' + this._flashVars;
	
	if (this._src) {
		//$src =  + '?' + $flashvars;
		if ($ActiveX)
			this._params.movie = this._src;
		else
			this._properties.src = this._src;
	} else {
		// throw an exception
		throw new Error('MissingValue', 'Please supply the missing required value "src".');
	}
	
	if ($ActiveX) {
		$html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
		$html += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+this._majorVersion+',0,'+this._minorVersion+','+this._betaVersion+'"';
	} else {
		$html += '<embed';
	}
	
	// output  properties
	for ($key in this._properties) {
		$html += ' ' + $key + '="' + this._properties[$key]+'"';
	}

	if ($ActiveX) {
		$html += '\>\n';
		for ($key in this._params) {
			$html += '<param name="' + $key + '" value="' + this._params[$key] + '"\>';
		}
		$html += '<param name="flashvars" value="' + $flashvars + '"\>';
	} else {
		for ($key in this._params) {
			$html += ' ' + $key + '="' + this._params[$key]+'"';
		}
		$html += ' flashvars="' + $flashvars + '"';
	}
	
	// outputs the rest of the platform specific stuff
	if ($ActiveX) {
		$html += '"></object\>';
	} else {
		$html += '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"\></embed\>';
	}
	return $html;
};