/*************************************************************************************************************
	
	developer : idw1@naver.com
	date: 2006-08-01

*************************************************************************************************************/

/*************************************************************************************************************
	
	MATRIX

*************************************************************************************************************/

MATRIX = function() {
	this.create = function(col, row) {
		this.col = col;
		this.row = row;
		this.array = new Array();

		for( var y=0; y<this.row; y++ ) {
			this.array.push([]);
			for( var x=0; x<this.col; x++ ) {
				this.array[y].push(0);
			}
		}
	}

	this.destroy = function() {
		for( var y=0; y<this.row; y++ ) {
			for( var x=0; x<this.col; x++ ) {
				delete this.array[y][x];
			}
			delete this.array[y];
		}
	}

	this.initValue = function(value) {
		for( var y=0; y<this.row; y++ ) {
			for( var x=0; x<this.col; x++ ) {
				this.array[y][x] = value;
			}
		}
	}

	this.getValue = function(x, y) {
		if( !this.array || x < 0 || x > this.col-1 || y < 0 || y > this.row-1 ) {
			return 0;
		}
		return this.array[y][x];
	}

	this.setValue = function(x, y, value) {
		if( !this.array || x < 0 || x > this.col-1 || y < 0 || y > this.row-1 ) {
			return;
		}
		this.array[y][x] = value;
	}
}

/*************************************************************************************************************
	
	cell

*************************************************************************************************************/

CELL = function(id, parent_id) {
	CELL.superclass.constructor.call(this, id, parent_id);
}

Class(
	CELL, OBJECT,

	"create", function(x, y) {
		this.x = x;
		this.y = y;
		DW_UTIL.addDiv(this.id, this.parent_id);
	},

	"destroy", function() {
		DW_UTIL.safeDestroy(this.id, this.parent_id);

		delete this.x;
		delete this.y;
	},

	"setXYWH", function(x, y, w, h) {
		DW_UTIL.setXYWH(this.id, x, y, w, h);
	},

	"setColor", function(color) {
		color = color || "#fff";
		YAHOO.util.Dom.setStyle(this.id, "background", color);
	},

	"setImage", function(image) {
		if( !this.image ) {
			this.image = DW_UTIL.addImage("", this.id);
			YAHOO.util.Dom.setStyle(this.image, "width", "100%");
			YAHOO.util.Dom.setStyle(this.image, "height", "100%");
		}
		DW_UTIL.setImage(this.image, image);
	},

	"setBorder", function(size, color) {
		var border = "solid " + size + "px " + color;
		YAHOO.util.Dom.setStyle(this.id, "border", border);
	}
);

/*************************************************************************************************************
	
	cell

*************************************************************************************************************/

CELLMAP = function(id, parent_id) {
	CELLMAP.superclass.constructor.call(this, id, parent_id);
}

Class(
	CELLMAP, OBJECT,

	"offset_size", 0,
	"border_size", 3,
	"border_color", "#eee",
	"bgcolor", "#f00",

	"init", function(id, parent_id) {
		CELLMAP.superclass.init.call(this, id, parent_id);
		this.cell = {
			border_size: 1,
			border_color: "#999",
			width: 15,
			height: 15,
			bgcolor: "#fff"
		};
	},

	"create", function(col, row) {
		this.map = new MATRIX();
		this.map.create(col, row);
		this.width = this.offset_size * 2 + this.cell.width * col + this.cell.border_size * (col + 1);
		this.height = this.offset_size * 2 + this.cell.height * row + this.cell.border_size * (row + 1);

		var el = DW_UTIL.addDiv(this.get_uid("container"), this.parent_id);
		DW_UTIL.setXYWH(el, 0, 0, this.width, this.height);
		YAHOO.util.Dom.setStyle(el, "background", this.bgcolor);
		YAHOO.util.Dom.setStyle(el, "border", "solid " + this.border_size + "px " + this.border_color);

		for( var x=0; x<col; x++ ) {
			for( var y=0; y<row; y++ ) {
				this.setCell(x, y);
			}
		}
	},

	"destroy", function() {
		for( var x=0; x<this.map.col; x++ ) {
			for( var y=0; y<this.map.row; y++ ) {
				var cell = this.map.getValue(x, y);
				if( typeof cell == "object" ) {
					cell.destroy();
				}
			}
		}
		this.map.destroy();

		delete this.map;
		delete this.cell;
		delete this.offset_size;
		delete this.border_size;
		delete this.border_color;
		delete this.bgcolor;
		delete this.width;
		delete this.height;

		DW_UTIL.safeDestroy(this.get_uid("container"), this.parent_id);
	},

	"setCell", function(x, y) {
		var id = this.get_uid(x + "_" + y);
		var parent_id = this.get_uid("container");
		var border = "solid " + this.cell.border_size + "px " + this.cell.border_color;

		var cell = new CELL(id, parent_id);
		cell.create(x, y);
		cell.setXYWH(this.offset_size + (this.cell.width + this.cell.border_size) * x, this.offset_size + (this.cell.height + this.cell.border_size) * y, this.cell.width, this.cell.height);
		cell.setColor(this.cell.bgcolor);
		cell.setBorder(this.cell.border_size, this.cell.border_color);

		this.map.setValue(x, y, cell);
	},

	"getCell", function(x, y) {
		return this.map.getValue(x, y);
	}
);
