(function(b){window.RPC={pageBaseURL:document.location.href.replace(/[^\/]+$/,""),init:function(){b().ajaxError(function(e,a,f){if(a.status){b.WindowManager.status({title:"The server response wasn't JSON format",content:a.responseText})}})},toArray:function(a){var d=[];if(a&&a.data&&a.columns){b(a.data).each(function(f){var c={index:f};b(this).each(function(e,h){c[a.columns[e]]=h});d.push(c)})}return d},exec:function(f,e,a){b.post(RPC.pageBaseURL+"../../rpc/index.php",{json_data:b.toJSON({method:f,params:[e],id:"c0"})},a,"json")},handleError:function(a){var i=this,g,j=a.response.result,h=a.response.error;if(h){if(h.level=="AUTH"){if(j.login_url.indexOf("return_url")!=-1){document.location=j.login_url}else{if(j.login_url.indexOf("://")==-1){document.location="../../"+j.login_url+"?return_url="+escape(document.location)}else{document.location=j.login_url+"?return_url="+escape(document.location)}}return true}b.WindowManager.info(b.translate(h.errstr));return true}else{g=[];b(this.toArray(j)).each(function(d,c){var e;if(c.status&&c.status.toLowerCase()!="ok"){e=a.visual_path||"/";e=e.substring(1);e=a.visual_path.replace(/^\/([^\/]+)\/.*$/,"$1");if(c.fromfile!=undefined){c.fromfile=c.fromfile.replace(/\{[0-9]+\}/,e);c.tofile=c.tofile.replace(/\{[0-9]+\}/,e);g.push({title:c.fromfile+" -> "+c.tofile,content:b.translate(c.message)})}if(c.file!=undefined){c.file=c.file.replace(/\{[0-9]+\}/,e);c.file=c.file.replace(/\/+/,"/");g.push({title:c.file,content:b.translate(c.message)})}}});if(g.length){b.WindowManager.status({title:b.translate(a.message),content:g});return true}}},insertFiles:function(e){var f=this,a={};b(e.paths).each(function(c,d){a["path"+c]=d});if(e.progress_message){b.WindowManager.showProgress({message:e.progress_message})}RPC.exec("im.insertFiles",a,function(d){var c={files:[]};b.WindowManager.hideProgress();b(RPC.toArray(d.result)).each(function(i,l){var k=l.url;if(e.relative_urls){k=b.parseURI(e.document_base_url||e.default_base_url).toRelative(k)}else{if(e.document_base_url){k=b.parseURI(e.document_base_url).toAbsolute(b.parseURI(k).getURI(1),e.no_host)}}l={name:l.name,path:l.path,url:k,size:l.size,type:l.type,created:l.created,modified:l.modified,attribs:l.attribs,custom:b.extend({},l.custom)};c.files.push(l)});if(e.oninsert){if(e.insert_filter){b(c.files).each(function(j,i){e.insert_filter(i)})}e.oninsert(c)}})},insertFile:function(d){var a=d.oninsert;d.paths=[d.path];d.oninsert=function(c){c.file=c.files[0];a.call(this,c)};this.insertFiles(d)}};RPC.init()})(jQuery);