/****************************************************
*This version is made by Jakob Dun. Orginal version made by:
*Brady Mulhollem- http://www.webtech101.com
*This game is available at http://www.dynamicdrive.com
****************************************************/
function cbsnake(language){
	
	this.language = language;
	//Pixels to move at once
	this.jump = 25;
	//Size of snake.
	this.sos = 25;
	//Size of board
	//DANGER!!! this.sofb must be EVENLY dividable by this.jump DANGER!!!!
	this.sofb = 500;
	//Set things up
	this.daway = this.sofb - this.jump;
	this.correct = new Array();
	this.correct[0] = 0;
	while(this.correct[this.correct.length -1] != this.daway){
		this.correct[this.correct.length] = this.correct[this.correct.length -1]+this.jump
	}
	this.zero = 0;
	if(language == 'nl'){
		var gameboard = ' <div class="board" id="board"><div id="i2"><img src="../snake/worm.jpg" height="60px" width="300px"></div> </div><div class="board" id="score"> <span id="cscore">0</span> <span id="buttons"> <button type="button" id="slow" onClick="snake.slow()">Langzaam</button> <button type="button" id="fast"  onClick="snake.fast()">Snel</button> </span></div>';
	}else if(language == 'en'){
		var gameboard = ' <div class="board" id="board"><div id="i2"><img src="../snake/worm.jpg" height="60px" width="300px"></div> </div><div class="board" id="score"> <span id="cscore">0</span> <span id="buttons"> <button type="button" id="slow" onClick="snake.slow()">Slow</button> <button type="button" id="fast"  onClick="snake.fast()">Fast</button> </span></div>';
	}
	document.write(gameboard);
	var innerHTML = document.getElementById('board').innerHTML;
	innerHTML = innerHTML + '<div id="snake0" class="snake"></div><div id="snake1" class="snake"></div><div id="snake2" class="snake"></div>';
	document.getElementById('board').innerHTML = innerHTML
}

cbsnake.prototype.setup = function(setspeed){
	var thisObj = this;
	//Score...
	this.score = 0;
	//saves the turns
	this.turns = new Array();
	this.upperCount = 0;
	this.lowerCount = 0;
	this.turnCheck = false;
	//Snake Direction
	this.sdir = 'right';
	this.sdirb = 'right'; 
	this.sdirp = 'right';
	this.snakeDir = new Array();
	this.snakeDir[0] = 'right';
	this.snakeDir[1] = 'right';
	this.snakeDir[2] = 'right';
	//Snake arrays
	this.ctop = new Array();
	this.cleft = new Array();
	//Top of snake class
	this.ctop[0] = 200;
	this.ctop[1] = 200;
	this.ctop[2] = 200;
	//Left of Snake class
	this.cleft[0] = 75;
	this.cleft[1] = 50;
	this.cleft[2] = 25;
	//current top of apple
	this.atop = 0;
	//current left of apple
	this.aleft = 0;
	document.getElementById('board').innerHTML = '<div id="food"></div><div id="snake0" class="snake"></div><div id="snake1" class="snake"></div><div id="snake2" class="snake"></div>';
	document.getElementById('snake0').style.top = this.ctop[0]+"px";
	document.getElementById('snake0').style.left = this.cleft[0]+"px";
	document.getElementById('snake1').style.top = this.ctop[1]+"px";
	document.getElementById('snake1').style.left = this.cleft[1]+"px";
	document.getElementById('snake2').style.top = this.ctop[2]+"px";
	document.getElementById('snake2').style.left = this.cleft[2]+"px";
	//Milliseconds between move
	this.speed = setspeed;
	this.moveapple();
	this.stopgame = false;

	
	setTimeout(function(){ thisObj.msnake() },this.speed);
	document.onkeydown = function(e){ return thisObj.snakedir(e); };
}
cbsnake.prototype.slow = function(){
	this.setup(110);
	this.buttons('true');
	document.getElementById('slow').blur();
}
cbsnake.prototype.fast = function(){
	this.setup(70);
	this.buttons('true');
	document.getElementById('fast').blur();
}
cbsnake.prototype.rannum = function(num1,num2){
	num1 = parseInt(num1);
	num2 = parseInt(num2);
	var generator = Math.random()*(Math.abs(num2-num1));
	generator = Math.round(num1+generator);
	return generator;
}
cbsnake.prototype.moveapple = function(){
	var usethis = false;
	while(!usethis){
		this.atop = this.correct[this.rannum(0,this.correct.length-1)];
		this.aleft = this.correct[this.rannum(0,this.correct.length-1)];
		if(this.numInArray(this.ctop,this.cleft,this.atop,this.aleft) == 0){
			usethis = true;
		}			
	}
	document.getElementById('food').style.top = this.atop+"px";
	document.getElementById('food').style.left = this.aleft+"px";
}

cbsnake.prototype.msnake = function(){
	if(this.stopgame === false){
		if(this.sdir != 'none'){
			this.moveall();
		}
		var thisObj = this;
		switch(this.sdir){
				case 'up':
					this.ctop[0] = this.ctop[0] - this.jump;
					document.getElementById('snake0').style.top = this.ctop[0]+"px";
					if((this.ctop[0] == this.zero && this.sdirb == 'up') || this.ctop[0] < this.zero){
						this.gover();
					}
					break;
				case 'down':
					this.ctop[0] = this.ctop[0] + this.jump;
					document.getElementById('snake0').style.top = this.ctop[0]+"px";
					if((this.ctop[0] == this.daway && this.sdirb == 'down') || this.ctop[0] > this.daway){
						this.gover();
					}
					break;
				case 'left':
					this.cleft[0] = this.cleft[0] - this.jump;
					document.getElementById('snake0').style.left = this.cleft[0]+"px";
					if((this.cleft[0] == this.zero && this.sdirb == 'left') || this.cleft[0] < this.zero){
						this.gover();
					}
					break;
				case 'right':
					this.cleft[0] = this.cleft[0] + this.jump;
					document.getElementById('snake0').style.left = this.cleft[0]+"px";
					if((this.cleft[0] == this.daway && this.sdirb == 'right') || this.cleft[0] > this.daway){
						this.gover();
					}
					break;
		}
		if(this.sdir != 'none'){
			this.hitself();
			this.happle();
		}
	this.sdir = this.sdirb
	setTimeout(function(){ thisObj.msnake() },this.speed);
	}
}

cbsnake.prototype.snakedir = function(e){  
		if(!e){
			//IE...
			e = window.event;
		}
		
		switch(e.keyCode){
			case 38:
				if(this.sdir != 'down' && this.sdirp != 'down' && this.sdir != 'up'){
					if (this.sdir != 'none') {
						this.turns[this.upperCount] = 'up|' + this.cleft[0] + 'px|' + this.ctop[0]+'px';
						this.upperCount++;
						this.snakeDir[0] = 'up';
					}
					this.sdirb = 'up';
					this.sdirp = 'up';
				}
				break;
			case 40:
				if(this.sdir != 'up' && this.sdirp != 'up' && this.sdir != 'down'){
					if (this.sdir != 'none') {
						this.turns[this.upperCount] = 'down|' + this.cleft[0] + 'px|' + this.ctop[0]+'px';
						this.upperCount++;
						this.snakeDir[0] = 'down';
					}
					this.sdirb = 'down';
					this.sdirp = 'down';
				}
				break;
			case 37:
				if(this.sdir != 'right' && this.sdirp != 'right' && this.sdir != 'left'){
					if (this.sdir != 'none') {
						this.turns[this.upperCount] = 'left|' + this.cleft[0] + 'px|' + this.ctop[0]+'px';
						this.upperCount++;
						this.snakeDir[0] = 'left';
					}
					this.sdirb = 'left';
					this.sdirp = 'left';
				}
				break;
			case 39:
				if(this.sdir != 'left' && this.sdirp != 'left' && this.sdir != 'right'){
					if (this.sdir != 'none') {
						this.turns[this.upperCount] = 'right|' + this.cleft[0] + 'px|' + this.ctop[0]+'px';
						this.upperCount++;
						this.snakeDir[0] = 'right';
					}
					this.sdirb = 'right';
					this.sdirp = 'right';
				}
				break;
			case 32:
				if(this.sdir == 'none' && this.sdirp != 'none'){
					this.sdirb = this.sdirp;
					this.sdirp = 'none';
				}
				else{
				this.sdirp = this.sdir;
				this.sdirb = 'none';
				}
				break;
		}
		return this.stopgame;
		
}

cbsnake.prototype.moveall = function(){
	var i = this.ctop.length - 1;
	while(i != 0){
		var topPrevious = document.getElementById('snake'+(i-1)).style.top;
		var leftPrevious = document.getElementById('snake'+(i-1)).style.left;
		var topCurrent = document.getElementById('snake'+i).style.top;
		var leftCurrent = document.getElementById('snake'+i).style.left; 
		document.getElementById('snake'+i).style.top = topPrevious;
		document.getElementById('snake'+i).style.left = leftPrevious;
		document.getElementById('snake'+i).style.display = 'block';
		
		if(this.turns[this.lowerCount] == null){//this means there is no turn atm
			if (i == this.ctop.length - 1) {
				document.getElementById('snake' + i).style.backgroundImage = this.getImage('back', this.snakeDir[i]);
			}else {
				document.getElementById('snake' + i).style.backgroundImage = this.getImage('mid', this.snakeDir[i], false);
			}
			this.ctop[i] = this.ctop[i - 1];
			this.cleft[i] = this.cleft[i - 1];
			i = i - 1;
		}else{
			var tempArray;
			
			for(count = this.lowerCount;count <= this.upperCount-1;count++){
				tempArray = this.turns[count].split("|");
				if (tempArray[1] == leftCurrent && tempArray[2] == topCurrent) {
					var prevDirection = this.snakeDir[i];
					this.snakeDir[i] = tempArray[0];
					if (i == this.ctop.length - 1) {//last part of snake
						document.getElementById('snake'+i).style.backgroundImage = this.getImage('back', this.snakeDir[i]);
						this.turnCheck = true;
					}else{//a middle part of the snake
						document.getElementById('snake'+i).style.backgroundImage = this.getImage('mid', this.snakeDir[i],true,prevDirection);
					}
					doneIf = true;
					break;
				}else{
					doneIf = false;
				}
			}
			
			if (!doneIf) {
				if (i == this.ctop.length - 1) {
					document.getElementById('snake' + i).style.backgroundImage = this.getImage('back', this.snakeDir[i]);
				}else {
					document.getElementById('snake' + i).style.backgroundImage = this.getImage('mid', this.snakeDir[i], false);
				}
			}
	
			this.ctop[i] = this.ctop[i - 1];
			this.cleft[i] = this.cleft[i - 1];
			i = i - 1;
			if (i == 0) {
				document.getElementById('snake' + i).style.backgroundImage = this.getImage('front', this.snakeDir[i]);
			}
		}
	}
	
	if(this.turnCheck){
		this.turns[this.lowerCount] = null;
		this.lowerCount++;
		this.turnCheck = false;
	}
}

cbsnake.prototype.getImage = function(snakePart,direction,corner,prevDirection){
	if(snakePart == 'mid'){
		if (corner) {
			switch(direction){
			case 'up':
				if(prevDirection == 'left'){
					return "url(../snake/cornerRight.png)";
				}else if(prevDirection == 'right'){
					return "url(../snake/cornerUp.png)";
				}
				break;
			case 'down':
				if(prevDirection == 'left'){
					return "url(../snake/cornerDown.png)";
				}else if(prevDirection == 'right'){
					return "url(../snake/cornerLeft.png)";
				}
				break;
			case 'left':
				if(prevDirection == 'up'){
					return "url(../snake/cornerLeft.png)";
				}else if(prevDirection == 'down'){
					return "url(../snake/cornerUp.png)";
				}
				break;
			case 'right':
				if(prevDirection == 'up'){
					return "url(../snake/cornerDown.png)";
				}else if(prevDirection == 'down'){
					return "url(../snake/cornerRight.png)";
				}
				break;
			}
		}else {
			if (direction == 'up' || direction == 'down') {
				return "url(../snake/midVertical.png)";
			}else if (direction == 'left' || direction == 'right') {
				return "url(../snake/midHorizontal.png)";
			}
		}		
	}else if(snakePart == 'back'){
		switch(direction){
			case 'up':
				return "url(../snake/backGoingUp.png)";
				break;
			case 'down':
				return "url(../snake/backGoingDown.png)";
				break;
			case 'left':
				return "url(../snake/backGoingLeft.png)";
				break;
			case 'right':
				return "url(../snake/backGoingRight.png)";
				break;
		}
	}else if(snakePart == 'front'){
		switch(direction){
			case 'up':
				return "url(../snake/frontUp.png)";
				break;
			case 'down':
				return "url(../snake/frontDown.png)";
				break;
			case 'left':
				return "url(../snake/frontLeft.png)";
				break;
			case 'right':
				return "url(../snake/frontRight.png)";
				break;
		}
	}
}

cbsnake.prototype.gover = function(){
	if(!this.stopgame){
		this.stopgame = true;
		var inner = document.getElementById('board').innerHTML;
		if(this.language == 'nl'){
			document.getElementById('board').innerHTML = inner+'<div id="notice">De worm is '+this.score+' maand(en) oud</div><div id="i2"><img src="../snake/worm.jpg" height="60px" width="300px"></div>';
		}else if(this.language == 'en'){
			document.getElementById('board').innerHTML = inner+'<div id="notice">The worm is '+this.score+' month(s) old</div><div id="i2"><img src="../snake/worm.jpg" height="60px" width="300px"></div>';
		}
		this.buttons('');
	}
}
cbsnake.prototype.happle = function(){
	if(this.atop == this.ctop[0] && this.aleft == this.cleft[0]){
		//HIT!!!
		this.score++;
		document.getElementById('cscore').innerHTML = this.score;
		this.moveapple();
		this.addsnake();
	}
}
cbsnake.prototype.addsnake = function(){
	var newsnake = document.createElement('div');
	var newid = this.cleft.length;
	newsnake.setAttribute('id','snake'+newid);
	//this crap is for IE. I would rather add the class name.
	newsnake.style.position = 'absolute';
	newsnake.style.top = '-10px';
	newsnake.style.left = '-10px';
	newsnake.style.display = 'none';
	var tempDing = 'snake'+(newid-1);
	newsnake.style.backgroundImage = document.getElementById(tempDing).style.backgroundImage;
	newsnake.style.height = '25px';
	newsnake.style.width = '25px';
	newsnake.style.overflow = 'hidden';
	document.getElementById('board').appendChild(newsnake);
	this.cleft[this.cleft.length] = -10;
	this.ctop[this.ctop.length] = -10;
	this.snakeDir[newid] = this.snakeDir[newid-1];
}

cbsnake.prototype.numInArray = function(array,array2,value,value2){
	var n = 0;
	for (var i=0; i < array.length; i++) {
		if (array[i] === value && array2[i] === value2) {
			n++;
		}
	}
	return n;
}
cbsnake.prototype.hitself = function(){
	if(this.numInArray(this.ctop,this.cleft,this.ctop[0],this.cleft[0]) > 1){
		this.gover();
	}
}
cbsnake.prototype.buttons = function(setto){
	document.getElementById('slow').disabled = setto;
	document.getElementById('fast').disabled = setto;
}
