// Copyright 2005 David Caldwell <david@porkrind.org> -*- c -*-
function img(source,x,y,width,height){image=document.createElement("IMG");image.src="hopeless/"+source;image.style.width=px(width);image.style.height=px(height);image.width=width;image.height=height;image.style.position="absolute";image.style.left=px(x);image.style.top=px(y);image.style.border="0";return image;}
function Hopeless(board_div,score_div,highscore_div,highscoretabs_div,colors_div,start_div){this.init("hopeless",score_div,highscore_div,highscoretabs_div);this.board=board_div;this.color_picker=colors_div;this.start_button=start_div;var me=this;this.start_button.onclick=function(){me.new_game();}
this.ColorLookup=["blue.jpeg","green.jpeg","cyan.jpeg","purple.jpeg","red.jpeg","yellow.jpeg"];this.colors=4;this.blocks=new Array(20);this.gameover=undefined;}
Hopeless.prototype=new score_game();Hopeless.prototype.shuffle_colors=function(){for(var i=0;i<this.ColorLookup.length;i++){var x=Math.floor(Math.random()*this.ColorLookup.length);var tmp=this.ColorLookup[i];this.ColorLookup[i]=this.ColorLookup[x];this.ColorLookup[x]=tmp;}}
Hopeless.prototype.random_color=function(){return rand()%this.colors;}
function block(div,color_lookup,color,x,y){this.color=color;this.image=img(color_lookup[color],x*16,y*16,16,16);div.appendChild(this.image);return this;}
block.prototype.move=function(new_x,new_y){this.image.style.left=px(new_x);this.image.style.top=px(new_y);}
block.prototype.remove=function(){this.image.parentNode.removeChild(this.image);delete this.image;}
Hopeless.prototype.get_color=function(x,y){return this.blocks[x]!=undefined&&this.blocks[x][y]?this.blocks[x][y].color:undefined;}
Hopeless.prototype.remove=function(x,y){if(this.blocks[x][y]){this.blocks[x][y].remove();delete this.blocks[x][y];}}
Hopeless.prototype.remove_all=function(){for(var x=0;x<this.blocks.length;x++)
for(var y=0;y<this.blocks[x].length;y++)
this.remove(x,y);}
Hopeless.prototype.remove_block=function(x,y,total){if(!total)total=0;if(this.blocks[x][y]&&this.get_color(x,y)!=undefined){var color=this.blocks[x][y].color;this.blocks[x][y].color=-1;if(x>0&&this.get_color(x-1,y)==color)total=this.remove_block(x-1,y,total+1);if(x<20-1&&this.get_color(x+1,y)==color)total=this.remove_block(x+1,y,total+1);if(y>0&&this.get_color(x,y-1)==color)total=this.remove_block(x,y-1,total+1);if(y<10-1&&this.get_color(x,y+1)==color)total=this.remove_block(x,y+1,total+1);if(total>0){this.remove(x,y);}else
this.blocks[x][y].color=color;}
return total;}
Hopeless.prototype.move_block=function(x,y,x1,y1){this.blocks[x][y]=this.blocks[x1][y1];if(this.blocks[x][y]){this.blocks[x][y].move(x*16,y*16);delete this.blocks[x1][y1];}}
Hopeless.prototype.collapse_blocks=function(){var didSomething=0;do{didSomething=0;for(var x=0;x<this.blocks.length;x++)
for(var y=1;y<this.blocks[x].length;y++){if(!this.blocks[x][y]&&this.blocks[x][y-1]){this.move_block(x,y,x,y-1);didSomething=1;}}}while(didSomething);do{didSomething=0;for(var x=0;x<this.blocks.length-1;x++)
if(!this.blocks[x][10-1]&&this.blocks[x+1][10-1]){for(var y=0;y<this.blocks[x].length;y++)
this.move_block(x,y,x+1,y);didSomething=1;}}while(didSomething);}
Hopeless.prototype.any_moves_left=function(){for(var x=0;x<this.blocks.length;x++)
for(var y=0;y<this.blocks[x].length;y++)
if(this.get_color(x,y)!=undefined&&(this.get_color(x,y)==this.get_color(x+1,y)||this.get_color(x,y)==this.get_color(x,y+1)))
return 1;return 0;}
Hopeless.prototype.new_game=function(seed){score_game.prototype.new_game.call(this,seed);if(seed==undefined)this.seed=this.seed&0x7ffffff0|(parseInt(this.color_picker.value)-1);this.stats.set("seed",this.seed);this.stats.set("game_id",this.game_id);this.stats.set("colors",this.colors);this.shuffle_colors();this.colors=(this.seed&0xf)%6+1;if(this.blocks[0])this.remove_all();if(this.gameover!=undefined){this.board.removeChild(this.gameover);this.gameover=undefined;}
for(var x=0;x<this.blocks.length;x++){this.blocks[x]=new Array(10);for(var y=0;y<this.blocks[x].length;y++){this.blocks[x][y]=new block(this.board,this.ColorLookup,this.random_color(),x,y);}}
var me=this;this.board.onclick=function(event){var e=new plat.event(event,me.board);var removed=me.remove_block(Math.floor(e.targetX/16),Math.floor(e.targetY/16));var increase=removed*removed;if(me.colors>2)
increase*=me.colors;else
increase/=50/me.colors;me.score.inc(Math.floor(increase));if(removed){me.collapse_blocks();if(!me.any_moves_left()){me.board.onclick=null;me.gameover=img("gameover.png",0,0,320,160);me.board.appendChild(me.gameover);me.end_game();}}}}
var the_game=new Hopeless(doc.id("board"),doc.id("score"),doc.id("global_high_score"),doc.id("scoretab"),doc.id("colors"),doc.id("start"));the_game.new_game(URL.param.game);gotd.start=function(){the_game.new_game(game_of_the_day);return false;};
