

boardColor				= "#ffff00"
boardBorder				= "solid" 		// none/solid
boardBorderColor		= "#000000"
backBoardBorder			= "none"		// none/solid
backBoardBorderColor	= "#cccccc"
backBoardColor			= "#000000"
snakeColor				= ""
foodColor				= "#ff0000"
scoreColor				= "#ffffff"
snLoaded				= true

gInterval=""
gLastDir=""
gPaus=false
started = false
intScore = 0
strScore = ""

function initMask(){
	var pass=false
	if(navigator.appVersion.indexOf("MSIE 5.5")!=-1){
		pass=true
	}
	if(navigator.appVersion.indexOf("MSIE 6")!=-1){
		pass=true
	}
	if(navigator.appVersion.indexOf("MSIE 7")!=-1){
		pass=true
	}
	if(!pass){
		alert("Internet Explorer 5.5 is required")
		return
	}
	
	GameOver.style.display = "none"
	scoreDiv.style.display = ""
	pressKey.style.display = "none"
	clearAll()
	
	var startMaskElem=maskTable.rows[7].cells[7]
	//placera ut start hallon som man sedan flyttar när mna har tatt det
	startMaskElem.style.backgroundColor=snakeColor
	arrMaskPoses=[[7,7]]
	gDir=[0,1]
	gInterval=setTimeout("moveMask()",0)
	placeHallon()
	changeDir=false
	if(!started)pause()
	started = true
	countSnake.src = "sncount.asp?" + parseInt(Math.random()*1000000)
}

function clearAll(){
	var mg=maskTable.getElementsByTagName("TD");i=0
	while(mg[i]!=null){
		mg[i].style.backgroundColor=boardColor
		i++
	}
	scoreDiv.innerText=0
}
function pause(){
	if(gPaus){
		gInterval=setTimeout("moveMask()",0)
		gPaus=false
	}
	else{
		clearTimeout(gInterval)
		gPaus=true
	}
}

function placeHallon(){
	clearTimeout(gInterval)
	arrHallonPoses=[parseInt(Math.random()*14),parseInt(Math.random()*16)]
	var startHallonElem=maskTable.rows[arrHallonPoses[0]].cells[arrHallonPoses[1]]
	if(startHallonElem.style.backgroundColor==snakeColor){
		placeHallon() //try new position if busy by mask
	}
	else{
		startHallonElem.style.backgroundColor=foodColor
		gInterval=setTimeout("moveMask()",100)
	}
}

function die(){
	clearTimeout(gInterval)
	document.onkeydown=null
	GameOver.style.display = ""
	scoreDiv.style.display = "none"
	var toList = scoreDiv.innerHTML*1;
	
	if(highScore)top.hoved.location = "edit/regScore.asp?score=" + toList + "," + toList%6 + "" + toList%7 + "" + toList%4;
	
	started = false
	intScore=0
	var temp = setTimeout("scoreDiv.style.display = '';GameOver.style.display = 'none'",2000)
	var temp2 = setTimeout("document.onkeydown=doKeyDown",4000)
	var temp3 = setTimeout("clearAll();scoreDiv.style.display = 'none';pressKey.style.display = ''",4000)
}

function moveMask(){

	clearTimeout(gInterval)
	//check boundaries, raises an error if so, see catch-statemaent
	try{
		var tmpMaskFront=arrMaskPoses[arrMaskPoses.length-1]
		var tmpMaskBack= arrMaskPoses[0]
		maskTable.rows[tmpMaskBack[0]].cells[tmpMaskBack[1]].style.backgroundColor=boardColor
		var nextMask=maskTable.rows[tmpMaskFront[0]+gDir[0]].cells[tmpMaskFront[1]+gDir[1]]
		if(nextMask.style.backgroundColor==snakeColor){
			die();
			return
		}
		nextMask.style.backgroundColor=snakeColor
		if(changeDir==false){
			arrMaskPoses.splice(0,1)
		}
		arrMaskPoses.push([tmpMaskFront[0]+gDir[0],tmpMaskFront[1]+gDir[1]])
		changeDir=false
		if(((tmpMaskFront[0]+gDir[0])==arrHallonPoses[0])&&((tmpMaskFront[1]+gDir[1])==arrHallonPoses[1])){
				changeDir=true //hit! 
				var addScore=9
				intScore += addScore
				scoreDiv.innerText=parseInt(scoreDiv.innerText)+addScore
				//scoreDiv.innerHTML=scoreToGraphic(intScore,"ffffff",0,50,9);
				//alert(scoreDiv.innerHTML)
				placeHallon()
				return
		}
			gInterval=setTimeout("moveMask()",100)
	}
	catch(e){
		die()
		return
	}
}

function doKeyDown(){
	if(!started){
		if(event.keyCode==37){initMask()}	
		if(event.keyCode==38){initMask()}
		if(event.keyCode==39){initMask()}
		if(event.keyCode==40){initMask()}
	}
	else{
	if(event.keyCode==37){
		if(new String(gDir)!="0,1"){
			gDir=[0,-1]
			kdSub()
			return false
		}
	}
	if(event.keyCode==38){
		if(new String(gDir)!="1,0"){
			gDir=[-1,0]
			kdSub()
			return false
		}
	}
	if(event.keyCode==39){
		if(new String(gDir)!="0,-1"){
			gDir=[0,1]
			kdSub()
			return false
		}
	}
	if(event.keyCode==40){
		if(new String(gDir)!="-1,0"){
			gDir=[1,0]
			kdSub()
			return false
		}
	}
	if(event.keyCode==80){
		pause()
	}
	
	function kdSub(){
		clearTimeout(gInterval)
		gInterval=setTimeout("moveMask()",0)
	}
	}
}

