
$(function() {




	
	var body = [{x: 20, y: 20}, {x: 30, y: 20}, {x: 40, y: 20}]	
	
	var direction = "E";
	
	var dead = false;

	var canvas = $('#canvas')[0],
	ctx;
	
	var score=0;
	
	var curx;
	var cury;
	var newx;
	var newy;
	var nextstep;
	
	var hit;
	
	var lock=0;
	
	var food={x:0,y:0};
	
	var d;
	
	
	
	function moves(arr){	
		
		for(var i = 0; i < arr.length; i++) {
			xx = body[i].x;
			yy = body[i].y;
			ctx.fillStyle = "rgba(0,0,0,1)";
			if (dead==true) {
			ctx.fillStyle = "rgba(230,0,0,1)";			
			}

			ctx.fillRect(xx,yy,10,10);
			}
	} 

	function moveW() {
			ctx.clearRect(0, 0, canvas.width, canvas.height);
			
			curx = body[body.length-1].x; 
			cury = body[body.length-1].y;			
			newx = curx-10;
			newy = cury;

			curpos = {x:curx, y:cury};

			nextstep = {x:newx, y:newy};

			check(body, nextstep);


			if ((body[body.length-1].x == 0) || (hit==1)) {
				
				if (hitfood(food,curpos)==true) {
				score++;
				$('#scorebox').replaceWith('<p id="scorebox">Score: '+ score +'</p>');}
							
				dead = true;
				moves(body);	
			} else {
			
			
			body.push({x:newx, y:newy})
			
			if (hitfood(food,curpos)==true) {

			score++;
			$('#scorebox').replaceWith('<p id="scorebox">Score: '+ score +'</p>');			


			placefood();
			} else {			
				body.shift();
				}
				
			moves(body);
			drawfood(food);
			}
	}
	
	function moveN() {
			ctx.clearRect(0, 0, canvas.width, canvas.height);
			
			curx = body[body.length-1].x; 
			cury = body[body.length-1].y;			
			newx = curx;
			newy = cury-10;			

			curpos = {x:curx, y:cury};

			nextstep = {x:newx, y:newy};

			check(body, nextstep);

			
			if ((body[body.length-1].y == 0) || (hit==1)) {

				if (hitfood(food,curpos)==true) {
				score++;
				$('#scorebox').replaceWith('<p id="scorebox">Score: '+ score +'</p>');}

				dead = true;
				moves(body);	
			} else {
			
			
			body.push({x:newx, y:newy})
			
			if (hitfood(food,curpos)==true) {
			score++;
			$('#scorebox').replaceWith('<p id="scorebox">Score: '+ score +'</p>');			


			placefood();
			} else {			
				body.shift();
				}
				
			moves(body);
			drawfood(food);
			}
	}

	function moveE() {
			ctx.clearRect(0, 0, canvas.width, canvas.height);

			curx = body[body.length-1].x; 
			cury = body[body.length-1].y;			
			newx = curx+10;
			newy = cury;

			curpos = {x:curx, y:cury};

			nextstep = {x:newx, y:newy};

			check(body, nextstep);			
			

			if ((body[body.length-1].x == 290) || (hit==1)) {

				if (hitfood(food,curpos)==true) {
				score++;
				$('#scorebox').replaceWith('<p id="scorebox">Score: '+ score +'</p>');}

				dead = true;
				moves(body);	
			} else {			

			
			body.push({x:newx, y:newy})
			
			if (hitfood(food,curpos)==true) {
			score++;
			$('#scorebox').replaceWith('<p id="scorebox">Score: '+ score +'</p>');			

			placefood();
			} else {			
				body.shift();
				}
				
			moves(body);
			drawfood(food);
			}
	}

	function moveS() {
			ctx.clearRect(0, 0, canvas.width, canvas.height);

			curx = body[body.length-1].x; 
			cury = body[body.length-1].y;			
			newx = curx;
			newy = cury+10;
			
			curpos = {x:curx, y:cury};
			

			nextstep = {x:newx, y:newy};

			check(body, nextstep);

			if ((body[body.length-1].y == 190) || (hit==1)) {

				if (hitfood(food,curpos)==true) {
				score++;
				$('#scorebox').replaceWith('<p id="scorebox">Score: '+ score +'</p>');}

				dead = true;
				moves(body);
			} else {
			
			body.push({x:newx, y:newy})
			
			if (hitfood(food,curpos)==true) {

			score++;
			$('#scorebox').replaceWith('<p id="scorebox">Score: '+ score +'</p>');			
			
			placefood();
			} else {			
				body.shift();
				}
				
			moves(body);
			drawfood(food);
			}
	}
	
	function move() {
		if (dead==false) {
			if (direction=="W") moveW();
			if (direction=="N") moveN();
			if (direction=="E") moveE();
			if (direction=="S") moveS();
			lock=0;
		} else {
		clearInterval(d);
		$('#scorebox').replaceWith('<p id="scorebox"><span class="red">Game Over.</span> Your Score: '+ score +'</p>');
		$('#result').replaceWith('<div id="result"></div>');
		
		$('h3').removeClass('hidden');
		$('#highscores').removeClass('hidden');
		

						
		
		}
	}
		
	function check(a,o) {
	hit=0;
	for (var i=0; i < a.length; i++) {
			if (a[i].x == o.x && a[i].y == o.y) {hit=1;}
			}
	}	

	function hitfood(a1,a2) {
	if ((a1.x==a2.x)&&(a1.y==a2.y)) return true;
	}


	function placefood() {
	var e,f;
	var g,h;
	
	var bad;
	bad=0;
	
	e = 0;
	f = 0;
	
	e = (Math.floor(Math.random()*30)*10);
	f = (Math.floor(Math.random()*20)*10);

	for (var j=0; j < body.length; j++) {
			if (body[j].x == e && body[j].y == f) {
			bad=1;}}

	if (bad==0) {
	food.x=e; food.y=f;
	}
	
	if (bad==1) {
	placefood();
	}
/*
//			alert("clash");
			g=(Math.floor(Math.random()*30)*10);
			h=(Math.floor(Math.random()*20)*10);
			food.x=g; food.y=h;
//			placefood()
			}
			
			else 
				{}
			
			}	
*/	
	
//	food.x=(Math.floor(Math.random()*30)*10);
//	food.y=(Math.floor(Math.random()*20)*10);
	}
	
	function drawfood(foodpos) {
	ctx.fillStyle = "rgba(180,150,0,1)";	
	xx=foodpos.x;
	yy=foodpos.y;
	ctx.fillRect(xx,yy,10,10);
	}

	function startgame() {	
		if(canvas.getContext){
		
			body = [{x: 20, y: 20}, {x: 30, y: 20}, {x: 40, y: 20}]	
			direction = "E";
			dead = false;
			score=0;
			lock=0;
			
			ctx = canvas.getContext('2d');		
			placefood();
			moves(body); //intitial drawing of the snake
	
			d = setInterval(move, 200);
		
		}
		else
		{
			alert('canvas not supported');
		}
	}

	$(document).keydown(function(event) {
		if (event.keyCode==37 && direction!="E" && lock==0) {
	 		direction = "W";
	 		lock=1;
		}		
		if (event.keyCode==38 && direction!="S" && lock==0) {
			direction = "N";
			lock=1;
		}		
		if (event.keyCode==39 && direction!="W" && lock==0) {
			direction = "E";
			lock=1;
		}		
		if (event.keyCode==40 && direction!="N" && lock==0) {
			direction = "S";
			lock=1;
		}		
	});



	startgame();

	$('#play').click(function() {
	clearInterval(d);
 	startgame();
	$('#scorebox').replaceWith('<p id="scorebox">Score: '+ score +'</p>');	
	$('h3').addClass('hidden');	

	$("input#name").val("");	
	

//	$('#thanks').replaceWith('<div id="input"><form name="input" action="">Enter your name to submit your score:<br /><input type="text" name="name" id="name" /><p id="button">Submit score</p></form></div>');	

		$('#input').show();	


		$("#press").show();
		$("#thanks").show();		

	$('#highscores').addClass('hidden');
	
	});





/*	NEW HIGHSCORE STUFF */




	var name;
//	var score;
	var dataString;


	$('#press').click(function() {
	
	$('#result').load('out.php');
	$("#press").hide(1000);
	
	
	});




  $("#button").click(function() {  


	name = $("input#name").val();  
//	score = $("input#score").val();  

	dataString = 'name='+ name + '&score=' + score;

	$.ajax({
    type: "GET",
    url: "in.php",
    data: dataString,
    success: function() {
		$('#input').hide(1000);	
    	}
  	});
  return false;
  });



	$('input').keypress(function (event){ return event.keyCode == 13 ? false : true; });



	
//END OF MAIN FUNCTION	
});


