// Divcanvas and pens

function divcanvas(pen)
{
	this.color = '#FFFF00';
	this.divin = document.createElement("div");
	this.getPen = dc_getPen;
	this.setPen = dc_setPen;
	this.getInDiv = dc_getInDiv;
	this.renderAsciiBuffer = dc_renderAsciiBuffer;
	if (pen == 'undefined')
	{
		alert('test');
		pen = new roundPen('16px', '16px');
	}
	this.setPen(pen);
}

function dc_getPen()
{
	return this.pen;
}

function dc_setPen(pen)
{
	this.pen = pen
}

function dc_getInDiv()
{
	return this.divin;
}

function squarePen(pensize, gridsize)
{
	this.rlo_ok = true;
	this.draw = sp_draw;
	this.renderAsciiBuffer = pen_renderAsciiBuffer;
	this.setSize = sp_setSize;
	if (pensize == 'undefined')
	{
		pensize = '4px';
	}
	if (gridsize == 'undefined')
	{
		gridsize = pensize;
	}
	this.setSize(pensize, gridsize);
}

function roundPen(pensize, gridsize)
{
	this.rlo_ok = false;
	this.draw = rp_draw;
	this.renderAsciiBuffer = pen_renderAsciiBuffer;
	this.setSize = rp_setSize;
	if (pensize == 'undefined')
	{
		pensize = '4px';
	}
	if (gridsize == 'undefined')
	{
		gridsize = pensize;
	}
	this.setSize(pensize, gridsize);
}

function rp_setSize(pensize, gridsize)
{
	this.pensize = pensize;
	if (gridsize == 'undefined')
	{
		this.gridsize = pensize
	}
	else
	{
		this.gridsize = gridsize;
	}
}

function sp_draw(x, y, width, height, color)
{
	var div = document.createElement("div");
	div.style.position = "absolute";
	div.style.top = y;
	div.style.left = x;
	div.style.width = width;
	div.style.height = height;
	div.style.backgroundColor = color;
	div.style.fontSize = '0px';
	return div;
}

function rp_draw(x, y, width, height, color)
{
	var div = document.createElement("div");
	div.style.position = "absolute";
	div.style.top = y;
	div.style.left = x;
	div.style.width = width;
	div.style.height = height;
	div.style.color = color;
	div.style.fontFamily = "Lucida sans unicode";
	div.innerHTML = '&#9679;';
	div.style.fontSize = parseInt(this.pensize) * 1.7 + "px";
	return div;
}


function sp_setSize(pensize, gridsize)
{
	this.pensize = pensize;
	if (gridsize == undefined)
	{
		this.gridsize = pensize
	}
	else
	{
		this.gridsize = gridsize;
	}
}

function dc_renderAsciiBuffer(buffer, transform)
{
	this.pen.renderAsciiBuffer(this.divin, buffer, transform);
}

function pen_renderAsciiBuffer(canvasdiv, buffer, transform)
{
	var height = buffer.length;
	this.pixsize = '20px';
	for (y = 0; y < height; y++)
	{
		var ycalc = y * parseInt(this.gridsize);
		var xcalc = 0;
		for (x = 0; x < buffer[y].length; x++)
		{
			var p = buffer[y].substr(x,1);
			var color = transform[p];
			if (color != undefined)
			{
				canvasdiv.appendChild(this.draw(xcalc + "px", ycalc + "px", this.pensize, this.pensize, color));
			}
			// TODO: support without transforms too
			xcalc += parseInt(this.gridsize);
		}
	}
}
