/*
*
*	Command line interface JavaScript enabler for WordPress
*	Author: Dmitry Kurilo
*	email: dima@kurilo.su
*	Licensed under the GPL
*
*/
var currx=0,curry=0,lock=true,strcount=100,onscr=50,ctrl=false,PGM=false;var buffercmd=[],curcmd=0,linkbuf=[],common=[],service=0;var cmd="";var cmdlist="|ls|read|comment|login|logout|clear|cls|man|ver|whoami|search|about|mail|startx|link|passwd|piton|cat|less|type|more|";var jsonpath="/wp/",jsonparam="?feed=json&cmd=";function moveCursor(){$("#cursor").css("top",(curry*12)+"px");$("#cursor").css("left",(currx*8)+"px");}
function showInstruction(){lock=true;$.getJSON(jsonpath+jsonparam+"page&page_id=8&tkn="+random(),function(data){if(data[0]!="empty"){for(var i=0;i<data.length;i++){writeln(data[i].text);}}
writeln('');$.getJSON(jsonpath+jsonparam+"whoami&tkn="+random(),function(data){if(data.login!=""){writeln("Здравствуйте, "+data.name+"\n");}
showHello();lock=false;});});}
function showHello(){currx=0;write(">");scrollDown();moveCursor();}
function regexfromstr(s){return eval("/"+s.replace(/\\/ig,"\\\\").replace(/\./ig,"\\.").replace(/\?/ig,"\\?").replace(/\*/ig,"\\*").replace(/\+/ig,"\\+").replace(/\//ig,"\\/").replace(/\|/ig,"\\|").replace(/\]/ig,"\\]").replace(/\[/ig,"\\[").replace(/\)/ig,"\\)").replace(/\(/ig,"\\(").replace(/\{/ig,"\\{").replace(/\}/ig,"\\}").replace(/\-/ig,"\\-")+"/");}
function regexfromstr_forlink(s){return eval("/"+s.replace(/\\/ig,"\\\\").replace(/\./ig,"\\.").replace(/\?/ig,"\\?").replace(/\*/ig,"\\*").replace(/\+/ig,"\\+").replace(/\//ig,"\\/").replace(/\|/ig,"\\|").replace(/\]/ig,"\\]").replace(/\[/ig,"\\[").replace(/\)/ig,"\\)").replace(/\(/ig,"\\(").replace(/\{/ig,"\\{").replace(/\}/ig,"\\}").replace(/\-/ig,"\\-")+"[^<>]+(?!<\\!\\-\\-l\d+\\-\\->)(?=<\\/a>)/ig");}
function write(s){var z="";s=s.replace(/<br *\/?>/gi,"\n");s=s.replace(/\r/gi,"");s=s.replace(/ +/gi," ");var imgarr=s.match(/<img [^>]*src="[^"]*"[^>]*>/ig);var aarr=s.match(/<a [^>]*href="[^"]*"[^>]*>/ig);var alltag=s.match(/<[a-zA-Z\/][^>]*>/gi);for(var i=0;alltag!=null&&i<alltag.length;i++){if(alltag[i].search(/<\/?a[ |>]/ig)<0&&alltag[i].search(/<\/?img[ |>]/ig)<0){s=s.replace(regexfromstr(alltag[i]),"");}}
s=s.replace(/>/gi,"&gt;");s=s.replace(/</gi,"&lt;");s=s.replace(/&lt;\/a&gt;/ig,"</a>");for(var i=0;aarr!=null&&i<aarr.length;i++){var link=aarr[i].match(/href="[^"]*"/ig)[0];linkbuf.push(link.replace(/href="|"/ig,""));s=s.replace(regexfromstr(aarr[i].replace(/>/gi,"&gt;").replace(/</gi,"&lt;")),'<a '+link+' target="_blank">');var temp=regexfromstr_forlink('<a '+link+' target="_blank">');s=s.replace(temp,s.match(temp)[0]+" ["+linkbuf.length+"]<!--l"+linkbuf.length+"-->");}
for(var i=0;imgarr!=null&&i<imgarr.length;i++){var link=imgarr[i].match(/src="[^"]*"/ig)[0].replace(/src="|"/ig,"");linkbuf.push(link);s=s.replace(regexfromstr(imgarr[i].replace(/>/gi,"&gt;").replace(/</gi,"&lt;")),'<a href="'+link+'" target="_blank">'+imgarr[i].match(/alt="[^"]*"/ig)[0].replace(/alt="|"/ig,"")+' ['+linkbuf.length+']<!--l'+linkbuf.length+'--></a>');}
var strarr=s.split("\n");var atag=false;for(var i=0;i<strarr.length;i++){var word="",atag=false;strarr[i]=strarr[i].split('');for(var k=0;k<strarr[i].length;k++){if(strarr[i][k]=="<")atag=true;if(strarr[i][k]!=" "||atag)word+=strarr[i][k];if(strarr[i][k]==">")atag=false;if(strarr[i][k]==" "&&!atag){var wlen=word.replace(/<[a-zA-Z\/][^>]*>/gi,"").replace(/&[^;]*;/gi," ").length;if((currx+wlen)>=strcount+1){z+="<br/>";curry++;currx=0;}
z+=(word+" ");currx+=(wlen+1);word="";}}
if(word!=""){var wlen=word.replace(/<[a-zA-Z\/][^>]*>/gi,"").replace(/&[^;]*;/gi," ").length;if((currx+wlen)>=strcount+1){z+="<br/>";curry++;currx=0;}
z+=word;currx+=wlen;word="";}
if((i+1)<strarr.length){z+="<br/>";curry++;currx=0;}}
$("#text").html($("#text").html()+z);scrollDown();moveCursor();}
function writeln(s){write(s);$("#text").html($("#text").html()+"<br/>");curry++;currx=0;scrollDown();moveCursor();}
function run(cmd){if(cmd.length>0){if((buffercmd.length>0&&buffercmd[buffercmd.length-1]!=cmd)||buffercmd.length==0)buffercmd.push(cmd);curcmd=buffercmd.length;var cmdarr=cmd.split(' ');if(cmdlist.search(regexfromstr("|"+cmdarr[0]+"|"))>=0){eval(cmdarr.shift()+"(cmdarr)");}else{writeln("Неверная команда!");showHello();}
scrollDown();}else{showHello();}
return true;}
function scrollDown(){$('#text').scrollTop($('#text')[0].scrollHeight);return true;}
function clearCurrentCmd(){while(cmd.length>0){if(currx>0){$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}$/ig,''));currx--;}else{$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}<br\/?>$/ig,''));currx=strcount-1;curry--;}
cmd=cmd.replace(/.$/ig,'');moveCursor();scrollDown();}}
$(document).keydown(function(e){switch(e.which){case 8:if(cmd!=''&&!lock){if(currx>0){$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}$/ig,''));currx--;}else{$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}<br\/?>$/ig,''));currx=strcount-1;curry--;}
cmd=cmd.replace(/.$/ig,'');}
moveCursor();return false;break;case 38:if(curcmd>0&&!lock){curcmd--;clearCurrentCmd();cmd=buffercmd[curcmd];write(cmd);}
break;case 40:if(curcmd<(buffercmd.length-1)&&!lock){curcmd++;clearCurrentCmd();cmd=buffercmd[curcmd];write(cmd);}
break;case 33:$('#text').scrollTop($('#text').scrollTop()-600);break;case 34:$('#text').scrollTop($('#text').scrollTop()+600);break;}});$(document).keyup(function(e){if(e.which==17)ctrl=false;});$(document).keypress(function(e){if(!lock&&!ctrl){switch(e.which){case 8:break;case 13:$("#text").html($("#text").html()+"<br/>");currx=0;curry++;e.which=0;run(cmd);cmd='';break;default:var pat=/(\w|\W){1}/ig;var c=String.fromCharCode(e.which);if(e.which>31&&c.search(pat)>=0){cmd+=c;if(c==" ")c='&nbsp;';if(c=="<")c='&lt;';if(c==">")c='&gt;';$("#text").html($("#text").html()+c);scrollDown();currx++;if(currx>=strcount){$("#text").html($("#text").html()+"<br/>");currx=0;curry++;scrollDown();}}
break;}
moveCursor();}
return false;});function random(){return Math.ceil(Math.random()*10000000000000000);}
$(document).ready(function(){showInstruction();});function ver(){lock=true;writeln("Version: 1.0");showHello();lock=false;}
function startx(){lock=true;document.location.href="/wp/";lock=false;}
function link(){lock=true;var cmdarr=cmd.split(' ');if(cmdarr.length>1){var l=parseInt(cmdarr[1]);if(l>0&&l<=linkbuf.length){var w=window.open();w.document.location.href=linkbuf[l-1];}}
showHello();lock=false;}
function ls(){lock=true;$.getJSON(jsonpath+jsonparam+"ls&tkn="+random(),function(data){writeln("Номер&nbsp;&nbsp;Дата&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Комментариев&nbsp;Заголовок");writeln("------ ---------------- ------------ -------------------------");for(var i=0;i<data.length;i++){writeln(("       "+data[i].id).substr(data[i].id.length+1,6).replace(/ /ig,"&nbsp;")+"&nbsp;"+data[i].date+"&nbsp;"+data[i].time+"&nbsp;"+("             "+data[i].comments).substr(data[i].comments.length+1,12).replace(/ /ig,"&nbsp;")+"&nbsp;"+data[i].title);}
showHello();lock=false;});}
function search(){lock=true;var cmdarr=cmd.split(" ");cmdarr.shift();if(cmdarr.length>0){var s=cmdarr[0];$.getJSON(jsonpath+jsonparam+"ls&s="+s+"&tkn="+random(),function(data){writeln("Номер&nbsp;&nbsp;Дата&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Комментариев&nbsp;Заголовок");writeln("------ ---------------- ------------ -------------------------");for(var i=0;i<data.length;i++){writeln(("       "+data[i].id).substr(data[i].id.length+1,6).replace(/ /ig,"&nbsp;")+"&nbsp;"+data[i].date+"&nbsp;"+data[i].time+"&nbsp;"+("             "+data[i].comments).substr(data[i].comments.length+1,12).replace(/ /ig,"&nbsp;")+"&nbsp;"+data[i].title);}
showHello();lock=false;});}else{showHello();lock=false;}}
function read(){lock=true;var cmdarr=cmd.split(" ");cmdarr.shift();if(cmdarr.length>0){var pid=parseInt(cmdarr[0]);if(pid>0){$.getJSON(jsonpath+jsonparam+"post&p="+pid+"&tkn="+random(),function(data){if(data.length>0&&data[0]!="empty"){for(var i=0;i<data.length;i++){if(data[i].id==pid){writeln("Сообщение: "+data[i].title+" от "+data[i].date+" "+data[i].time)
writeln(data[i].text);writeln("");for(var k=0;k<data[i].comments.length;k++){writeln("Коментарий от: "+data[i].comments[k].author+" написан: "+data[i].comments[k].date);writeln(data[i].comments[k].content);if(k<(data[i].comments.length-1))writeln("");}}}}
showHello();lock=false;});}else{showHello();lock=false;}}else{showHello();lock=false;}}
function cat(){read();}
function less(){read();}
function type(){read();}
function more(){read();}
function comment(){lock=true;var loggedin=false;var name='',email='';var cmdarr=cmd.split(" ");$.getJSON(jsonpath+jsonparam+"whoami&tkn="+random(),function(data){if(data.login!="")loggedin=true;var newstring=false;var pid=0;var comment="";cmdarr.shift();if(cmdarr.length>0){pid=parseInt(cmdarr[0]);cmdarr.shift();for(var i=0;i<cmdarr.length;i++){switch(cmdarr[i]){case'--name':case'-n':if(cmdarr.length>(i+1)&&cmdarr[i+1].search(/--/ig)!=0){name=cmdarr[i+1];i++;}
break;case'--email':case'-e':if(cmdarr.length>(i+1)&&cmdarr[i+1].search(/--/ig)!=0){mail=cmdarr[i+1];i++;}
break;}}
if(name==''&&!loggedin){write("Как вас зовут: ");$(document).bind("keypress",getname);$(document).bind("keydown",getname_service);}else if(email==''&&!loggedin){write("Ваш e-mail: ");$(document).bind("keypress",getemail);$(document).bind("keydown",getemail_service);}else if((name!=''&&email!='')||loggedin){writeln("Введите текст.\nДля окончания ввода и отправки комментария нажмите Ввод .(точка) Ввод.");$(document).bind("keypress",getcomment);$(document).bind("keydown",getcomment_service);}}else{showHello();lock=false;}
function getname(e){switch(e.which){case 8:break;case 13:e.which=0;$("#text").html($("#text").html()+"<br/>");currx=0;curry++;$(document).unbind("keypress",getname);$(document).unbind("keydown",getname_service);if(email!=''){writeln("Введите текст.\nДля окончания ввода и отправки комментария нажмите Ввод .(точка) Ввод.");$(document).bind("keypress",getcomment);$(document).bind("keydown",getcomment_service);}
else{write("Ваш e-mail: ");$(document).bind("keypress",getemail);$(document).bind("keydown",getemail_service);}
break;default:var pat=/(\w|\W){1}/i;var c=String.fromCharCode(e.which);if(e.which>31&&c.search(pat)>=0){name+=c;if(c==" ")c='&nbsp;';if(c=="<")c='&lt;';if(c==">")c='&gt;';$("#text").html($("#text").html()+c);scrollDown();currx++;if(currx>=strcount){$("#text").html($("#text").html()+"<br/>");currx=0;curry++;scrollDown();}}
break;}
moveCursor();return false;}
function getname_service(e){switch(e.which){case 8:if(name.length>0){name=name.substr(0,name.length-1);if(currx>0){$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}$/ig,''));currx--;}else{$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}<br\/?>$/ig,''));currx=strcount-1;curry--;}}
moveCursor();return false;break;}}
function getemail(e){switch(e.which){case 8:break;case 13:e.which=0;$("#text").html($("#text").html()+"<br/>");currx=0;curry++;$(document).unbind("keypress",getemail);$(document).unbind("keydown",getemail_service);writeln("Введите текст.\nДля окончания ввода и отправки комментария нажмите Ввод .(точка) Ввод.");$(document).bind("keypress",getcomment);$(document).bind("keydown",getcomment_service);break;default:var pat=/(\w|\W){1}/i;var c=String.fromCharCode(e.which);if(e.which>31&&c.search(pat)>=0){email+=c;if(c==" ")c='&nbsp;';if(c=="<")c='&lt;';if(c==">")c='&gt;';$("#text").html($("#text").html()+c);scrollDown();currx++;if(currx>=strcount){$("#text").html($("#text").html()+"<br/>");currx=0;curry++;scrollDown();}}
break;}
moveCursor();return false;}
function getemail_service(e){switch(e.which){case 8:if(email.length>0){email=email.substr(0,email.length-1);if(currx>0){$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}$/ig,''));currx--;}else{$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}<br\/?>$/ig,''));currx=strcount-1;curry--;}}
moveCursor();return false;break;}}
function getcomment(e){switch(e.which){case 13:$("#text").html($("#text").html()+"<br/>&nbsp;");newstring=true;comment+="\n";currx=0;curry++;if(comment.search(/\n\.\n/ig)>=0){comment=comment.replace(/\n\.\n/ig,'');$(document).unbind("keypress",getcomment);$(document).unbind("keydown",getcomment_service);$("#text").html($("#text").html().substr(0,$("#text").html().length-6));send_comment();}
scrollDown();break;default:var pat=/(\w|\W){1}/i;var c=String.fromCharCode(e.which);if(e.which>31&&c.search(pat)>=0){comment+=c;if(c==" ")c='&nbsp;';if(c=="<")c='&lt;';if(c==">")c='&gt;';if(newstring){if($("#text").html().substr($("#text").html().length-6,6)=="&nbsp;")$("#text").html($("#text").html().substr(0,$("#text").html().length-6));else if($("#text").html().substr($("#text").html().length-1,1)==String.fromCharCode(160))$("#text").html($("#text").html().substr(0,$("#text").html().length-1));newstring=false;}
$("#text").html($("#text").html()+c);scrollDown();currx++;if(currx>=strcount){$("#text").html($("#text").html()+"<br/>");currx=0;curry++;scrollDown();}}
break;}
moveCursor();return false;}
function getcomment_service(e){switch(e.which){case 8:if(comment.length>0){var del=comment.substr(comment.length-1,1);comment=comment.substr(0,comment.length-1);if(currx>0){$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}$/ig,''));currx--;}else{if(del=="\n")$("#text").html($("#text").html().replace(/<br\/?>$/ig,''));else $("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}<br\/?>$/ig,''));if(comment!=''){var commentarr=comment.split("\n");currx=commentarr[commentarr.length-1].length;curry--;}}}
moveCursor();return false;break;}}
function send_comment(){$.post(jsonpath,{feed:"json",cmd:"comment",comment_post_ID:pid,comment:comment,name:name,email:email},function(data){if(data=="success")writeln("Done!");else writeln("Something wrong. "+data);showHello();lock=false;},"json");}});return;}
function login(){lock=true;var cmdarr=cmd.split(' ');var user='',password='';cmdarr.shift();for(var i=0;i<cmdarr.length;i++){switch(cmdarr[i]){case'--user':case'-u':if(cmdarr.length>(i+1)&&cmdarr[i+1].search(/--/ig)!=0){user=cmdarr[i+1];i++;}
break;case'--password':case'-p':if(cmdarr.length>(i+1)&&cmdarr[i+1].search(/--/ig)!=0){password=cmdarr[i+1];i++;}
break;}}
if(user==''){write("login: ");$(document).bind("keypress",getname);$(document).bind("keydown",getname_service);}else if(password==''){write("password: ");$(document).bind("keypress",getpassword);$(document).bind("keydown",getpassword_service);}else if(user!=''&&password!='')get_login();return true;function getname(e){switch(e.which){case 8:break;case 13:e.which=0;$("#text").html($("#text").html()+"<br/>");currx=0;curry++;$(document).unbind("keypress",getname);$(document).unbind("keydown",getname_service);if(password!='')get_login();else{write("password: ");$(document).bind("keypress",getpassword);$(document).bind("keydown",getpassword_service);}
break;default:var pat=/(\w|\W){1}/i;var c=String.fromCharCode(e.which);if(e.which>31&&c.search(pat)>=0){user+=c;if(c==" ")c='&nbsp;';if(c=="<")c='&lt;';if(c==">")c='&gt;';$("#text").html($("#text").html()+c);scrollDown();currx++;if(currx>=strcount){$("#text").html($("#text").html()+"<br/>");currx=0;curry++;scrollDown();}}
break;}
moveCursor();return false;}
function getname_service(e){switch(e.which){case 8:if(user.length>0){user=user.substr(0,user.length-1);if(currx>0){$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}$/ig,''));currx--;}else{$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}<br\/?>$/ig,''));currx=strcount-1;curry--;}}
moveCursor();return false;break;}}
function getpassword(e){switch(e.which){case 8:break;case 13:$("#text").html($("#text").html()+"<br/>");currx=0;curry++;$(document).unbind("keypress",getpassword);$(document).unbind("keydown",getpassword_service);get_login();break;default:var pat=/(\w|\W){1}/ig;var c=String.fromCharCode(e.which);if(e.which>31&&c.search(pat)>=0){password+=c;}
break;}
moveCursor();return false;}
function getpassword_service(e){switch(e.which){case 8:if(password.length>0){password=password.substr(0,password.length-1);}
return false;break;}}
function get_login(){$.post(jsonpath,{feed:"json",cmd:"login",log:user,pwd:password},function(data){if(data.error=="")writeln("Hello "+data.name+"!");else writeln("Something wrong.");showHello();lock=false;},"json");moveCursor();scrollDown();}}
function mail(){lock=true;var cmdarr=cmd.split(' ');var mail="";var newstring=false;writeln("Введите текст.\nДля окончания ввода и отправки письма нажмите Ввод .(точка) Ввод.");$(document).bind("keypress",getmail);$(document).bind("keydown",getmail_service);return;function getmail(e){switch(e.which){case 13:$("#text").html($("#text").html()+"<br/>&nbsp;");newstring=true;mail+="\n";currx=0;curry++;if(mail.search(/\n\.\n/ig)>=0){mail=mail.replace(/\n\.\n/ig,'');$(document).unbind("keypress",getmail);$(document).unbind("keydown",getmail_service);$("#text").html($("#text").html().substr(0,$("#text").html().length-6));send_mail();}
scrollDown();break;default:var pat=/(\w|\W){1}/i;var c=String.fromCharCode(e.which);if(e.which>31&&c.search(pat)>=0){mail+=c;if(c==" ")c='&nbsp;';if(c=="<")c='&lt;';if(c==">")c='&gt;';if(newstring){if($("#text").html().substr($("#text").html().length-6,6)=="&nbsp;")$("#text").html($("#text").html().substr(0,$("#text").html().length-6));else if($("#text").html().substr($("#text").html().length-1,1)==String.fromCharCode(160))$("#text").html($("#text").html().substr(0,$("#text").html().length-1));newstring=false;}
$("#text").html($("#text").html()+c);scrollDown();currx++;if(currx>=strcount){$("#text").html($("#text").html()+"<br/>");currx=0;curry++;scrollDown();}}
break;}
moveCursor();return false;}
function getmail_service(e){switch(e.which){case 8:if(mail.length>0){var del=mail.substr(mail.length-1,1);mail=mail.substr(0,mail.length-1);if(currx>0){$("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}$/ig,''));currx--;}else{if(del=="\n")$("#text").html($("#text").html().replace(/<br\/?>$/ig,''));else $("#text").html($("#text").html().replace(/(\&lt;|&gt;|&nbsp;|.){1}<br\/?>$/ig,''));if(mail!=''){var mailarr=mail.split("\n");currx=mailarr[mailarr.length-1].length;curry--;}}}
moveCursor();return false;break;}}
function send_mail(){$.post(jsonpath,{feed:"json",cmd:"mail",message:mail},function(data){if(data=="success")writeln("Done!");else writeln("Something wrong.");showHello();lock=false;},"json");}}
function logout(){lock=true;$.getJSON(jsonpath+jsonparam+"logout&tkn="+random(),function(data){if(data=="success")writeln("Bye!");else writeln("Something wrong.");showHello();lock=false;});}
function cls(){lock=true;$("#text").html('<div id="cursor" class="cursor"></div>');currx=0;curry=0;showHello();lock=false;}
function clear(){cls();}
function man(){lock=true;$.getJSON(jsonpath+jsonparam+"page&page_id=5&tkn="+random(),function(data){if(data[0]!="empty"){for(var i=0;i<data.length;i++){writeln(data[i].title);writeln(data[i].text);}}
showHello();lock=false;});}
function about(){lock=true;$.getJSON(jsonpath+jsonparam+"page&page_id=2&tkn="+random(),function(data){if(data[0]!="empty"){for(var i=0;i<data.length;i++){writeln(data[i].title);writeln(data[i].text);}}
showHello();lock=false;});}
function whoami(){lock=true;$.getJSON(jsonpath+jsonparam+"whoami&tkn="+random(),function(data){if(data.login!="")writeln("Ваше имя: "+data.name+"\nВаш логин: "+data.login+"\nВаш e-mail: "+data.email);else writeln("Вы не залогинились.");showHello();lock=false;});}
function passwd(){lock=true;var cmdarr=cmd.split(' ');var password='';cmdarr.shift();if(cmdarr.length>1&&(cmdarr[0]=='--password'||cmdarr[0]=='-p')){password=cmdarr[1];set_passwd();}else{write("password: ");$(document).bind("keypress",getpassword);$(document).bind("keydown",getpassword_service);}
return true;function getpassword(e){switch(e.which){case 8:break;case 13:$("#text").html($("#text").html()+"<br/>");currx=0;curry++;$(document).unbind("keypress",getpassword);$(document).unbind("keydown",getpassword_service);set_passwd();break;default:var pat=/(\w|\W){1}/ig;var c=String.fromCharCode(e.which);if(e.which>31&&c.search(pat)>=0){password+=c;}
break;}
moveCursor();return false;}
function getpassword_service(e){switch(e.which){case 8:if(password.length>0){password=password.substr(0,password.length-1);}
return false;break;}}
function set_passwd(){$.post(jsonpath,{feed:"json",cmd:"passwd",pass:password},function(data){if(data.result!="success")writeln("Something wrong. "+data.result);showHello();lock=false;},"json");moveCursor();scrollDown();}}
function piton(){lock=true;writeln('');writeln('');common[0]=[];common[1]=0;common[2]=$("#text").html();$(document).bind("keydown",piton_control_service);$(document).bind("keypress",piton_control);var c='';for(var i=0;i<onscr;i++){common[0][i]=[];for(var k=0;k<strcount;k++){if(i==0||i==(onscr-1))c='#';else if(k==0||k==(strcount-1))c='#';else c='&nbsp;';common[0][i][k]=c;}}
common[0][20][20]='0';common[3]=[20,20];common[4]=[20,20];common[5]=1;common[6]=[0,0];curry+=(onscr-1);piton_writebuf();PGM=true;setTimeout('piton_process();',100);}
function piton_process(){switch(service){case 5:end();break;case 1:getold();common[0][common[3][1]][common[3][0]]='1';common[3][0]--;piton_writebuf();setTimeout('piton_process();',100);break;case 2:getold();common[0][common[3][1]][common[3][0]]='2';common[3][0]++;piton_writebuf();setTimeout('piton_process();',100);break;case 3:getold();common[0][common[3][1]][common[3][0]]='3';common[3][1]--;piton_writebuf();setTimeout('piton_process();',100);break;case 4:getold();common[0][common[3][1]][common[3][0]]='4';common[3][1]++;piton_writebuf();setTimeout('piton_process();',100);break;default:setTimeout('piton_process();',100);break;}
if(piton_random()>0.95){var x=Math.ceil(piton_random()*(strcount-2)+1),y=Math.ceil(piton_random()*(onscr-2)+1);if(common[0][y][x]=='&nbsp;')common[0][y][x]='*';}
return;function getold(){if(common[0][common[3][1]][common[3][0]]=='&nbsp;'||common[0][common[3][1]][common[3][0]]=='0'){var x=common[4][0],y=common[4][1];if(common[0][common[4][1]][common[4][0]]=='0')common[0][common[4][1]][common[4][0]]='';switch(common[0][common[4][1]][common[4][0]]){case'1':common[4][0]--;break;case'2':common[4][0]++;break;case'3':common[4][1]--;break;case'4':common[4][1]++;break;}
common[0][y][x]='&nbsp;';}else if(common[0][common[3][1]][common[3][0]]=='*')common[5]++;else{end();}}
function piton_random(){return Math.random();}
function end(){service=0;currx=0;curry++;moveCursor();PGM=false;var message="Это конец! Ваша длина: "+Math.floor(common[5]/5)+" попугаев";switch(common[5]%5){case 1:message+=" и одно попугайское крылышко.";break;case 2:message+=" и два попугайских крылышка.";break;case 3:message+=" и три попугайских крылышка.";break;case 4:message+=" и четыре попугайских крылышка.";break;}
writeln(message);message="";switch(Math.floor(common[5]/20)){case 0:message="Вы настоящий дождевой червь!";break;case 1:message="Вы уж!";break;case 2:message="Вы кобра! Это уже опасно!";break;case 3:message="Вы питон! Небольшой питон!";break;case 4:message="Вы питонище! Большой и прожорливый!";break;case 5:message="Вы удав! Вон сколько наели!";break;case 6:message="Анаконда!!! Уже боюсь!";break;default:message="Шаи-Хулуд! Би-ла кайфа!";break;}
writeln(message);common=[];scrollDown();moveCursor();showHello();lock=false;}}
function piton_control(e){switch(e.which){case 120:service=5;$(document).unbind("keypress",piton_control);$(document).unbind("keydown",piton_control_service);break;default:break;}
return false;}
function piton_control_service(e){switch(e.which){case 37:service=1;return false;break;case 39:service=2;return false;break;case 38:service=3;return false;break;case 40:service=4;return false;break;}}
function piton_writebuf(){if(lock){var buf="";for(var i=0;i<onscr;i++){for(var k=0;k<strcount;k++){if(common[0][i][k]==0||common[0][i][k]==1||common[0][i][k]==2||common[0][i][k]==3||common[0][i][k]==4)buf+="O";else buf+=common[0][i][k];if(k==(strcount-1))buf+='\n';}}
$("#text").html(common[2]);write(buf);curry-=onscr;moveCursor();}}