var direction = 0;
var prevdir = 0;
var running = false;
var terminate = false;
var pause = true;
var snake = null;
var x = 100;
var y = 100;
var cell = 10;
var speed = 200;
var segments = new Array();
var apple = null;
var arena = null;
var score = 0;
var cntapplemove = 50;
var nextapple = cntapplemove;
var timeleft = null;


function newgame() {
	// Clear snake
	var n = segments.length;
	for (i = 0; i < n; i++) {
		tmp = arena.removeChild(segments[i]);
		delete tmp;
	}
	delete tmp;
	// clear apple
	tmp = arena.removeChild(apple);
	delete tmp;
	apple = null;
	// Hide Message
	hideMessage();
	
	direction = 0;
	prevdir = 0;
	terminate = false;
	running = false;
	pause = true;
	x = 100;
	y = 100;
	makemove();
	segments.length = 0;
	for (i = 0; i < 10; i ++)
		addsegment();
	cell = 10;
	speed = 200;
	cntapplemove = 50;
	nextapple = cntapplemove;
	score = -1;
	updateScore();
}

function goleft() {
	if (prevdir != 3) direction = 1;
}

function goup() {
	if (prevdir != 4) direction = 2;
}

function goright() {
	if (prevdir != 1) direction = 3;
}

function godown() {
	if (prevdir != 2) direction = 4;
}

function addsegment() {
	if (segments.length > 0) {
		px = parseInt(segments[0].style.left);
		py = parseInt(segments[0].style.top);
	} else {
		px = x;
		py = y;
	}
	var tmp = document.createElement("div");
	tmp.className = 'snakeBody';
	tmp.style.left = px + 'px';
	tmp.style.top = py + 'px';
	arena.appendChild(tmp);
	segments.push(tmp);
}

function movebody() {
	var n = segments.length;
	for (i = (n - 1); i > 0; i--) {
		segments[i].style.left = segments[i - 1].style.left;
		segments[i].style.top = segments[i - 1].style.top;
	}
	
	if (n > 0) {
		segments[0].style.left = x + 'px';
		segments[0].style.top = y + 'px';
	}
}

function checkCollision() {
	if (snake == null) return false;
	for (i = (segments.length - 1); i >= 0; i--) {
		if ((segments[i].style.left == snake.style.left) &&	(segments[i].style.top == snake.style.top)) {
			return true;
		}
	}
	return (x < 0) || (x > 390) || (y < 0) || (y > 290);
}

function makemove() {
	if (snake == null) {
		//initialization ...
		arena = document.getElementById('arena');
		snake = document.getElementById('snake');
		//x = parseInt(snake.style.left);
		//y = parseInt(snake.style.top);
		x = 100; y = 100;
		for (i = 0; i < 10; i ++)
			addsegment();
			
		placeApple();
	}
	
	prevdir = direction;
	
	movebody();
	
	switch (direction) {
		case 1: x -= cell; break;
		case 2: y -= cell; break;
		case 3: x += cell; break;
		case 4: y += cell; break;
	}
	
	//if (x < 0) x = 390;
	//if (x > 390) x = 0;
	//if (y < 0) y = 290;
	//if (y > 290) y = 0;
	
	snake.style.left = x + "px";
	snake.style.top = y + "px";
}

function placeApple() {
	if (apple == null) {
		apple = document.createElement("div");
		apple.className = 'apple';
		arena.appendChild(apple);
	}
	
	ok = false;
	do {
		xx = Math.round(Math.random() * 39)*10 + 'px';
		xy = Math.round(Math.random() * 29)*10 + 'px';
		if ((xx == snake.style.left) && (xy == snake.style.top)) continue;
		bcol = false;
		for (i = (segments.length - 1); i >= 0; i--) {
			if ((xx == segments[i].style.left) && (xy == segments[i].style.top)) {
				bcol = true;
				break;
			}
		}
		ok = !bcol;
	} while (!ok);
	
	apple.style.left = xx;
	apple.style.top = xy;
	
	nextapple = cntapplemove + (segments.length * 2);
}

function updateScore() {
	scoreel = document.getElementById('score');
	score ++;
	scoreel.innerHTML = score;
}

function checkApple() {
	if ((apple == null) || (snake == null)) return false;
	return (apple.style.left == snake.style.left) && (apple.style.top == snake.style.top);
}

function loop() {
	if (!running) return;
	if (terminate) return;
	makemove();
	if (checkCollision()) {
		terminate = true;
		showMessage(msgGameOver, '<a href="javascript:newgame();">' + msgNewGame + '</a>')
	}
	if (apple==null) placeApple();
	if (!terminate && checkApple()) {
		addsegment();
		placeApple();
		//if (segments.length <= 20) {
		//	if (speed > 20) speed -= (25 - segments.length);
		//}
		//else if (speed > 20) speed -= 2;
		if (speed > 20) speed -= 5;
		
		updateScore();
	}
	nextapple --;
	if (timeleft==null) timeleft = document.getElementById('timeleft');
	if (nextapple > 300) h = 300;
	else h = nextapple;
	timeleft.style.height = h + 'px';
	if (nextapple <= 0) {
		placeApple();
		score --;
		score --;
		updateScore();
	}
	if (!terminate)
		setTimeout("loop()", speed);
}

function chdir(n) {
	switch (n) {
		case 1: goleft(); break;
		case 2: goup(); break;
		case 3: goright(); break;
		case 4: godown(); break;
	}
	
	if (!running) {
		running = true;
		loop();
	}
}

function onkey(event) {
	switch (event.keyCode) {
		case 37: chdir(1); break; //left
		case 38: chdir(2); break; //up
		case 39: chdir(3); break; //right
		case 40: chdir(4); break; //down
		case 32: running = false; break;
	}
	
	if (event.preventDefault) event.preventDefault();
    else event.returnValue = false;
}

function hideMessage() {
	var msg = document.getElementById('message');
	msg.style.display = 'none';
}

function showMessage(s1, s2) {
	var msg = document.getElementById('message');
	msg.innerHTML = '<span class="big">' + s1 + '</span><br><span class="small">' + s2 + '</span>';
	msg.style.display = 'block';
}