// Character widths of the default font.
UIDefaultCharWidths = new Array(0,8,8,7,7,7,7,8,8,8,8,8,8,8,8,8,
				7,7,8,8,8,8,8,8,8,8,7,7,7,8,8,8,
				4,1,4,5,5,5,5,2,4,4,4,5,1,5,1,5,
				5,5,5,5,5,5,5,5,5,5,1,1,4,5,4,5,
				6,5,5,5,5,5,5,5,5,3,5,5,3,5,5,5,
				5,5,5,5,5,5,5,5,5,5,5,3,5,3,5,5,
				2,5,5,5,5,5,4,5,5,1,5,5,2,5,5,5,
				5,5,5,5,3,5,5,5,5,5,5,4,1,4,6,5,
				5,5,5,5,5,5,5,5,5,5,5,3,5,2,5,5,
				5,5,5,5,5,5,5,5,5,5,5,5,5,5,3,5,
				5,2,5,5,5,5,5,5,5,6,5,5,5,1,5,5,
				8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
				8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
				8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
				7,6,7,7,6,7,7,7,6,7,7,6,8,8,5,6,
				6,6,6,6,8,8,6,7,6,8,8,8,7,6,8,0);

// Creates a color for use with a UICustomLabel							
Color = function(r, g, b)
{
	return {"r" : r, "g" : g, "b" : b};
};

// Label that will display text in a custom font. Takes a string, x position, y position,
// UICustomFont, and Color
UICustomLabel = function(s, x, y, f, c)
{
	this.container = new UIContainer(x,y);
	this.x = x;
	this.y = y;
	this.s = s;
	this.f = f;
	this.c = (typeof(c) == "undefined") ? Color(1,1,1) : c;
	
	var sX = 0;
	var sY = 0;
	for (var i=0; i<this.s.length; i++)
	{
		var c = this.s.charCodeAt(i);
		var u = (c % 16) * this.f.size;
		var v = ((c - (c % 16)) / 16) * this.f.size;
		
		var curSprite = UISprite(this.f.texture, sX, sY, this.f.size, this.f.size, u, v);
		curSprite.red = this.c.r;
		curSprite.green = this.c.g;
		curSprite.blue = this.c.b;
		
		this.container.add(curSprite);
		
		sX += this.f.widths[c] + 1
	}
}

UICustomLabel.prototype = {
	// Smoothly move to the given position
	"moveTo" : function(x, y)
	{
		this.container.moveTo(x, y);
		this.x = x;
		this.y = y;
	},

	// Smoothly move by the given ammount
	"moveBy" : function(dx, dy)
	{
		this.container.moveBy(dx, dy);
		this.x += dx;
		this.y += dy;
	},

	// Jumps to the given position without animating
	"jumpTo" : function(x, y)
	{
		this.container.x = x;
		this.container.y = y;
		this.x = x;
		this.y = y;
	},

	"changeString" : function(s)
	{
		this.s = s;
		this.update();
	},

	"changeFont" : function(f)
	{
		this.f = f;
		this.update();
	},

	"changeColor" : function(c)
	{
		this.c = c;
		this.update();
	},

	// This function should only be used if you change the objects properties directly without using the above methods
	"update" : function()
	{
		if (this.container.x != this.x || this.container.y != this.y)
		{
			this.container.x = this.x;
			this.container.y = this.y;
		}
		
		this.container.clear();
		
		var sX = 0;
		var sY = 0;
		for (var i=0; i<this.s.length; i++)
		{
			var c = this.s.charCodeAt(i);
			var u = (c % 16) * this.f.size;
			var v = ((c - (c % 16)) / 16) * this.f.size;
			
			var curSprite = UISprite(this.f.texture, sX, sY, this.f.size, this.f.size, u, v);
			curSprite.red = this.c.r;
			curSprite.green = this.c.g;
			curSprite.blue = this.c.b;
			
			this.container.add(curSprite);
			
			sX += this.f.widths[c] + 1
		}
		
		this.curText = this.s;
	}
};

// A font for use with a UICustomLabel. Takes the texture containing the characters 
// and an Array with the width of each character
UICustomFont = function(texture, charSize, charWidths)
{
	this.texture = texture;
	this.size = typeof(charSize) == "undefined" ? 16 : charSize;
	this.widths = typeof(charWidths) == "undefined" ? UIDefaultCharWidths : charWidths;
}

UICustomFont.prototype = {
	// Calculate the width of a string when displayed with this font
	"getStringWidth" : function(s)
	{
		var width = 0;
		
		for (var i=0; i<s.length; i++)
		{
			width += this.widths[s.charCodeAt(i)] + 1;
		}
		
		return width;
	}
};