//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014

function Text(name, alignment)
{
	this.__proto__ = new MenuItem(name);
	this.redraw = true;
	
	this.alignment = alignment ? alignment : 'center';
	
	this.SetFontSize = function(size)
	{
		this.__proto__.SetFontSize(size);
		this.GenerateSize();
	}
	
	this.SetText = function(text)
	{
		this.DrawClear();
		this.name = text;
		this.GenerateSize();
	}
	
	this.GenerateSize = function()
	{
		gDraw.menu.font	 = GetFont(this.fontSize);
		gDraw.menu.textAlign	 = this.alignment;
		gDraw.menu.textBaseline	 = 'middle';
		this.width = gDraw.menu.measureText(this.name).width;
		this.redraw = true;
	}

	this.GetDrawX = function()
	{
		return this.x - (this.width/2);
	}

	this.GetDrawY = function()
	{
		return this.y - (this.height/2);
	}

	this.Redraw = function()
	{
		this.redraw = true;
	}

	this.DrawClear = function()
	{
		if(this.redraw)
		{
			var clearPadding = 10;
			var x = this.GetDrawX() - clearPadding/2;
			var y = this.GetDrawY() - clearPadding/2;
			if(this.alignment == 'left' || this.alignment == 'start')
			{
				x += this.width/2;
			}
			else if(this.alignment == 'right' || this.alignment == 'end')
			{
				x -= this.width/2;
			}
			gDraw.menu.clearRect(x, y, this.width + clearPadding, this.height + clearPadding);
		}
	}
	
	this.Draw = function()
	{
		if(this.redraw)
		{
			gDraw.menu.save();
	
			gDraw.menu.font	 = GetFont(this.fontSize);
			gDraw.menu.textAlign	 = this.alignment;
			gDraw.menu.textBaseline	 = 'middle';
			gDraw.menu.fillTextShadow(this.name, this.x, this.y, "rgb(255, 255, 255)", "rgb(0, 0, 0)");
	
			gDraw.menu.restore();
			this.redraw = false;
		}
	}
}
