// Copyright 2005-2011 David Caldwell <david@greenfelt.net>
function elapsed(seconds){var s=[];s.unshift(Math.floor(seconds%60)+"s");seconds=Math.floor(seconds/60);if(seconds)s.unshift(Math.floor(seconds%60)+"m");seconds=Math.floor(seconds/60);if(seconds)s.unshift(Math.floor(seconds%24)+"h");seconds=Math.floor(seconds/24);if(seconds)s.unshift(Math.floor(seconds%7)+"d");seconds=Math.floor(seconds/7);if(seconds)s.unshift(Math.floor(seconds)+"w");return s.slice(0,2).join('');}
function highscore_query(url,completion){loadJSON(url,function(scores,error,status){if(error)
printf("Error loading high score table: %s\n",error);completion(scores&&scores.high_score_table,error,status);});}
function highscore_leaders(seed,options,completion){highscore_query(makeurl('query','leaders',seed.join(","),options),completion);}
function highscore_seed(title,seed,options,completion){highscore_query(makeurl('query','leaders',seed,title,options),function(scores,error,status){if(scores)scores=scores[seed];if(scores)scores=scores[title];return completion(scores,error,status);});}
function highscore_day(title,days,options,completion){highscore_query(makeurl('query','high','days',days,title,options),completion);}
function highscore_wins(title,days,options,completion){highscore_query(makeurl('query','wins',days,title,options),completion);}
function short_date(date){var d=new Date();d.setTime(date+"000");return sprintf("%d/%02d/%02d %2d:%02d%s",d.getFullYear(),d.getMonth()+1,d.getDate(),d.getHours()%12||12,d.getMinutes(),d.getHours()>=12?"pm":"am");}
function render_highscores(div,scores,play_id,append){var show_name=false,show_seed=false,show_moves=false;if(!scores){print("unable to load high scores\n");return;}
for(var i=1;i<scores.length;i++){if(scores[i].seed!=scores[i-1].seed)
show_seed=true;if(scores[i].name!=scores[i-1].name)
show_name=true;if(scores[i].moves!=null)
show_moves=true;}
var table=doc(["div",{className:"highscore",style:{border:"none"}},["table",["tbody",["tr",{className:"hiscore"},!scores[0]||!scores[0].place?{}:["th",{className:"place"},"#"],["th",{className:"name"},"Name"],["th",{className:"date"},"Date"],!show_name||!scores[0].name?{}:["th",{className:"title"},"Game"],!show_seed?{}:["th",{className:"seed"},"Game #"],["th",{className:"score"},"Score"],!show_moves?{}:["th",{className:"moves"},"Moves"],["th",{className:"time"},"Time"]],map(function(score,i){return(["tr",{className:(play_id&&score.play_id==play_id?" lastscore":"")
+(Login.is_logged_in()&&score.user==Login.user?" myscore":"")
+(i>0&&scores[i-1].place!=score.place&&score.place-1!=i?" discontinuous":"")},!score.place?{}:["td",{className:"place"},i>0&&scores[i-1].place==score.place?'':score.place],["td",{className:"name"},score.user],["td",{className:"date"},short_date(score.date)],!show_name||!score.name?{}:["td",{className:"title"},score.name],!show_seed?{}:["td",{className:"seed"},["a",{href:(score.name?score.name:document.URL.split('?')[0])+"?game="+score.seed},score.seed]],["td",{className:"score",align:"center"},score.score],!show_moves?{}:["td",{className:"moves",align:"center"},score.moves||""],["td",{className:"time"},elapsed(score.elapsed)]])},scores)]]]);var parent=div.parentNode;if(append)
div.appendChild(table);else if(parent)
parent.replaceChild(table,div);return table;}
function append_highscores(div,scores,play_id){return render_highscores(div,scores,play_id,true);}
function render_wins(wins){return doc(["div",{className:"highscore",style:{border:"none"}},["table",["tbody",["tr",{className:"hiscore"},["th",{className:"place"},"#"],["th",{className:"name"},"Name"],["th",{className:"wins"},"Wins"]],map(function(win,i){return["tr",{className:(Login.is_logged_in()&&win.user==Login.user?"myscore":"")},["td",{className:"place"},i>0&&wins[i-1].place==win.place?'':win.place],["td",{className:"name"},win.user],["td",{className:"wins"},win.wins]];},wins)]]]);}
function SetTextContents(div,text){while(div.firstChild)
div.removeChild(div.firstChild);div.appendChild(document.createTextNode(text));}
function interval_viewer(div,tabs_div,high_score_loader_f){function interval(days){return{days:days}};var timeframe={"Today":interval(1),"This Week":interval(7),"This Month":interval(30),"All Time":interval("all")};for(var t in timeframe){tabs_div.appendChild(timeframe[t].td=doc(["td",["a",{href:"#",onclick:wrap(this,function(t){return wrap(this,function(){this.switch_timeframe(t);return false;})})(t)},t]]));}
this.switch_timeframe=function(new_t){for(var t in timeframe)
timeframe[t].td.className=t==new_t?"selected":undefined;this.days=timeframe[new_t].days;this.timeframe=new_t;this.refresh();}
this.refresh=function(){if(!this.args)this.args=[];if(this.args.length==0)this.args=this.arguments;SetTextContents(div,"Loading High Scores...");high_score_loader_f.apply(null,[div,this.days].concat(this.args));};this.switch_timeframe("This Week");return this;}
function highscore_interval_viewer(highscore_div,highscoretabs_div,title){this.prototype=new interval_viewer(highscore_div,highscoretabs_div,function(render_div,days,play_id){highscore_day(title,days,play_id==undefined?{}:{include:play_id},function(scores,error,status){SetTextContents(render_div,Login.is_logged_in()?"":"   (log in or create a new user at the top of the page to get a high score)");append_highscores(render_div,scores,play_id);});});}
function wins_interval_viewer(div,title){var tabs_div,high_div;div.appendChild(doc(["div",{className:"",id:"highscore"},["table",{className:"intervaltabs"},["tbody",tabs_div=doc(["tr",{id:"scoretab"}])]],high_div=doc(["div"])]));return new interval_viewer(high_div,tabs_div,function(render_div,days,play_id){highscore_wins(title,days,{},function(wins,error,status){SetTextContents(render_div,Login.is_logged_in()?"":"   (log in or create a new user at the top of the page to get a high score)");render_div.appendChild(render_wins(wins[title]));});});}
