

		var CLICKS = 0;
		var DRAWING = 0;
		
		function drawing() {
			DRAWING = 1;
		}
		
		function notdrawing() {
			DRAWING = 0;
		}
		
		function maybepaint(obj) {
		
			if (DRAWING) { return paint(obj); }  else return false;
			
			
		}
		function redraw() {
				
			for (y = 0; y < DATA.length; y++) { 
				for (x = 0; x < DATA[y].length; x++) {
					pix = (x+1) + "x" + (y+1);
					document.getElementById(pix).style.background=DATA[y][x];
				}
			}

		
		}	
	
	
		function setBackground(bgcolor) { 
		
			document.getElementById('background').value = bgcolor;
		
		}
		function fill(fillcolor) {
		
			for (y = 0; y < DATA.length; y++) { 
				for (x = 0; x < DATA[y].length; x++) {
					DATA[y][x] = fillcolor;
				}
			}
			setBackground(fillcolor);
			redraw();
		}
	
		function showDrawing() {
			document.getElementById('start').style.display='none';
			document.getElementById('draw').style.display='block';

		}	
		function start(backgroundcolor) { 
	
			if (document.getElementById('save_id')) {
				document.getElementById('save_id').value='';
			}
			CLICKS = 0;	
			fill(backgroundcolor);
			showDrawing();
			return false;
		}
		function restart() { 
		
			fill(null);
			if (document.getElementById('save_id')) {
				document.getElementById('save_id').value='';
			}
			CLICKS = 0;
			document.getElementById('start').style.display='block';
			document.getElementById('draw').style.display='none';
			return false;
		
		}
		
		
		function paint(obj) { 

			id = obj.id;
			xandy = id.split("x");
			x = xandy[0];
			y = xandy[1];
			x--;
			y--;
			CLICKS++;
			
			newcolor = color;
			
			if (color=="shadow") { 
				current = DATA[y][x];
				if (!current) {
					current = "#FFFFFF";
				}	
				newcolor=shadow(current);		
			}
			if (color=="shine") { 
				current = DATA[y][x];
				if (!current) {
					current = "#FFFFFF";
				}	
				newcolor=shine(current);		
			}

			obj.style.background=newcolor;
			DATA[y][x] = newcolor;
			if (document.getElementById('clicks')) {
				document.getElementById('clicks').innerHTML = CLICKS;
				document.getElementById('click_input').value=CLICKS;
			}
			return false;
		}
		
		function shine(oldcolor) {
		
			rgb = oldcolor.split("");
			r = rgb[1] + rgb[2];	
			g = rgb[3] + rgb[4];
			b = rgb[5] + rgb[6];
			
			r_dec = h2d(r);
			g_dec = h2d(g);
			b_dec = h2d(b);
			
			r_dec += 25;
			g_dec += 25;
			b_dec += 25;
			
			if (r_dec > 255) { r_dec = 255; }
			if (g_dec > 255) { g_dec = 255; }
			if (b_dec > 255) { b_dec = 255; }
			if (r_dec < 0) { r_dec = 0; }
			if (g_dec < 0) { g_dec = 0; }
			if (b_dec < 0) { b_dec = 0; }

			r=d2h(r_dec);
			g=d2h(g_dec);
			b=d2h(b_dec);
			
			if (r.length==1) { r = "0" + r; }
			if (g.length==1) { g = "0" + g; }
			if (b.length==1) { b = "0" + b; }

			newcolor = "#"+r+g+b;
			//alert(oldcolor + " -> " + newcolor);
			return newcolor;
			
		}
		
		
		function shadow(oldcolor) {
		
			rgb = oldcolor.split("");
			r = rgb[1] + rgb[2];	
			g = rgb[3] + rgb[4];
			b = rgb[5] + rgb[6];
			
			r_dec = h2d(r);
			g_dec = h2d(g);
			b_dec = h2d(b);
			
			r_dec -= 25;
			g_dec -= 25;
			b_dec -= 25;
			
			if (r_dec > 255) { r_dec = 255; }
			if (g_dec > 255) { g_dec = 255; }
			if (b_dec > 255) { b_dec = 255; }
			if (r_dec < 0) { r_dec = 0; }
			if (g_dec < 0) { g_dec = 0; }
			if (b_dec < 0) { b_dec = 0; }

			r=d2h(r_dec);
			g=d2h(g_dec);
			b=d2h(b_dec);

			if (r.length==1) { r = "0" + r; }
			if (g.length==1) { g = "0" + g; }
			if (b.length==1) { b = "0" + b; }
			
			newcolor = "#"+r+g+b;
			//alert(oldcolor + " -> " + newcolor);
			return newcolor;
			
		}

		function d2h(d) {return d.toString(16);}
		function h2d(h) {return parseInt(h,16);} 	
			
		function changecolor(newcolor) { 
		
			p = document.getElementById('pen');
			e = document.getElementById('eraser');
			h = document.getElementById('shiner');
			s = document.getElementById('shadower');
		
			p.className='';
			e.className='';
			h.className='';
			s.className='';
			
			color = newcolor;
			if (!newcolor) {
				document.getElementById('currentcolor').style.background = 'url("/pixl/transparent.gif")';
				e.className='on';
			} else if (newcolor=='shadow') {
				s.className='on';
			} else if (newcolor=='shine') {
				h.className='on';

			} else {	
				p.className='on';	
				document.getElementById('currentcolor').style.background = newcolor;
			}
			return false;
		}
		
		
		function save() {
			res = '';
			for (y = 0; y < DATA.length; y++) { 
				for (x = 0; x < DATA[y].length; x++) {
					if (DATA[y][x]) { 
						res = res + (x) + 'x' + (y) + ':' + DATA[y][x] + ' ';
					}			
				}
			}
			document.getElementById('data').value=res;
			document.getElementById('saver').submit();
			return false;		
		}


	