function createCSS(selector, declaration) {
	var ua = navigator.userAgent.toLowerCase();
	var isIE = (/msie/.test(ua)) && !(/opera/.test(ua)) && (/win/.test(ua));

	var style_node = document.createElement("style");
	style_node.setAttribute("type", "text/css");
	style_node.setAttribute("media", "screen"); 

	if (!isIE) style_node.appendChild(document.createTextNode(selector + " {" + declaration + "}"));

	document.getElementsByTagName("head")[0].appendChild(style_node);

	if (isIE && document.styleSheets && document.styleSheets.length > 0) {
		var last_style_node = document.styleSheets[document.styleSheets.length - 1];
		if (typeof(last_style_node.addRule) == "object") last_style_node.addRule(selector, declaration);
	}
};

function fontGenerator() {
	this.chars = new Array(),
	
	this.init = function(font,name,image,gridwidth,grid,offset,spacing,jitter) {
	
		alphabet = ['A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i','J','j','K','k','L','l',
		'M','m','N','n','O','o','P','p','Q','q','R','r','S','s','T','t','U','u','V','v','W','w','X','x','Y','y','Z','z'];
		numbers = ['zero','one','two','three','four','five','six','seven','eight','nine'];
		for(x in alphabet) {
			this.chars.push([alphabet[x], alphabet[x]]);
		}
		this.chars.push(['blank', ' ']);
		for(x in numbers) { this.chars.push([numbers[x], x]); }
		this.chars.push(
			['minus','-'],['plus','+'],['dot','.'],['comma',','],['exclaim','!'],['question','?'],['less','<'],['greater','>'],
			['lbracket','('],['rbracket',')'],['equals','='],['colon',':'],['semi',';'],['at','@'],['percent','%'],['caret','^'],
			['and','&'],['star','*'],['under','_'],['speech','"'],['apos','\''],['dollar','$'],['pound','&pound;'],['squiggle','~'],
			['hash','#'],['lsbracket','['],['rsbracket',']'],['lcbracket','{'],['rcbracket','}'],['fslash','/'],['bslash','\\'],
			['pipe','|'],['tilde','¬'],['mark','`']
		);

		if(image) { this.image = image; } else { this.image = 'fonts/handwritten_font_2.png'; }
		if(gridwidth) { this.gridwidth = gridwidth; } else { this.gridwidth = 25; }
		if(grid) { this.grid = grid; } else { this.grid = [24,28]; }
		if(offset) { this.offset = offset; } else { this.offset = [4,1]; }
		if(spacing) { this.spacing = spacing; } else { this.spacing = [0,28]; }
		if(jitter) { this.jitter = jitter; } else { this.jitter = 0; }
		
		if(name) {
			this.name = name;
			this.generate();
		}
		
		if(font) {
			this.font = font;
			document.body.innerHTML += '<link rel="stylesheet" href="'+font+'" />';
		}
		
		
	},
		
	this.generate = function() {
		x=0;
		y=0;
		createCSS('.font.'+this.name,
			'width: '+this.grid[0]+'px; height: '+this.grid[1]+'px; background: transparent url('+this.image+') no-repeat top left; display: inline; display: inline-block; clear: none; border: none; position: relative;'
		);
		for(var z in this.chars) {
			offsetPos = 
				-(this.offset[0]+(this.grid[0]*x)+(this.spacing[0]*x))+'px '+
				-(this.offset[1]+(this.grid[1]*y)+(this.spacing[1]*y))+'px';
			createCSS('.font.'+this.name+'.'+this.chars[z][0], 'background-position: '+offsetPos);
			x+=1;
			nextchar = this.offset[0]+this.grid[0]*(x+1)
			if(x+1 > this.gridwidth) { y += 1; x=0; }
			x = parseInt(x);
			y = parseInt(y);
		}
	},
	
	this.parse = function(string) {
		buffer = ''
		style = ''
		strings = String(string).split(/( |\r|\n)/);
		for(string in strings) {
			string = String(strings[string]);
			if (string != 'undefined') {
				buffer += '<span class="font '+this.name+' word">';
				for(var i=0; i<string.length; i++) {
					for(var x in this.chars) {
						if(string.charAt(i) == this.chars[x][1]) {
							if(this.jitter > 0) {
								jitterX = Math.floor(Math.random()*this.jitter);
								jitterY = Math.floor(Math.random()*this.jitter);
								style = 'style="top: '+jitterX+'px; left: '+jitterY+'px"';
							}
							buffer += '<span class="font '+this.name+' '+this.chars[x][0]+'" '+style+'></span>';
							break;
						}
						else if(string.charAt(i) == '\n') { buffer += '<br />'; }
					}
				}
				buffer += '</span>';
			}
		}
		return buffer;
	}
		
}

