/* Utility functions */
var util = {
    // return a number from 0 to upper
    random : function(upper) {
	return Math.floor( Math.random() * (upper));
    }
};

var DivMap = function() {
    // prototype for DivMap objects

    // legend for how to read the bitmap
    this.on = "o";
    this.off = "_";

    // bitmap for what we want to display
    this.bitmap = [ 
	"oo_____o______________o_oo_________________o_",
	"_o_______________________o_________________o_",
	"_ooo__oo_oooo__oooo__oo__o_o____oooo___oo_ooo",
	"_o__o__o__o__o__o__o__o__oo______o__o_oooo_o_",
	"_o__o__o__o__o__o__o__o__o_o_____o__o_o____o_",
	"oo__o_oo__ooo__oo__o_oo_oo_oo_o_oo__o__oo__oo",
	"__________o__________________________________",
	"_________oo__________________________________"
    ];

    // color tables for the color blind painter
    this.redColors = ["#a88","#b99","#caa","#dbb","#ecc"];
    this.blueColors = ["#8a8","#9b9","#aca","#bdb","#cec"];

    // color table for the google painter
    this.googleColors = ["#d00", "#0d0", "#00d", "#dd0"];

};

// clear out the divmap container
DivMap.prototype.clear = function(container) {
    container.empty();
};

// paints the divmap with the given paintermethod
DivMap.prototype.paint = function(container, painter) {
    this.clear(container);
    var xMax = this.bitmap[0].length;
    var yMax = this.bitmap.length;
    for(y=0; y<yMax; y++) {
	var row = $('<div></div>').attr('class', 'bitrow').attr('id', 'row' + y);
	for(x=0; x<xMax; x++) {
	    var bit = $('<div></div>').attr('id', y + "x" + x);
	    // apply this as scope, in case the painter needs to access the bitmap
	    painter.apply(this, [bit, x, y] );
	    row.append(bit);
	}
	container.append(row);
    }
};


/* Painter methods:
 * swap out different painters to get different effects
 */
DivMap.prototype.blankpainter = function(bit, x, y) {
    bit.attr('class', 'bit off');
    return bit;
};

DivMap.prototype.bitmappainter = function(bit, x, y) {
    if (this.bitmap[y].charAt(x) == this.on)
	{ bit = bit.attr('class', 'bit on'); }
    else { bit = bit.attr('class', 'bit off'); }
    return bit;
};

DivMap.prototype.inversedpainter = function(bit, x, y) {
    if (this.bitmap[y].charAt(x) == this.off)
	{ bit = bit.attr('class', 'bit on'); }
    else { bit = bit.attr('class', 'bit off'); }
    return bit;
};

DivMap.prototype.colorblindpainter = function(bit, x, y) {
    if (this.bitmap[y].charAt(x) == this.on) {
	bit = bit.attr('class','bit').css(
	    {backgroundColor: this.redColors[util.random(this.redColors.length)] }
	);
    }
    else {
	bit = bit.attr('class','bit').css(
	    {backgroundColor: this.blueColors[util.random(this.blueColors.length)] }
	);
    }
    return bit;
};

DivMap.prototype.googlepainter = function(bit, x, y) {
    if (this.bitmap[y].charAt(x) == this.on) {
	bit = bit.attr('class','bit on').css(
	    {backgroundColor: this.googleColors[util.random(this.googleColors.length)] }
	);
    }
    else {
	bit = bit.attr('class','bit off');
    }
    return bit;
};
