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");
	document.getElementById("puzzleContainer").onmousedown = d;
	document.onmouseup = u;
	
	document.ondragstart = 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].onmouseup = u;
			document.images[i].onmouseover = ov;
			document.images[i].ondragstart = function() {return false};
		} else {
			document.images[i].onmousedown = d;
			document.images[i].ondragstart = function() {return false};
			document.images[i].onselectstart = new Function ("return false");
		}
	}
	timer();
}
var down = 0;
var lastAns = 'y';
var lastRightClick = 0;
function ov(e){
	if (down){
		if (!isIE && window.Event)	{
			sender = e.target;
		}else{
			var e = window.event;
			sender = e.srcElement;
		}
		if (sender.className == 'l'){
			if (!lastAns){
				lastAns = getnextsrc(getstatus(sender.src), lastRightClick);
			}
			setImg(sender, lastAns);
		}
	}
	return false;
}
function u(e){
	down = 0;
	return false;
}
function d(e){
	if (!down){
		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;
		}	
		//lastAns = rtightclick?'x':'y';
		lastAns = '';
		lastRightClick = rtightclick;
	}
	return false;
}

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 setImg(sender, ans){
	if (sender.name){
		var ar = new Array();
		ar = sender.name.split('_');
		dir = ar[0];
		i = ar[1];
		j = ar[2];
		w = document.answerForm.w.value;
		h = document.answerForm.h.value;

		//sender.src = ans + dir + '.gif';
		sender.src = eval(ans + dir).src;
		
		if (dir == 'v'){
			var s = document.answerForm.ansV.value;
			n = i * (1 * w + 1) + (j * 1);
			l = s.length;
			s = s.substr(0,n) + ans + s.substr(n + 1, l);
			document.answerForm.ansV.value = s;
		}else{
			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);
	lastAns = ans;
	setImg(sender, ans);
	return false;
}


var xh = new Image();
xh.src = 'xh.gif';
var xv = new Image();
xv.src = 'xv.gif';
var yh = new Image();
yh.src = 'yh.gif';
var yv = new Image();
yv.src = 'yv.gif';
var nh = new Image();
nh.src = 'nh.gif';
var nv = new Image();
nv.src = 'nv.gif';


