var specialcase = ((navigator.userAgent.indexOf('Mac') != -1) || document.all);
function init() {
	if (!(document.getElementById || document.all || document.layers)) return;
	document.getElementById("puzzleContainer").onselectstart = new Function ("return false");
	for (i=0;i<document.images.length;i++)	{
		if (document.images[i].className == 'l'){
			document.images[i].onmousedown = c;
			document.images[i].onmouseup = function() {return false};;
			document.images[i].ondragstart = function() {return false};
			document.images[i].onselectstart = new Function ("return false");
		}
	}
	lightAllBulbs();

	timer();
}
var down = 0;
var bulbs = new Array();
var lights = new Array();

function getstatus(s){
	var ar = new Array();
	ar = s.split('/');
	return ar[ar.length-1].charAt(0);
}
function getdirection(s){
	var ar = new Array();
	ar = s.split('_');
	return ar[0];
}
function getnextsrc(status, reverse){
	if (reverse){
		switch (status){
			case 'y': return RightClick?'x':'n';
			case 'n': return RightClick?'x':'y';
			case 'x': return RightClick?'n':'x';
		}
	}else{
		switch (status){
			case 'y': return RightClick?'n':'x';
			case 'n': return RightClick?'y':'y';
			case 'x': return RightClick?'y':'n';
		}
	}
}
function lightUpSquare(i,j){
	if (!lights[i][j]){
		sender = document.images['l_'+i+'_'+j];
		stat = getstatus(sender.src);
		sender.src = eval(stat + 'l').src;
	}
	lights[i][j]++;
	
	if (lights[i][j] > 1 && bulbs[i][j]){
		sender = document.images['l_'+i+'_'+j];
		sender.src = eval('ye').src;
	}
}
function lightDownSquare(i,j){
	lights[i][j]--;
	if (lights[i][j] == 1 && bulbs[i][j]){
		sender = document.images['l_'+i+'_'+j];
		sender.src = eval('yl').src;
	}
	if (!lights[i][j]){
		sender = document.images['l_'+i+'_'+j];
		stat = getstatus(sender.src);
		sender.src = eval(stat + 'd').src;
	}
}

function lightUp(i,j){
	bulbs[i][j] = 1;
	lightUpSquare(i,j);
	w = document.answerForm.w.value;
	h = document.answerForm.h.value;
	
	ii = i-1;
	jj = j;
	while (ii >= 0 && (!wall[ii][jj])){
		lightUpSquare(ii,jj);
		ii--;
	}

	ii = i;
	jj = j-1;
	while (jj >= 0 && (!wall[ii][jj])){
		lightUpSquare(ii,jj);
		jj--;
	}
	ii = i*1+1;
	jj = j;
	while (ii < h && (!wall[ii][jj])){
		lightUpSquare(ii,jj);
		ii++;
	}

	ii = i;
	jj = j*1+1;
	while (jj < w && (!wall[ii][jj])){
		lightUpSquare(ii,jj);
		jj++;
	}
}
function lightDown(i,j){
	bulbs[i][j] = 0;
	lightDownSquare(i,j);
	w = document.answerForm.w.value;
	h = document.answerForm.h.value;
	
	ii = i-1;
	jj = j;
	while (ii >= 0 && (!wall[ii][jj])){
		lightDownSquare(ii,jj);
		ii--;
	}

	ii = i;
	jj = j-1;
	while (jj >= 0 && (!wall[ii][jj])){
		lightDownSquare(ii,jj);
		jj--;
	}
	ii = i*1+1;
	jj = j;
	while (ii < h && (!wall[ii][jj])){
		lightDownSquare(ii,jj);
		ii++;
	}

	ii = i;
	jj = j*1+1;
	while (jj < w && (!wall[ii][jj])){
		lightDownSquare(ii,jj);
		jj++;
	}
}
function lightAllBulbs(){
	w = document.answerForm.w.value;
	h = document.answerForm.h.value;
	for (i = 0; i < h; i++){
		for (j = 0; j < w; j++){
			if (bulbs[i][j]){
				lightUp(i,j);
			}
		}
	}
}
function setImg(sender, ans){
	if (sender.name){
		var ar = new Array();
		ar = sender.name.split('_');
		i = ar[1];
		j = ar[2];
		sender.src = eval(ans + (lights[i][j] ? 'l' : 'd')).src;
		if (bulbs[i][j]){
			lightDown(i,j);
		}
		if (ans == 'y'){
			lightUp(i,j);
		}
		bulbs[i][j] = (ans == 'y');
		w = document.answerForm.w.value;
		h = document.answerForm.h.value;


		var s = document.answerForm.ansH.value;
		n = i * w + j * 1;
		l = s.length;
		s = s.substr(0,n) + ans + s.substr(n + 1, l);
		document.answerForm.ansH.value = s;
	}
}
function c(e){
	down = 1;
	var rtightclick = 0;
	if (!isIE && window.Event)	{
		rtightclick = (e.which == 3 || (e.modifiers & Event.CONTROL_MASK) || (e.modifiers & Event.SHIFT_MASK));
		sender = e.target;
	}else{
		rtightclick = (window.event.button == 2 || window.event.ctrlKey || window.event.shiftKey);
		var e = window.event;
		sender = e.srcElement;
	}
	ans = getnextsrc(getstatus(sender.src), rtightclick);
	setImg(sender, ans);
	return false;
}


var xd = new Image();
xd.src = 'xd.gif?light';
var xl = new Image();
xl.src = 'xl.gif';

var yd = new Image();
yd.src = 'yd.gif';
var yl = new Image();
yl.src = 'yl.gif';
var ye = new Image();
ye.src = 'ye.gif';

var nd = new Image();
nd.src = 'nd.gif?light';
var nl = new Image();
nl.src = 'nl.gif';

