(function(exports){var GAL=function(manifest,fsprefix){if(fsprefix==undefined){fsprefix=""}this._fsprefix=fsprefix;this._manifest=manifest;this.bundles={};this.bundleOrder=[];this.loaded={};this.progress={};this.error={};this.adapter=null};GAL.prototype.init=function(callback){console.log("gal init");var that=this;console.log(this._manifest);finishInit_.call(that,this._manifest,callback)};GAL.prototype.download=function(bundleName,callback){var bundle=this.bundles[bundleName];if(callback==undefined){callback=function(){}}if(!bundle){throw"Invalid bundle specified :: "+bundleName}var that=this;this.check(bundleName,function(result){if(result.success){fireCallback_(that.loaded,bundleName,{success:true,cached:true,bundleName:bundleName});callback()}else{if(!that.online()){fireCallback_(that.error,bundleName,{error:"Missing resources cant be downloaded while offline"});callback()}else{(function loop(index){if(index==bundle.length){fireCallback_(that.loaded,bundleName,{bundleName:bundleName,success:true});callback();return}var key=bundle[index];var url=encodeURI(that.manifest.assetRoot+key);var cb=function(){fireCallback_(that.progress,bundleName,{current:index+1,total:bundle.length});loop(index+1)};that.adapter.saveAsset(key,url,cb,function(){})})(0)}}})};GAL.prototype.onLoaded=function(opt_bundleName,callback){addCallback_(this.loaded,opt_bundleName,callback)};GAL.prototype.onProgress=function(opt_bundleName,callback){addCallback_(this.progress,opt_bundleName,callback)};GAL.prototype.onError=function(opt_bundleName,callback){addCallback_(this.error,opt_bundleName,callback)};GAL.prototype.downloadAndCall=function(bundleName,callback){console.log("download and call "+bundleName);var $this=this;var timeout=setTimeout(function(){$this.downloadAndCall(bundleName,callback)},1e3);this.check(bundleName,function(info){clearTimeout(timeout);if(info.success){console.log("already downloaded");callback()}else{console.log("i download");$this.download(bundleName,callback)}})};GAL.prototype.check=function(bundleName,callback){var bundle=this.bundles[bundleName];if(!bundle){callback({success:false});return}var adapter=this.adapter;(function loop(index){if(index==bundle.length){callback({success:true});return}var key=bundle[index];adapter.checkAsset(key,function(){loop(index+1)},function(){callback({success:false})})})(0)};GAL.prototype.get=function(assetPath){return this.adapter.getAssetUrl(assetPath)||null};GAL.prototype.cacheTime=function(assetPath){return Math.random()};GAL.prototype.online=function(){return navigator.onLine};GAL.prototype.getRootURL=function(){return this.adapter.root.toURL()};GAL.prototype.getRootNativeURL=function(){try{if(PLATFORM==="ios"){return cordova.file.dataDirectory+"/gal/"}else{return this.adapter.root.toNativeURL()}}catch(err){return this.getRootURL()}};GAL.prototype.initAdapter=function(callback){if(null==this.adapter){this.initAdapter_(callback)}else{callback(this.adapter)}};GAL.prototype.downloadAll=function(){downloadAll_.call(this)};GAL.prototype.clear=function(callback){var $this=this;this.initAdapter(function(adapter){adapter.clear(callback)})};GAL.prototype.initAdapter_=function(callback){console.log("gal init adapter");var $this=this;if(this.adapter!=null){callback(this.adapter);return}this.adapter=new GAL.adapterClass;this.adapter.init(this._fsprefix,function(){console.log("gal adapter inited");callback($this.adapter)})};function setManifest_(manifest){console.log("gal set manifest");this.manifest=manifest;for(var i=0,bundle;bundle=manifest.bundles[i];++i){this.bundles[bundle.name]=bundle.contents;this.bundleOrder.push(bundle.name)}}function finishInit_(manifest,callback){console.log("gal finish init");var context=this;this.initAdapter_.call(context,function(){console.log("gal set manifest");setManifest_.call(context,manifest);if(manifest.autoDownload){downloadAll_.call(context)}callback()})}function addCallback_(callbacks,bundleName,callback){if(typeof bundleName=="function"){callback=bundleName;bundleName="*"}if(!callbacks[bundleName]){callbacks[bundleName]=[]}callbacks[bundleName].push(callback)}function fireCallback_(callbacks,bundleName,params){fireCallbackHelper_(callbacks,bundleName,params);fireCallbackHelper_(callbacks,"*",params)}function fireCallbackHelper_(object,bundleName,params){var callbacks=object[bundleName];if(callbacks){for(var i=0,callback;callback=callbacks[i];++i){callbacks[i](params)}}}function downloadAll_(){var that=this;(function loop(bundleIndex){if(bundleIndex==that.bundleOrder.length){return}var bundleName=that.bundleOrder[bundleIndex];that.onLoaded(bundleName,function(){loop(bundleIndex+1)});that.download(bundleName)})(0)}exports.GameAssetLoader=GAL})(window);(function(gal){var ROOT_DIR;var DEFAULT_QUOTA=1024*1024*100;function onError(error){var message="unknown";switch(error.code){case FileError.NOT_FOUND_ERR:message="not found";break;case FileError.SECURITY_ERR:message="security";break;case FileError.ABORT_ERR:message="abort";break;case FileError.NOT_READABLE_ERR:message="not readable";break;case FileError.ENCODING_ERR:message="encoding";break;case FileError.NO_MODIFICATION_ALLOWED_ERR:message="no modification allowed";break;case FileError.INVALID_STATE_ERR:message="invalid state";break;case FileError.SYNTAX_ERR:message="syntax";break;case FileError.INVALID_MODIFICATION_ERR:message="invalid modification";break;case FileError.QUOTA_EXCEEDED_ERR:message="quota exceeded";break;case FileError.TYPE_MISMATCH_ERR:message="type mismatch";break;case FileError.PATH_EXISTS_ERR:message="path exists";break}console.log("Filesystem error:",error.code+" :: "+message)}function createDir_(root,folders,callback){console.log("createDir_ :: "+root.toURL()+" ;; "+folders.join("/"));if(folders.length&&(folders[0]==="."||folders[0]==="")){folders=folders.slice(1)}if(!folders.length){root.setMetadata(function(){},function(){},{"com.apple.MobileBackup":1});callback(root)}var f=folders.shift();var timeout=setTimeout(function(){window.location.reload(true)},1e3);root.getDirectory(f,{create:true},function(dirEntry){console.log("dirCreated !! "+folders.join("/")+" :: "+folders.length);clearTimeout(timeout);if(folders.length>0){createDir_(dirEntry,folders,callback)}else{callback(dirEntry)}},onError)}function dirname_(path){var match=path.match(/(.*)\//);return match&&match[1]||""}function basename_(path){return path.replace(/.*\//,"")}function GALFS(){this.lookupTable={}}GALFS.prototype.init=function(fsprefix,callback,opt_quota){console.log("gal fs init");if(fsprefix==undefined){fsprefix=""}if(fsprefix!=""){fsprefix+="/"}ROOT_DIR=fsprefix+"gal";var requestFileSystem=window.requestFileSystem||window.webkitRequestFileSystem;var storageInfo=window.storageInfo||window.webkitStorageInfo;var quota=opt_quota||DEFAULT_QUOTA;var that=this;var onInitFs=function(fs){console.log("fs inited");that.fs=fs;that.regenerate(callback)};console.log("request file system");requestFileSystem(LocalFileSystem.PERSISTENT,0,onInitFs,onError);var interval=setInterval(function(){if(that.fs){clearInterval(interval)}else{console.log("request file system again");requestFileSystem(LocalFileSystem.PERSISTENT,0,onInitFs,onError)}},500)};GALFS.prototype.saveAsset=function(key,url,callback,failCallback){var BlobBuilder=window.BlobBuilder||window.WebKitBlobBuilder;var root=this.root;var lookupTable=this.lookupTable;createDir_(root,dirname_(key).split("/"),function(dir){var file=basename_(key);dir.getFile(file,{create:true},function(fileEntry){var fileTransfer=new FileTransfer;var u=fileEntry.toURL();if(PLATFORM=="ios"){u=cordova.file.dataDirectory+fileEntry.fullPath}console.log("download to "+u);fileTransfer.download(url,u,function(entry){var e=entry.toURL();if(PLATFORM=="ios"){e=cordova.file.dataDirectory+entry.fullPath}lookupTable[key]=e;entry.setMetadata(function(entry){},failCallback,{"com.apple.MobileBackup":1});callback()},failCallback)},failCallback)})};GALFS.prototype.getAssetUrl=function(key){return this.lookupTable[key]};GALFS.prototype.checkAsset=function(key,callback,failCallback){var lookupTable=this.lookupTable;this.root.getFile(key,{},function(fileEntry){lookupTable[key]=fileEntry.toURL();callback()},failCallback)};GALFS.prototype.clear=function(cb){var $this=this;this.root.removeRecursively(function(){$this.regenerate(cb)},onError)};GALFS.prototype.regenerate=function(callback){console.log("gal fs regenerate");var that=this;var fs=this.fs;createDir_(fs.root,ROOT_DIR.split("/"),function(res){console.log("root dir created :: "+res.toURL());that.root=res;callback()})};if(!gal){throw"Game asset loader needs to be loaded before loading the fs adapter"}gal.adapterClass=GALFS})(GameAssetLoader);