// grid.js

var mGridDebug = null;
var kDefaultCellHeight = 64;
var kDefaultCellWidth = 22;

function SetUpDebug()
	{
	if (mGridDebug == null)
		{
		mGridDebug = new PanelDebug();
		}	
	}

//---------------------------------------------------------
function Grid(o)
	{
	SetUpDebug();
	o.tag = "div";
	o.overflow = "auto";
	
	if (o.parent == undefined) o.parent = document.getElementsByTagName("body")[0];
	
	this.div = document.createElement("div");
	this.currentRow = -1;
	var s = this.div.style;
	s.position = "absolute";
	s.top = o.top;
	s.left = o.left;
	s.height = o.height;
	s.width = o.width;
	s.overflow = "auto";
	if (o.border != undefined) s.border = o.border;
	
	if (o.fontColor != undefined) this.fontColor = o.fontColor;
	if (o.cellCursor != undefined) this.cellCursor = o.cellCursor;
	
	if (o.pictFolder != undefined) {
		this.pictFolder = o.pictFolder;
		this.rows = o.pictFolder.GetTotalFiles();
		this.cols = 1;
	}
	if (o.dragPictFolderPath != undefined) {
		this.dragPictFolderPath = o.dragPictFolderPath;
	}
	if (o.cellView != undefined){
		this.cellView = o.cellView;
		}
		
	if (o.dragArea != undefined) {
		this.dragArea = o.dragArea;
	}
	
	if (o.selectRowHandler == undefined) this.selectRowHandler = DummyFunc;
	else this.selectRowHandler = o.selectRowHandler;
	this.DrawTable(o);
	o.parent.appendChild(this.div);
	}
//---------------------------------------------------------
Grid.prototype.DrawTable = function(o)
	{
	// create <table> and <tbody> elements
	this.table = document.createElement("table");
	this.table.obj = this;
	var s = this.table.style;
	s.position = "absolute";
	s.left = 0;
	s.top = 0;
    var tblBody = document.createElement("tbody");

	// create all cells
	for(var row = 0; row < this.rows; row++) 
		{
		// create a <tr> element
        var rowElem = document.createElement("tr");
		var cellElem;
		
		for(var col = 0; col < this.cols; col++) 
			{
			cellElem = document.createElement("td");
			cellElem.setAttribute("id",row+","+col);
			cellElem.grid = this;
			if (this.cellCursor != undefined) cellElem.cursor = this.cellCursor;

			cellElem.onmousedown = GridMouseDownFunc;
			cellElem.onmouseover = GridMouseOverFunc;
			cellElem.onmouseout = GridMouseOutFunc;
			
			var s = cellElem.style;
			s.top = 0;
			s.left = 0;
			if (o.maxCellSize != undefined) {
				s.height = o.maxCellSize.height;
				s.width = o.maxCellSize.width;
			}
			else {
				s.height = kDefaultCellHeight;
				s.width = kDefaultCellWidth;
				}
			if (this.pictFolder != undefined)
				{
				cellElem.setAttribute("align","center");
				cellElem.setAttribute("valign","middle");
				var img = this.pictFolder.GetImageByIndex(row);
				if (this.cellView == "bkgnd") this.PutBkgndInCell(img, cellElem);
				this.PutImageInCell(img, cellElem);
				}
			else
				{
          		var textElem = document.createTextNode(".");
				cellElem.appendChild(textElem);		
				}
 			
			if (this.fontColor != undefined) s.color = this.fontColor;
			s += " border: 1 dotted rgb(0,255,0)";
			rowElem.appendChild(cellElem);
            }
		tblBody.appendChild(rowElem);
        }

	this.table.appendChild(tblBody);
	this.div.appendChild(this.table);
	this.table.setAttribute("border","1");
    }

//---------------------------------------------------------
Grid.prototype.GetCell = function(row, col)
	{
	var theRow = this.table.getElementsByTagName("tr")[row];
	return theRow.getElementsByTagName("td")[col];	
	}
//---------------------------------------------------------
Grid.prototype.SetCellImage = function(path, row, col)
	{
	var cell = GetCell(row,col);
	var imgObj = new Image({
		src:path
	});
	imgObj.elem.id = cell.id;
	imgObj.elem.grid = this;
	cell.appendChild(imgObj.elem);
	delete imgObj;
	}

//---------------------------------------------------------
Grid.prototype.PutImageInCell = function(path, cell)
	{
//	name = cell.name;
//	cell.innerHTML = "<img src='"+path+"';/>";
//	cell.name = name;
	
//	cell.innerHTML = "<div name:'"+cell.name+"'; valign:'middle'; align:'center'; <img src='"+path+"';/>/>";
//return;

	var imgObj = new Image({
		src:path
	});
	var e = imgObj.elem;
//	e.name = cell.name;
	e.grid = this;
	var id = cell.id;
	cell.id = "";
	e.id = id;
	cell.appendChild(e);
	delete imgObj;
	}
//---------------------------------------------------------
Grid.prototype.PutBkgndInCell = function(path, cell)
	{
	cell.style.backgroundImage = "url("+path+")";
	}


//---------------------------------------------------------
Grid.prototype.SelectRow = function(row)
	{
	if (this.currentRow > -1)
		{
		this.GetCell(this.currentRow,0).style.border = "";
		}
	this.GetCell(row,0).style.border = "medium dashed red";
	this.currentRow = row;
	this.selectRowHandler(this,row);
	}

//---------------------------------------------------------
function GridMouseDownFunc(e)
	{
	mGridDebug.AddText("Freakin' mouse went down\n");
	//    document.attachEvent("onmousemove", MouseMoved);
	if (e == undefined) e = window.event;
	var target = GetEventTarget(e);
	var d = target.id.split(",");
	target.grid.SelectRow(d[0]);
	}

//---------------------------------------------------------
function GridMouseOverFunc(e){
//	GridMouseDownFunc(e);
}
//---------------------------------------------------------
function GridMouseOutFunc(e){
//	GridMouseDownFunc(e);
	//	Drag_Begin(e);
}


//---------------------------------------------------------
function TrackGrid(e){
	if (e == undefined) e = window.event;
	var target = GetEventTarget(e);
	target.onmousedown = DragObject_Begin;
}

//---------------------------------------------------------
function DummyFunc(){
}

