/**

 jQuery Flash Text Replacement plugin (ftr)
  
 Version 0.2b
 12.02.2009
 
 Copyright (c) 2009 Ville Huumo, Byroo
 Dual licensed under the MIT and GPL licenses.
 http://www.opensource.org/licenses/mit-license.php
 http://www.opensource.org/licenses/gpl-license.php

**/

(function ()
{
	var $$;
	
	/**
	
	Replaces matched elements with a custom font 
	@use
		This plugin needs:
		- jQuery {@link http://www.jquery.com}
		- jQuery flash plugin {@link http://jquery.lukelutman.com/plugins/flash}
		- Ftr custom font file {@link http://www.byroo.fi/dev/JS/ftr/source/FtrFont.zip}
		
		Plugin source
		- {@link http://www.byroo.fi/dev/JS/ftr/source/jquery.ftr.js}
	@param font: Custom font file  (.swf) <code>String</code>
	@param userOptions: options <code>Object</code>
	@return jQuery
	@example
		Replaces all <code>h1</code> tags with Bookman font.
		<code>
			$('h1').ftr('fonts/bookman.swf');
		</code>
		Replaces all <code>h1</code> tags with Caslon font and adds space over the text
		<code>
			$('h1').ftr('fonts/caslon.swf',{topOffset:10});
		</code>
		Replaces all first <code>p</code> tag with Caslon font and sets element height to match text in swf
		<code>
			$('p:first').ftr('fonts/caslon.swf',{multiline:true});
		</code>
		
	*/
	
	$$ = jQuery.fn.ftr = function (font, userOptions)
	{	
		if (!font)
			return false;
			
		var options = {}; 
		for (var i in $$.options) options[i] = $$.options[i];
		for (var i in userOptions) options[i] = userOptions[i];
		
		var $block = $(this).flash(
			{
				src: options.path+font
			},
			{
				version: options.version,
				update: false
			},
			function (htmlOptions)
			{
				
				// Size
				htmlOptions.height = options.height || $(this).height();
				htmlOptions.width = options.width || $(this).width();				
				htmlOptions.flashvars.w = htmlOptions.width;
				htmlOptions.flashvars.h = htmlOptions.height;
				if (options.width) htmlOptions.flashvars.forceWidth = true;
				if (options.height) htmlOptions.flashvars.forceHeight = true;
				// Color && Wmode
				htmlOptions.bgcolor = options.background;
				htmlOptions.wmode = options.wmode;
				// Id
				$$.replacedElements++;
				htmlOptions.id = options.id + "-flash-" + $$.replacedElements;
				// Text
				htmlOptions.flashvars.txt = this.innerHTML;
				// Styles
				htmlOptions.flashvars.size = options.size || $(this).css("font-size");
				htmlOptions.flashvars.color = options.color || "#000000";//$$.rgbToHex($(this).css("color"));
				htmlOptions.flashvars.leading = options.leading || $(this).css("line-height");
				htmlOptions.flashvars.transform = options.transform || $(this).css("text-transform");
				htmlOptions.flashvars.multiline = options.multiline;
				htmlOptions.flashvars.topOffset = options.topOffset || 0;
				htmlOptions.flashvars.lefOffset = options.leftOffset || 0;
				htmlOptions.flashvars.extendWidth = options.extendWidth || 0;
				htmlOptions.flashvars.extendHeight = options.extendHeight || 0;
				htmlOptions.flashvars.hoverColor = options.hoverColor || htmlOptions.flashvars.color;
				
				// Callback
				htmlOptions.flashvars.callbackHandler = "$.fn.ftr.fit";
				htmlOptions.flashvars.callbackId = htmlOptions.id;
				htmlOptions.allowScriptAccess ="always";
				// Alternative
				this.innerHTML = '<span>'+this.innerHTML+'</span>';
				var $alt = $(this.firstChild);
				// Hide alt
				$alt.hide().addClass("alt")
				
				// Do the flash
				$(this)
					.addClass(options.id)
					.prepend($.fn.flash.transform(htmlOptions));
			}
		)
		return $block;
	}
	
	/**
	
	Changes given element width and height. Custom font will call this function to resize.
	
	@param id: id of the element <code>String</code>
	@param w: new width <code>Number</code>
	@param w: new height <code>Number</code>
	@return boolean	
	*/

	$$.fit = function (id,w,h)
	{	
		
		$("#"+id)
			.attr("width",w)
			.attr("height",h);
		return true;
	}
	
	/**
	
	Set default options
	
	@param options: options <code>Object</code>
	@example
		Available options are:
		
		<code>height</code> Height of the custom font. Default: replaced elements height determined by <code>jQuery.height()</code>.
		<code>width</code> Width of the custom font. Default: replaced elements width determined by <code>jQuery.width()</code>.
		<code>background</code> Background of the custom font. Default: <code>"#ffffff"</code>.
		<code>wmode</code> Wmode of the custom font. Default: <code>"transparent"</code>.
		<code>id</code> Id for the custom font. Default: <code>"ftr-replaced"</code>.	
		<code>size</code> Font size of the custom font. Default: <code>font-size</code> of the replaced element.	
		<code>color</code> Color of the custom font. Default: <code>color</code> of the replaced element.	
		<code>hoverColor</code> Hover color of the custom font. Default: same as <code>color</code>.	
		<code>leading</code> Leading of the custom font. Default: <code>line-height</code> of the replaced element.	
		<code>transform</code> Transform of the custom font. Default: <code>text-transform</code> of the replaced element.	
		<code>multiline</code> Text flow of the custom font. If false, element is resized to left, if <code>true</code>, element is resized to down. Default: <code>false</code>.
		<code>topOffset</code> Y position of the custom font. Default: <code>0</code>.
		<code>lefOffset</code> X position of the custom font. Default: <code>0</code>.
		<code>extendWidth</code> Relative width extend of the custom font. Default: <code>0</code>.
		<code>extendHeight</code> Relative width extend of the custom font. Default: <code>0</code>.
		<code>path</code> Path of the custom font file: Default <code>""</code>.
		<code>version</code> Flash version of the font file: Default <code>8</code>.
		
	*/
	
	$$.defaults = function (options)
	{
		$$.options = jQuery.extend($$.options, options);
	}
	
	$$.rgbToHex = function (rgb)
	{
		// To be fixed
		/*
		var re = /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/;
		var bits = re.exec(rgb);
		var r = parseInt(bits[1]).toString(16);
		var g = parseInt(bits[2]).toString(16);
		var b = parseInt(bits[3]).toString(16);
		if (r.length == 1) r = '0' + r;
		if (g.length == 1) g = '0' + g;
		if (b.length == 1) b = '0' + b;
		return '#' + r + g + b;
		*/
	}
	
	$$.replacedElements = 0;

	$$.options = {
		multiline: false,
		path: "",
		version: 8,
		background: "#ffffff",
		wmode: "transparent",
		id: "ftr-replaced"
	}
})();
