/*

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

