/*
 *    See http://swapped.cc/font-smoothing for details
 */
function getFontSmoothing() {

	try {
		var canvasNode = document.createElement('canvas');
		canvasNode.width = "35";
		canvasNode.height = "35"

		canvasNode.style.display = 'none';
		document.body.appendChild(canvasNode);
		var ctx = canvasNode.getContext('2d');

		ctx.fillStyle = "white";
		ctx.fillRect(0,0,35,35);

		ctx.textBaseline = "top";
		ctx.font = "32px Georgia";
		ctx.fillStyle = "black";
		ctx.strokeStyle = "black";

		ctx.fillText("O", 0, 0);

		var type = 10;
		
		for (var j = 0; j <= 32; j++) {
			for (var i = 0; i <= 32; i++) {
				var pixel = ctx.getImageData(i, j, 1, 1).data;

				if (pixel[0] == 255 && pixel[1] == 255 && pixel[2] == 255)
					continue;

				if (pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 0)
					continue;

				if (pixel[0] != pixel[1] || 
				    pixel[0] != pixel[2] ||
				    pixel[1] != pixel[2])
				{
					return 30;
				}
				else
				{
					type = 20;
				}
			}
		}
		
		return type;
			
	} catch (ex) { 
	
		if (typeof(screen.fontSmoothingEnabled) != "undefined")
			return screen.fontSmoothingEnabled ? 25 : 0;

		return 0;
	}
}

/**
 *	$ap - this is a reworked version of the original 
 *	      javascript to pack the code into a simple
 *	      function instead of a class
 *
 *	http://www.lalit.org/lab/javascript-css-font-detect
 */

/**
 * JavaScript code to detect available availability of a 
 * particular font in a browser using JavaScript and CSS. 
 * 
 * Author : Lalit Patel
 * Website: http://www.lalit.org/lab/jsoncookies
 * License: Creative Commons Attribution-ShareAlike 2.5
 *          http://creativecommons.org/licenses/by-sa/2.5/
 * Version: 0.15 
 *          changed comparision font to serif from sans-serif, 
 *          as in FF3.0 font of child element didn't fallback 
 *          to parent element if the font is missing.
 * Updated: 09 July 2009 10:52pm
 * 
 */
function isFontPresent(font){

	var h = document.getElementsByTagName("BODY")[0];
	var d = document.createElement("DIV");
	var s = document.createElement("SPAN");

	d.appendChild(s);
	d.style.fontFamily = "sans";
	s.style.fontFamily = "sans";
	s.style.fontSize   = "72px";
	s.innerHTML        = "mmmmmmmmmmlil";
	h.appendChild(d);

	var defWidth   = s.offsetWidth;
	var defHeight  = s.offsetHeight;

	s.style.fontFamily = font;
	curWidth  = s.offsetWidth;
	curHeight = s.offsetHeight;
	h.removeChild(d);

	return (defWidth != curWidth || defHeight != curHeight);
}

/*
 *    The following code automatically runs the test on page 
 *    load and submits the result to the sample repository via 
 *    http://swapped.cc/font-smoothing/sample
 */
function addHandler(obj, ev, func)
{
	if (obj.addEventListener)
		obj.addEventListener(ev, func, false);
	else
	if (obj.attachEvent)
		obj.attachEvent('on' + ev, func);
}

function loadUrl(url)
{
	var iframe = document.createElement('iframe');
	iframe.style.display = 'none';
	document.body.appendChild(iframe);
	iframe.src = url;
}

function checkFontSmoothing()
{
	var fonts = [ 
			'calibri', 
			'candara', 
			'segoe ui', 
			'lucida sans unicode', 
			'ms trebuchet', 
			'verdana',
			'helvetica',
			'lucida grande',
			'georgia',
			'arial',
			'droid sans',
			'some random name xyzz',
			'trebuchet ms',
			'optima',
			'helvetica neue',
	         ];
	
	/*
	 *	v2 - trebuchet ms
	 *	v3 - optima, helvetica neue
	 */
	var fl = '3';

	for (var i=0, n=fonts.length; i<n; i++)
	{
		fl = fl + (isFontPresent(fonts[i]) ? 'y' : 'n');	
	}

	var fs = getFontSmoothing();
	loadUrl( 'http://swapped.cc/font-smoothing/sample?' +
	         's=' + fs + '&' +
		 'f=' + fl + '&' +
		 '_=' + (1000000 + Math.floor(Math.random()*8999999)) );
}

addHandler(window, 'load', checkFontSmoothing);


