/*-------------------------------------------------
Compatible with jQuery 1.3.2
Requires jQuery.flash
written by Andrew Gruner
-------------------------------------------------*/
(function($)
{
	$.fn.flashtxt = function(htmlOptions, pluginOptions)
	{
		// Set the options.
		var htmlOptions = $.extend({}, $.fn.flashtxt.defaults.htmlOptions, htmlOptions);
		var pluginOptions = $.extend({}, $.fn.flashtxt.defaults.pluginOptions, pluginOptions);

		// Go through the matched elements and return the jQuery object.
		return this.each(function()
		{
			var $this = $(this);
			
			// get the object's text for passing to flash
			htmlOptions.flashvars.txt = $this.html();
			
			// get the object's color (not sure how to set a default color without it being a global setting)
			htmlOptions.flashvars.txtColor = '0x' + rgbToHex($this.css('color'));
			
			// call the flash plugin with our new options
			$this.flash(htmlOptions, pluginOptions);
		});
	};

	// Public defaults.
	$.fn.flashtxt.defaults = 
	{
		htmlOptions:
		{
			wmode: 'transparent',
			flashvars: {}
		},
		pluginOptions:
		{
			version: 9,
			update: false // don't replace anything if flash is not installed
		}
	};
	
	// Private functions.
	function rgbToHex(rgbString)
	{
		var parts = rgbString.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
		// parts now should be ["rgb(0, 70, 255", "0", "70", "255"]
		if (parts) {
			delete (parts[0]);
			for (var i = 1; i <= 3; ++i)
			{
			    parts[i] = parseInt(parts[i], 10).toString(16);
			    if (parts[i].length == 1) parts[i] = '0' + parts[i];
			}
			var hexString = parts.join(''); // "0070ff"
			return hexString;
		};
	};
})(jQuery);
