(function(){var d=new Class({Implements:[Events,Options],options:{stateClass:{error:"error2",loading:"loading",complete:"complete",success:""},showStep:".appNum",showAppName:".appName",container:!1,dataClass:".tasks_ipt"},initialize:function(a,b){this.setOptions(b);if(this.tasks=$splat(a))this.num=this.tasks.length||0,this.container=$(this.options.container)},init:function(a){this.iframe||(this.iframe=this.options.iframe?this.options.iframe:new Element("iframe",{src:"blank.html",name:"_TASK_IFRM_",
style:"display:none;height:100%;width:100%"}));a=a||this.container;this.iframe.inject(a||document.body);this.form=this.form||(new Element("form",{style:"display:none",method:"post",target:this.iframe.name})).inject(document.body);var b=this.options;if(!a)return this;this.showStep=a.getElement(b.showStep);this.appName=a.getElement(b.showAppName);this.items=a.getElements(".box");return this},getText:function(){return this[null==document.createElement("div").innerText?"textContent":"innerText"]},createFormData:function(a){if(!this.form)return this;
this.form.empty();var b=this.options,c=document.createDocumentFragment();(a=a?a:$ES(b.dataClass))&&a.length&&a.each(function(a,b){c.appendChild(new Element("input",{type:"hidden",name:a.name,value:a.value}))});this.form.appendChild(c);return this},loader:function(){this.items&&this.items.length&&this.items[this.prestep].addClass(this.options.stateClass.loading);this.showStep&&this.showStep.setText(this.step);this.appName&&this.appName.setText(this.items[this.prestep].get("appname"));return this.fireEvent("loader")},
cancel:function(){return this.fireEvent("cancel",this.step)},run:function(a){this.extra_action=a;this.init(this.container).fireEvent("load",[this.tasks]).createFormData();return a?this.progress(a):this.start()},start:function(a,b){this.step=a||1;this.prestep=this.step-1;this.actions=b||this.tasks[this.prestep];return this.fireEvent("start").loader().progress(this.actions)},error:function(a){a&&alert(a);var b=this.options.stateClass;this.items&&this.items.length&&this.items[this.prestep]&&this.items[this.prestep].removeClass(b.loading).addClass(b.error);
return this.fireEvent("error",a).cancel()},next:function(a){return this.start((a||0)+1)},progress:function(a){if(!a)return this;var b=this.iframe;this.form.action=a||this.actions;this.createFormData().form.submit();b.addEvent("load",this.check.bind(this,b));return this.fireEvent("progress")},check:function(a){a.removeEvents("load");a=this.getText.call(a.contentWindow.document.body);this.result=/(\s*)ok\.(\s*)/.test(a.slice(-4));this.fireEvent("check",a);var b=a.slice(-500);a.slice(-500).replace(/Error:(\s\S+)/,
function(){b=arguments[1]});return this.result?this.complete():this.error(b)},complete:function(){this.fireEvent("complete");if(this.extra_action)return delete this.extra_action,this.start(1);var a=this.options.stateClass;this.items&&this.items.length&&this.items[this.prestep].removeClass(a.loading).addClass(a.complete);if(!this.step&&!this.num)return this.success();a=this.step||0;return this[a==this.num?"success":"next"](a)},success:function(){this.form&&this.form.destroy();return this.fireEvent("success")}}),
e=this.cmdrunner=new Class({Extends:d,options:{title:"",singlon:!0},delayT:function(){if(!this.iframe.contentWindow)return this;var a=this.iframe.contentWindow.document.body;if(!a)return this;this.getText.call(a).replace(/[>|\n]([^\n]+)/gi,function(a,c){1<c.trim().length&&this.csolinfo.set("text",c)}.bind(this));return this},startTimer:function(){this.timer=this.delayT.periodical(200,this);return this},stopTimer:function(){this.timer&&$clear(this.timer);return this},check:function(a){this.stopTimer().delayT();
return this.parent(a)},createTpl:function(){var a=this.tasks,b=a.filter(function(a){return"dialog"!==a.type}),c=this.options.singlon?"":"<h5>"+LANG_Cmdrunner.title+"</h5>",c=c+'<ul class="division apptip">';b.length&&b.each(function(a,b){var d=a.name||this.options.title;c+='<li class="box" appname="'+d+'">'+d+"</li>"},this);a=a.filter(function(a){return"dialog"!=a.type}).length;c+="</ul>";this.theme=this.options.singlon?c:c+'<div class="division loader"><em class="appNum">0</em>/<em>'+a+'</em><span class="appName"></span></div>';
this.container.innerHTML=this.theme+('<div class="console"><p class="csolinfo">loading...</p><span class="lnk csol" onClick="_open(\'index.php?app=desktop&ctl=appmgr&act=app_console\')">'+LANG_Cmdrunner.console+"</span></div>");this.csolinfo=$E(".csolinfo",this.container);return this},close:function(a){(a=a||this.dialog)&&a.close.delay(800,a);return this},adddialog:function(a){return new Dialog(a,{title:this.options.title,height:200,width:600,modal:!0,resizeable:!1,onClose:function(){this.stopTimer()}.bind(this)})},
progress:function(a){switch(a.type){case "command":a="index.php?app=desktop&ctl=appmgr&act=command&command_id="+a.command_id+"&data="+encodeURIComponent(a.data);this.startTimer().parent(a);break;case "dialog":a="index.php?app=desktop&ctl=appmgr&act="+a.action+"&data="+encodeURIComponent(a.data);this.tasks.splice(this.prestep,1);this.num=this.tasks.length;this.stopTimer();(new Request.HTML({url:a,append:this.appinfo,onComplete:function(){var a=this.appinfo.getElement(".appbtn");a?(this.container.hide(),
a.addEvent("click",function(){this.container.show();this.appinfo.hide();this.start(this.step)}.bind(this))):this.start(this.step)}.bind(this)})).get();break;default:this.startTimer().parent(a)}},init:function(){this.container=this.container||new Element("div",{"class":"appbox",html:this.theme||""});this.dialog=this.adddialog.call(this,this.container);this.appinfo=(new Element("div",{"class":"appinfo"})).inject(this.container,"before");this.createTpl().parent(this.container);return this},success:function(){this.parent();
this.options.singlon&&this.close();MessageBox.success(LANG_Cmdrunner.success)}});this.appmgr=function(a){return new f(a,{onSuccess:function(){if(finderGroup)for(var a in finderGroup)finderGroup[a]&&finderGroup[a].refresh()}})};var f=new Class({Extends:d,type:{install:LANG_Cmdrunner.install,uninstall:LANG_Cmdrunner.uninstall,update:LANG_Cmdrunner.update,pause:LANG_Cmdrunner.stop,active:LANG_Cmdrunner.start,download:LANG_Cmdrunner.download},run:function(a){this.app_id=a;this.parent()},init:function(){return this},
progress:function(a){(new Request.JSON({url:"index.php?app=desktop&ctl=appmgr&act=prepare&action="+a,onSuccess:this.prepare.bind(this)})).post({action:a,app_id:this.app_id})},prepare:function(a){if(!a)return this.error();switch(a.status){case "confirm":if(!window.confirm(a.message))return this.dialog&&this.dialog.close(),this;break;case "alert":alert(a.message);return}var b=this.type;this.queue=a.queue;this.dialog&&this.dialog.close();this.runner=new e(this.queue,{title:b[this.tasks[this.prestep]]+
":"+this.app_id,singlon:!1,onSuccess:this.complete.bind(this)});this.runner.run();return this.dialog=this.runner.dialog},success:function(){this.parent().runner.close(this.dialog)}})})();