var self=this;self.URL=self.URL||self.webkitURL;self.requestFileSystem=self.requestFileSystem||self.webkitRequestFileSystem;self.resolveLocalFileSystemURL=self.resolveLocalFileSystemURL||self.webkitResolveLocalFileSystemURL;self.BlobBuilder=self.BlobBuilder||self.MozBlobBuilder||self.WebKitBlobBuilder;
var Util={toArray:function(a){return Array.prototype.slice.call(a||[],0)},strToDataURL:function(a,e,b){return(void 0!=b?b:1)?"data:"+e+";base64,"+self.btoa(a):"data:"+e+","+a},strToObjectURL:function(a,e){for(var b=new Uint8Array(a.length),d=0;d<b.length;++d)b[d]=a.charCodeAt(d);d=new BlobBuilder;d.append(b.buffer);b=e?d.getBlob(e):d.getBlob();return self.URL.createObjectURL(b)},fileToObjectURL:function(a){return self.URL.createObjectURL(a)},fileToArrayBuffer:function(a,e,b){var d=new FileReader;
d.onload=function(a){e(a.target.result)};d.onerror=function(a){b&&b(a)};d.readAsArrayBuffer(a)},arrayBufferToBlob:function(a,e){var b=new BlobBuilder;b.append(a);return e?b.getBlob(e):b.getBlob()},arrayBufferToBinaryString:function(a,e,b){var d=new FileReader;d.onload=function(a){e(a.target.result)};d.onerror=function(a){b&&b(a)};var h=new BlobBuilder;h.append(a);d.readAsBinaryString(h.getBlob())},arrayToBinaryString:function(a){if("object"!=typeof a)return null;for(var e=a.length,b=Array(e);e--;)b[e]=
String.fromCharCode(a[e]);return b.join("")},getFileExtension:function(a){var e=a.lastIndexOf(".");return-1!=e?a.substring(e):""}},MyFileError=function(a){this.prototype=FileError.prototype;this.code=a.code;this.name=a.name};FileError.BROWSER_NOT_SUPPORTED=1E3;FileError.prototype.__defineGetter__("name",function(){for(var a=Object.keys(FileError),e=0,b;b=a[e];++e)if(FileError[b]==this.code)return b;return"Unknown Error"});
var Filer=new function(){function a(c){if(e=c||null)b=e.root,d=!0}var e=null,b=null,d=!1,h=function(c){return 0==c.indexOf("filesystem:")},k=function(c){h(c)||(c="/"==c[0]?e.root.toURL()+c.substring(1):0==c.indexOf("./")||0==c.indexOf("../")?"../"==c&&b!=e.root?b.toURL()+"/"+c:b.toURL()+c:b.toURL()+"/"+c);return c},l=function(c,a){var b=arguments[1],f=arguments[2],e=function(c){if(c.code==FileError.NOT_FOUND_ERR){if(f)throw Error('"'+b+'" or "'+f+'" does not exist.');throw Error('"'+b+'" does not exist.');
}throw Error("Problem getting Entry for one or more paths.");},d=k(b);if(3==arguments.length){var i=k(f);self.resolveLocalFileSystemURL(d,function(a){self.resolveLocalFileSystemURL(i,function(b){c(a,b)},e)},e)}else self.resolveLocalFileSystemURL(d,c,e)},n=function(c,a,b,f,g,d){if(!e)throw Error("Filesystem has not been initialized.");if(typeof c!=typeof a)throw Error("These method arguments are not supported.");var i=b||null,m=void 0!=d?d:!1;(c.isFile||a.isDirectory)&&a.isDirectory?m?c.moveTo(a,i,
f,g):c.copyTo(a,i,f,g):l(function(c,a){if(a.isDirectory)m?c.moveTo(a,i,f,g):c.copyTo(a,i,f,g);else{var b=Error('Oops! "'+a.name+" is not a directory!");if(g)g(b);else throw b;}},c,a)};a.DEFAULT_FS_SIZE=1048576;a.prototype={get fs(){return e},get isOpen(){return d}};a.prototype.pathToFilesystemURL=function(c){return k(c)};a.prototype.init=function(c,a,j){if(!self.requestFileSystem)throw new MyFileError({code:FileError.BROWSER_NOT_SUPPORTED,name:"BROWSER_NOT_SUPPORTED"});var c=c?c:{},f=c.size||1048576;
this.type=self.TEMPORARY;if("persistent"in c&&c.persistent)this.type=self.PERSISTENT;self.requestFileSystem(this.type,f,function(c){this.size=f;e=c;b=e.root;d=!0;a&&a(c)}.bind(this),j)};a.prototype.ls=function(c,a,d){if(!e)throw Error("Filesystem has not been initialized.");var f=function(c){b=c;var e=[],f=b.createReader(),m=function(){f.readEntries(function(c){c.length?(e=e.concat(Util.toArray(c)),m()):(e.sort(function(c,a){return c.name<a.name?-1:a.name<c.name?1:0}),a(e))},d)};m()};c.isDirectory?
f(c):h(c)?l(f,k(c)):b.getDirectory(c,{},f,d)};a.prototype.mkdir=function(c,a,d,f){if(!e)throw Error("Filesystem has not been initialized.");var g=null!=a?a:!1,h=c.split("/"),i=function(a,b){if("."==b[0]||""==b[0])b=b.slice(1);a.getDirectory(b[0],{create:!0,exclusive:g},function(a){if(a.isDirectory)b.length&&1!=h.length?i(a,b.slice(1)):d(a);else if(a=Error(c+" is not a directory"),f)f(a);else throw a;},function(a){if(a.code==FileError.INVALID_MODIFICATION_ERR)if(a.message="'"+c+"' already exists",
f)f(a);else throw a;})};i(b,h)};a.prototype.open=function(a,b,d){if(!e)throw Error("Filesystem has not been initialized.");a.isFile?a.file(b,d):l(function(a){a.file(b,d)},k(a))};a.prototype.create=function(a,d,j,f){if(!e)throw Error("Filesystem has not been initialized.");b.getFile(a,{create:!0,exclusive:null!=d?d:!0},j,function(b){if(b.code==FileError.INVALID_MODIFICATION_ERR)b.message="'"+a+"' already exists";if(f)f(b);else throw b;})};a.prototype.mv=function(a,b,e,f,d){n.bind(this,a,b,e,f,d,!0)()};
a.prototype.rm=function(a,b,d){if(!e)throw Error("Filesystem has not been initialized.");var f=function(a){a.isFile?a.remove(b,d):a.isDirectory&&a.removeRecursively(b,d)};a.isFile||a.isDirectory?f(a):l(f,a)};a.prototype.cd=function(a,d,j){if(!e)throw Error("Filesystem has not been initialized.");a.isDirectory?(b=a,d&&d(b)):(a=k(a),l(function(a){if(a.isDirectory)b=a,d&&d(b);else if(a=Error("Path was not a directory."),j)j(a);else throw a;},a))};a.prototype.cp=function(a,b,e,d,g){n.bind(this,a,b,e,
d,g)()};a.prototype.write=function(a,d,j,f){if(!e)throw Error("Filesystem has not been initialized.");var g=function(a){a.createWriter(function(b){b.onerror=f;b.onwrite=function(){j(a,b)};var c=new BlobBuilder;c.append(d.data);b.write(d.type?c.getBlob(d.type):c.getBlob())},f)};a.isFile?g(a):h(a)?l(g,a):b.getFile(a,{create:!0,exclusive:!1},g,f)};return a};
