function Sprite(x, y, canvas) {
	this.img = null;
	this.x = x;
	this.y = y;
    this.xPixels = 1;
    this.yPixels = 1;
    this.directionH = 1;
	this.directionV = 1;
    this.spriteMap = null;
    this.colorMap = null;
    this.pixelWidth = 1;
    this.pixelHeight = 1;
	this.imageData = null;
	this.canvas = canvas;
	this.context = this.canvas.getContext('2d');
	this.defaultPixelSize = 1;
}

// Constants
Sprite.SPRDIR_LEFT = -1;
Sprite.SPRDIR_RIGHT = 1;

Sprite.prototype.setDefaultPixelSize = function(size) {
	this.defaultPixelSize = size;
	return this;
}

Sprite.prototype.getWidth = function() {
	return this.xPixels * this.pixelWidth;
}

Sprite.prototype.getHeight = function() {
	return this.yPixels * this.pixelHeight;
}

Sprite.prototype.setPosition = function(x, y) {
	return this.setX(x).setY(y);
}

Sprite.prototype.setX = function(x) {
	this.x = x;
	return this;
}

Sprite.prototype.setY = function(y) {
	this.y = y;
	return this;
}

Sprite.prototype.renderPixels = function(imageData, spriteMap, colorMap) {
	var i, m, j = 0;

	for (i = 0; i < this.yPixels * this.pixelHeight; i++) { // entire "original" sprite row
		if (this.directionH != Sprite.SPRDIR_LEFT) {
			for (m = 0; m < this.xPixels; m++) { // entire sprite "pixel"
				j = this.setPixelColors(imageData.data, i, m, j, spriteMap, colorMap);
			}
		} else {
			for (m = this.xPixels - 1; m >= 0; m--) { // entire sprite "pixel"
				j = this.setPixelColors(imageData.data, i, m, j, spriteMap, colorMap);
			}
		}
	}
	return this;
}

Sprite.prototype.setPixelColors = function(pixels, row, col, curr, spriteMap, colorMap) {
	var max = curr + (this.pixelWidth * 4);
	var spriteMapVal = spriteMap[Math.floor(row / this.pixelHeight)][col];
	var colorIndex = colorMap[spriteMapVal];
	if (spriteMapVal == 0 || !colorIndex) {
		colorIndex = [0, 0, 0, 0];
	}
	for (curr; curr < max; curr += 4) {
		pixels[curr]     = colorIndex[0];
		pixels[curr + 1] = colorIndex[1];
		pixels[curr + 2] = colorIndex[2];
		pixels[curr + 3] = colorIndex[3];
	}
	return curr;
}

Sprite.prototype.render = function() {
	this.imageData = this.context.createImageData(this.getWidth(), this.getHeight());
	this.renderPixels(this.imageData, this.spriteMap, this.colorMap);
	return this;
}

Sprite.prototype.setDirectionH = function(direction) {
	if (this.directionH != direction) {
	    this.directionH = direction;
		this.render();
	}
	return this;
}

Sprite.prototype.setSpriteMap = function(map) {
	if (!map.length) {
		console.log("Error: the sprite map provided is not an array.");
		return false;
	}
	if (map.length < 1) {
		console.log("Error: the sprite map provided must have at least one element.");
		return false;
	}
    this.spriteMap = map;
	this.yPixels = map.length;
	
	// find the longest vertical length, assign it to yPixels
	var longestVert = 0;
	for (var i = 0; i < map.length; i++) {
		if (!map[i].length) { // check if array
			continue;
		}
		longestVert = (map[i].length > longestVert) ? map[i].length : longestVert;
	}
	this.xPixels = longestVert;
	
	return this;
}

Sprite.prototype.setColorMap = function(map) {
    this.colorMap = map;
	return this;
}

Sprite.prototype.setPixelSize = function(width, height) {
    switch (arguments.length) {
        case 1:
            this.pixelWidth = this.pixelHeight = width;
            break;
        case 2:
            this.pixelWidth = width;
            this.pixelHeight = height;
            break;
        default:
            this.pixelWidth = this.pixelHeight = 1;
    }
	return this;
}

Sprite.prototype.draw = function() {
	if (!this.imageData || !this.context)
		return false;
	this.context.putImageData(this.imageData, this.x, this.y);
	return this;
}
