function XmarksSettings(){this.LoadSettings()}XmarksSettings.VERSION="1.0.17";XmarksSettings.SERVER_ROOT="xmarks.com";
XmarksSettings.prototype={prefs:{},Get:function(a,b){if(this.prefs.hasOwnProperty(a))return this.prefs[a];return b},GetInt:function(a,b){var c=this.Get(a,b);return parseInt(c)},GetBool:function(a,b){return this.Get(a,b)=="true"?true:false},Set:function(a,b,c){b=String(b);this.prefs[a]=b;c&&localStorage.setItem("pref-"+a,b)},Remove:function(a){delete this.prefs[a];localStorage.removeItem("pref-"+a)},SetWindowParams:function(a){this.windowParams=a},GetWindowParams:function(){var a=this.windowParams;
delete this.windowParams;return a},WindowSetChanged:function(a){var b=this.windowSet;if(!b)return true;b=JSON.stringify(b);a=JSON.stringify(a);return b!=a},SaveWindowSet:function(a){this.windowSet=a},SetSyncInfo:function(a){this.syncInfo=a},GetSyncInfo:function(){return this.syncInfo},GetUserHash:function(){return this.prefs["current-username"]+"@"+this.prefs["server-root"]+"-"},GetBaseline:function(a){return this[this.GetUserHash()+"baseline"+a]},LoadLocalBaseline:function(a,b){if(this.GetBaseline(a.suffix))b();
else{var c=this.GetUserHash(),d=openDatabase(c+"baseline"+a.suffix,"","Baseline files for ["+c+"]",1);if(d){var f=this,e="rev-"+this.Get(c+"rev"+a.suffix)+"-"+this.Get(c+"profileid",0)+a.suffix;c=new a.ds;var g=new Nodeset(c);d.transaction(function(i){i.executeSql("SELECT * FROM '"+e+"';",[],function(h,j){if(j&&j.rows)for(var k=0;k<j.rows.length;k++){var l=unescape(j.rows.item(k).data);l=eval(l);g.AddNode(l)}})},function(i){f.LogWrite("Error finding table ["+e+"]; no persisted baseline.  Error: "+
i.message);b()},function(){f.LogWrite("Success reading from db");f.SetBaseline(g,a.suffix,false,b)})}else{this.LogWrite("Unable to open baseline db!  No persisted baseline.");b()}}},SetBaseline:function(a,b,c,d,f){var e=this.GetUserHash();this[e+"baseline"+b]=a;if(c){c=300;if(a)c=a.length*300;var g=openDatabase(e+"baseline"+b,"","Baseline files for ["+e+b+"]",c);if(g){var i=this,h="rev-"+this.Get(e+"rev"+b)+"-"+this.Get(e+"profileid",0)+b;g.transaction(function(j){f&&j.executeSql("DROP TABLE '"+h+
"';",[]);j.executeSql("CREATE TABLE '"+h+"' (rowId INTEGER PRIMARY KEY AUTOINCREMENT, data TEXT);",[],function(k){function l(m){m=a.Node(m).toSource();m="INSERT INTO '"+h+"' ('data') VALUES ('"+escape(m)+"');";k.executeSql(m,[])}function n(){i.LogWrite("Baseline inserts complete")}a&&a.OnTree(l,n)})},function(j){i.LogWrite("Error dropping and creating table ["+h+"]; not saving new baseline.  Error: "+j.message);d&&d(-2)},function(){i.LogWrite("Baseline stored in: "+h);i.RemoveBaselines(g,h,d)})}else{this.LogWrite("Unable to open baseline db!  Cannot save user baseline.");
d&&d(-1)}}else d&&d(0)},DeleteUserBaselineData:function(a,b){var c=this.GetUserHash()+"baseline"+a;this[c]=null;var d=openDatabase(c,"","Baseline files for ["+this.GetUserHash()+"]",300);if(d)this.RemoveBaselines(d,null,b);else{this.LogWrite("Unable to open baseline database for "+c+", no saved baseline data to remove?");b&&b(-1)}},RemoveBaselines:function(a,b,c){function d(e,g){if(g&&g.rows)for(var i=0;i<g.rows.length;i++){var h=g.rows.item(i).name;h.indexOf("rev-")==0&&h!=b&&e.executeSql("DROP TABLE '"+
h+"';",[])}}b||(b="dummyTableName");if(a){var f=this;a.transaction(function(e){e.executeSql("SELECT name FROM sqlite_master WHERE type = 'table'",[],d)},function(e){f.LogWrite("Error removing old baselines: "+e.message);c&&c(-2)},function(){f.LogWrite("Old baselines removed");c&&c(0)})}else{this.LogWrite("No baseline db specified!  Cannot remove user baseline.");c&&c(-1)}},GetUserSettings:function(){if(!this.Get("current-username"))return{username:null};var a=this.GetUserHash(),b={};b.username=this.Get(a+
"username",null);b.password=this.Get(a+"password",null);b.authtoken=this.Get(a+"authtoken",null);b.lastsync=this.Get(a+"lastsync",null);b.profilename=this.Get(a+"profilename",null);b.profileid=this.Get(a+"profileid",null);b.bookmarksrevision=this.Get(a+"rev",null);b.passwordsrevision=this.Get(a+"rev-passwords",null);return b},SetUserSettings:function(a){var b=a.remember;if(b==undefined)b=localStorage.getItem("pref-current-name")==null;if(a.username){this.Set("current-username",a.username,b);var c=
Xmarks.Settings.GetUserHash();this.Set(c+"username",a.username,b);this.Set(c+"password",a.password,b);this.Set(c+"authtoken",a.authtoken)}else this.Remove("current-username")},LoadSettings:function(){function a(f,e,g){f=f.split(".");e=e.split(".");for(var i=f.length>e.length?f.length:e.length,h=0;h<i;h++){if(g&&h>1)break;var j=f.length>h?parseInt(f[h]):0,k=e.length>h?parseInt(e[h]):0;if(j>k)return-1;else if(k>j)return 1}return 0}for(var b=0;b<localStorage.length;b++){var c=localStorage.key(b);c.indexOf("pref-")==
0&&this.Set(c.substr(5),localStorage.getItem(c))}this.Get("server-root")||this.Set("server-root",XmarksSettings.SERVER_ROOT);if(b=this.Get("version")){if(a(XmarksSettings.VERSION,b)<0){a(XmarksSettings.VERSION,b,true)!=0&&this.Set("major-upgrade-run",true);this.Set("version",XmarksSettings.VERSION,true);this.Set("upgrade-run",true)}}else{this.Set("version",XmarksSettings.VERSION,true);this.Set("first-run",true)}b=this.Get("server-root");c={webserver:"www.",authserver:"login.",cloudserver:"cloud.",
staticserver:"static.",syncserver:"sync."};for(var d in c)this.Get(d)||this.Set(d,c[d]+b)},GetMachineId:function(){var a=this.Get("machineId");if(!a){a=Date.now().toString(36);this.Set("machineId",a,true)}return a},LogWrite:function(a){if(this.GetBool("disableLogging","false")==false){var b=new Date,c=b.getMonth()+1,d=b.getDate(),f=b.getFullYear(),e=b.getHours(),g=b.getMinutes();if(g<10)g="0"+g;b=b.getSeconds();if(b<10)b="0"+b;console.log("["+c+"/"+d+"/"+f+" "+e+":"+g+":"+b+"] "+a)}},GetPostSyncCallback:function(){return this._postSyncCallback},
SetPostSyncCallback:function(a){this._postSyncCallback=a}};function XmarksSingleton(){}
XmarksSingleton.prototype={Settings:new XmarksSettings,LogWrite:function(a){this.Settings.LogWrite(a)},GenerateNid:function(){var a=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".","+"];if(!XmarksSingleton.gNidCounter)XmarksSingleton.gNidCounter=(new Date).getTime();XmarksSingleton.gNidCounter++;
for(var b="",c=XmarksSingleton.gNidCounter;c>0;){b+=a[c&63];c>>>=6}return b},GetUserCredentials:function(a,b){function c(f,e){f.close();a(e)}this.LogWrite("Starting authentication");var d=this.Settings.GetUserSettings();if(!b&&d.username&&d.password&&a&&typeof a=="function")a(d);else{b?this.Settings.SetWindowParams({callback:c,username:d.username}):this.Settings.SetWindowParams({callback:c,username:d.username,password:d.password});this.ShowWindow("authdialog.html","xmarksAuthDialog",200,400)}},VerifyUserPin:function(a,
b){this.LogWrite("Getting PIN from user");this.Settings.SetWindowParams({callback:function(c,d){c.close();a(d)},pinConfirmCallback:b});this.ShowWindow("pindialog.html","xmarksPinDialog",200,450)},ShowDialog:function(a,b,c,d,f,e){f||(f=170);e||(e=400);b||(b={});if(!b.button1)b.button1="Ok";this.Settings.SetWindowParams({message:a,callback:function(g,i){g.close();if(c&&typeof c=="function")d&&typeof d=="object"?c.call(d,i):c(i)},buttons:b});this.ShowWindow("popupdialog.html","xmarksPopupDialog",f,e)},
ShowWindow:function(a,b,c,d){a=window.open(a,b,"height="+c+",width="+d+",left="+(window.screen.width-d)/2+",top="+(window.screen.height-c)/2+"toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,location=no");a.focus();return a},IsValidChromeBookmark:function(a,b){if(!a)return false;a=a.trim();if(a==""||a.substr(0,5)=="data:")return false;var c=a.indexOf("://");if(!a.substring(c+3))return false;if(b&&b.length>0&&a.indexOf("%s")>=0)return false;return true},ExtractProtocolAndDomain:function(a){if(!a)return null;
var b=a.indexOf("://");if(b<0)return null;b=a.indexOf("/",b+3);if(b<0)return a;return a.substr(0,b)},HasProtocol:function(a){var b=a.indexOf("."),c=a.indexOf("/");a=a.indexOf(":");var d=a>0;if(d)if(b>=0&&b<a)d=false;else if(c>=0&&c<a)d=false;return d},GetRequestArgs:function(a){a||(a={});a.manual=this.Settings.GetBool("auto-sync","false")==false;a.log={mid:this.Settings.GetMachineId()};var b=this.Settings.GetInt(this.Settings.GetUserHash()+"profileid",0);if(b!=0)a.view=b;if(this.Settings.GetInt("failcount",
0)>0)a.retry=this.Settings.GetInt("failcount",0);return a}};var Xmarks=new XmarksSingleton;
