/** cleartype.js
 */
(function() {
	var p = getScriptPath();
	jQuery.fn.cleartype = function(opt){
		opt = jQuery.extend({
			font: '_ƒSƒVƒbƒN'
		},opt);
		
		var target = this;
		var flashVersion = getFlashVersion().version;
		var src = p.substring( 0, p.lastIndexOf( '/' )+1 ) + 'cleartype.swf';
		
		if ( flashVersion[0] >= 8 ) {
			jQuery(target).each(function() {
				var $$ = jQuery(this);
				$$.prepend('<span style="visibility: hidden;">&nbsp;</span>');
				var s = $$.children('span').eq(0).height();
				$$.children('span').eq(0).remove();
				
				var styles = {
					ff:		opt.font,
					fw:		fixFontWeight( $$.css('fontWeight') ),
					fs:		s,
					ml:		$$.css('paddingLeft').replace('px',''),
					mr:		$$.css('paddingRight').replace('px',''),
					w:		$$.outerWidth(),
					h:		$$.height(),
					c:		fixColor( $$.css('color') ),
					ta:		$$.css('textAlign'),
					td:		$$.css('textDecoration'),
					ti:		$$.css('textIndent').replace('px',''),
					ls:		$$.css('letterSpacing'),
					va:		$$.css('verticalAlign')
				};
				
				var flashvars = '';
				for ( var prop in styles ) {
					flashvars += prop + '=' + styles[ prop ] + '&';
				}
				flashvars += 't='+$$.text();
				
				if ( opt.shadow ) {
					flashvars += '&ds='+opt.shadow.distance+','+opt.shadow.angle+','+opt.shadow.color.replace('#','0x')+','+opt.shadow.alpha+','+opt.shadow.blur+','+opt.shadow.strength;
					styles.h  += opt.shadow.distance;
				}
				if ( opt.glow ) {
					flashvars += '&gl='+opt.glow.color.replace('#','0x')+','+opt.glow.alpha+','+opt.glow.blur+','+opt.glow.strength;
					styles.h  += opt.glow.blur;
				}
				
				$$.css({ paddingLeft: 0, paddingRight: 0 });
				$$.html('<object width="'+styles.w+'" height="'+styles.h+'" style="display: block;">\
<param name="movie" value="'+src+'" />\
<param name="quality" value="high" />\
<param name="wmode" value="transparent" />\
<param name="salign" value="TL" />\
<param name="FlashVars" value="'+flashvars+'" />\
<embed src="'+src+'" quality="high" salign="TL" wmode="transparent" width="'+styles.w+'" height="'+styles.h+'" FlashVars="'+flashvars+'"\
 type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" style="display: block;" />');
			});
		}
	};
})(jQuery);

/** getScriptPath
 *  reference: http://d.hatena.ne.jp/Climber/20070711/1184115807
 */
function getScriptPath () {
	var f=function(e){
		var name=e.tagName;
		if(!!name && name.toUpperCase()=='SCRIPT') return e;
		var c=e.lastChild;
		return (!!c)?f(c):null;
	};
	var es=f(document);
	if(!es) return window.location;
	return es.getAttribute('src') || window.location;
};

/** getFlashVersion
 *	reference: www.yahoo.com
 */
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;
}

/** fixColor
 */
function fixColor ( c ) {
	if ( c.match(/rgb.*\((\d+),\s(\d+),\s(\d+)\)/i) ) {
		c = ( parseInt(RegExp.$1) * Math.pow(16,4) + parseInt(RegExp.$2) * Math.pow(16,2) + parseInt(RegExp.$3) ).toString(16);
		while ( c.length < 6 ) {
			c = '0' + c;
		}
		c = '#' + c;
	}
	return c;
}

/** fixFontWeight
 */
function fixFontWeight ( n ) {
	if ( !isNaN( n ) ) {
		if ( n-0 > 500 )
			n = 'bold';
		else
			n = 'normal';
	}
	return n;
}

/* End Of File */