Text = new Class({ 
	Implements: [Options],

	options: {
		textfile: 'images/berlin.png', //font file
		width: 32,//width of characters in pixels
		height:32,//height of characters in pixels
		onload: ''
		//rows:16,
		//cols:16
	},
	initialize: function(options){
		this.setOptions(options);
		window.addEvent('domready', function() {
			this.loadText();
			ctx.drawText = function(text,x,y,fontsize){Text.drawText(text,x,y,fontsize)};
		}.bind(this));
		this.img = new Image();
		this.img.src = this.options.textfile;
		this.img.onload = (function(){this.options.onload.delay(500,this)}).bind(this);
	},
	loadText: function(){
		this.fontArray = new Array();
		f = this.fontArray;//shorten Naming convention
		f[' '] = [0,2,7];
		f['0'] = [0,3,17];
		f['1'] = [1,3,16];
		f['2'] = [2,3,16];
		f['3'] = [3,3,16];
		f['4'] = [4,3,16];
		f['5'] = [5,3,16];
		f['6'] = [6,3,16];
		f['7'] = [7,3,16];
		f['8'] = [8,3,16];
		f['9'] = [9,3,16];
		f[':'] = [10,3,16];
		f['A'] = [1,4,18];
		f['B'] = [2,4,18];
		f['C'] = [3,4,18];
		f['D'] = [4,4,18];
		f['E'] = [5,4,18];
		f['F'] = [6,4,18];
		f['G'] = [7,4,16];
		f['H'] = [8,4,18];
		f['I'] = [9,4,18];
		f['J'] = [10,4,18];
		f['K'] = [11,4,18];
		f['L'] = [12,4,18];
		f['M'] = [13,4,18];
		f['N'] = [14,4,18];
		f['O'] = [15,4,19];
		f['P'] = [0,5,18];
		f['Q'] = [1,5,18];
		f['R'] = [2,5,18];
		f['S'] = [3,5,14];
		f['T'] = [4,5,18];
		f['U'] = [5,5,18];
		f['V'] = [6,5,18];
		f['W'] = [7,5,18];
		f['X'] = [8,5,18];
		f['Y'] = [9,5,18];
		f['Z'] = [10,5,18];
		f['a'] = [1,6,17];
		f['b'] = [2,6,14];
		f['c'] = [3,6,14];
		f['d'] = [4,6,14];
		f['e'] = [5,6,14];
		f['f'] = [6,6,14];
		f['g'] = [7,6,14];
		f['h'] = [8,6,14];
		f['i'] = [9,6,14];
		f['j'] = [10,6,14];
		f['k'] = [11,6,14];
		f['l'] = [12,6,14];
		f['m'] = [13,6,18];
		f['n'] = [14,6,14];
		f['o'] = [15,6,14];
		f['p'] = [0,7,14];
		f['q'] = [1,7,14];
		f['r'] = [2,7,13];
		f['s'] = [3,7,15];
		f['t'] = [4,7,13];
		f['u'] = [5,7,14];
		f['v'] = [6,7,14];
		f['w'] = [7,7,14];
		f['x'] = [8,7,14];
		f['y'] = [9,7,14];
		f['z'] = [10,7,14];
	},
	drawCharacter: function(ch,x,y){
		if(this.fontArray[ch] != undefined){
			ctx.drawImage(this.img, 
				this.options.width*this.fontArray[ch][0], 
				this.options.height*this.fontArray[ch][1], 
				this.options.width, 
				this.options.height, 
				x, y, 
				(this.options.width/32)*this.fontSize, 
				(this.options.height/32)*this.fontSize);

		}
	},
	drawText: function(text,x,y,fontsize){
		if(fontsize)
			this.fontSize = fontsize;
		else 
			this.fontSize = 32;
		runningWidth = 0;
		for(i=0;i<text.length;i++){
			character = text.substr(i,1);
			this.drawCharacter(character,x+runningWidth,y);
			if(this.fontArray[character] !=undefined && this.fontArray[character][2] !=undefined)
				runningWidth += (this.fontArray[character][2]/32)*this.fontSize; 
			else
				runningWidth += this.options.width;
		}
	}
});
//Text = new Text();
