function Chip8ScreenASCII(oScreenSpace)
{
	this._strBackColor = "#000000";
	this._strForeColor = "#eeeeee";

	this._aPixels = new Array();
	this._aPixelColors = new Array();
	this._iScreenWidth = 64;
	this._iScreenHeight = 32;
	this._iPixelSize = 8;

	this._iScrollOffsetX = 0;
	this._iScrollOffsetY = 0;
	this._bScroll = false;

	this._iScrollScreenOffsetX = 0;
	this._iScrollScreenOffsetY = 0;

	this._fYScale = 1.3;

	this._oScreen = document.createElement("div");
	this._oScreen.style.position = "absolute";
	this._oScreen.style.width = this._iScreenWidth * this._iPixelSize;
	this._oScreen.style.height = this._iScreenHeight * this._iPixelSize * this._fYScale;

	this._oScreen.style.backgroundColor = this._strForeColor;
	this._strForeColor = this._oScreen.style.backgroundColor;

	this._oScreen.style.backgroundColor = this._strBackColor;
	this._strBackColor = this._oScreen.style.backgroundColor;

	this._oPixelCtr = document.createElement("div");
	this._oPixelCtr.style.position = "absolute";
	this._oPixelCtr.style.width = this._iScreenWidth * this._iPixelSize;
	this._oPixelCtr.style.height = this._iScreenHeight * this._iPixelSize * this._fYScale;
	this._oPixelCtr.style.left = 0;
	this._oPixelCtr.style.top = 0;

	this._oScreen.appendChild(this._oPixelCtr);

	oScreenSpace.appendChild(this._oScreen);

	this._bInit = false;
}

Chip8ScreenASCII.prototype.setScale = function(fScale)
{
}

Chip8ScreenASCII.prototype.setSize = function(iW, iH)
{
	var bRedraw = false;

	if (iW != this._iScreenWidth || iH != this._iScreenHeight)
		bRedraw = true;

	this._iScreenWidth = iW;
	this._iScreenHeight = iH;

	if (bRedraw) {
		this.redraw();
	} else {
		this.clear();
	}
}


Chip8ScreenASCII.prototype.clear = function()
{
	if (!this._bInit) {
		this.init();
		return;
	}

	for (var x=0;x<this._aPixels.length;x++) {
		var col = this._aPixels[x];
		for (var y=0;y<col.length;y++) {
			col[y].style.color = this._strBackColor;
		}
	}
}

Chip8ScreenASCII.prototype.init = function()
{
	this._aPixels = new Array();
	for (var x=0;x<this._iScreenWidth;x++) {
		this._aPixels[x] = new Array();
		var col = this._aPixels[x];
		var l = x * this._iPixelSize;
		for (var y=0;y<this._iScreenHeight;y++) {
			var p = document.createElement("span")
			col[y] = p;
			var s = p.style;
			s.position = "absolute";
			s.overflow = "hidden";
			s.color = this._strBackColor;
			s.left = l;
			s.top = y * this._iPixelSize * this._fYScale;
			s.width = this._iPixelSize;
			s.height = this._iPixelSize * this._fYScale;
			s.fontFamily = "terminal";
			//s.fontWeight = "bold";
			s.fontSize = "8px";
			p.innerHTML = "0";
			p.onselectstart = function(){return false;}
			this._oPixelCtr.appendChild(p);
		}
	}
	this._iScrollOffsetX = 0;
	this._iScrollOffsetY = 0;

	this._oScreen.style.width = this._iScreenWidth * this._iPixelSize;
	this._oScreen.style.height = this._iScreenHeight * this._iPixelSize * this._fYScale;
	this._oPixelCtr.style.width = this._iScreenWidth * this._iPixelSize;
	this._oPixelCtr.style.height = this._iScreenHeight * this._iPixelSize * this._fYScale;
}


Chip8ScreenASCII.prototype.redraw = function()
{
	for (var x=0;x<this._aPixels.length;x++) {
		for (var y=0;y<this._aPixels[x].length;y++) {
			this._oPixelCtr.removeChild(this._aPixels[x][y]);
			delete this._aPixels[x][y];
		}
	}
	this.init();
}

Chip8ScreenASCII.prototype.drawPixel = function(x,y,i,j) {
	var px = x; // + i;
	var py = y; // + j;

	var p = this._getPixel(px,py);

	if (p) {
		if (p.style.color == this._strForeColor) {
			p.style.color = this._strBackColor;
			return 1;
		} else {
			p.style.color = this._strForeColor;
		}
	}
}

Chip8ScreenASCII.prototype._getPixel = function(x,y)
{
	x -= this._iScrollOffsetX;
	while (x > this._iScreenWidth-1) x -= this._iScreenWidth;
	while (x < 0) x += this._iScreenWidth;

	y -= this._iScrollOffsetY % this._iScreenHeight;
	while (y > this._iScreenHeight-1) y -= this._iScreenHeight;
	while (y < 0) y += this._iScreenHeight;

	if (!this._aPixels[x]) {
		//wrtdbg("ERR: Tried to get invalid pixel x:" + x + " y:" + y + "\r\n");
		return;
	}

	if (!this._aPixels[x][y]) {
		//wrtdbg("ERR: Tried to get invalid pixel x:" + x + " y:" + y + "\r\n");
		return;
	}

	var p = this._aPixels[x][y];
	return p;
}

Chip8ScreenASCII.prototype.cleanUp = function()
{
}
