// Copyright (c) 2005 David Caldwell,  All Rights Reserved.
function getElementsByClassName(name,node)
{if(!node)node=document;if(document.getElementsByClassName)
return node.getElementsByClassName(name);return filter(function(n){return n.className&&n.className.match("\\b"+name+"( |$)")},node.getElementsByTagName('*'));}
function menu_config(){var menus=[];var top=getElementsByClassName("menu",document.getElementById("menubar"));var quit_menus;var mouse_held;for(var m=0;m<top.length;m++){var sub=getElementsByClassName("menubody",top[m]);if(sub.length!=1)
printf("Can't have more than one menubody in a menu!!\n");var menu={"top":top[m],"sub":sub[0],list:[]};if(menu.top!=undefined&&menu.sub!=undefined){menu.top.style.cursor="default";menu.list=filter(function(e){return e},map(function(e){return e.parentNode&&e.parentNode.nodeName=='LI'?{a:e,li:e.parentNode}:null;},arrayize(menu.sub.getElementsByTagName('A')).concat(arrayize(menu.sub.getElementsByTagName('INPUT')))));for(var l in menu.list){var menuitem=menu.list[l];menuitem.li.style.cursor="pointer";if(plat.ie<=6||plat.webkit){menuitem.li.onmouseout=function(){if(!plat.webkit||mouse_held)this.className="";}
menuitem.li.onmouseover=function(){if(!plat.webkit||mouse_held)this.className="highlight";}}
menuitem.li.onmouseup=(function(li,top){return function(event){var e=new plat.event(event);var count=0;var stay_open=top.className.match(/stay_open/);var f=function(){li.className=count&1?"highlight":"nohighlight";if(!stay_open&&count++<3)
timeout(75,f);else{li.className="";if(mouse_held||e.target!=li.firstChild)
doc.click(li.firstChild);if(!stay_open)
quit_menus();else
mouse_held=false;}};f();return true;};})(menuitem.li,menu.top);}
menus.push(menu);}}
var down_time,old_mousemove,old_mouseup,old_mousedown;quit_menus=function(){document.onmouseup=old_mouseup||function(){};document.onmousemove=old_mousemove||function(){};document.onmousedown=old_mousedown||function(){};mouse_held=old_mousedown=old_mousemove=old_mouseup=undefined;for(var m in menus){menus[m].top.onmouseover=function(){return true};menus[m].sub.style.display="none";}}
var drop_down=function(menu){return function(event){var e=new plat.event(event);for(var n=e.target;n!=menu.top;n=n.parentNode)
if(n.className=="menubody")
return;if(menu.sub==undefined)return;down_time=(new Date()).getTime();if(e.type=="mousedown"&&menu.sub.style.display=="block"){quit_menus();return false;}
menu.sub.style.display="block";menu.sub.style.position="absolute";var pc=PageCoords(menu.top,document.body);menu.sub.style.top=px(pc.y+(menu.top.height||menu.top.offsetHeight));menu.sub.style.left=px(pc.x);for(var m in menus){menus[m].top.onmouseover=drop_down(menus[m]);if(menus[m]!=menu)
menus[m].sub.style.display="none";}
if(!old_mousemove)old_mousemove=document.onmousemove;if(!old_mouseup)old_mouseup=document.onmouseup;if(!old_mousedown)old_mousedown=document.onmousedown;document.onmousemove=function(event){return false;}
document.onmousedown=document.onmouseup=function(event){var e=new plat.event(event);var now=(new Date()).getTime();if(e.type=="mouseup"&&now-down_time<500){mouse_held=false;return false;}
for(var n=e.target;n!=document;n=n.parentNode)
if(e.type=="mousedown"&&n==menu.top||e.type=="mouseup"&&n==menu.sub)
return;quit_menus();return true;}
if(mouse_held==undefined)
mouse_held=true;return false;}};for(var m in menus)
menus[m].top.onmousedown=drop_down(menus[m]);}
on_load(menu_config);// Copyright (C) 2005-2011 David Caldwell, All Rights Reserved.
function login()
{this.div=document.getElementById("login");this.state="idle";if(this.is_logged_in()){this.user=Cookie.list["greenfelt_user"];}
this.update_div();}
login.prototype.is_logged_in=function(){return Cookie.list["greenfelt_user"];}
login.prototype.set_error=function(message){this.error=message;this.logout();};login.prototype.logout=function(){loadJSON(makeurl("user","logout"),wrap(this,function(){Cookie.reload();this.user=undefined;this.update_div();}));};login.prototype.on_login_f=function(){};login.prototype.on_login=function(f){if(f){var old_f=this.on_login_f;this.on_login_f=function(){old_f();f();};}else
this.on_login_f();}
login.prototype.authenticate=function(user,pass,create,after){if(this.state=="authenticating")return;var old_state=this.state;this.state="authenticating";this.update_div();loadJSON(makeurl("user",create?"create":"login",user,{pass:rot13(pass)}),wrap(this,function(result,error,status){var m;if(result==undefined){this.error=sprintf("%j",error);}else{Cookie.reload();this.user=Cookie.list.greenfelt_user;this.error=undefined;this.on_login();if(piwikTracker&&create)
piwikTracker.trackGoal(3);}
this.state=old_state;this.update_div();if(after)after();}));}
function clear_div(div){while(div.firstChild)
div.removeChild(div.firstChild);}
function gen_return_wrapper(input,extra){plat.register_key_events(input,function(event){if(event.key=="return"){if(extra)extra();if(input.onchange)input.onchange();return false;}});}
login.prototype.update_div=function(){var me=this;clear_div(this.div);if(this.state=="creating"){}else if(this.state=="authenticating"){this.div.appendChild(doc.text("Authenticating... Please wait..."));}else if(this.is_logged_in()){doc.appendTo(this.div,doc.el("input",{type:"button",id:"logout",value:("Logout \""+this.user+"\""),onclick:function(){me.logout();}}));}else{if(this.error){var span;this.div.appendChild(span=document.createElement("span").appendChild(document.createTextNode("* "+this.error+" * ")).parentNode);span.className="login-error";}
var u,p,f;doc.appendTo(this.div,doc.el("input",{type:"button",value:"New User",id:"new_user",onclick:function(){me.create_dialog();}}),u=doc.el("input",{type:"text",size:"10",id:"user",placeholder:"Username"}),p=doc.el("input",{type:"password",size:"10",id:"pass",placeholder:"Password",onchange:function(){if(!u.className&&u.value!=""&&p.value!="")me.authenticate(u.value,p.value);}}));if(plat.has_placeholder)
f=p;else{merge_into(u,{className:"disabled",value:"Username",onfocus:function(){if(u.className)u.className=u.value="";},onblur:function(){if(u.value==""){u.className="disabled";u.value="Username";}}});merge_into(p,{onfocus:function(){p.className=p.style.display="";f.style.display="none";},onblur:function(){if(p.value==""){p.style.display="none";f.style.display="";}}});p.style.display="none";doc.appendTo(this.div,f=doc.el("input",{type:"text",size:"10",id:"fake_pass",className:"disabled",value:"Password",onfocus:function(){p.style.display="";p.focus();}}));}
gen_return_wrapper(u,function(){f.focus();});gen_return_wrapper(p);}}
login.prototype.login_dialog=function(){var e,u,p,l,c,s;var a=new_alert(doc(["div",e=doc(["span",{className:"login-error"}]),["table",["tbody",["tr",["td","Username:"],["td",u=doc(["input",{type:"text",size:"10"}])]],["tr",["td","Password:"],["td",p=doc(["input",{type:"password",size:"10"}])]]]],["p",l=doc(["input",{type:"button",value:"Login"}]),c=doc(["input",{type:"button",value:"Cancel"}]),s=doc(["img",{src:doc.id("spinner").src,style:{display:"none"}}])]]),{title:"Login",cancel:wrap(this,function(){this.update_div();})});this.div.style.display="none";var got_error=wrap(this,function(){clear_div(e);e.appendChild(doc("* "+this.error));s.style.display="none";l.disabled=false;c.disabled=false;});l.onclick=wrap(this,function(){s.style.display="inline";l.disabled=true;c.disabled=true;this.authenticate(u.value,p.value,false,wrap(this,function(){if(this.error)
got_error();else
c.onclick();}));});c.onclick=wrap(this,function(){close_alert(a);this.div.style.display="inline";this.update_div();});gen_return_wrapper(u,function(){p.focus();});gen_return_wrapper(p,function(){l.onclick();});u.focus();}
login.prototype.create_dialog=function(){if(this.state=="creating")return;this.state="creating";this.update_div();var alert=new_alert(doc.el("div",{},doc.el("p",{},doc.text("Thanks for joining Green Felt. Please enter a login name and a password. "+
"The login name will be what you use to log in to green felt and what "+
"you will be known as to other users (in high score tables, etc.)."+
"")),doc.el("ul",{id:"error",style:{color:"orange"}}),doc.el("table",{},doc.el("tr",{},doc.el("td",{border:"none"},doc.text("Login Name:")),doc.el("td",{},doc.el("input",{type:"text",size:"20",id:"new_user"}))),doc.el("tr",{},doc.el("td",{},doc.el("label",{'for':"new_password"},doc.text("Password:"))),doc.el("td",{},doc.el("input",{type:"password",size:"10",id:"new_password"}))),doc.el("tr",{},doc.el("td",{},doc.el("label",{'for':"new_password2"},doc.text("Verify Password:"))),doc.el("td",{},doc.el("input",{type:"password",size:"10",id:"new_password2"})))),doc.el("input",{type:"button",value:"Create New User",id:"ok"}),doc.el("input",{type:"button",value:"Cancel",id:"cancel"})),{title:"Create a new user",cancel:wrap(this,function(){this.cancel(alert);})});var me=this;doc.id("ok").onclick=function(){var error=doc.id("error");while(error.firstChild)
error.removeChild(error.firstChild);if(!doc.id("new_user").value)error.appendChild(doc.el("li",{},doc.text("Missing a Login Name!")));if(!doc.id("new_password").value)error.appendChild(doc.el("li",{},doc.text("Missing a Password!")));if(doc.id("new_password").value!=doc.id("new_password2").value)
error.appendChild(doc.el("li",{},doc.text("Passwords don't match!")));if(!error.firstChild){error.appendChild(doc.el("li",{},doc.text("Creating user. Please wait...")));doc.id("ok").disabled=true;doc.id("cancel").disabled=true;me.authenticate(doc.id("new_user").value,doc.id("new_password").value,true,function(){while(error.firstChild)
error.removeChild(error.firstChild);if(me.error)
error.appendChild(doc.el("li",{},doc.text(me.error)));else
me.cancel(alert);doc.id("ok").disabled=false;doc.id("cancel").disabled=false;});}
return false;};doc.id("cancel").onclick=function(){me.cancel(alert);return false;};}
login.prototype.cancel=function(alert){close_alert(alert);this.state="idle";this.update_div();}
var Login=new login;
