(function(a,b,c,d,e,f){varg=function(b,c){vard=f.storage(b,c,"base"),e={};e.username=b.username||"",e.applicationname=b.applicationname||"untitled";varg="jio/local_user_array",h="jio/local_file_name_array/"+e.username+"/"+e.applicationname,i=d.serialized;returnd.serialized=function(){vara=i();returna.applicationname=e.applicationname,a.username=e.username,a},d.validateState=function(){returne.username?"":'Need at least one parameter: "username".'},e.getUserArray=function(){returna.getItem(g)||[]},e.addUser=function(b){varc=e.getUserArray();c.push(b),a.setItem(g,c)},e.userExists=function(a){varb=e.getUserArray(),c,d;for(c=0,d=b.length;c<d;c+=1)if(b[c]===a)return!0;return!1},e.getFileNameArray=function(){returna.getItem(h)||[]},e.addFileName=function(b){varc=e.getFileNameArray();c.push(b),a.setItem(h,c)},e.removeFileName=function(b){varc,d,f=e.getFileNameArray(),g=[];for(c=0,d=f.length;c<d;c+=1)f[c]!==b&&g.push(f[c]);a.setItem(h,g)},d.saveDocument=function(b){setTimeout(function(){varc=null,f="jio/local/"+e.username+"/"+e.applicationname+"/"+b.getPath();c=a.getItem(f),c?(c.last_modified=Date.now(),c.content=b.getContent()):(c={name:b.getPath(),content:b.getContent(),creation_date:Date.now(),last_modified:Date.now()},e.userExists(e.username)||e.addUser(e.username),e.addFileName(b.getPath())),a.setItem(f,c),d.success()})},d.loadDocument=function(b){setTimeout(function(){varc=null;c=a.getItem("jio/local/"+e.username+"/"+e.applicationname+"/"+b.getPath()),c?(b.getOption("metadata_only")&&deletec.content,d.success(c)):d.error({status:404,statusText:"Not Found.",message:'Document "'+b.getPath()+'" not found in localStorage.'})})},d.getDocumentList=function(b){setTimeout(function(){varc=[],f=[],g,h,i="key",j="jio/local/"+e.username+"/"+e.applicationname,k={};f=e.getFileNameArray();for(g=0,h=f.length;g<h;g+=1)k=a.getItem(j+"/"+f[g]),k&&(b.getOption("metadata_only")?c.push({name:k.name,creation_date:k.creation_date,last_modified:k.last_modified}):c.push({name:k.name,content:k.content,creation_date:k.creation_date,last_modified:k.last_modified}));d.success(c)})},d.removeDocument=function(b){setTimeout(function(){varc="jio/local/"+e.username+"/"+e.applicationname+"/"+b.getPath();a.deleteItem(c),e.removeFileName(b.getPath()),d.success()})},d};f.addStorageType("local",g);varh=function(a,d){vare=f.storage(a,d,"base"),g={};g.username=a.username||"",g.applicationname=a.applicationname||"untitled",g.url=a.url||"",g.password=a.password||"";varh=e.serialized;returne.serialized=function(){vara=h();returna.username=g.username,a.applicationname=g.applicationname,a.url=g.url,a.password=g.password,a},e.validateState=function(){returng.username&&g.url?"":'Need at least 2 parameters: "username" and "url".'},g.newAsyncModule=function(){vara={};returna.call=function(a,b,c){returna._wait=a._wait||{},a._wait[b]?(a._wait[b]--,function(){}):(c=c||[],a[b].apply(a[b],c))},a.neverCall=function(a,b){a._wait=a._wait||{},a._wait[b]=-1},a.wait=function(a,b,c){a._wait=a._wait||{},a._wait[b]=c},a.end=function(){a.call=function(){}},a},e.saveDocument=function(a){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"PUT",data:a.getContent(),async:!0,dataType:"text",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(){e.success()},error:function(b){b.message='Cannot save "'+a.getPath()+'" into DAVStorage.',e.retry(b)}})},e.loadDocument=function(a){vard={},f=function(){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"GET",async:!0,dataType:"text",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(a){d.content=a,e.success(d)},error:function(b){b.status===404?(b.message='Document "'+a.getPath()+'" not found in localStorage.',e.error(b)):(b.message='Cannot load "'+a.getPath()+'" from DAVStorage.',e.retry(b))}})};d.name=a.getPath(),b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"PROPFIND",async:!0,dataType:"xml",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(c){b(c).find("lp1\\:getlastmodified, getlastmodified").each(function(){d.last_modified=b(this).text()}),b(c).find("lp1\\:creationdate, creationdate").each(function(){d.creation_date=b(this).text()}),a.getOption("metadata_only")?e.success(d):f()},error:function(b){b.message='Cannot load "'+a.getPath()+'" informations from DAVStorage.',b.status===404?e.error(b):e.retry(b)}})},e.getDocumentList=function(a){vard=[],f={},h=[],i=g.newAsyncModule(),j={};j.getContent=function(a){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.name,type:"GET",async:!0,dataType:"text",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(b){a.content=b,d.push(a),i.call(j,"success")},error:function(a){a.message="Cannot get a document content from DAVStorage.",i.call(j,"error",[a])}})},j.getDocumentList=function(){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/",async:!0,type:"PROPFIND",dataType:"xml",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password),Depth:"1"},success:function(c){vare=b(c).find("D\\:response, response"),g=e.length;if(g===1)returni.call(j,"success");i.wait(j,"success",g-2),e.each(function(c,e){if(c>0){f={},b(e).find("D\\:href, href").each(function(){h=b(this).text().split("/"),f.name=h[h.length-1]?h[h.length-1]:h[h.length-2]+"/"});if(f.name===".htaccess"||f.name===".htpasswd")return;b(e).find("lp1\\:getlastmodified, getlastmodified").each(function(){f.last_modified=b(this).text()}),b(e).find("lp1\\:creationdate, creationdate").each(function(){f.creation_date=b(this).text()}),a.getOption("metadata_only")?(d.push(f),i.call(j,"success")):i.call(j,"getContent",[f])}})},error:function(a){a.message="Cannot get a document list from DAVStorage.",a.status===404?i.call(j,"error",[a]):i.call(j,"retry",[a])}})},j.retry=function(a){i.neverCall(j,"retry"),i.neverCall(j,"success"),i.neverCall(j,"error"),e.retry(a)},j.error=function(a){i.neverCall(j,"retry"),i.neverCall(j,"success"),i.neverCall(j,"error"),e.error(a)},j.success=function(){i.neverCall(j,"retry"),i.neverCall(j,"success"),i.neverCall(j,"error"),e.success(d)},i.call(j,"getDocumentList")},e.removeDocument=function(a){b.ajax({url:g.url+"/dav/"+g.username+"/"+g.applicationname+"/"+a.getPath(),type:"DELETE",async:!0,headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(){e.success()},error:function(a){a.status===404?e.success():(a.message='Cannot remove "'+e.getFileName()+'" from DAVStorage.',e.retry(a))}})},e};f.addStorageType("dav",h);vari=function(a,b){varc=f.storage(a,b,"handler"),d={};d.return_value_array=[],d.storagelist=a.storagelist||[],d.nb_storage=d.storagelist.length;vare=c.serialized;returnc.serialized=function(){vara=e();returna.storagelist=d.storagelist,a},c.validateState=function(){returnd.storagelist.length===0?'Need at least one parameter: "storagelist" containing at least one storage.':""},d.isTheLast=function(){returnd.return_value_array.length===d.nb_storage},d.doJob=function(a,b){vare=!1,f=[],g,h=function(a){d.return_value_array.push(a),e||(f.push(a),d.isTheLast()&&c.error({status:207,statusText:"Multi-Status",message:b,array:f}))},i=function(a){d.return_value_array.push(a),e||(e=!0,c.success(a))};for(g=0;g<d.nb_storage;g+=1){varj=a.clone(),k=c.newStorage(d.storagelist[g]);j.onErrorDo(h),j.onSuccessDo(i),c.addJob(k,j)}},c.saveDocument=function(a){d.doJob(a,'All save "'+a.getPath()+'" requests have failed.'),c.end()},c.loadDocument=function(a){d.doJob(a,'All load "'+a.getPath()+'" requests have failed.'),c.end()},c.getDocumentList=function(a){d.doJob(a,"All get document list requests have failed."),c.end()},c.removeDocument=function(a){d.doJob(a,'All remove "'+a.getPath()+'" requests have failed.'),c.end()},c};f.addStorageType("replicate",i);varj=function(b,c){vard=f.storage(b,c,"handler"),e={},g=b.storage||!1;e.secondstorage_spec=b.storage||{type:"base"},e.secondstorage_string=JSON.stringify(e.secondstorage_spec);varh="jio/indexed_storage_array",i="jio/indexed_file_array/"+e.secondstorage_string,j=d.serialized;returnd.serialized=function(){vara=j();returna.storage=e.secondstorage_spec,a},d.validateState=function(){returng?"":'Need at least one parameter: "storage" containing storage specifications.'},e.isStorageArrayIndexed=function(){returna.getItem(h)?!0:!1},e.getIndexedStorageArray=function(){returna.getItem(h)||[]},e.indexStorage=function(b){varc=e.getIndexedStorageArray();c.push(typeofb=="string"?b:JSON.stringify(b)),a.setItem(h,c)},e.isAnIndexedStorage=function(a){varb=typeofa=="string"?a:JSON.stringify(a),c,d,f=e.getIndexedStorageArray();for(c=0,d=f.length;c<d;c+=1)if(JSON.stringify(f[c])===b)return!0;return!1},e.fileArrayExists=function(){returna.getItem(i)?!0:!1},e.getFileArray=function(){returna.getItem(i)||[]},e.setFileArray=function(b){returna.setItem(i,b)},e.isFileIndexed=function(a){varb,c,d=e.getFileArray();for(b=0,c=d.length;b<c;b+=1)if(d[b].name===a)return!0;return!1},e.addFile=function(b){varc=e.getFileArray();c.push(b),a.setItem(i,c)},e.removeFile=function(b){varc,d,f=e.getFileArray(),g=[];for(c=0,d=f.length;c<d;c+=1)f[c].name!==b&&g.push(f[c]);a.setItem(i,g)},e.update=function(){vara=function(a){e.isAnIndexedStorage(e.secondstorage_string)||e.indexStorage(e.secondstorage_string),e.setFileArray(a)};d.addJob(d.newStorage(e.secondstorage_spec),d.newCommand("getDocumentList",{path:".",option:{success:a,max_retry:3}}))},d.saveDocument=function(a){varb=a.clone();b.onSuccessDo(function(b){e.isFileIndexed(a.getPath())||e.addFile({name:a.getPath(),last_modified:0,creation_date:0}),e.update(),d.success()}),b.onErrorDo(function(a){d.error(a)}),d.addJob(d.newStorage(e.secondstorage_spec),b)},d.loadDocument=function(a){varb,c,f,g,h=function(a){d.success(a)},i=function(a){d.error(a)},j=function(){varb=a.clone();b.onErrorDo(i),b.onSuccessDo(h),d.addJob(d.newStorage(e.secondstorage_spec),b)};e.update(),a.getOption("metadata_only")?setTimeout(function(){if(e.fileArrayExists()){b=e.getFileArray();for(c=0,f=b.length;c<f;c+=1)if(b[c].name===a.getPath())returnd.success(b[c])}elsej()},100):j()},d.getDocumentList=function(a){varb,c,f=!1;e.update(),a.getOption("metadata_only")?(b=setInterval(function(){f&&(d.error({status:0,statusText:"Timeout",message:"The request has timed out."}),clearInterval(b)),e.fileArrayExists()&&(d.success(e.getFileArray()),clearInterval(b))},100),setTimeout(function(){f=!0},1e4)):(c=a.clone(),c.onSuccessDo(function(a){d.success(a)}),c.onErrorDo(function(a){d.error(a)}),d.addJob(d.newStorage(e.secondstorage_spec),c))},d.removeDocument=function(a){varb=a.clone();b.onSuccessDo(function(b){e.removeFile(a.getPath()),e.update(),d.success()}),b.onErrorDo(function(a){d.error(a)}),d.addJob(d.newStorage(e.secondstorage_spec),b)},d};f.addStorageType("indexed",j);vark=function(a,c){vare=f.storage(a,c,"handler"),g={},h=a.storage?!0:!1;g.username=a.username||"",g.password=a.password||"",g.secondstorage_spec=a.storage||{type:"base"},g.secondstorage_string=JSON.stringify(g.secondstorage_string);vari=e.serialized;returne.serialized=function(){vara=i();returna.username=g.username,a.password=g.password,a.storage=g.secondstorage_string,a},e.validateState=function(){returng.username&&h?"":'Need at least two parameters: "username" and "storage".'},g.encrypt_param_object={iv:"kaprWwY/Ucr7pumXoTHbpA",v:1,iter:1e3,ks:256,ts:128,mode:"ccm",adata:"",cipher:"aes",salt:"K4bmZG9d704"},g.decrypt_param_object={iv:"kaprWwY/Ucr7pumXoTHbpA",ks:256,ts:128,salt:"K4bmZG9d704"},g.encrypt=function(a,b){varc=d.encrypt(g.username+":"+g.password,a,g.encrypt_param_object);b(JSON.parse(c).ct)},g.decrypt=function(a,c){vare,f=b.extend(!0,{},g.decrypt_param_object);f.ct=a||"",f=JSON.stringify(f);try{e=d.decrypt(g.username+":"+g.password,f)}catch(h){c({status:0,statusText:"Decrypt Fail",message:"Unable to decrypt."});return}c(undefined,e)},g.newAsyncModule=function(){vara={};returna.call=function(a,b,c){a._wait=a._wait||{};if(a._wait[b])returna._wait[b]--,function(){};c=c||[],setTimeout(function(){a[b].apply(a[b],c)})},a.neverCall=function(a,b){a._wait=a._wait||{},a._wait[b]=-1},a.wait=function(a,b,c){a._wait=a._wait||{},a._wait[b]=c},a.end=function(){a.call=function(){}},a},e.saveDocument=function(a){varb,c,d=g.newAsyncModule(),f={};f.encryptFilePath=function(){g.encrypt(a.getPath(),function(a){b=a,d.call(f,"save")})},f.encryptFileContent=function(){g.encrypt(a.getContent(),function(a){c=a,d.call(f,"save")})},f.save=function(){vard=a.cloneOption(),f;d.success=function(){e.success()},d.error=function(a){e.error(a)},f=e.newCommand("saveDocument",{path:b,content:c,option:d}),e.addJob(e.newStorage(g.secondstorage_spec),f)},d.wait(f,"save",1),d.call(f,"encryptFilePath"),d.call(f,"encryptFileContent")},e.loadDocument=function(a){varb,c,d=g.newAsyncModule(),f={};f.encryptFilePath=function(){g.encrypt(a.getPath(),function(a){b=a,d.call(f,"loadDocument")})},f.loadDocument=function(){varc=a.cloneOption(),d;c.error=f.loadOnError,c.success=f.loadOnSuccess,d=e.newCommand("loadDocument",{path:b,option:c}),e.addJob(e.newStorage(g.secondstorage_spec),d)},f.loadOnSuccess=function(b){b.name=a.getPath(),a.getOption("metadata_only")?e.success(b):g.decrypt(b.content,function(a,c){a?e.error(a):(b.content=c,e.success(b))})},f.loadOnError=function(a){e.error(a)},d.call(f,"encryptFilePath")},e.getDocumentList=function(a){varb=[],c=g.newAsyncModule(),d={};d.getDocumentList=function(){varb=a.cloneOption();b.success=d.getListOnSuccess,b.error=d.getListOnError,e.addJob(e.newStorage(g.secondstorage_spec),e.newCommand("getDocumentList",{path:a.getPath(),option:b}))},d.getListOnSuccess=function(e){b=e;varf,h=function(e){g.decrypt(b[e].name,function(a,b){a?c.call(d,"error",[a]):c.call(d,"pushResult",[b,e,"name"])}),a.getOption("metadata_only")||g.decrypt(b[e].content,function(a,b){a?c.call(d,"error",[a]):c.call(d,"pushResult",[b,e,"content"])})};a.getOption("metadata_only")?c.wait(d,"success",e.length-1):c.wait(d,"success",e.length*2-1);for(f=0;f<b.length;f+=1)h(f)},d.getListOnError=function(a){c.call(d,"error",[a])},d.pushResult=function(a,e,f){b[e][f]=a,c.call(d,"success")},d.error=function(a){c.end(),e.error(a)},d.success=function(){c.end(),e.success(b)},c.call(d,"getDocumentList")},e.removeDocument=function(a){varb,c={};c.encryptFilePath=function(){g.encrypt(a.getPath(),function(a){b=a,c.removeDocument()})},c.removeDocument=function(){vard=a.cloneOption();d.error=c.removeOnError,d.success=c.removeOnSuccess,e.addJob(e.newStorage(g.secondstorage_spec),e.newCommand("removeDocument",{path:b,option:d}))},c.removeOnSuccess=function(a){e.success()},c.removeOnError=function(a){e.error(a)},c.encryptFilePath()},e};f.addStorageType("crypt",k);varl=function(a,b){varc=f.storage(a,b,"handler"),d={};a=a||{},b=b||{};varg=a.storage?!0:!1;d.secondstorage_spec=a.storage||{type:"base"},d.secondstorage_string=JSON.stringify(d.secondstorage_spec);varh="jio/conflictmanager/"+d.secondstorage_string+"/",i=function(){},j=c.serialized;returnc.serialized=function(){vara=j();returna.storage=d.secondstorage_spec,a},c.validateState=function(){returng?"":'Need at least one parameter: "storage".'},d.getDistantMetadata=function(a,b,e,f){varg=a.cloneOption();g.metadata_only=!1,g.max_retry=a.getOption("max_retry")||3,g.error=f,g.success=e;varh=c.newCommand("loadDocument",{path:b,option:g});c.addJob(c.newStorage(d.secondstorage_spec),h)},d.saveMetadataToDistant=function(a,b,e,f,g){varh=a.cloneOption();h.error=g,h.success=f;vari=c.newCommand("saveDocument",{path:b,content:JSON.stringify(e),option:h});c.addJob(c.newStorage(d.secondstorage_spec),i)},d.saveNewRevision=function(a,b,e,f,g){varh=a.cloneOption();h.error=g,h.success=f;vari=c.newCommand("saveDocument",{path:b,content:e,option:h});c.addJob(c.newStorage(d.secondstorage_spec),i)},d.loadRevision=function(a,b,e,f){varg=a.cloneOption();g.error=f,g.success=e;varh=c.newCommand("loadDocument",{path:b,option:g});c.addJob(c.newStorage(d.secondstorage_spec),h)},d.deleteAFile=function(a,b,e,f){varg=a.cloneOption();g.max_retry=0,g.error=f,g.success=e;varh=c.newCommand("removeDocument",{path:b,option:g});c.addJob(c.newStorage(d.secondstorage_spec),h)},d.chooseARevision=function(a){varb=0,c="";for(vardina)b<a[d].last_modified&&(b=a[d].last_modified,c=d);returnc},d.solveConflict=function(a,b,c){varf={},g=d.newAsyncModule(),h=c.command,j=a+".metadata",k="",l="",m=null,n=!1,o={},p=c.deleted,q=c.previous_revision,r=c.revision_remove_object||{},s=r[q]||{},t=newDate,u;f.getDistantMetadata=function(){d.getDistantMetadata(h,j,function(c){vard=parseInt(q.split("-")[0],10);m=JSON.parse(c.content),k=d+1+"-"+e(""+b+q+JSON.stringify(m)),l=a+"."+k,p||(g.wait(f,"saveMetadataOnDistant",1),g.call(f,"saveNewRevision")),g.call(f,"previousUpdateMetadata")},function(a){g.call(f,"error",[a])})},f.saveNewRevision=function(){d.saveNewRevision(h,l,b,function(a){g.call(f,"saveMetadataOnDistant")},function(a){g.call(f,"error",[a])})},f.previousUpdateMetadata=function(){for(varainr)deletem[a];g.call(f,"checkForConflicts")},f.checkForConflicts=function(){for(varainm){varb;n=!0,u={status:20,statusText:"Conflict",message:"There is one or more conflicts"};break}g.call(f,"updateMetadata")},f.updateMetadata=function(){m[k]={creation_date:s.creation_date||t.getTime(),last_modified:t.getTime(),conflict:n,deleted:p},o=d.createConflictObject(h,m,k),g.call(f,"saveMetadataOnDistant")},f.saveMetadataOnDistant=function(){d.saveMetadataToDistant(h,j,m,function(a){g.call(f,"deleteAllConflictingRevision"),n?g.call(f,"error"):g.call(f,"success")},function(a){g.call(f,"error",[a])})},f.deleteAllConflictingRevision=function(){for(varbinr)d.deleteAFile(h,a+"."+b,i,i)},f.success=function(){g.neverCall(f,"error"),g.neverCall(f,"success"),c.success&&c.success({revision:k})},f.error=function(a){varb=a||u||{};n&&(b.conflict_object=o),g.neverCall(f,"error"),g.neverCall(f,"success"),c.error&&c.error(b)},g.call(f,"getDistantMetadata")},d.createConflictObject=function(a,b,c){vare=a.clone(),f={path:a.getPath(),revision:c,revision_object:b,getConflictRevisionList:function(){returnthis.revision_object},solveConflict:function(a,b){returntypeofa=="undefined"?(b=b||{},b.deleted=!0):typeofa=="object"?(b=a,b.deleted=!0):(b=b||{},b.deleted=!1,a=a||""),b.previous_revision=this.revision,b.revision_remove_object=this.revision_object,b.command=e,d.solveConflict(this.path,a,b)}};returnf},d.newAsyncModule=function(){vara={};returna.call=function(a,b,c){a._wait=a._wait||{};if(a._wait[b])returna._wait[b]--,i;c=c||[],setTimeout(function(){a[b].apply(a[b],c)})},a.neverCall=function(a,b){a._wait=a._wait||{},a._wait[b]=-1},a.wait=function(a,b,c){a._wait=a._wait||{},a._wait[b]=c},a.end=function(){a.call=i},a},c.saveDocument=function(a){varb={},f=d.newAsyncModule(),g=a.getPath()+".metadata",h="",j="",k=null,l=!1,m={},n=a.getOption("previous_revision"),o=a.getPath()+"."+n,p=newDate,q;if(!n)returnsetTimeout(function(){c.error({status:0,statusText:"Parameter missing",message:"Need a previous revision."})});b.getDistantMetadata=function(){d.getDistantMetadata(a,g,function(c){vard=parseInt(n.split("-")[0],10);k=JSON.parse(c.content),h=d+1+"-"+e(""+a.getContent()+n+JSON.stringify(k)),j=a.getPath()+"."+h,f.wait(b,"saveMetadataOnDistant",1),f.call(b,"saveNewRevision"),f.call(b,"checkForConflicts")},function(c){c.status===404?(h="1-"+e(a.getContent()),j=a.getPath()+"."+h,f.wait(b,"saveMetadataOnDistant",1),f.call(b,"saveNewRevision"),f.call(b,"createMetadata")):f.call(b,"error",[c])})},b.saveNewRevision=function(){d.saveNewRevision(a,j,a.getContent(),function(a){f.call(b,"saveMetadataOnDistant")},function(a){f.call(b,"error",[a])})},b.checkForConflicts=function(){for(varaink)if(a!==n){l=!0,q={status:20,statusText:"Conflict",message:"There is one or more conflicts"};break}f.call(b,"updateMetadata")},b.createMetadata=function(){k={},k[h]={creation_date:p.getTime(),last_modified:p.getTime(),conflict:!1,deleted:!1},f.call(b,"saveMetadataOnDistant")},b.updateMetadata=function(){varc;k[n]&&(c=k[n].creation_date,deletek[n]),k[h]={creation_date:c||p.getTime(),last_modified:p.getTime(),conflict:l,deleted:!1},l&&(m=d.createConflictObject(a,k,h)),f.call(b,"saveMetadataOnDistant")},b.saveMetadataOnDistant=function(){d.saveMetadataToDistant(a,g,k,function(a){f.call(b,"deletePreviousRevision"),l?f.call(b,"error"):f.call(b,"success")},function(a){f.call(b,"error",[a])})},b.deletePreviousRevision=function(){n!=="0"&&d.deleteAFile(a,o,i,i)},b.success=function(){f.neverCall(b,"error"),f.neverCall(b,"success"),c.success({revision:h})},b.error=function(a){vard=a||q||{status:0,statusText:"Unknown",message:"Unknown error."};l&&(d.conflict_object=m),f.neverCall(b,"error"),f.neverCall(b,"success"),c.error(d)},f.call(b,"getDistantMetadata")},c.loadDocument=function(a){varb={},e=d.newAsyncModule(),f=a.getPath()+".metadata",g=a.getOption("revision")||"",h=null,i=a.getOption("metadata_only"),j=!1,k={},l=newDate,m={name:a.getPath()},n=function(a){e.call(b,"error",[{status:404,statusText:"Not Found",message:a}])};b.getDistantMetadata=function(){d.getDistantMetadata(a,f,function(a){h=JSON.parse(a.content),i||e.wait(b,"success",1),e.call(b,"affectMetadata"),e.call(b,"checkForConflicts")},function(a){e.call(b,"error",[a])})},b.affectMetadata=function(){if(g){if(!h[g])returnn("Document revision does not exists.")}elseg=d.chooseARevision(h);m.last_modified=h[g].last_modified,m.creation_date=h[g].creation_date,m.revision=g,m.revision_object=h,i?e.call(b,"success"):e.call(b,"loadRevision")},b.loadRevision=function(){if(!g||h[g].deleted)returnn("Document has been removed.");d.loadRevision(a,m.name+"."+g,function(a){m.content=a.content,e.call(b,"success")},function(a){e.call(b,"error",[a])})},b.checkForConflicts=function(){h[g].conflict&&(j=!0,k=d.createConflictObject(a,h,g),m.conflict_object=k),e.call(b,"success")},b.success=function(){e.neverCall(b,"error"),e.neverCall(b,"success"),c.success(m)},b.error=function(a){vard=a||{status:0,statusText:"Unknown",message:"Unknown error."};j&&(d.conflict_object=k),e.neverCall(b,"error"),e.neverCall(b,"success"),c.error(d)},e.call(b,"getDistantMetadata")},c.getDocumentList=function(a){varb={},e=d.newAsyncModule(),f=a.getOption("metadata_only"),g=[],h=0,i=0,j=0;b.retreiveList=function(){varf=a.cloneOption();f.metadata_only=!0,f.error=function(a){e.call(b,"error",[a])},f.success=function(a){e.call(b,"filterTheList",[a])};varg=c.newCommand("getDocumentList",{path:a.getPath(),option:f});c.addJob(c.newStorage(d.secondstorage_spec),g)},b.filterTheList=function(a){varc;j++;for(c=0;c<a.length;c+=1){vard=a[c].name.split(".")||[];d.length>0&&d[d.length-1]==="metadata"&&(j++,d.length--,e.call(b,"loadMetadataFile",[d.join(".")]))}e.call(b,"success")},b.loadMetadataFile=function(c){d.getDistantMetadata(a,c+".metadata",function(a){a=JSON.parse(a.content);varf=d.chooseARevision(a);a[f].deleted?e.call(b,"success"):e.call(b,"loadFile",[c,f,a])},function(a){e.call(b,"error",[a])})},b.loadFile=function(c,h,i){varj={name:c,last_modified:i[h].last_modified,creation_date:i[h].creation_date,revision:h,revision_object:i};i[h].conflict&&(j.conflict_object=d.createConflictObject(a,i,h)),f?(g.push(j),e.call(b,"success")):d.loadRevision(a,c+"."+h,function(a){j.content=a.content,g.push(j),e.call(b,"success")},function(a){e.call(b,"error",[a])})},b.success=function(){i++,i>=j&&(e.end(),c.success(g))},b.error=function(a){e.end(),c.error(a)},e.call(b,"retreiveList")},c.removeDocument=function(a){varb={},f=d.newAsyncModule(),g=a.getPath()+".metadata",h="",j="",k=null,l=!1,m={},n=a.getOption("revision"),o=a.getPath()+"."+n,p=newDate,q;if(!n)returnsetTimeout(function(){c.error({status:0,statusText:"Parameter missing",message:"Need a revision."})});b.getDistantMetadata=function(){d.getDistantMetadata(a,g,function(c){k=JSON.parse(c.content),n==="last"&&(n=d.chooseARevision(k),o=a.getPath()+"."+n);varg=parseInt(n.split("-")[0],10);h=g+1+"-"+e(""+n+JSON.stringify(k)),j=a.getPath()+"."+h,f.call(b,"checkForConflicts")},function(a){a.status===404?f.call(b,"success",["0"]):f.call(b,"error",[a])})},b.checkForConflicts=function(){for(varaink)if(a!==n){l=!0,q={status:20,statusText:"Conflict",message:"There is one or more conflicts"};break}f.call(b,"updateMetadata")},b.updateMetadata=function(){varc;k[n]&&(c=k[n].creation_date,deletek[n]),k[h]={creation_date:c||p.getTime(),last_modified:p.getTime(),conflict:l,deleted:!0},l&&(m=d.createConflictObject(a,k,h)),f.call(b,"saveMetadataOnDistant")},b.saveMetadataOnDistant=function(){d.saveMetadataToDistant(a,g,k,function(a){f.call(b,"deletePreviousRevision"),l?f.call(b,"error"):f.call(b,"success")},function(a){f.call(b,"error",[a])})},b.deletePreviousRevision=function(){n!=="0"&&d.deleteAFile(a,o,i,i)},b.success=function(a){f.neverCall(b,"error"),f.neverCall(b,"success"),c.success({revision:a||h})},b.error=function(a){vard=a||q||{status:0,statusText:"Unknown",message:"Unknown error."};l&&(d.conflict_object=m),f.neverCall(b,"error"),f.neverCall(b,"success"),c.error(d)},f.call(b,"getDistantMetadata")},c};f.addStorageType("conflictmanager",l)})(LocalOrCookieStorage,jQuery,Base64,sjcl,hex_sha256,jio);
(function(a,b,c,d,e,f){varg=function(b,c){vard=f.storage(b,c,"base"),e={};e.secureDocId=function(a){varb=a.split("/"),c;b[0]===""&&(b=b.slice(1));for(c=0;c<b.length;c+=1)if(b[c]==="")return"";returnb.join("%2F")},e.convertSlashes=function(a){returna.split("/").join("%2F")},e.restoreSlashes=function(a){returna.split("%2F").join("/")},e.username=b.username||"",e.secured_username=e.convertSlashes(e.username),e.applicationname=b.applicationname||"untitled",e.secured_applicationname=e.convertSlashes(e.applicationname);varg="jio/local_user_array",h="jio/local_file_name_array/"+e.secured_username+"/"+e.secured_applicationname,i=d.serialized;returnd.serialized=function(){vara=i();returna.applicationname=e.applicationname,a.username=e.username,a},d.validateState=function(){returne.secured_username?"":'Need at least one parameter: "username".'},e.getUserArray=function(){returna.getItem(g)||[]},e.addUser=function(b){varc=e.getUserArray();c.push(b),a.setItem(g,c)},e.userExists=function(a){varb=e.getUserArray(),c,d;for(c=0,d=b.length;c<d;c+=1)if(b[c]===a)return!0;return!1},e.getFileNameArray=function(){returna.getItem(h)||[]},e.addFileName=function(b){varc=e.getFileNameArray();c.push(b),a.setItem(h,c)},e.removeFileName=function(b){varc,d,f=e.getFileNameArray(),g=[];for(c=0,d=f.length;c<d;c+=1)f[c]!==b&&g.push(f[c]);a.setItem(h,g)},e.checkSecuredDocId=function(a,b,c){returna?!0:(d.error({status:403,statusText:"Method Not Allowed",error:"method_not_allowed",message:"Cannot "+c+' "'+b+'", file name is incorrect.',reason:"Cannot "+c+' "'+b+'", file name is incorrect'}),!1)},d.post=function(a){d.put(a)},d.put=function(b){setTimeout(function(){varc=e.secureDocId(b.getDocId()),f=null,g="jio/local/"+e.secured_username+"/"+e.secured_applicationname+"/"+c;if(!e.checkSecuredDocId(c,b.getDocId(),"put"))return;f=a.getItem(g),f?(f.content=b.getDocContent(),f._last_modified=Date.now()):(f={_id:b.getDocId(),content:b.getDocContent(),_creation_date:Date.now(),_last_modified:Date.now()},e.userExists(e.secured_username)||e.addUser(e.secured_username),e.addFileName(c)),a.setItem(g,f),d.success({ok:!0,id:b.getDocId()})})},d.get=function(b){setTimeout(function(){varc=e.secureDocId(b.getDocId()),f=null;if(!e.checkSecuredDocId(c,b.getDocId(),"get"))return;f=a.getItem("jio/local/"+e.secured_username+"/"+e.secured_applicationname+"/"+c),f?(b.getOption("metadata_only")&&deletef.content,d.success(f)):d.error({status:404,statusText:"Not Found.",error:"not_found",message:'Document "'+b.getDocId()+'" not found.',reason:"missing"})})},d.allDocs=function(b){setTimeout(function(){varc=[],f=[],g,h,i="key",j="jio/local/"+e.secured_username+"/"+e.secured_applicationname,k={};f=e.getFileNameArray();for(g=0,h=f.length;g<h;g+=1)k=a.getItem(j+"/"+f[g]),k&&(b.getOption("metadata_only")?c.push({id:k._id,key:k._id,value:{_creation_date:k._creation_date,_last_modified:k._last_modified}}):c.push({id:k._id,key:k._id,value:{content:k.content,_creation_date:k._creation_date,_last_modified:k._last_modified}}));d.success({total_rows:c.length,rows:c})})},d.remove=function(b){setTimeout(function(){varc=e.secureDocId(b.getDocId()),f="jio/local/"+e.secured_username+"/"+e.secured_applicationname+"/"+c;if(!e.checkSecuredDocId(c,b.getDocId(),"remove"))return;a.deleteItem(f),e.removeFileName(c),d.success({ok:!0,id:b.getDocId()})})},d};f.addStorageType("local",g);varh=function(a,d){vare=f.storage(a,d,"base"),g={};g.secureDocId=function(a){varb=a.split("/"),c;b[0]===""&&(b=b.slice(1));for(c=0;c<b.length;c+=1)if(b[c]==="")return"";returnb.join("%2F")},g.convertSlashes=function(a){returna.split("/").join("%2F")},g.restoreSlashes=function(a){returna.split("%2F").join("/")},g.username=a.username||"",g.secured_username=g.convertSlashes(g.username),g.applicationname=a.applicationname||"untitled",g.secured_applicationname=g.convertSlashes(g.applicationname),g.url=a.url||"",g.password=a.password||"";varh=e.serialized;returne.serialized=function(){vara=h();returna.username=g.username,a.applicationname=g.applicationname,a.url=g.url,a.password=g.password,a},e.validateState=function(){returng.secured_username&&g.url?"":'Need at least 2 parameters: "username" and "url".'},g.newAsyncModule=function(){vara={};returna.call=function(a,b,c){returna._wait=a._wait||{},a._wait[b]?(a._wait[b]--,function(){}):(c=c||[],a[b].apply(a[b],c))},a.neverCall=function(a,b){a._wait=a._wait||{},a._wait[b]=-1},a.wait=function(a,b,c){a._wait=a._wait||{},a._wait[b]=c},a.end=function(){a.call=function(){}},a},e.post=function(a){e.put(a)},e.put=function(a){vard=g.secureDocId(a.getDocId());b.ajax({url:g.url+"/"+g.secured_username+"/"+g.secured_applicationname+"/"+d,type:"PUT",data:a.getDocContent(),async:!0,dataType:"text",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(){e.success({ok:!0,id:a.getDocId()})},error:function(b){b.error=b.statusText,b.reason='Cannot save "'+a.getDocId()+'"',b.message=b.reason+".",e.retry(b)}})},e.get=function(a){vard=g.secureDocId(a.getDocId()),f={},h=function(){b.ajax({url:g.url+"/"+g.secured_username+"/"+g.secured_applicationname+"/"+d,type:"GET",async:!0,dataType:"text",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(a){f.content=a,e.success(f)},error:function(b){b.error=b.statusText,b.status===404?(b.message='Document "'+a.getDocId()+'" not found.',b.reason="missing",e.error(b)):(b.reason='An error occured when trying to get "'+a.getDocId()+'"',b.message=b.reason+".",e.retry(b))}})};f._id=a.getDocId(),b.ajax({url:g.url+"/"+g.secured_username+"/"+g.secured_applicationname+"/"+d,type:"PROPFIND",async:!0,dataType:"xml",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(c){b(c).find("lp1\\:getlastmodified, getlastmodified").each(function(){f._last_modified=(newDate(b(this).text())).getTime()}),b(c).find("lp1\\:creationdate, creationdate").each(function(){f._creation_date=(newDate(b(this).text())).getTime()}),a.getOption("metadata_only")?e.success(f):h()},error:function(b){b.status===404?(b.message='Cannot find "'+a.getDocId()+'" informations.',b.reason="missing",e.error(b)):(b.reason='Cannot get "'+a.getDocId()+'" informations',b.message=b.reason+".",e.retry(b))}})},e.allDocs=function(a){vard=[],f=g.newAsyncModule(),h={};h.getContent=function(a){b.ajax({url:g.url+"/"+g.secured_username+"/"+g.secured_applicationname+"/"+g.secureDocId(a.id),type:"GET",async:!0,dataType:"text",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(b){a.value.content=b,d.push(a),f.call(h,"success")},error:function(a){a.error=a.statusText,a.reason="Cannot get a document content from DAVStorage",a.message=a.message+".",f.call(h,"error",[a])}})},h.getDocumentList=function(){b.ajax({url:g.url+"/"+g.secured_username+"/"+g.secured_applicationname+"/",async:!0,type:"PROPFIND",dataType:"xml",headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password),Depth:"1"},success:function(c){vare=b(c).find("D\\:response, response"),i=e.length;if(i===1)returnf.call(h,"success");f.wait(h,"success",i-2),e.each(function(c,e){if(c>0){vari={value:{}};b(e).find("D\\:href, href").each(function(){vara=b(this).text().split("/");i.id=a[a.length-1],i.id=g.restoreSlashes(i.id),i.key=i.id});if(i.id===".htaccess"||i.id===".htpasswd")return;b(e).find("lp1\\:getlastmodified, getlastmodified").each(function(){i.value._last_modified=(newDate(b(this).text())).getTime()}),b(e).find("lp1\\:creationdate, creationdate").each(function(){i.value._creation_date=(newDate(b(this).text())).getTime()}),a.getOption("metadata_only")?(d.push(i),f.call(h,"success")):f.call(h,"getContent",[i])}})},error:function(a){a.status===404?(a.error="not_found",a.reason="missing",f.call(h,"error",[a])):(a.error=a.statusText,a.reason="Cannot get a document list from DAVStorage",a.message=a.reason+".",f.call(h,"retry",[a]))}})},h.retry=function(a){f.neverCall(h,"retry"),f.neverCall(h,"success"),f.neverCall(h,"error"),e.retry(a)},h.error=function(a){f.neverCall(h,"retry"),f.neverCall(h,"success"),f.neverCall(h,"error"),e.error(a)},h.success=function(){f.neverCall(h,"retry"),f.neverCall(h,"success"),f.neverCall(h,"error"),e.success({total_rows:d.length,rows:d})},f.call(h,"getDocumentList")},e.remove=function(a){vard=g.secureDocId(a.getDocId());b.ajax({url:g.url+"/"+g.secured_username+"/"+g.secured_applicationname+"/"+d,type:"DELETE",async:!0,headers:{Authorization:"Basic "+c.encode(g.username+":"+g.password)},success:function(b,c,d){e.success({ok:!0,id:a.getDocId()})},error:function(a,b,c){a.status===404?(a.error="not_found",a.reason="missing",a.message="Cannot remove missing file.",e.error(a)):(a.reason='Cannot remove "'+e.getDocId()+'"',a.message=a.reason+".",e.retry(a))}})},e};f.addStorageType("dav",h);vari=function(a,b){varc=f.storage(a,b,"handler"),d={};d.return_value_array=[],d.storagelist=a.storagelist||[],d.nb_storage=d.storagelist.length;vare=c.serialized;returnc.serialized=function(){vara=e();returna.storagelist=d.storagelist,a},c.validateState=function(){returnd.storagelist.length===0?'Need at least one parameter: "storagelist" containing at least one storage.':""},d.isTheLast=function(a){returna.length===d.nb_storage},d.doJob=function(a,b,e){varf=!1,g=[],h,i=function(h){f||(g.push(h),d.isTheLast(g)&&c.error({status:207,statusText:"Multi-Status",error:"multi_status",message:"All "+b+(e?"":' "'+a.getDocId()+'"')+" requests have failed.",reason:"requests fail",array:g}))},j=function(a){f||(f=!0,c.success(a))};for(h=0;h<d.nb_storage;h+=1){vark=a.cloneOption();c.addJob(a.getLabel(),d.storagelist[h],a.cloneDoc(),k,j,i)}},c.post=function(a){d.doJob(a,"post"),c.end()},c.put=function(a){d.doJob(a,"put"),c.end()},c.get=function(a){d.doJob(a,"get"),c.end()},c.allDocs=function(a){d.doJob(a,"allDocs",!0),c.end()},c.remove=function(a){d.doJob(a,"remove"),c.end()},c};f.addStorageType("replicate",i);varj=function(b,c){vard=f.storage(b,c,"handler"),e={},g=b.storage||!1;e.secondstorage_spec=b.storage||{type:"base"},e.secondstorage_string=JSON.stringify(e.secondstorage_spec);varh="jio/indexed_storage_object",i="jio/indexed_file_object/"+e.secondstorage_string,j=d.serialized;returnd.serialized=function(){vara=j();returna.storage=e.secondstorage_spec,a},d.validateState=function(){returng?"":'Need at least one parameter: "storage" containing storage specifications.'},e.secureDocId=function(a){varb=a.split("/"),c;b[0]===""&&(b=b.slice(1));for(c=0;c<b.length;c+=1)if(b[c]==="")return"";returnb.join("%2F")},e.indexStorage=function(){varb=a.getItem(h)||{};b[e.secondstorage_spec]=(newDate).getTime(),a.setItem(h,b)},e.formatToFileObject=function(a){varb,c={_id:a.id};for(bina.value)c[b]=a.value[b];returnc},e.allDocs=function(a){varb,c={rows:[]},d=0;for(bina)c.rows[d]={},c.rows[d].value=a[b],c.rows[d].id=c.rows[d].key=c.rows[d].value._id,deletec.rows[d].value._id,d++;returnc.total_rows=c.rows.length,c},e.setFileArray=function(b){varc,d={};for(c=0;c<b.length;c+=1)d[b[c].id]=e.formatToFileObject(b[c]);a.setItem(i,d)},e.getFileObject=function(b){varc=a.getItem(i)||{};returnc[b]},e.addFile=function(b){varc=a.getItem(i)||{};c[b._id]=b,a.setItem(i,c)},e.removeFile=function(b){varc=a.getItem(i)||{};deletec[b],a.setItem(i,c)},e.update=function(){vara=function(a){e.setFileArray(a.rows)};d.addJob("allDocs",e.secondstorage_spec,null,{max_retry:3},a,function(){})},d.post=function(a){d.put(a)},d.put=function(a){varb=a.cloneDoc(),c=a.cloneOption(),f=function(a){e.update(),d.success(a)},g=function(a){d.error(a)};e.indexStorage(),d.addJob("put",e.secondstorage_spec,b,c,f,g)},d.get=function(a){varb,c=function(a){d.success(a)},f=function(a){d.error(a)},g=function(){varb=a.cloneOption();d.addJob("get",e.secondstorage_spec,a.cloneDoc(),b,c,f),d.end()};e.indexStorage(),e.update(),a.getOption("metadata_only")?setTimeout(function(){varb=e.getFileObject(a.getDocId());b&&(b._last_modified||b._creation_date)?d.success(b):g()}):g()},d.allDocs=function(b){varc=a.getItem(i);if(c)e.update(),setTimeout(function(){d.success(e.allDocs(c))});else{varf=function(a){e.setFileArray(a.rows),d.success(a)},g=function(a){d.error(a)};d.addJob("allDocs",e.secondstorage_spec,null,b.cloneOption(),f,g)}},d.remove=function(a){varb=function(b){e.removeFile(a.getDocId()),e.update(),d.success(b)},c=function(a){d.error(a)};d.addJob("remove",e.secondstorage_spec,a.cloneDoc(),a.cloneOption(),b,c)},d};f.addStorageType("indexed",j);vark=function(a,c){vare=f.storage(a,c,"handler"),g={},h=a.storage?!0:!1;g.username=a.username||"",g.password=a.password||"",g.secondstorage_spec=a.storage||{type:"base"},g.secondstorage_string=JSON.stringify(g.secondstorage_string);vari=e.serialized;returne.serialized=function(){vara=i();returna.username=g.username,a.password=g.password,a.storage=g.secondstorage_string,a},e.validateState=function(){returng.username&&h?"":'Need at least two parameters: "username" and "storage".'},g.encrypt_param_object={iv:"kaprWwY/Ucr7pumXoTHbpA",v:1,iter:1e3,ks:256,ts:128,mode:"ccm",adata:"",cipher:"aes",salt:"K4bmZG9d704"},g.decrypt_param_object={iv:"kaprWwY/Ucr7pumXoTHbpA",ks:256,ts:128,salt:"K4bmZG9d704"},g.encrypt=function(a,b){varc=d.encrypt(g.username+":"+g.password,a,g.encrypt_param_object);b(JSON.parse(c).ct)},g.decrypt=function(a,c){vare,f=b.extend(!0,{},g.decrypt_param_object);f.ct=a||"",f=JSON.stringify(f);try{e=d.decrypt(g.username+":"+g.password,f)}catch(h){c({status:403,statusText:"Forbidden",error:"forbidden",message:"Unable to decrypt.",reason:"unable to decrypt"});return}c(undefined,e)},g.newAsyncModule=function(){vara={};returna.call=function(a,b,c){a._wait=a._wait||{};if(a._wait[b])returna._wait[b]--,function(){};c=c||[],setTimeout(function(){a[b].apply(a[b],c)})},a.neverCall=function(a,b){a._wait=a._wait||{},a._wait[b]=-1},a.wait=function(a,b,c){a._wait=a._wait||{},a._wait[b]=c},a.end=function(){a.call=function(){}},a},e.post=function(a){e.put(a)},e.put=function(a){varb,c,d=g.newAsyncModule(),f={};f.encryptFilePath=function(){g.encrypt(a.getDocId(),function(a){b=a,d.call(f,"save")})},f.encryptFileContent=function(){g.encrypt(a.getDocContent(),function(a){c=a,d.call(f,"save")})},f.save=function(){vard=function(b){b.id=a.getDocId(),e.success(b)},f=function(a){e.error(a)},h=a.cloneDoc();h._id=b,h.content=c,e.addJob("put",g.secondstorage_spec,h,a.cloneOption(),d,f)},d.wait(f,"save",1),d.call(f,"encryptFilePath"),d.call(f,"encryptFileContent")},e.get=function(a){varb,c,d=g.newAsyncModule(),f={};f.encryptFilePath=function(){g.encrypt(a.getDocId(),function(a){b=a,d.call(f,"get")})},f.get=function(){e.addJob("get",g.secondstorage_spec,b,a.cloneOption(),f.success,f.error)},f.success=function(b){b._id=a.getDocId(),a.getOption("metadata_only")?e.success(b):g.decrypt(b.content,function(a,c){a?e.error(a):(b.content=c,e.success(b))})},f.error=function(a){e.error(a)},d.call(f,"encryptFilePath")},e.allDocs=function(a){varb=[],c=g.newAsyncModule(),d={};d.allDocs=function(){e.addJob("allDocs",g.secondstorage_spec,null,a.cloneOption(),d.onSuccess,d.error)},d.onSuccess=function(e){if(e.total_rows===0)returnc.call(d,"success");b=e.rows;varf,h=function(e){g.decrypt(b[e].id,function(a,f){a?c.call(d,"error",[a]):(b[e].id=f,b[e].key=f,c.call(d,"success"))}),a.getOption("metadata_only")||g.decrypt(b[e].value.content,function(a,f){a?c.call(d,"error",[a]):(b[e].value.content=f,c.call(d,"success"))})};a.getOption("metadata_only")?c.wait(d,"success",e.total_rows*1-1):c.wait(d,"success",e.total_rows*2-1);for(f=0;f<b.length;f+=1)h(f)},d.error=function(a){c.end(),e.error(a)},d.success=function(){c.end(),e.success({total_rows:b.length,rows:b})},c.call(d,"allDocs")},e.remove=function(a){varb,c={};c.encryptDocId=function(){g.encrypt(a.getDocId(),function(a){b=a,c.removeDocument()})},c.removeDocument=function(){vard=a.cloneDoc();d._id=b,e.addJob("remove",g.secondstorage_spec,d,a.cloneOption(),c.success,e.error)},c.success=function(b){b.id=a.getDocId(),e.success(b)},c.encryptDocId()},e};f.addStorageType("crypt",k);varl=function(a,b){varc=f.storage(a,b,"handler"),d={};a=a||{},b=b||{};varg=a.storage?!0:!1;d.secondstorage_spec=a.storage||{type:"base"},d.secondstorage_string=JSON.stringify(d.secondstorage_spec);varh="jio/conflictmanager/"+d.secondstorage_string+"/",i=function(){},j=c.serialized;returnc.serialized=function(){vara=j();returna.storage=d.secondstorage_spec,a},c.validateState=function(){returng?"":'Need at least one parameter: "storage".'},d.getDistantMetadata=function(a,b,e,f){varg=a.cloneOption();g.metadata_only=!1,g.max_retry=a.getOption("max_retry")||3,c.addJob("get",d.secondstorage_spec,b,g,e,f)},d.saveMetadataToDistant=function(a,b,e,f,g){c.addJob("put",d.secondstorage_spec,{_id:b,content:JSON.stringify(e)},a.cloneOption(),f,g)},d.saveNewRevision=function(a,b,e,f,g){c.addJob("put",d.secondstorage_spec,{_id:b,content:e},a.cloneOption(),f,g)},d.loadRevision=function(a,b,e,f){c.addJob("get",d.secondstorage_spec,b,a.cloneOption(),e,f)},d.deleteAFile=function(a,b,e,f){varg=a.cloneOption();g.max_retry=0,c.addJob("remove",d.secondstorage_spec,{_id:b},a.cloneOption(),e,f)},d.chooseARevision=function(a){varb=0,c="",d;for(dina)b<a[d]._last_modified&&(b=a[d]._last_modified,c=d);returnc},d._revs=function(a,b){returna[b]?{start:a[b]._revisions.length,ids:a[b]._revisions}:null},d._revs_info=function(a){varb,c=[];for(bina)c.push({rev:b,status:a[b]?a[b]._deleted?"deleted":"available":"missing"});returnc},d.solveConflict=function(a,b,c){varf={},g=d.newAsyncModule(),h=c.command,j=c.docid+".metadata",k="",l="",m=null,n=!1,o={total_rows:0,rows:[]},p=c._deleted,q=c.previous_revision,r=null,s=newDate,t;f.getDistantMetadata=function(){d.getDistantMetadata(h,j,function(b){vard=parseInt(q.split("-")[0],10);m=JSON.parse(b.content),k=d+1+"-"+e(""+a.content+q+JSON.stringify(m)),l=c.docid+"."+k,r=m[q]||{},p||(g.wait(f,"saveMetadataOnDistant",1),g.call(f,"saveNewRevision")),g.call(f,"previousUpdateMetadata")},function(a){g.call(f,"error",[a])})},f.saveNewRevision=function(){d.saveNewRevision(h,l,a.content,function(a){g.call(f,"saveMetadataOnDistant")},function(a){g.call(f,"error",[a])})},f.previousUpdateMetadata=function(){vara;for(a=0;a<c.key.length;a+=1)deletem[c.key[a]];g.call(f,"checkForConflicts")},f.checkForConflicts=function(){vara;for(ainm){varb;n=!0,t={status:409,error:"conflict",statusText:"Conflict",reason:"document update conflict",message:"There is one or more conflicts"};break}g.call(f,"updateMetadata")},f.updateMetadata=function(){vara,b="";b=k.split("-"),b.shift(),b=b.join("-"),a=r._revisions,a.unshift(b),m[k]={_creation_date:r._creation_date||s.getTime(),_last_modified:s.getTime(),_revisions:a,_conflict:n,_deleted:p},n&&(o=d.createConflictObject(h,m,k)),g.call(f,"saveMetadataOnDistant")},f.saveMetadataOnDistant=function(){d.saveMetadataToDistant(h,j,m,function(a){g.call(f,"deleteAllConflictingRevision"),n?g.call(f,"error"):g.call(f,"success")},function(a){g.call(f,"error",[a])})},f.deleteAllConflictingRevision=function(){vara;for(a=0;a<c.key.length;a+=1)d.deleteAFile(h,c.docid+"."+c.key[a],i,i)},f.success=function(){vara={ok:!0,id:c.docid,rev:k};g.neverCall(f,"error"),g.neverCall(f,"success"),b.revs&&(a.revisions=d._revs(m,k)),b.revs_info&&(a.revs_info=d._revs_info(m)),b.conflicts&&(a.conflicts=o),c.success(a)},f.error=function(a){vare=a||t||{status:0,statusText:"Unknown",error:"unknown_error",message:"Unknown error.",reason:"unknown error"};k&&(e.rev=k),b.revs&&(e.revisions=d._revs(m,k)),b.revs_info&&(e.revs_info=d._revs_info(m)),b.conflicts&&(e.conflicts=o),g.neverCall(f,"error"),g.neverCall(f,"success"),c.error(e)},g.call(f,"getDistantMetadata")},d.createConflictObject=function(a,b,c){return{total_rows:1,rows:[d.createConflictRow(a,a.getDocId(),b,c)]}},d.getParam=function(a){varb={},c=0;returntypeofa[c]=="string"&&(b.content=a[c],c++),typeofa[c]=="object"?(b.options=a[c],c++):b.options={},b.callback=function(a,b){},b.success=function(a){b.callback(undefined,a)},b.error=function(a){b.callback(a,undefined)},typeofa[c]=="function"&&(typeofa[c+1]=="function"?(b.success=a[c],b.error=a[c+1]):b.callback=a[c]),b},d.createConflictRow=function(a,b,c,e){varf={id:b,key:[],value:{_solveConflict:function(){varc={},g=d.getParam(arguments);returng.content===undefined?c._deleted=!0:c._deleted=!1,c.success=g.success,c.error=g.error,c.previous_revision=e,c.docid=b,c.key=f.key,c.command=a.clone(),d.solveConflict({_id:b,content:g.content,_rev:e},g.options,c)}}},g;for(ginc)f.key.push(g);returnf},d.newAsyncModule=function(){vara={};returna.call=function(a,b,c){a._wait=a._wait||{};if(a._wait[b])returna._wait[b]--,i;c=c||[],setTimeout(function(){a[b].apply(a[b],c)})},a.neverCall=function(a,b){a._wait=a._wait||{},a._wait[b]=-1},a.wait=function(a,b,c){a._wait=a._wait||{},a._wait[b]=c},a.end=function(){a.call=i},a},c.post=function(a){c.put(a)},c.put=function(a){varb={},f=d.newAsyncModule(),g=a.getDocId()+".metadata",h="",j="",k=null,l=!1,m={total_rows:0,rows:[]},n=a.getDocInfo("_rev")||"0",o=a.getDocId()+"."+n,p=newDate,q;b.getDistantMetadata=function(){d.getDistantMetadata(a,g,function(c){vard=parseInt(n.split("-")[0],10);k=JSON.parse(c.content),h=d+1+"-"+e(""+a.getDocContent()+n+JSON.stringify(k)),j=a.getDocId()+"."+h,f.wait(b,"saveMetadataOnDistant",1),f.call(b,"saveNewRevision"),f.call(b,"checkForConflicts")},function(c){c.status===404?(h="1-"+e(a.getDocContent()),j=a.getDocId()+"."+h,f.wait(b,"saveMetadataOnDistant",1),f.call(b,"saveNewRevision"),f.call(b,"createMetadata")):f.call(b,"error",[c])})},b.saveNewRevision=function(){d.saveNewRevision(a,j,a.getDocContent(),function(a){f.call(b,"saveMetadataOnDistant")},function(a){f.call(b,"error",[a])})},b.checkForConflicts=function(){vara;for(aink)if(a!==n){l=!0,q={status:409,error:"conflict",statusText:"Conflict",reason:"document update conflict",message:"Document update conflict."};break}f.call(b,"updateMetadata")},b.createMetadata=function(){vara=h;a=a.split("-"),a.shift(),a=a.join("-"),k={},k[h]={_creation_date:p.getTime(),_last_modified:p.getTime(),_revisions:[a],_conflict:!1,_deleted:!1},f.call(b,"saveMetadataOnDistant")},b.updateMetadata=function(){varc,e=[],g="";k[n]&&(c=k[n]._creation_date,e=k[n]._revisions,deletek[n]),g=h.split("-"),g.shift(),g=g.join("-"),e.unshift(g),k[h]={_creation_date:c||p.getTime(),_last_modified:p.getTime(),_revisions:e,_conflict:l,_deleted:!1},l&&(m=d.createConflictObject(a,k,h)),f.call(b,"saveMetadataOnDistant")},b.saveMetadataOnDistant=function(){d.saveMetadataToDistant(a,g,k,function(a){f.call(b,"deletePreviousRevision"),l?f.call(b,"error"):f.call(b,"success")},function(a){f.call(b,"error",[a])})},b.deletePreviousRevision=function(){n!=="0"&&d.deleteAFile(a,o,i,i)},b.success=function(){vare={ok:!0,id:a.getDocId(),rev:h};f.neverCall(b,"error"),f.neverCall(b,"success"),a.getOption("revs")&&(e.revisions=d._revs(k,h)),a.getOption("revs_info")&&(e.revs_info=d._revs_info(k)),a.getOption("conflicts")&&(e.conflicts=m),c.success(e)},b.error=function(e){varg=e||q||{status:0,statusText:"Unknown",error:"unknown_error",message:"Unknown error.",reason:"unknown error"};h&&(g.rev=h),a.getOption("revs")&&(g.revisions=d._revs(k,h)),a.getOption("revs_info")&&(g.revs_info=d._revs_info(k)),a.getOption("conflicts")&&(g.conflicts=m),f.neverCall(b,"error"),f.neverCall(b,"success"),c.error(g)},f.call(b,"getDistantMetadata")},c.get=function(a){varb={},e=d.newAsyncModule(),f=a.getDocId()+".metadata",g=a.getOption("rev")||"",h=null,i=a.getOption("metadata_only"),j=!1,k={total_rows:0,rows:[]},l=newDate,m={_id:a.getDocId()},n=function(a){e.call(b,"error",[{status:404,statusText:"Not Found",error:"not_found",message:a,reason:a}])};b.getDistantMetadata=function(){d.getDistantMetadata(a,f,function(a){h=JSON.parse(a.content),i||e.wait(b,"success",1),e.call(b,"affectMetadata"),e.call(b,"checkForConflicts")},function(a){e.call(b,"error",[a])})},b.affectMetadata=function(){if(g){if(!h[g])returnn("Document revision does not exists.")}elseg=d.chooseARevision(h);m._last_modified=h[g]._last_modified,m._creation_date=h[g]._creation_date,m._rev=g,a.getOption("revs")&&(m._revisions=d._revs(h,g)),a.getOption("revs_info")&&(m._revs_info=d._revs_info(h)),i?e.call(b,"success"):e.call(b,"loadRevision")},b.loadRevision=function(){if(!g||h[g]._deleted)returnn("Document has been removed.");d.loadRevision(a,m._id+"."+g,function(a){m.content=a.content,e.call(b,"success")},function(a){e.call(b,"error",[a])})},b.checkForConflicts=function(){h[g].conflict&&(j=!0,k=d.createConflictObject(a,h,g)),a.getOption("conflicts")&&(m._conflicts=k),e.call(b,"success")},b.success=function(){e.neverCall(b,"error"),e.neverCall(b,"success"),c.success(m)},b.error=function(a){vard=a||{status:0,statusText:"Unknown",message:"Unknown error."};j&&(d.conflict_object=k),e.neverCall(b,"error"),e.neverCall(b,"success"),c.error(d)},e.call(b,"getDistantMetadata")},c.allDocs=function(a){varb={},e=d.newAsyncModule(),f=a.getOption("metadata_only"),g=[],h=0,i=0,j=0;b.retreiveList=function(){varf=a.cloneOption(),g=function(a){e.call(b,"filterTheList",[a])},h=function(a){e.call(b,"error",[a])};f.metadata_only=!0,c.addJob("allDocs",d.secondstorage_spec,null,f,g,h)},b.filterTheList=function(a){varc;j++;for(c=0;c<a.total_rows;c+=1){vard=a.rows[c].id.split(".")||[];d.length>0&&d[d.length-1]==="metadata"&&(j++,d.length--,e.call(b,"loadMetadataFile",[d.join(".")]))}e.call(b,"success")},b.loadMetadataFile=function(c){d.getDistantMetadata(a,c+".metadata",function(a){a=JSON.parse(a.content);varf=d.chooseARevision(a);a[f]._deleted?e.call(b,"success"):e.call(b,"loadFile",[c,f,a])},function(a){e.call(b,"error",[a])})},b.loadFile=function(c,h,i){varj={id:c,key:c,value:{_last_modified:i[h]._last_modified,_creation_date:i[h]._creation_date,_rev:h}};a.getOption("revs_info")&&(j.value._revs_info=d._revs_info(i,h)),a.getOption("conflicts")&&(i[h]._conflict?j.value._conflicts=d.createConflictObject(a,i,h):j.value._conflicts={total_rows:0,rows:[]}),f?(g.push(j),e.call(b,"success")):d.loadRevision(a,c+"."+h,function(a){j.content=a.content,g.push(j),e.call(b,"success")},function(a){e.call(b,"error",[a])})},b.success=function(){i++,i>=j&&(e.end(),c.success({total_rows:g.length,rows:g}))},b.error=function(a){e.end(),c.error(a)},e.call(b,"retreiveList")},c.remove=function(a){varb={},f=d.newAsyncModule(),g=a.getDocId()+".metadata",h="",j="",k=null,l=!1,m={total_rows:0,rows:[]},n=a.getOption("rev")||"0",o=a.getDocId()+"."+n,p=newDate,q;b.getDistantMetadata=function(){d.getDistantMetadata(a,g,function(c){k=JSON.parse(c.content),n==="last"&&(n=d.chooseARevision(k),o=a.getDocId()+"."+n);varg=parseInt(n.split("-")[0],10)||0;h=g+1+"-"+e(""+n+JSON.stringify(k)),j=a.getDocId()+"."+h,f.call(b,"checkForConflicts")},function(a){a.status===404?f.call(b,"error",[{status:404,statusText:"Not Found",error:"not_found",reason:"missing",message:"Document not found."}]):f.call(b,"error",[a])})},b.checkForConflicts=function(){vara;for(aink)if(a!==n){l=!0,q={status:409,error:"conflict",statusText:"Conflict",reason:"document update conflict",message:"There is one or more conflicts"};break}f.call(b,"updateMetadata")},b.updateMetadata=function(){varc,e=[],g="";k[n]&&(c=k[n]._creation_date,e=k[n]._revisions,deletek[n]),g=h,g=g.split("-"),g.shift(),g=g.join("-"),e.unshift(g),k[h]={_creation_date:c||p.getTime(),_last_modified:p.getTime(),_revisions:e,_conflict:l,_deleted:!0},l&&(m=d.createConflictObject(a,k,h)),f.call(b,"saveMetadataOnDistant")},b.saveMetadataOnDistant=function(){d.saveMetadataToDistant(a,g,k,function(a){f.call(b,"deletePreviousRevision"),l?f.call(b,"error"):f.call(b,"success")},function(a){f.call(b,"error",[a])})},b.deletePreviousRevision=function(){n!=="0"&&d.deleteAFile(a,o,i,i)},b.success=function(e){varg={ok:!0,id:a.getDocId(),rev:e||h};f.neverCall(b,"error"),f.neverCall(b,"success"),a.getOption("revs")&&(g.revisions=d._revs(k,h)),a.getOption("revs_info")&&(g.revs_info=d._revs_info(k)),a.getOption("conflicts")&&(g.conflicts=m),c.success(g)},b.error=function(e){varg=e||q||{status:0,statusText:"Unknown",error:"unknown_error",message:"Unknown error.",reason:"unknown error"};h&&(g.rev=h),a.getOption("revs")&&(g.revisions=d._revs(k,h)),a.getOption("revs_info")&&(g.revs_info=d._revs_info(k)),a.getOption("conflicts")&&(g.conflicts=m),f.neverCall(b,"error"),f.neverCall(b,"success"),c.error(g)},f.call(b,"getDistantMetadata")},c};f.addStorageType("conflictmanager",l)})(LocalOrCookieStorage,jQuery,Base64,sjcl,hex_sha256,jio);