var imTablePaint=false;
var size=5;
var imTableRows=30;
var imTableCols=30;
var imFg="rgb(255, 0, 0)";
var imBg="rgb(255, 255, 255)";
var imTable;

function imBuildSrchTable_new(numRows,numCols, pixSize)
{
   var table = document.createElement("table");
   var thisRow, thisCol, thisTbody,thisImg;

   if (!table) {
      return;
   }

   // Save the dimensions for future use
   imTableRows = numRows;
   imTableCols = numCols;

   // allocate the imTable also
   imTable = new Array(imTableRows);
   for (var i=0;i<imTableRows;i++) {
      imTable[i] = new Array(imTableCols); 
   }
   
   table.className = "imSrch";
   table.id = "imSrchTable";
   table.addEventListener("click", imTogglePaint, true);
   
   thisTbody = document.createElement("tbody");

   for (var row=0;row<numRows;row++) {
      thisRow = document.createElement("tr");
      thisRow.id = "imSrchRow";
      thisRow.className = "imSrch";
      for (col=0;col<numCols;col++) {
         thisCol = document.createElement("td");
         thisCol.id = "imSrchTd-"+row+"-"+col;
         thisCol.className = "imSrch";
         thisCol.onmouseover=imHighLightWrap;
         // thisCol.addEventListener("mouseover",imHighLightWrap,false);
         thisCol.width = pixSize;
         thisCol.border = 0;
         thisCol.style.background = imBg;
         imTable[row,col] = imBg;

         // add the spacer image to this table
         // data item
         thisImg = document.createElement("img");
         thisImg.src="imgs/spacer.gif";
         thisImg.width = pixSize;
         thisImg.height = pixSize;
         thisImg.border = 0;

         thisCol.appendChild(thisImg);

         thisRow.appendChild(thisCol);
      }
      thisTbody.appendChild(thisRow);
   }

   table.appendChild(thisTbody);

   var div = document.getElementById("imsrchTable");
   if (!div) {
      alert("Null div");
      return;
   }

   div.appendChild(table);

   // set bgcolor
   document.getElementById("imsrchPaper").style.background = imBg;
   document.getElementById("imsrchPen").style.background = imFg;

}

function imBuildSrchTable ()
{
   document.write("<table class=\"imSrch\"> id=\"imSrchTable\"");
   for (row=0;row<rows;row++) {
      document.write("<tr class=\"imSrch\">");
      for (col=0;col<cols;col++) {
         document.write("<td class=\"imSrch\" id=\"imSrchTd-"+row+"-"+col+"\" onMouseOver=\'imHighLight(\""+row+"\",\""+col+"\");\' width="+size+" onClick=\"togglePaint();\">");
         document.write("<img src=\"imgs/spacer.gif\" width="+size+" height="+size+" border=0></td>");
         // document.write("<a></a></td>");
      }
      document.write("</tr>");
   }
   document.write("</table>");
}


function imHighLightWrap(e)
{
   if (!imTablePaint) {
      return;
   }

   // more info about the event??
   e.currentTarget.style.background=imFg;
}

function imHighLight(row,col)
{
   if (!imTablePaint) {
      return;
   }

   var td = document.getElementById("imSrchTd-"+row+"-"+col);

   if (!td) {
      alert ("cant find " + row + ":" + col);
      return;
   }

   td.style.background=imFg;
   imTable[row,col] = imFg;
}

function imTogglePaint()
{
   imTablePaint = imTablePaint?false:true;
   document.getElementById("imsrchPen").style.backgroundColor =
      imFg;
}

function imAddButtons()
{
   var subButton = document.createElement ("button");
   var clearButton = document.createElement("button");

   subButton.value = "Search";
   clearButton.value = "Clear";
   clearButton.onClick = imClearTable();

   /*
   document.appendChild (subButton);
   document.appendChild (clearButton);
   */
}

function imClearTable()
{
   var table = document.getElementById('imSrchTable');
   for (var row=0;row<imTableRows;row++) {
      for (var col=0;col<imTableCols;col++) {
         var cellId = "imSrchTd-"+row+"-"+col;
         var cell = document.getElementById(cellId);
         if (cell) {
            cell.style.background = imBg;
            imTable[row,col] = imBg;
         }
      }
   }
}

function imToggleActiveColor()
{
   if (imFg == "#ff0000") {
      imFg = "#ffffff";
   }
   else {
      imFg = "#ff0000";
   }
}

function imSearch()
{
   alert ("Coming soon!");

   return;

   var table = new Array(imTableRows);
   for (var i=0;i<imTableRows;i++) {
      table[i] = new Array(imTableCols);
   }

   for (var row=0;row<imTableRows;row++) {
      for (var col=0;col<imTableCols;col++) {
         var cellId = "imSrchTd-"+row+"-"+col;
         var cell = document.getElementById(cellId);
         if (cell) {
            table[row,col] = cell.style.backgroundColor;
         }
      }
   }

   // convert the object to string
   var stringVal = JSONstring.make(imTable);

   alert(stringVal);
}

function imCreatePalette(tabId, divId, evLsnr)
{
   var colors = new Array (
         "#ff0000","#00ff00","#0000ff",
         "#ff00ff","#ffff00","#00ffff",
         "#000000","#888888","#ffffff" );

   var table = document.createElement("table");
   var colIndex=0;

   var row, col, body, img;

   if (!table)
      return;

   table.className="imsrchPalette";
   table.id = tabId;

   body = document.createElement("tbody");

   for (row=0;row<3;row++) {
      var thisRow = document.createElement("tr");

      for (col=0;col<3;col++) {
         var thisCol = document.createElement("td");
         thisCol.className = "imPalTd";

         var thisImg = document.createElement("img");

         thisImg.src="imgs/spacer.gif";
         thisImg.border = 0;

         thisCol.appendChild(thisImg);

         thisCol.style.background = colors[colIndex++];
         thisCol.addEventListener("click",evLsnr, true);

         thisRow.appendChild(thisCol);
      }
      body.appendChild(thisRow);

      table.appendChild(body);

      var div = document.getElementById(divId);
      div.appendChild(table);
   }
}

function imSetPen(e)
{
   imFg = e.currentTarget.style.backgroundColor;

   document.getElementById("imsrchPen").style.backgroundColor =
      imFg;
}

function imSetBg(e)
{
   var oldBg = imBg;
   imBg = e.currentTarget.style.backgroundColor;

   document.getElementById("imsrchPaper").style.background = imBg;

   var table = document.getElementById("imSrchTable");
   for (var row=0;row<imTableRows;row++) {
      for (var col=0;col<imTableCols;col++) {
         var cellId = "imSrchTd-"+row+"-"+col;
         var cell = document.getElementById(cellId);
         if (cell) {
            if (cell.style.background.indexOf(oldBg) != -1) {
               cell.style.background = imBg;
            }
            else {
               if (cell.style.background.indexOf(imBg) != -1) {
                  cell.style.background = oldBg;
               }
            }
         }
      }
   }
}
/*
JSONstring v 1.0
copyright 2006 Thomas Frank

This program is free software under the terms of the 
GNU General Public License version 2 as published by the Free 
Software Foundation. It is distributed without any warranty.


Based on Steve Yen's implementation:
http://trimpath.com/project/wiki/JsonLibrary
*/

JSONstring={
	compactOutput:false, 		
	includeProtos:false, 	
	includeFunctions: false,
	detectCirculars:true,
	restoreCirculars:true,
	make:function(arg,restore) {
		this.restore=restore;
		this.mem=[];this.pathMem=[];
		return this.toJsonStringArray(arg).join('');
	},
	toObject:function(x){
		eval("this.myObj="+x);
		if(!this.restoreCirculars || !alert){return this.myObj};
		this.restoreCode=[];
		this.make(this.myObj,true);
		var r=this.restoreCode.join(";")+";";
		eval('r=r.replace(/\\W([0-9]{1,})(\\W)/g,"[$1]$2").replace(/\\.\\;/g,";")');
		eval(r);
		return this.myObj
	},
	toJsonStringArray:function(arg, out) {
		if(!out){this.path=[]};
		out = out || [];
		var u; // undefined
		switch (typeof arg) {
		case 'object':
			this.lastObj=arg;
			if(this.detectCirculars){
				var m=this.mem; var n=this.pathMem;
				for(var i=0;i<m.length;i++){
					if(arg===m[i]){
						out.push('"JSONcircRef:'+n[i]+'"');return out
					}
				};
				m.push(arg); n.push(this.path.join("."));
			};
			if (arg) {
				if (arg.constructor == Array) {
					out.push('[');
					for (var i = 0; i < arg.length; ++i) {
						this.path.push(i);
						if (i > 0)
							out.push(',\n');
						this.toJsonStringArray(arg[i], out);
						this.path.pop();
					}
					out.push(']');
					return out;
				} else if (typeof arg.toString != 'undefined') {
					out.push('{');
					var first = true;
					for (var i in arg) {
						if(!this.includeProtos && arg[i]===arg.constructor.prototype[i]){continue};
						this.path.push(i);
						var curr = out.length; 
						if (!first)
							out.push(this.compactOutput?',':',\n');
						this.toJsonStringArray(i, out);
						out.push(':');                    
						this.toJsonStringArray(arg[i], out);
						if (out[out.length - 1] == u)
							out.splice(curr, out.length - curr);
						else
							first = false;
						this.path.pop();
					}
					out.push('}');
					return out;
				}
				return out;
			}
			out.push('null');
			return out;
		case 'unknown':
		case 'undefined':
		case 'function':
			out.push(this.includeFunctions?arg:u);
			return out;
		case 'string':
			if(this.restore && arg.indexOf("JSONcircRef:")==0){
				this.restoreCode.push('this.myObj.'+this.path.join(".")+"="+arg.split("JSONcircRef:").join("this.myObj."));
			};
			out.push('"');
			var a=['\n','\\n','\r','\\r','"','\\"'];
			arg+=""; for(var i=0;i<6;i+=2){arg=arg.split(a[i]).join(a[i+1])};
			out.push(arg);
			out.push('"');
			return out;
		default:
			out.push(String(arg));
			return out;
		}
	}
}
