/**
 * @author Bruno
 */
function MB_dadi($canvas, dim, pos)
{
	var left = dim/5, center = dim/2, right = dim - left;
	this.radius = dim/9;
	this.$canvas = $canvas;
	this.width = dim;
	this.cornerRadius = Math.round(dim/6);
	this._position = {x:0, y:0};
	this._color = "#383";
	this._bgcolor = "#eee";
	if(pos.x && pos.y)
	{
		this._position.x = pos.x;
		this._position.y = pos.y;
		
	}
	this.value= 1;
	this.seedPositions = new Object();
	this.seedPositions.no = {x:left, y:left};
	this.seedPositions.n = {x:center, y:left};
	this.seedPositions.ne = {x:right, y:left};
	this.seedPositions.c = {x:center, y:center};
	this.seedPositions.so = {x:left, y:right};
	this.seedPositions.s = {x:center, y:right};
	this.seedPositions.se = {x:right, y:right};
	
	this.drawBackground = function()
	{
		this.$canvas.drawRect(
			{
				x:this._position.x,
				y:this._position.y,
				width:this.width,
				height:this.width,
				fromCenter:false,
				strokeStyle:'#000000',
				lineWidth:2,
				cornerRadius:this.cornerRadius,
				fillStyle:this.bgcolor()
			}
		)
		return this;
	};
	this.drawNumber = function()
	{
		var n = this.value;
		switch(n)
		{
			case 1:
				this.drawSeed('c');
			break;
			case 2:
				this.drawSeed('no');
				this.drawSeed('se');
			break;
			case 3:
				this.drawSeed('no');
				this.drawSeed('se');
				this.drawSeed('c');
			break;
			case 4:
				this.drawSeed('ne');
				this.drawSeed('no');
				this.drawSeed('se');
				this.drawSeed('so');
			break;
			case 5:
				this.drawSeed('ne');
				this.drawSeed('no');
				this.drawSeed('se');
				this.drawSeed('so');
				this.drawSeed('c');
			break;
			case 6:
				this.drawSeed('ne');
				this.drawSeed('n');
				this.drawSeed('no');
				this.drawSeed('se');
				this.drawSeed('s');
				this.drawSeed('so');
			break;

		}
		return this;
	};
	this.drawSeed = function(seed)
	{
		this.$canvas.drawArc(
			{
				x:this._position.x + this.seedPositions[seed].x,
				y:this._position.y + this.seedPositions[seed].y,
				fillStyle:this.color(),
				radius:this.radius
			}
		)
	};
	this.draw = function(n)
	{
		if (!isNaN(n) && n > 0 && n < 7)
			this.value = n;
		this
			.clear()
			.drawBackground()
			.drawNumber();
		return this;
	};
	this.clear = function()
	{
		this.$canvas.clearCanvas(
			{
				x:this._position.x + this.width/2 -1,
				y:this._position.y + this.width/2 -1,
				width:this.width +4,
				height:this.width +4,
				fromCenter:false
			}
		)
		return this;
	}
	this.position = function (pos)
	{
		if(!pos)
			return this._position;
		if(pos.x && pos.y)
			this.move(pos.x, pos.y);
		return this;
	}
	this.move = function(x, y)
	{
		this.clear();
		this._position.x = x;
		this._position.y = y;
		this.draw();
		return this;
	}
	this.color = function(c)
	{
		if (!c)
			return this._color;
		this._color = c;
		return this;
	}
	this.bgcolor = function(bgc)
	{
		if (!bgc)
			return this._bgcolor;
		this._bgcolor = bgc;
		return this;
	}
}
$(document).ready(function() {
	var 
		dado1 = new MB_dadi($('canvas'),150, {x:10,y:10}),
		dado2 = new MB_dadi($('canvas'), 150, {x:180,y:10}),
		randomDice, randomEnd;
	randomDice = function (delay)
	{
		var 
			n1 = n = 1 + Math.floor(Math.random() * 6),
			d = delay * 1.2;
		/*if (Math.random()>0.5)
		{
			c = dado1.color();
			bgc = dado1.bgcolor();
			dado1
				.color(bgc)
				.bgcolor(c);
			dado2
				.color(bgc)
				.bgcolor(c);
		}*/
		dado1.draw(n);
		n = 1 + Math.floor(Math.random() * 6);
		dado2.draw(n);
		if (delay > 400)
		{
			randomEnd(n1 + n);
			return;
			
		}
		setTimeout(randomDice, delay, [d]);
	}
	randomEnd = function (result)
	{
		$('#lancia_btn').show();
		window.console.log(result);
	}
	
	$('button').button();
	
	$('#lancia_btn').click(function()
	{
/*		var n = 1 + Math.floor(Math.random() * 6);
		dado1.draw(n);
		n = 1 + Math.floor(Math.random() * 6);
		dado2.draw(n);*/
		var p = randomDice(50);
		$(this).hide();
	})
});
