/* JavaScript Game by Heiner Sextro - www.sextro.de */
/* Demo Version and Free Ware except the two functions in_array and GetRandom */
/* */
var game = null;
var rows = 9;
var cols = 17;
var c_number = 3;
var pic = 'q';
var selected = new Array();
var test = new Array();
var index = new Array();
function initGame() {
game = new initMatrix( cols , rows );

document.getElementById('board').style.visibility = "hidden";
document.getElementById('board').innerHTML = game.html;
document.getElementById('board').style.visibility = "visible";
game_menue();
/*
writeHtml( 'sound' , '<embed src="http://www.da-geh-ich-doch-gleich-zu-tonarchiv.de/sounds2007/demos/rewind01.swf" loop="false" width="12" height="12"></embed>' );
*/
}
function initMatrix( a , b ) {
this.matrix = new Array();
this.html = '';
this.gamePoints = 0;
var c = 0;
var width = 11;
switch (rows) {
  case 6:
    width = 60;
    break;
  case 9:
    width = 40;
    break;
  case 11:
    width = 35;
    break;
  default:
    break;
}
for ( i = 0; i <= b ;i++ )
   {
   this.matrix[i] = new Array;
   this.html = this.html + '<div>';
   for ( j = 0; j <= a ;j++ )
      {
	  index[c] = new Array();
      this.matrix[i][j] = new initSquare( c ,  i , j , a , b ) ;
	  this.html = this.html + '<span class="p" id="s_' + c + '" onmouseover="checkSquare(' + i + ',' + j +')" onmouseout="unmark()" onclick="startDelete(' + i + ',' + j +')"><img id="p_' + c + '"  src="system/grafx/boxdisaster/' + pic + '_' + this.matrix[i][j].color + '.png" height="'+width+'" width="'+width+'" /></span>';
	  index[c][0] = i;
	  index[c][1] = j;
	  c++;
      }
   this.html = this.html + '</div>';
   }
return this;
}
function initSquare( c , i , j , a , b ) {
this.erased = false;
this.deleted = false;
this.selected = false;
this.points = i;
this.c = c;
this.id = 's_' + c;

this.row = i;
this.col = j;

this.lastCol = a;
this.lastRow = b;

this.tell = tell;
this.check = check;
this.testEnd = testEnd;
this.mark = mark;
this.updateColor = updateColor;
this.color = GetRandom( 1 , c_number );
return this;
}

function testEnd() {
var top = this.row - 1;
if ( !this.erased ) {
   if ( top >= 0 && this.color == game.matrix[ top  ][ this.col  ].color ) {
      return true;
      }
   var bottom = this.row + 1;
   if ( bottom <= this.lastRow && this.color == game.matrix[ bottom  ][ this.col  ].color ) {
      return true;
      }
   var left = this.col - 1;
   if ( left >= 0 && this.color == game.matrix[ this.row  ][ left  ].color ) {
      return true;
      }
   var right = this.col + 1;
   if ( right <= this.lastCol && this.color == game.matrix[ this.row  ][ right  ].color ) {
      return true;
      }
   }
return false;
}
function check() {
/* Check the neighbour */
var top = this.row - 1;
if ( !this.erased ) {
if ( !in_array( this.c , test ) ) {
   if ( top >= 0 && this.color == game.matrix[ top  ][ this.col  ].color ) {
      test[ test.length ] = this.c;
	      this.mark();
              game.matrix[ top  ][ this.col  ].mark();
	     
		 	  game.matrix[ top ][ this.col ].check();
      }
   var bottom = this.row + 1;
   if ( bottom <= this.lastRow && this.color == game.matrix[ bottom  ][ this.col  ].color ) {
      test[ test.length ] = this.c;
	      this.mark();
              game.matrix[ bottom  ][ this.col  ].mark();
	     
		 	  game.matrix[ bottom ][ this.col ].check();
      }
   var left = this.col - 1;
   if ( left >= 0 && this.color == game.matrix[ this.row  ][ left  ].color ) {
      test[ test.length ] = this.c;
	      this.mark();
              game.matrix[ this.row  ][ left  ].mark();
	     
		 	  game.matrix[ this.row ][ left ].check();
      }
   var right = this.col + 1;
   if ( right <= this.lastCol && this.color == game.matrix[ this.row  ][ right  ].color ) {
      test[ test.length ] = this.c;
	      this.mark();
              game.matrix[ this.row  ][ right  ].mark();
	     
		 	  game.matrix[ this.row ][ right ].check();
      }
   }

}
/* uncomment for test issues
writeHtml( 'test' , pic + '<br />' + rows + '<br />' + cols + '<br />' );
   */


}
function mark() {
this.selected = true;
document.getElementById(this.id).style.border = 'solid #ff5500 1px';
}

function updateColor() {
var temp = 'p_' + this.c;
if ( this.erased ) {
   document.getElementById(temp ).src = 'system/grafx/boxdisaster/' + pic + '_x.png';
   }
else
   {
   document.getElementById(temp ).src = 'system/grafx/boxdisaster/' + pic + '_'+ this.color +'.png';
   }

}


function unmark() {
test = new Array();
for ( i = game.matrix.length - 1 ;i >= 0 ; i--)
  {
  for ( j = game.matrix[i].length - 1 ;j >= 0 ; j--)
     {
	 if ( game.matrix[i][j].selected ) {
	    game.matrix[i][j].selected = false;
		document.getElementById( game.matrix[i][j].id ).style.border = 'solid #005599 1px';
	    }
     }
  }
}


function startDelete(i,j) {
checkSquare( i , j );
deleteSelected();
}
function deleteSelected() {
var tempPoints = 0;
var countTest = test.length;
for (i=0;i < countTest; i++)
  {
  var a = index[ test[i] ][0];
  var b = index[ test[i] ][1];
  game.matrix[ a ][ b ].deleted = true;
  tempPoints = tempPoints + game.matrix[ a ][ b ].points;
  document.getElementById('p_' + test[i]).src = 'system/grafx/boxdisaster/' + pic + '_x.png';
  }
  /*setTimeout("moveDown()",30);*/
  moveDown();
  unmark();
  update_color();
  

  
test = new Array();
game.gamePoints = game.gamePoints + tempPoints * countTest;
if ( test_end() == false ) {
     writeHtml( 'sound' , '<embed src="http://www.samplegate.com/demos/tusch.swf" loop="false" width="12" height="12"></embed>' );
	 var temp = c_number + '_' + cols + '_' + rows;
     doAjaxRequest('index.php?ajax=1&com=start_boxdisaster&command=save_score&points=' + game.gamePoints + '&temp=' + temp ,'high_score',true);
	 }
else {
   if ( countTest > 3 ) {
      /*
      writeHtml( 'sound' , '<embed src="http://www.download-sounds.de/flash/clapping11.swf" loop="false" width="12" height="12"></embed>' );
	  */
      }
   else {
      /*
	  writeHtml( 'sound' , '<embed src="http://www.samplegate.com/demos/blubbern01.swf" loop="false" width="12" height="12"></embed>' );
      */
	  }
   }
writeHtml( 'points' , '<br /> Last Selection: ' + tempPoints * countTest + ' Points <br /> Points: ' + game.gamePoints);
}

function moveDown() {
for ( c = cols ;c >= 0 ; c--)
  {
  if ( game.matrix[rows][c].erased ) {
     pushCols(c);
	 }
  for ( r = rows ;r > 0 ; r--)
    {
	 if ( game.matrix[r][c].deleted ) {
	    
        for ( a = r ;a >= 0 ; a--) {
		   if (a > 0 ) {
		      game.matrix[a][c].erased = game.matrix[a - 1][c].erased;
		      game.matrix[a][c].deleted = game.matrix[a - 1][c].deleted;
		      game.matrix[a][c].color = game.matrix[a - 1][c].color;
			  }
		   else {
		      game.matrix[0][c].erased = true;
			  game.matrix[0][c].deleted = false;
			  game.matrix[0][c].color = 0;
			  }

		   
		   
		   }
	    
		
		moveDown();
		}
    }
  }
}
function test_end() {
var test = false;
for ( i = game.matrix.length - 1 ;i >= 0 ; i--)
  {
  for ( j = game.matrix[i].length - 1 ;j >= 0 ; j--)
     {
     if ( game.matrix[i][j].testEnd() ){
	    return true;
		}
     }
  }
return test;
}
function update_color() {
for ( i = game.matrix.length - 1 ;i >= 0 ; i--)
  {
  for ( j = game.matrix[i].length - 1 ;j >= 0 ; j--)
     {
     game.matrix[i][j].updateColor();
     }
  }
}
function pushCols(c) {
var temp = '';
for ( i = game.matrix.length - 1 ;i >= 0 ; i--)
  {
  /*temp = temp + '<hr />';*/
  for ( j = c ;j <= cols ; j++)
     {
		   if (j < cols ) {
	 		  game.matrix[i][j].erased = game.matrix[i][j + 1].erased;
		      game.matrix[i][j].deleted = game.matrix[i][j + 1].deleted;
		      game.matrix[i][j].color = game.matrix[i][j + 1].color;
			  }
		   else {
		      game.matrix[i][cols].erased = true;
			  game.matrix[i][cols].deleted = false;
			  game.matrix[i][cols].color = 0;
			  }

     /*temp = temp + ' : ' + game.matrix[i][j].selected;*/
     }
  }
/*writeHtml( 'test' , temp);*/
}
function tell() {
var temp = '';
for ( i = game.matrix.length - 1 ;i >= 0 ; i--)
  {
  temp = temp + '<hr />';
  for ( j = game.matrix[i].length - 1 ;j >= 0 ; j--)
     {
     temp = temp + ' : ' + game.matrix[i][j].selected;
     }
  }
writeHtml( 'test' , temp);
}
function GetRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
 
        return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

function game_menue() {
var temp = '';
temp = temp + '<hr />';
temp = temp + '<div class="threat_list">Select Pictures<br />';
temp = temp + '<a href="javascript:setPic(\'p\');">[ boxes ]</a>';
temp = temp + '<a href="javascript:setPic(\'o\');">[ fruits ]</a>';
temp = temp + '<a href="javascript:setPic(\'q\');">[ unicolor ]</a>';
temp = temp + '<br />Select Number of colors<br />';
temp = temp + '<a href="javascript:setC_number(3);">[ 3 ]</a>';
temp = temp + '<a href="javascript:setC_number(4);">[ 4 ]</a>';
temp = temp + '<a href="javascript:setC_number(5);">[ 5 ]</a>';
temp = temp + '<a href="javascript:setC_number(6);">[ 6 ]</a>';
temp = temp + '<a href="javascript:setC_number(7);">[ 7 ]</a>';
temp = temp + '<br />Select Board<br />';
temp = temp + '<a href="javascript:setMatrix_number(6,10);">[ 7 * 11 ]</a>';
temp = temp + '<a href="javascript:setMatrix_number(9,17);">[ 10 * 18 ]</a>';
temp = temp + '<a href="javascript:setMatrix_number(11,19);">[ 12 * 19 ]</a>';
temp = temp + '</div>';
writeHtml( 'test' , temp);
}
function setPic( temp ) {
pic = temp;
initGame();
}
function setC_number( cn ) {
c_number = cn;
initGame();
var temp = c_number + '_' + cols + '_' + rows;
doAjaxRequest('index.php?ajax=1&com=start_boxdisaster&command=get_scorelist&temp=' + temp ,'high_score',true);
}
function setMatrix_number( r , c ) {
rows = r;
cols = c;
initGame();
var temp = c_number + '_' + cols + '_' + rows;
doAjaxRequest('index.php?ajax=1&com=start_boxdisaster&command=get_scorelist&temp=' + temp ,'high_score',true);
}

function checkSquare( row , col ) {
game.matrix[ row ][ col ].check() ;
}
function writeHtml( id , temp )
{
document.getElementById( id ).style.visibility = "hidden";
document.getElementById( id ).innerHTML = temp;
document.getElementById( id ).style.visibility = "visible";
}

/* this nice function in_array ( known from PHP but not implemented in javascript ) I found at http://www.dodwin.de/weblog/2007/03/in_array-fuer-javascript */
function in_array(item,arr) {
for(p=0;p<arr.length;p++) if (item == arr[p]) return true;
return false;
}
/* another function from web: GetRandom found at http://www.naden.de/blog/zufallszahlen-in-javascript-mit-mathrandom */
function GetRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
	}
 
        return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

