
(function() {
	jQuery.fn.cleartype = function(opt){
		opt = jQuery.extend({
			src: 'cleartype.swf'
		},opt);
		
		var target = this;
		var flashVersion = _getFlashVersion().version;
		
		if ( flashVersion[0] >= 8 ) {
			jQuery(target).each(function() {
				jQuery(this).prepend('<span style="visibility: hidden;">&nbsp;</span>');
				var s = $(this).children('span').eq(0).height();
				$(this).children('span').eq(0).remove();
				
				var styles = {
					ff:		$(this).css('fontFamily').replace(/"/g, "'"),
					fw:		_fontWeightFix( $(this).css('fontWeight') ),
					fs:		s,
					//pt:		$(this).css('paddingTop').replace('px',''),
					//pb:		$(this).css('paddingBottom').replace('px',''),
					//pl:		$(this).css('paddingLeft').replace('px',''),
					//pr:		$(this).css('paddingRight').replace('px',''),
					w:		$(this).width(),
					h:		$(this).height() * 1.5,
					c:		_colorFix( $(this).css('color') ),
					ta:		$(this).css('textAlign'),
					ti:		$(this).css('textIndent').replace('px',''),
					ls:		$(this).css('letterSpacing'),
					va:		$(this).css('verticalAlign')
				};
				
				var flashvars = '';
				for ( var prop in styles ) {
					flashvars += prop + '=' + styles[ prop ] + '&';
				}
				flashvars += 'tx='+$(this).text();
				flashvars = flashvars.replace(/"/,"'");
				
				$(this).css({ /*padding: 0,*/ textIndent: 0, overflow: 'hidden' });
				
				$(this).html('<object width="'+styles.w+'" height="'+styles.h+'">\
<param name="movie" value="'+opt.src+'" />\
<param name="quality" value="high" />\
<param name="wmode" value="transparent" />\
<param name="salign" value="TL" />\
<param name="FlashVars" value="'+flashvars+'" />\
<embed src="'+opt.src+'" quality="high" wmode="transparent" width="'+styles.w+'" height="'+styles.h+'" FlashVars="'+flashvars+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
			});
		}
	};
})(jQuery);

function _colorFix ( code ) {
	if ( code.match(/rgb.*\((\d+),\s(\d+),\s(\d+)\)/i) ) {
		code = ( parseInt(RegExp.$1) * Math.pow(16,4) + parseInt(RegExp.$2) * Math.pow(16,2) + parseInt(RegExp.$3) ).toString(16);
		while ( code.length < 6 ) {
			code = '0' + code;
		}
		code = '#' + code;
	}
	return code;
}

function _fontWeightFix ( num ) {
	if ( !isNaN( num ) ) {
		if ( num-0 > 500 )
			num = 'bold';
		else
			num = 'normal';
	}
	return num;
}

/* Refer to www.yahoo.com */
/* To get version of flash player */
function _getFlashVersion () {
var o = {installed:0, version:[]}, description, oActiveX, nMajor, nMinor;
if (navigator.plugins && typeof navigator.plugins["Shockwave Flash"] == "object") {
o.pluginType = "npapi";
description = navigator.plugins["Shockwave Flash"].description;
if (typeof description != "undefined") {
description = description.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
nMajor = parseInt(description.replace(/^(.*)\..*$/, "$1"), 10);
nMinor = /r/.test(description) ? parseInt(description.replace(/^.*r(.*)$/, "$1"), 10) : 0;
o.version = [nMajor, nMinor];
o.installed = 1;
}
}
else if (window.ActiveXObject) {
o.pluginType = "ax";
try {
oActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
}
catch (e) {
try {
oActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
o.version = [6, 0];
o.installed = 1;
oActiveX.AllowScriptAccess = "always";
}
catch (e) {
if (o.version[0] == 6) {o.installed=1; return; }
}
try {
oActiveX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
}
catch (e) {
}
}
if (typeof oActiveX == "object") {
description = oActiveX.GetVariable("$version");
if (typeof description != "undefined") {
description = description.replace(/^\S+\s+(.*)$/, "$1").split(",");
o.version = [parseInt(description[0], 10), parseInt(description[2], 10)];
o.installed = 1;
}
}
}
return o;
}