var maxIterations = 30;

function colorChooser(n) {
	ch = Math.floor(n/maxIterations*255);
	if (n > (maxIterations/2)) {
		ch = 255 - ch;
	}
  b = Math.min(Math.floor(ch*1.7),255);
  r = Math.floor(ch*.6);
  g = Math.floor(ch*.9);
  return "rgb("+r+","+g+"," + b + ")";
}

function drawMset(x_center,y_center,zoom) {
	theTable = document.createElement("table");
	theTable.setAttribute("id","MsetTable");
	theTable.setAttribute("cellpadding","0");
	theTable.setAttribute("cellspacing","0");
	theTable.setAttribute("border","0");
	theTable.setAttribute("style","border: 1px solid black; cursor: pointer;");
	document.getElementById("mset").appendChild(theTable);
	resolution = 100;
	pixel_size = 2;
	if (zoom > 1) zoom = zoom*3;
	dist = 1.5/(zoom);
	min_y = y_center - dist;
	max_y = y_center + dist;
	min_x = x_center - dist;
	max_x = x_center + dist;
	dx = (max_x - min_x) / resolution;
	dy = (max_y - min_y) / resolution;
	for (var y=min_y;y<max_y;y+=dy) {
		yrow = document.createElement("tr");
		width_check = 0;
		for (var x=min_x;x<max_x && width_check<resolution;x+=dx) {
			width_check++;
			zi = 0;
			zr = 0;
			zi2 = 0;
			zr2 = 0;
			count = 0;
			abs2 = 0;
			while ((zr2 + zi2) < 4 && count < maxIterations) {
				zr2 = zr*zr;
				zi2 = zi*zi;
				zi = 2*zr*zi + y;
				zr = zr2 - zi2 + x;
				count++;
			}
			color = colorChooser(count);
			xrow = document.createElement("td");
			xrow.setAttribute("style","background-color:" + color);
			xrow.setAttribute("height",pixel_size);
			xrow.setAttribute("width",pixel_size);
			xrow.setAttribute("onClick","drawMset("+x+","+y+","+(zoom+1)+");");
			yrow.appendChild(xrow);
		}
		theTable.appendChild(yrow);
	}
}
