

var LeftDecrease=4;
var TopDecrease=27;
window.onload=fillStones;
function right(e,imgT) {
	if(!e) e = window.event;
	var msg = imgT.id;
	if (navigator.appName == 'Netscape' && e.which == 3) {
		writeStone(imgT,true);
		return false;
	}
	else if (navigator.appName == 'Microsoft Internet Explorer' && (event.button==0 || event.button==2)) {
		writeStone(imgT,true);
		return false;
	}
	return true;
}
//potlačení context menu na right click myši
document.oncontextmenu = function(){return false};

var lastStoneColorIsBlack = true;
var lastStoneId="";
var blackStone = "/scripts/images/sblack.png";
var whiteStone = "/scripts/images/swhite.png";
var altBlackStone = "/scripts/images/shoverblack.png";
var altWhiteStone = "/scripts/images/shoverwhite.png";
var emptyStone = "/scripts/images/empty.png";
var lastnumber=0;
var lastAlternativeNumber=96;//97=a,98=b,99=c,... 96 = nulový stav
var lastStoneType="normal";
// událost na checked u přepínače zobrazení čísel u kamenů
function displayNumber()
{
	var check = document.getElementById("chcNumbers");
	if(check)
	{
		if(check.checked)
			showNumber(true);
		else 
			showNumber(false);
	}
}
// ne/zobrazí čísla u kamenů
function showNumber(show)
{
	for(x=1;x<16;x++)
	{
		for(y=1;y<16;y++)
		{
			var s = "span{idn}";
			s = s.replace("{idn}",x+"-"+y);
			var span = document.getElementById(s);
			if(span)
			{
				if(show)
				{
					span.style.display="inline";
				}
				else
				{
					span.style.display="none";
				}
			}
		}
	}
}
// smaže desku
function clearDesk()
{
lastStoneColorIsBlack = true;
lastStoneId="";
lastnumber=0;
lastAlternativeNumber=96;
lastStoneType="normal";
clearNextStone();
fillStones();
displayNumber();
clearInputPosition();

}
// mění proměnné poslední kámen - kvůli číslování atd..
function changeLastStoneId()
{
	//hledání posledního alt kamene, existuje-li..
	for(x=1;x<16;x++)
	{
		for(y=1;y<16;y++)
		{
			var s = "span{idn}";
			s = s.replace("{idn}",x+"-"+y);
			var span = document.getElementById(s);
			if(span)
			{
				if(span.innerHTML.toString().length<2)continue;
				var stonenum = parseInt(span.innerHTML.toString().substring(0,span.innerHTML.toString().length-1));
				if(stonenum==lastnumber)
				{
					lastchar = span.innerHTML.toString().substring(span.innerHTML.toString().length-1);
					for (var i=96;i<=122;i++) {
						if (unescape('%' + i.toString(16)) == lastchar)
						{
							if(lastAlternativeNumber<i)lastAlternativeNumber=i;
						}
					}
				}
			}
		}
	}
	//hledání posledního kamene (alt či normal type)
	for(x=1;x<16;x++)
	{
		for(y=1;y<16;y++)
		{
			var s = "span{idn}";
			s = s.replace("{idn}",x+"-"+y);
			var span = document.getElementById(s);
			if(span)
			{
				if(span.innerHTML==lastnumber+getCharFromInt(lastAlternativeNumber))
				{
					
					if(getCharFromInt(lastAlternativeNumber)=="")
						lastStoneType="normal";
					else
						lastStoneType="alt";
					lastStoneId = span.id.replace("span","img");
				}
			}
		}
	}
	//hledání nejvyššího alt. kamene v řadě..		
}
// converze z char na int
function getCharFromInt(num){

	if(num=="")return "";
	try{
		var n = parseInt(num);
		if(n<97 || n>122)return "";
		return unescape("%"+n.toString(16));
	}
	catch(e)
	{
		return "";
	}
}
//nastaví desku podle vstupního parametru inputString
function initDesk()
{
	if(inputString==null)return;
	if(inputString=="")return;
	var s = inputString.split("|");
	if(s.length==0)return;
	var i = 0;
	for(i;i<s.length;i++)
	{
		var imgStone = s[i].split(";");
		var img = document.getElementById("img"+imgStone[0]);
		if(img==null)continue;
		if(imgStone[2]=="n")
		{
			writeStone(img,false);
		}
		else
		{
			writeStone(img,true);
		}
	}	
}
//zapisuje kameny na desku při kliku
function writeStone(imgT,clickright)
{
var img = imgT;
var span = imgT;

	if(img.id.indexOf("span")>-1)
	{
		img = document.getElementById(img.id.replace("span","img"));
	}
	if(span.id.indexOf("img")>-1)
	{
		span = document.getElementById(img.id.replace("img","span"));
	}
	if(img && !clickright && img.src.indexOf(emptyStone)>-1)
	{
		reverseLastStoneColor();
		if(!lastStoneColorIsBlack)
		{
			img.src=blackStone;		
			lastStoneId=img.id;
			span.style.color="white";
		}
		else
		{
			img.src=whiteStone;
			lastStoneId=img.id;
			span.style.color="black";
		}
		lastnumber++;
		lastAlternativeNumber=96;
		span.innerHTML=lastnumber;
		positionSpanInStone(span, lastnumber.toString().length,false);
		lastStoneType="normal";
		writePositionToBox(img.id, !lastStoneColorIsBlack, "n");
		changeNextNumber(false);
	}	
	else if(img && clickright)
	{
		
		if(img.src.indexOf(emptyStone)==-1 && lastStoneId==img.id)
		{
			img.src=emptyStone;
			positionSpanInStone(span, span.innerHTML.toString().length,true);
			span.innerHTML="";
			if(lastStoneType!="normal") lastAlternativeNumber--;
			if(lastAlternativeNumber<97) 
			{
				lastnumber--;
				reverseLastStoneColor();
			}
			changeLastStoneId();
			deletePositionFromBox();
			changeNextNumber(false);
			return;
		}
		else if(img.src.indexOf(emptyStone)>-1)
		{
			if(lastStoneType=="normal") reverseLastStoneColor();
			
			if(!lastStoneColorIsBlack)
			{
				img.src=altBlackStone;
				lastStoneId=img.id;
				span.style.color="white";
			}
			else
			{
				img.src=altWhiteStone;
				lastStoneId=img.id;
				span.style.color="black";
			}
			if(lastStoneType=="normal") lastnumber++;
			lastAlternativeNumber++;
			span.innerHTML=lastnumber+getCharFromInt(lastAlternativeNumber);
			positionSpanInStone(span, (lastnumber+getCharFromInt(lastAlternativeNumber)).toString().length,false);
			lastStoneType="alt";
			writePositionToBox(img.id, !lastStoneColorIsBlack, "a");
			changeNextNumber(false);
		}
	}
}
//přidání pozice do inputu hidden
function writePositionToBox(imgId, stoneColor , stoneType)
{
	var hiddenInput = document.getElementById("inputDeskPosition");
	if(hiddenInput == null)return;
	var positionString = hiddenInput.value;
	var color = "b";
	if(!stoneColor)color="w";
	imgId = imgId.replace("img","");
	if(positionString=="")
		positionString += imgId+";"+color+";"+stoneType;
	else
		positionString += "|"+imgId+";"+color+";"+stoneType;
	hiddenInput.value = positionString;
}
//smazání poslední pozice
function deletePositionFromBox()
{
	var hiddenInput = document.getElementById("inputDeskPosition");
	if(hiddenInput == null)return;
	var positionString = hiddenInput.value;
	if(positionString=="")return;
	var lastIndex = positionString.lastIndexOf("|");
	if(lastIndex==-1)positionString="";
	else
	{
		positionString = positionString.substring(0,lastIndex);
	}
	hiddenInput.value = positionString;
}
// mění pozici čísla v kameni podle cifer čísla 
// např. č. 12 jsou 2 cifry - tzn. číslo musí být více vlevo než jednociferné, aby bylo na středu
function positionSpanInStone(span, lengthNum, erase)
{
	if(span!=null)
	{
		if(erase)
		{
			if(lengthNum==2) 
			{
				span.style.left=(parseInt(span.style.left.replace("px",""))+4).toString()+"px";
			}
			if(lengthNum==3) 
			{
				span.style.left=(parseInt(span.style.left.replace("px",""))+9).toString()+"px";
			}
		}
		else
		{
			if(lengthNum==2) 
			{
				span.style.left=(parseInt(span.style.left.replace("px",""))-4).toString()+"px";
			}
			if(lengthNum==3) 
			{
				span.style.left=(parseInt(span.style.left.replace("px",""))-9).toString()+"px";
			}
		}
		
	}
}
// reverse poslední barvy kamene
function reverseLastStoneColor()
{
	if(lastStoneColorIsBlack)lastStoneColorIsBlack=false;
	else lastStoneColorIsBlack=true;
	changeNextStone();
}
// změní barvu "dalšího kamene"
function changeNextStone()
{
	var nextS = document.getElementById("nextStone");
	if(nextS)
	{
		if(lastStoneColorIsBlack)
			nextS.src=blackStone;
		else
			nextS.src=whiteStone;
	}
}
// mění následující číslo kamene
function changeNextNumber(init)
{
	var nextS = document.getElementById("nextStone");
	if(!nextS)return;
	var spanObj = document.getElementById("nextStoneNumber");
	if(spanObj)
	{
		spanObj.style.position="absolute";
		spanObj.style.top = nextS.offsetTop+5;
		spanObj.style.left = nextS.offsetLeft+10;
		positionSpanInStone(spanObj, (lastnumber+2).toString().length, false)
		spanObj.innerHTML = lastnumber+1;
			
		if(lastStoneColorIsBlack)
		{
			spanObj.style.color="white";
		}
		else
		{
			spanObj.style.color="black";
		}
	}
}
// nuluje obrázek "dalšího kamene"
function clearNextStone()
{
	var nextS = document.getElementById("nextStone");
	if(nextS) nextS.src=blackStone;
}
// nuluje checkbox s číslováním
function clearchcNumber()
{
	var check = document.getElementById("chcNumbers");
	if(check)
	{
		check.checked=true;
	}
}
function clearInputPosition()
{
	var posInput = document.getElementById("inputDeskPosition");
	if(posInput)
	{
		posInput.value="";
	}
}
// naplní desku kameny..
function fillStones()
{


var check2 = document.getElementById("deskDiv");
	if(check2) {
	
var deskDiv = document.getElementById("deskDiv");


var imgs = "";
deskDiv.innerHTML="";
if(deskDiv)
{
	for(x=1;x<16;x++)
	{
		for(y=1;y<16;y++)
		{
			var s = "<img id=\"img{id}\" src=\"/scripts/images/empty.png\" alt=\"\" style=\"position: absolute; top:{t}px; left:{l}px\"  onmousedown = \"right(event,this);\" onclick=\"writeStone(this,false);\"/><span id=\"span{idn}\" class=\"number\" style=\"position:absolute;top:{tn}px;left:{ln}px\" onmousedown = \"right(event,this);\" onclick=\"writeStone(this,false);\"></span>";
			s = s.replace("{t}",y*30-TopDecrease);
			s = s.replace("{l}",x*30-LeftDecrease);
			s = s.replace("{tn}",y*30-TopDecrease+5);
			s = s.replace("{ln}",x*30-LeftDecrease+10);
			s = s.replace("{idn}",x+"-"+y);
			s = s.replace("{id}",x+"-"+y);
						
				imgs+=s;
			
		}
	}
}


deskDiv.innerHTML+=imgs;
initDesk();
changeNextNumber(true);

}

}



