// Copyright (c) 2006 David Caldwell,  All Rights Reserved. -*- c -*-
function seed_game(){}
seed_game.prototype.init=function(name){this.undo=[];this.redo=[];this.stats=new stats(this.name=name);this.control_panel=doc.id("control_panel");var highscore_table=function(title,game,seed){var div;new_alert(doc.el("div",{},div=doc.el("div",{},doc.text("Loading high score table..."),doc.img("spinner.gif")),this.seed!=game_of_the_day?undefined:doc.el("p",{align:"center",style:{fontSize:"x-small"}},doc.el("a",{href:"leader.html"},doc.text("Check the Leader Board")),doc.text(" for current game of the day standings."))),{goaway:true,title:title,width:375});highscore_seed(game,seed,{},function(scores){if(scores.length==0){div.parentNode.replaceChild(doc.el("div",{},doc.text("Sorry, there are no scores for this game yet. "),this.seed==game_of_the_day?undefined:doc.el("span",{},doc.text("Try playing the "),doc.el("a",{href:"?game="+game_of_the_day},doc.text("Game of the day")),doc.text("."))),div);return;}
render_highscores(div,scores);});}
var button=function(title,arg1,arg2){var extra=arg2?arg1:{};var onclick=arg2||arg1;return doc.el("input",merge({type:"button",value:title,onclick:onclick},extra));};doc.appendTo(this.control_panel,button("New Game",wrap(this,function(){this.push_state();this.new_game();})),button("Replay",wrap(this,function(){this.push_state();this.new_game(this.seed);})),button("Give Up",wrap(this,function(){this.push_state();this.end(GAVE_UP);})),doc.text("Game #:"),this.game_entry=doc.el("input",{type:"text",size:12,id:"game",style:{display:"none"},onchange:wrap(this,function(){this.push_state();this.new_game(Number(this.game_entry.value));doc.id("game").style.display="none";doc.id("link").style.display="inline";}),onblur:function(){doc.id("game").style.display="none";doc.id("link").style.display="inline";}}),this.game_link=doc.el("a",{href:"safari",id:"link",onclick:wrap(this,function(){doc.id("game").style.display="inline";doc.id("link").style.display="none";return false;})}),button("High Scores",wrap(this,function(){highscore_table("High Scores for Game "+this.seed,this.name,this.seed);})),!(this.instructions=document.getElementById("instructions"))?null:this.instructions_button=button("Show Rules",wrap(this,function(){if(this.instructions.style.display=="block"){this.instructions.style.display="none";this.instructions_button.value="Show Rules";this.stats.inc("Showed Instructions");}else{this.instructions.style.display="block";this.instructions_button.value="Hide Rules";this.stats.inc("Hid Instructions");}})),button("Undo",{disabled:true,id:"Undo",accesskey:"Z",title:"Ctrl-Z"},wrap(this,function(){this.undo_state();})),button("Redo",{disabled:true,id:"Redo",accesskey:"E",title:"Ctrl-Shift-Z"},wrap(this,function(){this.redo_state();})),!this.complete?null:button("Auto-finish",{accesskey:"A",title:"Ctrl-A"},wrap(this,function(){this.push_state();this.complete()||this.pop_state();})));plat.register_key_events(document,wrap(this,function(event){switch(event.key){case'C-z':case'M-z':case'A-z':this.undo_state();return false;case'C-Z':case'M-Z':case'A-Z':this.redo_state();return false;case'C-a':case'M-a':case'A-a':this.push_state();this.complete&&this.complete()||this.pop_state();return false;}
return true;}));}
seed_game.prototype.update_game_link=function(force){if(force||this.game_entry.value!=this.seed){this.game_entry.value=this.seed;if(this.game_link.childNodes.length==0)
this.game_link.appendChild(document.createTextNode("safari"));this.game_link.childNodes[0].data=this.seed;this.game_link.href=document.URL.split('?')[0]+"?game="+this.seed;}}
seed_game.prototype.new_game=function(seed){this.seed=gotd.pick_seed(seed,this.name);srand(this.seed);this.game_id=(new Date()).getTime()+Math.random();this.update_game_link();}
seed_game.prototype.start_stats=function(){this.stats.start();this.stats.set("seed",this.seed);this.stats.set("game_id",this.game_id);}
seed_game.prototype.end_of_move=function(){var complete;if(this.is_complete!=undefined&&(complete=this.is_complete()))
this.end(complete);}
seed_game.prototype.set_state=function(state){}
seed_game.prototype.get_state=function(){return undefined;}
seed_game.prototype.set_stats_state=function(state){this.set_state(state.state);this.stats.current=state.stats;}
seed_game.prototype.get_stats_state=function(state){return{state:state||this.get_state(),stats:this.stats.current};}
seed_game.prototype.button_disable=function(button,disabled){button.disabled=disabled;}
seed_game.prototype.push_state=function(state){var state=this.get_stats_state(state);if(!state.state)return;this.undo.push(state);this.button_disable(doc.id("Undo"),this.undo.length==0);this.redo=[];this.button_disable(doc.id("Redo"),this.redo.length==0);}
seed_game.prototype.pop_state=function(){if(this.undo.length>0){this.set_stats_state(this.undo.pop());this.button_disable(doc.id("Undo"),this.undo.length==0);}
this.redo=[];this.button_disable(doc.id("Redo"),this.redo.length==0);}
seed_game.prototype.undo_state=function(){if(this.undo.length>0){this.stats.inc("undo");this.redo.push(this.get_stats_state());this.button_disable(doc.id("Redo"),this.redo.length==0);this.set_stats_state(this.undo.pop());this.button_disable(doc.id("Undo"),this.undo.length==0);}}
seed_game.prototype.redo_state=function(){if(this.redo.length>0){this.stats.inc("redo");this.undo.push(this.get_stats_state());this.button_disable(doc.id("Undo"),this.undo.length==0);this.set_stats_state(this.redo.pop());this.button_disable(doc.id("Redo"),this.redo.length==0);}}
var NOT_COMPLETE=0,GAME_OVER=1,WON=2,GAVE_UP=3;var complete_text=["NOT_COMPLETE","GAME_OVER","WON","GAVE_UP"];seed_game.prototype.end=function(complete){var hs,hs_list,anon_hs,a,loading,no_hs;this.win=new_alert(doc.el("div",{},doc.el("p",{align:"center"},doc.text(complete==WON?"You beat the game!":"")),loading=doc.el("span",{},doc.el("p",{align:"center"},doc.text("Checking for a high score..."),doc.img("spinner.gif"))),hs=doc.el("span",{style:{display:"none"}},doc.el("p",{align:"center"},doc.text(complete==WON?"And you got a high score!":"You got a high score!"))),no_hs=doc.el("span",{style:{display:"none"}},doc.el("p",{align:"center"},doc.text(complete==WON?"But you did not get a high score!":"You did not get a high score!"))),anon_hs=doc.el("span",{style:{display:"none"}},doc.el("p",{align:"center"},doc.text("You are not logged in!")),doc.el("p",{align:"center"},doc.text("To get your high score listed in the high score table "+
"you need to be a registered user. Don't fear, registration is free! "),doc.el("a",{onclick:function(){Login.create_dialog();},href:"#"},doc.text("Click here to register now")),doc.text(" and your score will show up under your new name for everyong else "+
"to aspire to and admire with awe and reverence.")),doc.el("p",{align:"center"},doc.text("If you are already a registered user, go to the top of the page and "+
"log in to get the credit for this score that you so richly deserve."))),hs_list=doc.el("div",{}),this.seed!=game_of_the_day?undefined:doc.el("p",{align:"center",style:{fontSize:"x-small"}},doc.el("a",{href:"leader.html"},doc.text("Check the Leader Board")),doc.text(" for current game of the day standings.")),doc.el("p",{align:"center"},doc.el("a",{href:"mailto:?subject="+escape("I beat this game. Can you?")+"&body="+escape("<"+URL.base+"?game="+this.seed+">")},doc.text("Challenge a friend to this game"))),doc.el("p",{align:"center",style:{fontSize:"x-small"}},a=doc.el("a",{href:"close"},doc.text("Click to close")))),{title:complete==WON?"Congratulations!":"Game Over!",goaway:true,width:400});a.onclick=wrap(this,function(){document.body.removeChild(this.win);return false;});this.stats.set("complete",complete_text[complete]);this.stats.done(wrap(this,function(play_id){highscore_seed(this.name,this.seed,{include:play_id},wrap(this,function(scores,error){loading.style.display="none";for(var i in scores){if(scores[i].play_id==play_id){if(scores[i].user=="anonymous")
anon_hs.style.display="inline";else
hs.style.display="inline";render_highscores(hs_list,scores,play_id);return;}}
no_hs.style.display="inline";render_highscores(hs_list,scores);loadJSON('query/play_id/'+play_id,wrap(this,function(data,error,status){no_hs.appendChild(doc.el("p",{align:"center"},doc.text("Your score: "+data.play.score+
", your time: "+elapsed(data.play.elapsed))));}));}));}));}