/*
	unFocusFlash, version 1.5a (alpha) (2005/08/20)
	Copyright: 2004-2005, Kevin Newman (http://www.unfocus.com/)
	License: http://creativecommons.org/licenses/LGPL/2.1/

	Version History
	1.0 - initial release
	1.1 - added mousewheel event forwarder - updates the flash variable UnfocusFlashWheelEvent
	1.2 - removed large portions (migrated to unFocusFlashCommunicator).
	
	1.5a - complete rewrite/rename getting ready for 2.0
*/
if (typeof com == 'undefined') var com = {};
if (!com.unFocus) com.unFocus = {};
if (!com.unFocus.Flash) com.unFocus.Flash = {};
com.unFocus.Flash.HTML = function() {
	var _properties = {},
		_params = {},
		_majorVersion = 0,
		_minorVersion = 0,
		_betaVersion = 0,
		_flashVars='',
		_src='';
	
	// setters check against valid values
	// ref http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_12701
	this.SetSrc = function($src) {
		_src = $src;
	};
	this.SetWidth = function($width) {
		_properties.width = $width;
	};
	this.SetHeight = function($height) {
		_properties.height = $height;
	};
	this.SetId = function($id) {
		_properties.id = $id;
	};
	this.SetName = function($name) {
		_properties.name = $name;
	};
	
	this.SetMovie = function($src) {
		this.SetSrc($src);
	};
	this.SetSwliveconnect = function($swliveconnect) {
		if (typeof $swliveconnect == 'boolean')
			_params.swliveconnect = $swliveconnect;
		else
			throw new InvalidValueException('InvalidValue', 'Valid Values for swliveconnect: true, false');
	};
	this.SetPlay = function($play) {
		if (typeof $loop == 'boolean')
			_params.play= $play;
		else
			throw new InvalidValueException('InvalidValue', 'Valid Values for play: true, false');
	};
	this.SetLoop = function($loop) {
		if (typeof $loop == 'boolean')
			_params.loop= $loop;
		else
			throw new InvalidValueException('InvalidValue', 'Valid Values for loop: true, false');
	};
	this.SetMenu = function($menu) {
		if (typeof $menu == 'boolean')
			_params.menu= $menu;
		else
			throw new InvalidValueException('InvalidValue', 'Valid Values for menu: true, false');
	};
	this.SetQuality = function($quality) {
		switch ($quality) {
			case 'low':
			case 'medium':
			case 'high':
			case 'autolow':
			case 'autohigh':
			case 'best':
				_params.quality = $quality; /* low, medium, high, autolow, autohigh, best */
				break;
			default:
				throw new InvalidValueException('InvalidValue', 'Valid Values for quality: low, medium, high, autolow, autohigh, best');
		}
	};
	this.SetScale = function($scale) {
		switch ($scale) {
			case 'showall':
			case 'noborder':
			case 'exactfit':
			case 'noscale':
				_params.scale = $scale; /* showall, noborder, exactfit, noscale (missing from the documentation) */
				break;
			default:
				throw new InvalidValueException('InvalidValue', 'Valid Values for scale: showall, noborder, exactfit, noscale');
		}
	};
	this.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':
				_params.align = $align; /* l, t, r, b (defaults to center, which isn't in the list) */
				break;
			default:
				throw new InvalidValueException('InvalidValue', 'Valid Values for align: l, t, r, b');
		}
	};
	this.SetSalign = function($salign) {
		switch ($salign) {
			case 'l':
			case 't':
			case 'r':
			case 'b':
			case 'tl':
			case 'tr':
			case 'bl':
			case 'br':
				_params.salign = $salign;
				break;
			default:
				throw new InvalidValueException('InvalidValue', 'Valid Values for salign: l, t, r, b, tl, tr, bl, br');
		}
	};
	this.SetWmode = function($wmode) {
		switch($wmode) {
			case 'window':
			case 'opaque':
			case 'transparent':
				_params.wmode = $wmode;
				break;
			default:
				throw new InvalidValueException('InvalidValue', 'Valid Values for wmode: window, opaque, transparent');
		}
	};
	this.SetBgcolor = function($bgcolor) {
		// /^#[\dA-Fa-f]{6}$/
		if (/^#[\dA-F]{6}$/.test($bgcolor))
			_params.bgcolor = $bgcolor; /* #RRGGBB, hexadecimal RGB value */
		else
			throw new InvalidValueException('InvalidValue', 'Valid Values for bgcolor: a valid html color hex value (#0099FF)');
	};
	this.SetBase = function($base) {
		_params.base = $base;
	};
	this.SetFlashvars = function($flashvars) {
		_flashVars = $flashvars;
	};
	
	
	
	// http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_16494
	this.SetAllowscriptaccess = function($allowscriptaccess) {
		switch ($allowscriptaccess) {
			case 'never':
			case 'always':
				_params.allowscriptaccess = $allowscriptaccess;
				break;
			default:
				throw new InvalidValueException('InvalidValue', 'Valid Values for allowscriptaccess: never, always');
		}
	};
	
	// for version stuffs
	this.SetMinorVersion = function($minorVersion) {
		_minorVersion = $minorVersion;
	};
	this.SetMajorVersion = function($majorVersion) {
		_majorVersion = $majorVersion;
	};
	this.SetBetaVersion = function($betaVersion) {
		_betaVersion = $betaVersion;
	};
	
	/**
	 * Generates the platform specific HTML for the flash movie
	 *
	 * @return String containing the platform specific HTML for the flash movie
	 */
	this.GetHTML = function() {
		// initialize local variables
		var $key, $html = '', $src;
		// process flashVars
		//if (_flashVars) {
		_params.flashvars = _flashVars + '&unFocusFlashMovieId=' + _properties.id;
		//}
		if (_src) {
			//if (_flashVars)
			$src = _src + '?' + _params.flashvars;
			if (typeof ActiveXObject != 'undefined')
				_params.movie = $src;
			else
				_properties.src = $src;
		} else {
			// throw an exception
			throw new MissingValueException('MissingValue', 'Please supply the missing required value "src".');
		}
		
		
		if (typeof ActiveXObject != 'undefined') {
			$html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
			$html += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+_majorVersion+',0,'+_minorVersion+','+_betaVersion+'"';
		} else {
			$html += '<embed';
		}
		
		// output  properties
		for ($key in _properties) {
			$html += ' ' + $key + '="' + _properties[$key]+'"';
		}
		if (typeof ActiveXObject != 'undefined') {
			$html += '\>\n';
			for ($key in _params) {
				$html += '<param name="' + $key + '" value="' + _params[$key] + '"\>';
			}
		} else {
			for ($key in _params) {
				$html += ' ' + $key + '="' + _params[$key]+'"';
			}
		}
		
		// outputs the rest of the platform specific stuff
		if (typeof ActiveXObject != 'undefined') {
			$html += '"></object\>';
		} else {
			$html += '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"\></embed\>';
		}
		return $html;
	};
};
com.unFocus.Flash.HTML.prototype.toString = function() {
	// :NOTE: we have to deal with exceptions here
	try {
		var html = this.GetHTML();
	} catch (e) {
		html = '<em\>' + e.name + ':</em\> ' + e.message;
	} finally {
		return html;
	}
};