(function($,$$){
	
	$$ = $.fn.typeset = function(htmlOptions, pluginOptions) {
		return this.flash(
			$.fn.flash.copy($$.htmlOptions, htmlOptions),
			$.fn.flash.copy($$.pluginOptions, pluginOptions),
			$$.replace
		);
	};
	
	$$.proxy = function(swf, css) {	
		console.log('proxy %s: %s', swf, css);	
		$$.swfs[swf].css(eval(css));
	};
	
	$$.replace = function(htmlOptions) {
			
		var node = this.cloneNode(true), $node = $(node);
		
		var tmp = this.cloneNode(true), $tmp = $(tmp)
			.html('<span class="typeset-tmp">'+node.innerHTML+'</span>');
				
		this.parentNode.replaceChild(tmp, this);
		
		$tmp.find(tmp.firstChild);
	
			$.extend(htmlOptions, {
				bgcolor: '#EEEEEE',
				height: $tmp.height(),
				width: $tmp.width()
			});
	
			$.extend(htmlOptions.flashvars, {
				css: htmlOptions.flashvars.css || $$.css,
				id: $$.swfs.length,
				txt: node.innerHTML
			});
		
			if(htmlOptions.flashvars.css.constructor == Function)
				htmlOptions.flashvars.css = htmlOptions.flashvars.css($tmp);
		
		$tmp.end();
						
		var replaced = this.cloneNode(true), $replaced = $(replaced)
			.addClass('typeset-replaced')
			.html('<span class="typeset-hidden">' + node.innerHTML + '</span>')
			.prepend($.fn.flash.transform(htmlOptions))
			.find('embed')
				.addClass('typeset-flash')
				.each(function(){
					$$.swfs.push($(this));
				})
				.bind('mouseover', function(){
					this.SetVariable('event','mouseover');
				})
				.bind('mouseout', function(){
					this.SetVariable('event','mouseout');
				})
				.bind('emchange', function(){
					//this.SetVariable('css','...');
				});
			
		tmp.parentNode.replaceChild(replaced, tmp);		
	};
	
	$$.css = 'base { font-size: 36px; line-height: 48px; }';
	
	$$.htmlOptions = {
		allowScriptAccess: 'always',
		flashvars: {
			txt: null,
			css: null
		}
	};
	
	$$.pluginOptions = {
		version: 7,
		express: false,
		update: false
	};
	
	$$.swfs = [];
	
	$$.version = '0.0.1';
	
	$(function(){
		var cssText = [
			'.typeset-hidden {',
				'display: block;',
				'height: 0;',
				'position: absolute;',
				'overflow: hidden;',
				'width: 0; }',
			'.typeset-tmp {',
				'display: block; ',
				'visibility: hidden; }',
			'.typeset-replaced embed {',
				'display: inline-block;',
				'margin: 0;',
				'padding: 0;',
				'position: relative;',
				'vertical-align: bottom; }',		
			'@media print {',				
				'.typeset-hidden {',
					'height: auto;',
					'position: static;',
					'width: auto; }',				
				'.typeset-replaced embed {',
					'display: none; }',				
			'}'		
		].join('');
		if(document.createStyleSheet) {		
			var o = document.createStyleSheet(null, 0);
			o.addRule('.dummy','display: static;');
			o.cssText = cssText;
		} else {
			$('head').prepend('<style type="text/css">'+cssText+'</style>');
		}
	});
	
})(jQuery);