/*
Klicks 0.9 main engine 
(c) 2006 lichtbruch.com - Thomas Zettelmayr

*/


var klicks = {
	gameRunning: 'false',
	stones: [],
	columns: [false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false],
	empty: [false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false],
	timeElapsed:0,
	timer:null,
	stonecount: 216,
	newEmptyColumns:false,
	leftborder:0,
	rightborder:17,
	inputDisabled:true,
	
	start: function (difficulty) {
		// difficulty can range from 0-2
		
		// fix the position of the victory display:
		var vicpos=0;
		if (window.innerWidth) {
			vicpos=Math.floor(window.innerWidth/2-160);
			}
		else {
			vicpos=Math.floor(document.body.clientWidth/2-160);
			}			
		document.getElementById('divvictor').style.left=vicpos;
		
		this.stonecount=216;
		this.updateCounter();
		this.leftborder=0;
		this.rightborder=17;
		// populate the array meaningfully
		this.populateStones(difficulty);
		// display the array
		this.showAllStones();
		this.gameRunning=true;
		this.inputDisabled=false;

		// reset the stone counter to its maximum
		// this.stopTimer();
		this.startTimer();
		},
		
	stoneClicked: function (stoneId) {
		// bail out if this isnt an active game
		if (!this.gameRunning) return;
		if (this.inputDisabled) {
			// alert ("disabled");
			return;
			}
		// ignore if the tile is empty
		if (this.stones[stoneId]==0) return;
		this.inputDisabled=true;
			if (this.removeStone (stoneId,this.stones[stoneId], true)) {
				this.inputDisabled=false;
				return;
				}
		this.validateColumns();
		this.updateCounter();
		this.columns=[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false];
		
		// ranrücken
		if (this.newEmptyColumns) {
			this.columnShift();
			this.newEmptyColumns=false;			
			}
		// haben wir keine Steine mehr ?
		if (this.stonecount==0) this.victory();
		this.inputDisabled=false;			
		},

	removeStone: function (stoneId, color, remove)	{
		var col = Math.floor(stoneId / 12);
		var row = stoneId % 12;
		var neighbour = 0;
		var notRemoved=true;
		// tries to remove the current stone
		// do we have a neighbour with the same color ?

		// check left
		// ignore if at left border
		if (col > this.leftborder ) {
			// get the left neighbour stoneId
			neighbour = (col-1)*12+row;
			// alert ('left neighbour = ' + Math.floor(neighbour / 12) + " : " + neighbour % 12 );
			if (this.stones[neighbour] == color) {
				// remove the current stone
				if (remove) {
					this.stones[stoneId]=0;
					this.stonecount--;
					remove=false;
					this.columns[col]=true;
					notRemoved=false;
					}
				this.stones[neighbour]=0;
				this.stonecount--;
				this.columns[col-1]=true;
				// recursively trace the new stone
				this.removeStone(neighbour,color,false);
				}
			}

		// check right
		if (col < this.rightborder ) {
			// get the  right neighbour stoneId
			neighbour = (col+1)*12+row;
			// alert ('right neighbour = ' + Math.floor(neighbour / 18) + "col: " + neighbour % 18 );
			if (this.stones[neighbour] == color) {
				if (remove) {
					// remove the current stone
					this.stones[stoneId]=0;
					this.stonecount--;
					remove=false;
					this.columns[col]=true;
					notRemoved=false;
					}
				this.stones[neighbour]=0;
				this.stonecount--;
				this.columns[col+1]=true;
				// recursively trace the new stone
				this.removeStone(neighbour,color,false);
				}
			}

		// check bottom

		if (row > 0 ) {
			// get the bottom neighbour stoneId
			neighbour = col*12+row-1;
			// alert ('top neighbour = ' + Math.floor(neighbour / 18) + "col: " + neighbour % 18 );
			if (this.stones[neighbour] == color) {
				if (remove) {
					// remove the current stone
					this.stones[stoneId]=0;
					this.stonecount--;
					remove=false;
					this.columns[col]=true;
					notRemoved=false;
					}
				this.stones[neighbour]=0;
				this.stonecount--;
				// recursively trace the new stone
				this.removeStone(neighbour,color,false);
				}
			}

		if (row < 11 ) {
			// get the top neighbour stoneId
			neighbour = col*12+row+1;
			if (this.stones[neighbour] == color) {
				if (remove) {
					// remove the current stone
					this.stones[stoneId]=0;
					this.stonecount--;
					remove=false;
					this.columns[col]=true;
					notRemoved=false;
					}
				this.stones[neighbour]=0;
				this.stonecount--;
				// recursively trace the new stone
				this.removeStone(neighbour,color,false);
				}
			}


		return notRemoved;
		},

	victory: function() {
		// timer stoppen
		this.stopTimer();
		
		// fields übernehmen
		document.forms[1].ptime.value=this.timeElapsed;
		var seconds = this.timeElapsed % 60;
		if (seconds<10) seconds = '0'+seconds;
		document.getElementById('etime').firstChild.data=Math.floor(this.timeElapsed / 60 ) + ':' + seconds;
		// Formular anzeigen
		document.getElementById('divvictor').style.visibility="visible";
		},

	populateStones: function (difficulty) {
		var maxDifficulty=3+difficulty;
		var column=0;
		var color=0;
		var direction=0;
		var stoneSet=false;
		
		// first we create an array defining the height fields
		var height=[11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11];
		// then we do a number of randomized fillups, thinking that we have 216 stones, the maximum number of pure randomization
		// tries should be somehow limited

		// ausnullen		
		for (var i = 0; i<216 ; i++) {
			this.stones[i]=0;
			}
		
		for (i = 0; i<200 ; i++) {
			stoneSet=false;
			// ok, I create 2 sets of random numbers, first a column
			column=this.rand(16);
			color=this.rand(maxDifficulty)+1;
			// alert (color);
			if (height[column]>=0 && height[column+1]>=0) {
				// ok, wir haben Platz, steine setzen
				this.stones[column*12+height[column]]=color;
				this.stones[(column+1)*12+height[column+1]]=color;
				// alert (this.stones[column*12+height[column+1]]);
				height[column]--;
				height[column+1]--;
				}
			}					
			
		// ok, und jetzt füllen wir den Rest auf:
		for (i = 0; i<=17 ; i++) {
			// wir gehen die Spalten von links nach rechts durch und füllen auf
			while (height[i]>=1) {
				// also haben wir noch mindestens 2 Steine
				color=this.rand(maxDifficulty)+1;
				this.stones[i*12+height[i]]=color;
				this.stones[i*12+height[i]-1]=color;
				height[i]--;
				}
			// bleiben wir bei 0 hängen
			if (height[i]==0) {
				this.stones[i*12]=this.stones[i*12+1];
				}
			}
		
		
		
		
			

		},

	showAllStones: function () {
		// renders all stones on the screen according to the array
		var maxS=this.rightborder*12+11;
		for (var i = (this.leftborder*12); i<=maxS ; i++) {

			// document.getElementById('stone'+i).src="tile" + i + ".src";
			document.getElementById('stone'+i).src=eval("tile" + this.stones[i] + ".src");

			}

		// alert("updated");
		},
		
	repaintColumn: function (col) {
		// renders all stones on the screen according to the array
		var myId=0;
		for (var i = 11; i>=0 ; i--) {
			myId=col*12+i;
			document.getElementById('stone'+myId).src=eval("tile" + this.stones[myId] + ".src");
			}
		},		

	validateColumns: function () {
		/*
		ok, second approach
		I go through the colums from left to right
		for each colum I start at the 2nd one from the bottom and move up
		*/

		for (var col = this.leftborder; col<=this.rightborder;col++) { // go from left to right
			if (this.columns[col]) this.updateColumn(col);
			}
		
		},
	
	updateColumn: function (col) {
		var row = 0;
		var currentStone=0;
		var stoneCount=0;
		var stoneFound=false;
		
		// wieviele Steine haben wir noch in der Spalte?
		for (var i=0;i<=11;i++) {
			if (this.stones[col*12+i] > 0) stoneCount++;
			}
		if (stoneCount==0) {
			this.repaintColumn(col);
			this.empty[col]=true;
			this.newEmptyColumns=true;
			return;
			}
			
		for (var i=1;i<=11;i++) {
			// ist an dieser Stelle ein Stein?
			if (this.stones[col*12+i]>0) {
				currentStone=i;
				while (this.stones[col*12+currentStone-1]==0 && currentStone>0) {
					// alert ("loch auf" + (currentStone-1));
					this.stones[col*12+currentStone-1]=this.stones[col*12+currentStone];
					this.stones[col*12+currentStone]=0;
					currentStone--;
					}
				}				
			}				
			this.repaintColumn(col);			
		}, // end of if repaint, nothin
		
	
	columnShift: function () {
		// 9er ist mein Ausgangspunkt
		var i=0;
		var j=0;
		// linke und rechte Seite wenn empty ausnullen
		while (this.empty[this.leftborder]) {
			this.empty[this.leftborder]=false;
			this.leftborder++;
			}
		while (this.empty[this.rightborder]) {
			this.empty[this.rightborder]=false;
			this.rightborder--;
			}
		
		
		
		for (i=9;i<this.rightborder;i++) {
			if (this.empty[i]) {
				// alle Reihen verschieben
				for (j=i*12;j <= (this.rightborder*12-1);j++) {
					this.stones[j]=this.stones[j+12];
					}
				// alle flags verschieben
				for (j=9;j < this.rightborder;j++) {
					this.empty[j]=this.empty[j+1];
					}
				// rightborder ausnullen					
				for (j=0;j < 12;j++) {
					this.stones[this.rightborder*12+j]=0;
					// myId=col*12+i;
					document.getElementById('stone'+(this.rightborder*12+j)).src=eval("tile0.src");
					}
				// this.repaintColumn(this.rightborder);					
				this.rightborder--;
				i--;
				}
			}
		for (i=8;i>this.leftborder;i--) {
			if (this.empty[i]) {
				// alert ('von links verschieben');
				// alle Reihen verschieben
				for (j=i*12+11;j >= (this.leftborder*12+12);j--) {
					this.stones[j]=this.stones[j-12];
					}
				// alle flags verschieben
				for (j=8;j > this.leftborder;j--) {
					this.empty[j]=this.empty[j-1];
					}
				// leftborder ausnullen					
				for (j=0;j < 12;j++) {
					this.stones[this.leftborder*12+j]=0;
					// myId=col*12+i;
					document.getElementById('stone'+(this.leftborder*12+j)).src=eval("tile0.src");
					}
				this.leftborder++;
				i++;
				}
			}			
		this.empty=[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false];
		this.showAllStones(); // repaint all;
		}, // end of if repaint, nothin		



	startTimer: function () {
		this.timeElapsed=0;
		if (this.timer) window.clearInterval(this.timer);
		this.timer=window.setInterval("klicks.updateTimer()",1000);

		},
	stopTimer: function () {
		// this.timeElapsed=0;
	 	if (this.timer) window.clearInterval(this.timer);
		},

	updateCounter: function () {
		document.getElementById('spanCounter').firstChild.data=this.stonecount;
		},
		
	switchColumns: function (cola,colb) {
		// transfers the content from cola to colb
		for (var i=0; i<12;i++) {
			this.stones[12*colb+i]=this.stones[12*cola+i];
			this.stones[12*cola+i]=0;
			}
		this.empty[colb]=this.empty[cola];
		this.empty[cola]=true;
		},


	updateTimer: function() {
		// updates the timer
		this.timeElapsed++;
		var seconds = this.timeElapsed % 60;
		if (seconds<10) seconds = '0'+seconds;
		document.getElementById('spanTimer').firstChild.data=Math.floor(this.timeElapsed / 60 ) + ':' + seconds;
		// recursive if game is running
		},
		
	// Sound functions
	
	
	rand: function (maxV) {
		// helper function - returns a random number from 0 to max
		return Math.floor(Math.random()*(maxV+1));
		}

}; // end of game object

