function Screen(w, h, s, div) {
	this.width = w;
	this.height = h;
	this.scale = s;
	this.data = new Array(this.width);
	this.canvas;
	this.ctx;
	this.newpixels;
	this.scanline;
	if (this.scale > 0) {
		this.width = this.width * this.scale;
		this.height = this.height * this.scale;
		// Create the canvas element
		this.canvas = document.createElement("canvas");
		this.canvas.width = this.width;
		this.canvas.height = this.height;
		this.ctx= this.canvas.getContext("2d");
		this.ctx.fillStyle = "rgb(0,0,0)";
		this.ctx.fillRect(0, 0, this.width, this.height);
		this.scanline = 4 * this.width;
		
		var dest = document.getElementById(div);
		dest.appendChild(this.canvas);
	}
	else {
		this.screenDiv = document.createElement("DIV");
		this.screenDiv.style.position = "relative";
		this.screenDiv.style.left = "0px";
		this.screenDiv.style.top = "0px";
		this.screenDiv.style.width = this.width * -this.scale;
		this.screenDiv.style.height = this.height * -this.scale;
		this.screenDiv.style.overflow = "hidden";
		this.screenDiv.style.backgroundColor = "000000";
		for (y = 0; y < this.height; y ++) 
		{
			var offsety = (this.width  * y) 
			for(x = 0; x < this.width; x ++) 
			{
				
				var offset = offsety+ x;
				
				var pixel = document.createElement("DIV");
				pixel.style.position = "absolute";
				pixel.style.left = x * -this.scale;
				pixel.style.top = y * -this.scale;
				pixel.style.width = -this.scale;
				pixel.style.height = -this.scale;
				pixel.style.overflow = "hidden";
				pixel.style.backgroundColor = "000000";
				pixel.style.value = 0;

				this.data[offset] = pixel.style;
				this.screenDiv.appendChild(pixel);
			}
		}
		
		var dest = document.getElementById(div);
		dest.appendChild(this.screenDiv);
	}
}

Screen.prototype.lockGraphics = function() {
	this.newpixels  =  this.ctx.getImageData(0, 0, this.width, this.height);
	this.data = this.newpixels.data;
}

Screen.prototype.unlockGraphics = function() {
	this.ctx.putImageData(this.newpixels, 0, 0);
}

Screen.prototype.putScaledPixel = function(offset, color) {
	var offsety = 0;
	if (this.scale > 0) {
		for (var y = 0; y < this.scale; y++) {
			for (var x = 0; x < this.scale; x++) {
				this.data[offset + offsety + (x<<2)] = (color>>16)&0xff;
				this.data[offset + offsety + (x<<2) + 1] = (color>>8)&0xff;
				this.data[offset + offsety + (x<<2) + 2] = color&0xff;
			}
			offsety += this.scanline;
		}
	}
	else {
		this.data[offset].backgroundColor = color;
	}
}

Screen.prototype.putScaledPixelXY = function(xp, yp, color) {
	var offsety = 0;
	if (this.scale > 0) {
		offsety = (yp * this.scanline);
		
		offset = (offsety +(xp<<2))*this.scale;
		offsety = 0;
		for (var y = 0; y < this.scale; y++) {
			for (var x = 0; x < this.scale; x++) {
				this.data[offset + offsety + (x<<2)] = (color>>16)&0xff;
				this.data[offset + offsety + (x<<2) + 1] = (color>>8)&0xff;
				this.data[offset + offsety + (x<<2) + 2] = color&0xff;
			}
			offsety += this.scanline;
		}
	}
	else {
		this.data[offset].backgroundColor = color;
	}
}

Screen.prototype.clearScreen = function() 
{
	if (this.scale > 0) {
		for (var i = 0; i < this.data.length; i ++) 
		{
			if ((i%4) == 3)
				this.data[i] = 0xff;
			else
				this.data[i] = 0x00;
		}
	}
	else {
		for (i = 0; i < this.data.length; i ++) 
		{
			this.data[i].backgroundColor = "000000";
			this.data[i].value = 0;
		}
	}
}

Screen.prototype.clearScreenRGB = function(color) 
{
	if (this.scale > 0) {
	}
	else {
		for (i = 0; i < this.data.length; i ++) 
		{
			this.data[i].backgroundColor = color;
			this.data[i].value = 0;
		}
	}
}

Screen.prototype.destroy = function() {
        var cell = this.screenDiv;
	if (!cell) return;
        if ( cell.hasChildNodes())
        {
            while ( cell.hasChildNodes() )
            {
                cell.removeChild( cell.firstChild );       
            }
        }
	if (cell.parentNode)
		cell.parentNode.removeChild(cell);
}	

