/*

 |       __ _____                     ________                              __
 |      / // _  /__ __ _____ ___ __ _/__  ___/__ ___ ______ __ __  __ ___  / /
 |  __ / // // // // // _  // _// // / / // _  // _//     // //  \/ // _ \/ /
 | /  / // // // // // ___// / / // / / // ___// / / / / // // /\  // // / /__
 | \___//____ \\___//____//_/ _\_  / /_//____//_/ /_/ /_//_//_/ /_/ \__\_\___/
 |           \/              /____/                              version 0.4.3
 http://terminal.jcubic.pl

 Licensed under GNU LGPL Version 3 license
 Copyright (c) 2011 Jakub Jankiewicz <http://jcubic.pl>

 Includes:

 Storage plugin Distributed under the MIT License
 Copyright (c) 2010 Dave Schindler

 LiveQuery plugin Dual MIT and GPL
 Copyright (c) 2008 Brandon Aaron (http://brandonaaron.net)

 jQuery Timers licenced with the WTFPL
 <http://jquery.offput.ca/every/>

 Date: Fri, 04 Nov 2011 19:55:17 +0000
*/
Array.prototype.has=function(g){for(var z=this.length;z--;)if(this[z]==g)return true;return false};function get_stack(g){return g?[g.toString().match(/.*\n.*\n/)].concat(get_stack(g.caller)):[]}
(function(g,z){function Y(a,d){var f;if(typeof a==="string"&&typeof d==="string"){localStorage[a]=d;return true}else if(typeof a==="object"&&typeof d==="undefined"){for(f in a)if(a.hasOwnProperty(f))localStorage[f]=a[f];return true}return false}function V(a,d){var f,e;f=new Date;f.setTime(f.getTime()+31536E6);f="; expires="+f.toGMTString();if(typeof a==="string"&&typeof d==="string"){document.cookie=a+"="+d+f+"; path=/";return true}else if(typeof a==="object"&&typeof d==="undefined"){for(e in a)if(a.hasOwnProperty(e))document.cookie=
e+"="+a[e]+f+"; path=/";return true}return false}function Z(a){return localStorage[a]}function $(a){var d,f,e;a+="=";d=document.cookie.split(";");for(f=0;f<d.length;f++){for(e=d[f];e.charAt(0)===" ";)e=e.substring(1,e.length);if(e.indexOf(a)===0)return e.substring(a.length,e.length)}return null}function aa(a){return delete localStorage[a]}function ba(a){return V(a,"",-1)}function U(a,d){var f=[],e=a.length;if(e<d)return[a];for(var j=0;j<e;j+=d)f.push(a.substring(j,j+d));return f}function E(a){if(typeof a==
"string"){a=a.replace(/&(?!#[0-9]*;)/g,"&amp;");a=a.replace(/</g,"&lt;").replace(/>/g,"&gt;");a=a.replace(/\n/g,"<br/>");a=a.replace(/ /g,"&nbsp;");a=a.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");var d=a.split(ca);if(d.length>1)a=g.map(d,function(f){return f===""?f:f[0]=="["?f.replace(da,function(e,j,q,y,F){if(F==="")return"<span>&nbsp;</span>";e="";if(j.indexOf("b")!=-1)e+="font-weight:bold;";if(j.indexOf("u")!=-1)e+="text-decoration:underline;";if(j.indexOf("i")!=-1)e+="font-style:italic; ";if(q.match(W))e+=
"color:"+q+";";if(y.match(W))e+="background-color:"+y;return a='<span style="'+e+'">'+F+"</span>"}):"<span>"+f+"</span>"}).join("");return a}else return""}function X(a){var d=a instanceof Array?a:a?[a]:[],f=0;g.extend(this,{left:function(){if(f===0)f=d.length-1;else--f;return d[f]},right:function(){if(f==d.length-1)f=0;else++f;return d[f]},current:function(){return d[f]},data:function(){return d},length:function(){return d.length},reset:function(){f=0},append:function(e){d.push(e);this.reset()}})}
function ea(a){var d=a?[a]:[];g.extend(this,{size:function(){return d.length},pop:function(){if(d.length===0)return null;else{var f=d[d.length-1];d=d.slice(0,d.length-1);return f}},push:function(f){d=d.concat([f]);return f},top:function(){return d.length>0?d[d.length-1]:null}})}function fa(a){var d=true;if(typeof a==="string"&&a!=="")a+="_";var f=g.Storage.get(a+"commands"),e=new X(f?eval("("+f+")"):[""]);g.extend(this,{append:function(j){if(d&&e.current()!=j){e.append(j);g.Storage.set(a+"commands",
g.json_stringify(e.data()))}},data:function(){return e.data()},next:function(){return e.right()},last:function(){e.reset()},previous:function(){return e.left()},clear:function(){e=new X;g.Storage.remove(a+"commands")},enable:function(){d=true},disable:function(){d=false}})}g.extend(g.fn,{livequery:function(a,d,f){var e=this,j;if(g.isFunction(a)){f=d;d=a;a=z}g.each(g.livequery.queries,function(q,y){if(e.selector==y.selector&&e.context==y.context&&a==y.type&&(!d||d.$lqguid==y.fn.$lqguid)&&(!f||f.$lqguid==
y.fn2.$lqguid))return(j=y)&&false});j=j||new g.livequery(this.selector,this.context,a,d,f);j.stopped=false;j.run();return this},expire:function(a,d,f){var e=this;if(g.isFunction(a)){f=d;d=a;a=z}g.each(g.livequery.queries,function(j,q){if(e.selector==q.selector&&e.context==q.context&&(!a||a==q.type)&&(!d||d.$lqguid==q.fn.$lqguid)&&(!f||f.$lqguid==q.fn2.$lqguid)&&!this.stopped)g.livequery.stop(q.id)});return this}});g.livequery=function(a,d,f,e,j){this.selector=a;this.context=d||document;this.type=
f;this.fn=e;this.fn2=j;this.elements=[];this.stopped=false;this.id=g.livequery.queries.push(this)-1;e.$lqguid=e.$lqguid||g.livequery.guid++;if(j)j.$lqguid=j.$lqguid||g.livequery.guid++;return this};g.livequery.prototype={stop:function(){var a=this;if(this.type)this.elements.unbind(this.type,this.fn);else this.fn2&&this.elements.each(function(d,f){a.fn2.apply(f)});this.elements=[];this.stopped=true},run:function(){if(!this.stopped){var a=this,d=this.elements,f=g(this.selector,this.context),e=f.not(d);
this.elements=f;if(this.type){e.bind(this.type,this.fn);d.length>0&&g.each(d,function(j,q){g.inArray(q,f)<0&&g.event.remove(q,a.type,a.fn)})}else{e.each(function(){a.fn.apply(this)});this.fn2&&d.length>0&&g.each(d,function(j,q){g.inArray(q,f)<0&&a.fn2.apply(q)})}}}};g.extend(g.livequery,{guid:0,queries:[],queue:[],running:false,timeout:null,checkQueue:function(){if(g.livequery.running&&g.livequery.queue.length)for(var a=g.livequery.queue.length;a--;)g.livequery.queries[g.livequery.queue.shift()].run()},
pause:function(){g.livequery.running=false},play:function(){g.livequery.running=true;g.livequery.run()},registerPlugin:function(){g.each(arguments,function(a,d){if(g.fn[d]){var f=g.fn[d];g.fn[d]=function(){var e=f.apply(this,arguments);g.livequery.run();return e}}})},run:function(a){if(a!=z)g.inArray(a,g.livequery.queue)<0&&g.livequery.queue.push(a);else g.each(g.livequery.queries,function(d){g.inArray(d,g.livequery.queue)<0&&g.livequery.queue.push(d)});g.livequery.timeout&&clearTimeout(g.livequery.timeout);
g.livequery.timeout=setTimeout(g.livequery.checkQueue,20)},stop:function(a){a!=z?g.livequery.queries[a].stop():g.each(g.livequery.queries,function(d){g.livequery.queries[d].stop()})}});g.livequery.registerPlugin("append","prepend","after","before","wrap","attr","removeAttr","addClass","removeClass","toggleClass","empty","remove");g(function(){g.livequery.play()});var ga=g.prototype.init;g.prototype.init=function(a,d){var f=ga.apply(this,arguments);if(a&&a.selector){f.context=a.context;f.selector=
a.selector}if(typeof a=="string"){f.context=d||document;f.selector=a}return f};g.prototype.init.prototype=g.prototype;var Q=typeof window.localStorage!=="undefined";g.extend({Storage:{set:Q?Y:V,get:Q?Z:$,remove:Q?aa:ba}});jQuery.fn.extend({everyTime:function(a,d,f,e,j){return this.each(function(){jQuery.timer.add(this,a,d,f,e,j)})},oneTime:function(a,d,f){return this.each(function(){jQuery.timer.add(this,a,d,f,1)})},stopTime:function(a,d){return this.each(function(){jQuery.timer.remove(this,a,d)})}});
jQuery.extend({timer:{guid:1,global:{},regex:/^([0-9]+)\s*(.*s)?$/,powers:{ms:1,cs:10,ds:100,s:1E3,das:1E4,hs:1E5,ks:1E6},timeParse:function(a){if(a==z||a===null)return null;var d=this.regex.exec(jQuery.trim(a.toString()));return d[2]?parseInt(d[1],10)*(this.powers[d[2]]||1):a},add:function(a,d,f,e,j,q){var y=0;if(jQuery.isFunction(f)){j||(j=e);e=f;f=d}d=jQuery.timer.timeParse(d);if(!(typeof d!="number"||isNaN(d)||d<=0)){if(j&&j.constructor!=Number){q=!!j;j=0}j=j||0;q=q||false;if(!a.$timers)a.$timers=
{};a.$timers[f]||(a.$timers[f]={});e.$timerID=e.$timerID||this.guid++;var F=function(){if(!(q&&this.inProgress)){this.inProgress=true;if(++y>j&&j!==0||e.call(a,y)===false)jQuery.timer.remove(a,f,e);this.inProgress=false}};F.$timerID=e.$timerID;a.$timers[f][e.$timerID]||(a.$timers[f][e.$timerID]=window.setInterval(F,d));this.global[f]||(this.global[f]=[]);this.global[f].push(a)}},remove:function(a,d,f){var e=a.$timers,j;if(e){if(d){if(e[d]){if(f){if(f.$timerID){window.clearInterval(e[d][f.$timerID]);
delete e[d][f.$timerID]}}else for(f in e[d]){window.clearInterval(e[d][f]);delete e[d][f]}for(j in e[d])break;if(!j){j=null;delete e[d]}}}else for(d in e)this.remove(a,d,f);for(j in e)break;if(!j)a.$timers=null}}}});if(jQuery.browser.msie)jQuery(window).one("unload",function(){var a=jQuery.timer.global,d;for(d in a)for(var f=a[d],e=f.length;--e;)jQuery.timer.remove(f[e],d)});var ca=/(\[\[[biu]*;[^;]*;[^\]]*\][^\]\[]*\])/g,da=/\[\[([biu]*);([^;]*);([^\]]*)\]([^\]\[]*)\]/g,W=/#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})/;
g.json_stringify=function(a,d){var f="",e;d=d===z?1:d;switch(typeof a){case "function":f+=a;break;case "boolean":f+=a?"true":"false";break;case "object":if(a===null)f+="null";else if(a instanceof Array){f+="[";var j=a.length;for(e=0;e<j-1;++e)f+=g.json_stringify(a[e],d+1);f+=g.json_stringify(a[j-1],d+1)+"]"}else{f+="{";for(j in a)if(a.hasOwnProperty(j))f+='"'+j+'":'+g.json_stringify(a[j],d+1);f+="}"}break;case "string":j=a;var q={"\\\\":"\\\\",'"':'\\"',"/":"\\/","\\n":"\\n","\\r":"\\r","\\t":"\\t"};
for(e in q)if(q.hasOwnProperty(e))j=j.replace(RegExp(e,"g"),q[e]);f+='"'+j+'"';break;case "number":f+=String(a)}f+=d>1?",":"";if(d==1)f=f.replace(/,([\]}])/g,"$1");return f.replace(/([\[{]),/g,"$1")};g.fn.cmd=function(a){function d(){L.toggleClass("inverted")}function f(){j.focus();e.oneTime(1,function(){e.insert(j.val());j.blur();j.val("")})}var e=this;e.addClass("cmd");e.append('<span class="prompt"></span><span></span><span class="cursor">&nbsp;</span><span></span>');var j=g("<textarea/>").addClass("clipboard").appendTo(e);
a.width&&e.width(a.width);var q,y,F=a.mask||false,o="",p=0,I,J=a.enabled,R,G,L=e.find(".cursor"),c=function(h){function u(t,v){if(v==t.length){B.html(E(t));L.html("&nbsp;");H.html("")}else if(v===0){B.html("");L.html(E(t.slice(0,1)));H.html(E(t.slice(1)))}else{var k=E(t.slice(0,v));B.html(k);k=t.slice(v,v+1);L.html(k==" "?"&nbsp;":E(k));v==t.lenght-1?H.html(""):H.html(E(t.slice(v+1)))}}function K(t){return"<div>"+E(t)+"</div>"}function P(t){var v=H;g.each(t,function(k,l){v=g(K(l)).insertAfter(v).addClass("clear")})}
function n(t){g.each(t,function(v,k){B.before(K(k))})}var B=L.prev(),H=L.next();return function(){var t=F?o.replace(/./g,"*"):o,v;h.find("div").remove();B.html("");if(t.length>q-y-1||t.match(/\n/)){var k;if(t.match(/\n/)){var l=t.split("\n"),b=q-y-1;for(v=0;v<l.length-1;++v)l[v]+=" ";if(l[0].length>b){k=[l[0].substring(0,b)];k=k.concat(U(l[0].substring(b),q))}else k=[l[0]];for(v=1;v<l.length;++v)if(l[v].length>q)k=k.concat(U(l[v],q));else k.push(l[v])}else{k=t.substring(0,q-y-1);b=t.substring(q-y-
1);k=[k].concat(U(b,q))}b=k[0].length;if(p<b){u(k[0],p);P(k.slice(1))}else if(p==b){B.before(K(k[0]));u(k[1],0);P(k.slice(2))}else{v=k.length;if(p<b){u(k[0],p);P(k.slice(1))}else if(p==b){B.before(K(k[0]));u(k[1],0);P(k.slice(2))}else{l=k.slice(-1)[0];var i=t.length-p;t=0;if(i<=l.length){n(k.slice(0,-1));t=l.length==i?0:l.length-i;u(l,t)}else if(v==3){B.before("<div>"+E(k[0])+"</div>");u(k[1],p-b-1);H.after('<div class="clear">'+E(k[2])+"</div>")}else{t=p;for(v=0;v<k.length;++v)if(t>k[v].length)t-=
k[v].length;else break;b=k[v];if(t==b.length){t=0;b=k[++v]}u(b,t);n(k.slice(0,v));P(k.slice(v+1))}}}}else if(t===""){B.html("");L.html("&nbsp;");H.html("")}else u(t,p)}}(e),M=function(){var h=e.find(".prompt");return function(){if(typeof I=="string"){y=I.length;h.html(E(I)+"&nbsp;")}else I(function(u){y=u.length;h.html(E(u)+"&nbsp;")})}}();g.extend(e,{name:function(h){if(h!==z){R=h;G=new fa(h)}else return R},history:function(){return G},set:function(h,u){if(h!==z){o=h;if(!u)p=o.length;c()}},insert:function(h,
u){if(p==o.length)o+=h;else o=p===0?h+o:o.slice(0,p)+h+o.slice(p);u||(p+=h.length);c()},get:function(){return o},commands:function(h){if(h)a.commands=h;else return h},destroy:function(){g(document.documentElement).unbind(".commandline");e.find(".prompt").remove()},prompt:function(h){if(h===z)return I;else{if(typeof h=="string"||typeof h=="function")I=h;else throw"prompt must be a function or string";M()}},position:function(h){if(typeof h=="number"){p=h<0?0:h>o.length?o.length:h;c()}else return p},
resize:function(h){if(h)q=h;else{h=e.width();var u=L.innerWidth();q=Math.floor(h/u)}c()},enable:function(){if(!J){e.everyTime(500,"blink",d);J=true}},isenabled:function(){return J},disable:function(){if(J){e.stopTime("blink",d);e.find(".cursor").removeClass("inverted");J=false}},mask:function(h){if(typeof h=="boolean"){F=h;c()}else return F}});e.name(a.name||"");I=a.prompt||">";M();if(a.enabled===z||a.enabled===true)e.enable();g(document.documentElement).keypress(function(h){var u;if(h.ctrlKey&&h.which==
99)return true;if(a.keypress)u=a.keypress(h);if(u===z||u){if(J)if([38,32,13,0,8].has(h.which)&&h.keyCode!=123&&!(h.which==38&&h.shiftKey))return false;else if(!h.ctrlKey&&!(h.altKey&&h.which==100)){e.insert(String.fromCharCode(h.which));return false}}else return u}).keydown(function(h){if(J){if(a.keydown&&a.keydown(h)===false)return false;var u;if(h.keyCode==13){G&&o&&G.append(o);G.last();h=o;e.set("");typeof I=="function"&&M();a.commands&&a.commands(h)}else if(h.which==32)e.insert(" ");else if(h.which==
8){if(o!==""&&p>0){o=o.slice(0,p-1)+o.slice(p,o.length);--p;c()}}else if(h.which==9&&!(h.ctrlKey||h.altKey))e.insert("\t");else if(h.which==46){if(o!==""&&p<o.length){o=o.slice(0,p)+o.slice(p+1,o.length);c()}return true}else if(G&&h.which==38||h.which==80&&h.ctrlKey)e.set(G.previous());else if(G&&h.which==40||h.which==78&&h.ctrlKey)e.set(G.next());else if(h.which==37||h.which==66&&h.ctrlKey)if(h.ctrlKey&&h.which!=66){u=p-1;h=0;for(o[u]==" "&&--u;u>0;--u)if(o[u]==" "&&o[u+1]!=" "){h=u+1;break}else if(o[u]==
"\n"&&o[u+1]!="\n"){h=u;break}e.position(h)}else{if(p>0){--p;c()}}else if(h.which==39||h.which==70&&h.ctrlKey)if(h.ctrlKey&&h.which!=70){o[p]==" "&&++p;h=o.slice(p).match(/\S[\n\s]{2,}|[\n\s]+\S?/);if(!h||h[0].match(/^\s+$/))p=o.length;else if(h[0][0]!=" ")p+=h.index+1;else{p+=h.index+h[0].length-1;h[0][h[0].length-1]!=" "&&--p}c()}else{if(p<o.length){++p;c()}}else if(h.which==123)return true;else if(h.which==36)e.position(0);else if(h.which==35)e.position(o.length);else if(h.ctrlKey||h.metaKey)if(h.shiftKey){if(h.which==
84)return true}else if(h.which==65)e.position(0);else if(h.which==69)e.position(o.length);else if(h.which==88||h.which==67||h.which==87||h.which==84)return true;else if(h.which==86){f();return true}else if(h.which==75)if(p===0)e.set("");else p!=o.length&&e.set(o.slice(0,p));else if(h.which==85)e.set("");else{if(h.which==17)return true}else if(h.altKey)h.which==68&&e.set(o.slice(0,p)+o.slice(p).replace(/[^ ]+ |[^ ]+$/,""),true);else return true;return false}});return e};g.jrpc=function(a,d,f,e,j,q){d=
g.json_stringify({jsonrpc:"2.0",method:f,params:e,id:d});return g.ajax({url:a,data:d,success:j,error:q,contentType:"application/json",dataType:"json",async:true,cache:false,type:"POST"})};Q=/ {13}$/;var ha=[["jQuery Terminal","(c) 2011 jcubic"],["JQuery Terminal Emulator v. 0.4.3","Copyright (c) 2011 Jakub Jankiewicz <http://jcubic.pl>".replace(/ *<.*>/,"")],["JQuery Terminal Emulator version version 0.4.3","Copyright (c) 2011 Jakub Jankiewicz <http://jcubic.pl>"],["      _______                 ________                        __",
"     / / _  /_ ____________ _/__  ___/______________  _____  / /"," __ / / // / // / _  / _/ // / / / _  / _/     / /  \\/ / _ \\/ /","/  / / // / // / ___/ // // / / / ___/ // / / / / /\\  / // / /__","\\___/____ \\\\__/____/_/ \\__ / /_/____/_//_/ /_/ /_/  \\/\\__\\_\\___/","         \\/          /____/                                   ".replace(Q,"")+"version 0.4.3","Copyright (c) 2011 Jakub Jankiewicz <http://jcubic.pl>"],["      __ _____                     ________                              __",
"     / // _  /__ __ _____ ___ __ _/__  ___/__ ___ ______ __ __  __ ___  / /"," __ / // // // // // _  // _// // / / // _  // _//     // //  \\/ // _ \\/ /","/  / // // // // // ___// / / // / / // ___// / / / / // // /\\  // // / /__","\\___//____ \\\\___//____//_/ _\\_  / /_//____//_/ /_/ /_//_//_/ /_/ \\__\\_\\___/","          \\/              /____/                                          ".replace(Q,"")+"version 0.4.3","Copyright (c) 2011 Jakub Jankiewicz <http://jcubic.pl>"]],ia=[],N=new function(a){var d=
a?[a]:[],f=0;g.extend(this,{rotate:function(){if(d.length==1)return d[0];else{if(f==d.length-1)f=0;else++f;return d[f]}},length:function(){return d.length},set:function(e){for(var j=d.length;j--;)if(d[j]===e){f=j;return}this.append(e)},front:function(){return d[f]},append:function(e){d.push(e)}})};g.fn.terminal=function(a,d){function f(){var b=g("<span>x</span>").appendTo(c),i=Math.floor(c.width()/b.width());b.remove();return i}function e(b,i){c.error("&#91;"+i+"&#93;: "+(typeof b=="string"?b:b.fileName+
": "+b.message));c.pause();typeof b.fileName=="string"&&g.get(b.fileName,function(m){c.resume();var w=b.lineNumber-1;(m=m.split("\n")[w])&&c.error("&#91;"+b.lineNumber+"&#93;: "+m)})}function j(b,i){try{if(typeof i=="function")i(function(){});else if(typeof i!="string")throw b+" must be string or function";}catch(m){e(m,b.toUpperCase());return false}return true}function q(){var b=c.prop?c.prop("scrollHeight"):c.attr("scrollHeight");c.scrollTop(b)}function y(b){b=typeof b=="string"?b:String(b);var i,
m,w;if(b.length>K){i=K;var r=[];m=/(\[\[[biu]*;[^;]*;\][^\]\[]*\]?)/g;w=/(\[\[[biu]*;[^;]*;\])/;b=b.split(/\n/g);for(var x="",s=0,C=b.length;s<C;++s){if(x!=="")if(b[s]===""){r.push(x+"]");continue}else{b[s]=x+b[s];x=""}for(var A=0,T=b[s].length;A<T;A+=i){var D=b[s].substring(A,A+i);if(x!=="")D=x+D;var O=D.match(m);if(O&&O.length>0){var S=0;D=0;for(var ja=O.length;D<ja;++D){S+=O[D].match(w)[1].length;if(O[D][O[D].length-1]=="]")S+=1}if(x!=="")S-=x.length;D=x+b[s].substring(A,A+i+S);O=D.match(m);x=
O[O.length-1];A+=S;if(x[x.length-1]!="]"){D+="]";x=x.match(w)[1]}else x=""}else x="";r.push(D)}}i=g("<div></div>");m=0;for(w=r.length;m<w;++m)r[m]===""||r[m]=="\r"?i.append("<div>&nbsp;</div>"):g("<div/>").html(E(r[m])).appendTo(i)}else i=g("<div/>").html(E(b));h.append(i);i.width("100%");q();return i}function F(b,i){var m=1,w=function(r,x){i.pause();g.jrpc(b,m++,r,x,function(s){if(s.error)i.error("&#91;RPC&#93; "+s.error.message);else if(typeof s.result=="string")i.echo(s.result);else if(s.result instanceof
Array)i.echo(s.result.join(" "));else if(typeof s.result=="object"){var C="",A;for(A in s.result)if(s.result.hasOwnProperty(A))C+=A+": "+s.result[A]+"\n";i.echo(C)}i.resume()},function(s,C){i.error("&#91;AJAX&#93; "+C+" - Server reponse is: \n"+s.responseText);i.resume()})};return function(r,x){if(r!==""){var s,C;if(r.match(/[^ ]* /)){r=r.split(/ +/);s=r[0];C=r.slice(1)}else{s=r;C=[]}if(!n.login||s=="help")w(s,C);else{var A=x.token();A?w(s,[A].concat(C)):x.error("&#91;AUTH&#93; Access denied (no token)")}}}}
function o(b){var i=l.prompt();if(l.mask())b=b.replace(/./g,"*");typeof i=="function"?i(function(m){c.echo(m+" "+b)}):c.echo(i+" "+b)}function p(b){try{var i=k.top();if(b=="exit"&&n.exit)if(k.size()==1)n.login?J():c.echo("You can exit from main interpeter");else c.pop("exit");else{o(b);if(b=="clear"&&n.clear)c.clear();else if(b.match(/\|/)){var m=b.split(/\s*\|\s*/),w=[],r=c.echo;c.echo=function(D){w.push(D)};i.eval(m[0],c);b=1;for(var x=m.length;b<x-1;++b)for(var s=0,C=w.length;s<C;++s){var A=m[b]+
" "+w[s];i.eval(A,c)}c.echo=r;s=0;for(C=w.length;s<C;++s){A=m[x-1]+" "+w[s];i.eval(A,c)}}else i.eval(b,c)}}catch(T){e(T,"USER");c.resume();throw T;}}function I(){var b=null;l.prompt("login:");n.history&&l.history().disable();l.commands(function(i){try{o(i);if(b){l.mask(false);c.pause();if(typeof n.login!="function")throw"Value of login property must be a function";n.login(b,i,function(w){if(w){var r=n.name;r=r?"_"+r:"";g.Storage.set("token"+r,w);g.Storage.set("login"+r,b);l.commands(p);G()}else{c.error("Wrong password try again");
l.prompt("login:");b=null}c.resume();n.history&&l.history().enable()})}else{b=i;l.prompt("password:");l.mask(true)}}catch(m){e(m,"LOGIN",c);throw m;}})}function J(){var b=n.name;b=b?"_"+b:"";g.Storage.remove("token"+b,null);g.Storage.remove("login"+b,null);n.history&&l.history().disable();I()}function R(){var b=k.top(),i="";if(b.name!==z&&b.name!=="")i+=b.name+"_";i+=u;l.name(i);l.prompt(b.prompt);n.history&&l.history().enable();l.set("");if(typeof b.onStart=="function")b.onStart(c)}function G(){R();
if(d.greetings===z)c.echo(c.signature);else d.greetings&&c.echo(d.greetings);if(typeof n.onInit=="function")n.onInit(c)}function L(b){if(!c.paused()){if(n.keydown&&n.keydown(b,c)===false)return false;if(b.which!=9)H=0;if(b.which==68&&b.ctrlKey){if(n.exit)if(l.get()==="")if(k.size()>1||n.login!==z)c.pop("");else{c.resume();c.echo("")}else c.set_command("");b.preventDefault()}else if(n.tabcompletion&&b.which==9){++H;b=l.get();if(!b.match(" ")){for(var i=RegExp("^"+b),m=k.top().command_list,w=[],r=m.length;r--;)i.test(m[r])&&
w.push(m[r]);if(w.length==1)c.set_command(w[0]);else if(w.length>1)if(H>=2){o(b);c.echo(w.join("\t"));H=0}}return false}else if(b.which==86&&b.ctrlKey){c.oneTime(1,function(){q()});return true}else if(b.which==9&&b.ctrlKey){N.length()>1&&c.focus(false);b.preventDefault()}else if(b.which==34)c.scroll(c.height());else b.which==33?c.scroll(-c.height()):c.attr({scrollTop:c.attr("scrollHeight")})}}var c=this,M=[],h,u=N.length(),K,P=[],n={name:"",prompt:">",history:true,exit:true,clear:true,enabled:true,
login:null,tabcompletion:false,onInit:null,onExit:null,keypress:null,keydown:null};if(d){d.width&&c.width(d.width);d.height&&c.height(d.height);g.extend(n,d)}var B=!n.enabled;if(c.length===0)throw'Sorry, but terminal said that "'+c.selector+'" is not valid selector!';c.ajaxSend(function(b,i){ia.push(i)});if(c.data("terminal"))return c.data("terminal");h=g("<div>").addClass("terminal-output").appendTo(c);c.addClass("terminal").append("<div/>");g.extend(c,{clear:function(){h.html("");l.set("");M=[];
c.attr({scrollTop:0});return c},paused:function(){return B},pause:function(){if(l){c.disable();l.hide()}return c},resume:function(){if(l){c.enable();l.show();q()}return c},cols:function(){return K},rows:function(){return M.length},history:function(){return l.history().data()},next:function(){if(N.length()==1)return c;else{var b=c.offset().top;c.height();c.scrollTop();var i=c,m=g(window).scrollTop(),w=m+g(window).height(),r=g(i).offset().top;if(r+g(i).height()>=m&&r<=w){N.front().disable();b=N.rotate().enable();
i=b.offset().top-50;g("html,body").animate({scrollTop:i},500);return b}else{c.enable();g("html,body").animate({scrollTop:b-50},500);return c}}},focus:function(b){c.oneTime(1,function(){if(N.length()==1)b===false?c.disable():c.enable();else if(b===false)c.next();else{N.front().disable();N.set(c);c.enable()}});return c},enable:function(){K===z&&c.resize();if(B)if(l){l.enable();B=false}return c},disable:function(){if(l){B=true;l.disable()}return c},enabled:function(){return B},signature:function(){var b=
c.cols();b=b<15?null:b<35?0:b<55?1:b<64?2:b<75?3:4;return b!==null?ha[b].join("\n")+"\n":""},version:function(){return"0.4.3"},get_command:function(){return l.get()},insert:function(b){l.insert(b);return c},set_prompt:function(b){j("prompt",b)&&l.prompt(b);return c},set_command:function(b){l.set(b);return c},set_mask:function(b){l.mask(b);return c},get_output:function(){return g.map(M,function(b,i){return typeof i=="function"?i():i}).get().join("\n")},resize:function(b,i){if(b&&i){c.width(b);c.height(i)}K=
f();l.resize(K);var m=h.detach();h.html("");g.each(M,function(w,r){y(typeof r=="function"?r():r)});c.prepend(m);q();return c},echo:function(b){M.push(b);return y(typeof b=="function"?b():b)},error:function(b){c.echo("[[;#f00;]"+b.replace(/\[/g,"&#91;").replace(/\]/g,"&#93;")+"]")},scroll:function(b){var i;if(c.prop){b>c.prop("scrollTop")&&b>0&&c.prop("scrollTop",0);i=c.prop("scrollTop");c.prop("scrollTop",i+b)}else{b>c.attr("scrollTop")&&b>0&&c.attr("scrollTop",0);i=c.attr("scrollTop");c.attr("scrollTop",
i+b)}return c},logout:n.login?function(){for(;k.size()>1;)k.pop();J();return c}:function(){throw"You don't have login function";},token:n.login?function(){var b=n.name;return g.Storage.get("token"+(b?"_"+b:""))}:null,login_name:n.login?function(){var b=n.name;return g.Storage.get("login"+(b?"_"+b:""))}:null,name:function(){return n.name},push:function(b,i){if(!i.prompt||j("prompt",i.prompt)){if(typeof b=="string")b=F(i.eval,c);k.push(g.extend({eval:b},i));R()}return c},pop:function(b){b!==z&&o(b);
if(k.top().name===n.name){if(n.login){J();if(typeof n.onExit=="function")n.onExit(c)}}else{b=k.pop();R();if(typeof b.onExit=="function")b.onExit(c)}return c}});var H=0,t;if(a.constructor==String){t=a;a=F(a,c)}else if(a.constructor==Array)throw"You can't use array as eval";else if(typeof a=="object"){for(var v in a)P.push(v);a=function b(i){return function(m){if(m!==""){m=m.split(/ +/);var w=m[0],r=m.slice(1);m=i[w];var x=typeof m;if(x=="function")m.apply(c,r);else if(x=="object"||x=="string"){r=[];
if(x=="object"){for(var s in m)r.push(s);m=b(m)}c.push(m,{prompt:w+">",name:w,command_list:r})}else c.error("Command '"+w+"' Not Found")}}}(a)}else if(typeof a!="function")throw'Unknow object "'+String(a)+'" passed as eval';if(t&&(typeof n.login=="string"||n.login))n.login=function(b){var i=1;return function(m,w,r){c.pause();g.jrpc(t,i++,b,[m,w],function(x){c.resume();!x.error&&x.result?r(x.result):r(null)},function(x,s){c.resume();c.error("&#91;AJAX&#92; Response: "+s+"\n"+x.responseText)})}}(typeof n.login==
"boolean"?"login":n.login);if(j("prompt",n.prompt)){var k=new ea({name:n.name,eval:a,prompt:n.prompt,command_list:P,greetings:n.greetings}),l=c.find(".terminal-output").next().cmd({prompt:n.prompt,history:n.history,width:"100%",keydown:L,keypress:n.keypress?function(b){return n.keypress(b,c)}:null,commands:p});c.livequery(function(){c.resize()});N.append(c);n.enabled===true?c.focus():c.disable();g(window).resize(c.resize);c.click(function(){c.focus()});c.token&&!c.token()&&c.login_name&&!c.login_name()?
I():G();typeof g.fn.init.prototype.mousewheel==="function"&&c.mousewheel(function(b,i){i>0?c.scroll(-40):c.scroll(40);return false},true)}c.data("terminal",c);return c}})(jQuery);
