﻿Type.registerNamespace("Ag");Ag.AppState=function(){Ag.AppState.initializeBase(this);this.$_autoPromote=true;this.$_storage=null};Ag.AppState.prototype={initialize:function(){Ag.AppState.callBaseMethod(this,"initialize");switch(Sys.Browser.storage){case Ag.StorageType.dom:try{this.$_storage=globalStorage[AgContext.ApplicationName+"."+window.location.hostname]}catch(g){Sys.Debug.trace("Global DOM storage is not available, cross-document state sharing is disabled");this.$_storage=sessionStorage}break;case Ag.StorageType.userData:this.$_storage=$common.createElementFromTemplate({nodeName:"div",properties:{id:this.get_id()+"_userData",style:{behavior:"url(#default#userdata)"}}},document.getElementsByTagName("body")[0]);break;case Ag.StorageType.gears:this.$_storage=google.gears.factory.create("beta.database");this.$_storage.open("AppGeo.Web.Extensions");this.$_storage.execute("create table if not exists AppState (ItemKey text, ItemValue text)");break}if(this.$_autoPromote){switch(Sys.Browser.storage){case Ag.StorageType.dom:for(var d=0;d<this.$_storage.length;d++){var c=this.$_storage.getItem(this.$_storage.key(d));if(c!=null){this[this.$_storage.key(d)]=Sys.Serialization.JavaScriptSerializer.deserialize(c.toString())}}break;case Ag.StorageType.userData:this.$_storage.load("xmlStore");for(var d=0;d<this.$_storage.XMLDocument.childNodes[0].attributes.length;d++){var a=this.$_storage.XMLDocument.childNodes[0].attributes[d];if(a!=null){this[a.nodeName]=Sys.Serialization.JavaScriptSerializer.deserialize(a.nodeValue)}}break;case Ag.StorageType.gears:var b=this.$_storage.execute("select * from AppState");while(b.isValidRow()){var f=b.field(1);if(f!=null){this[b.field(0)]=Sys.Serialization.JavaScriptSerializer.deserialize(f)}b.next()}b.close();break}}},dispose:function(){Ag.AppState.callBaseMethod(this,"dispose")},clear:function(){switch(Sys.Browser.storage){case Ag.StorageType.dom:if(this.$_autoPromote){for(var c=0;c<this.$_storage.length;c++){delete this[this.$_storage.key(c)]}}while(this.$_storage.length>0){this.$_storage.removeItem(this.$_storage.key(0))}break;case Ag.StorageType.userData:this.$_storage.load("xmlStore");var d=0;while(this.$_storage.XMLDocument.childNodes[0].attributes.length>d){var a=this.$_storage.XMLDocument.childNodes[0].attributes[d];if(a!=null){if(this.$_autoPromote){delete this[a.nodeName]}this.$_storage.removeAttribute(a.nodeName)}else{d++}}this.$_storage.save("xmlStore");break;case Ag.StorageType.gears:if(this.$_autoPromote){var b=this.$_storage.execute("select ItemKey from AppState");while(b.isValidRow()){delete this[b.field(0)];b.next()}b.close()}this.$_storage.execute("delete from AppState");break}},contains:function(c){switch(Sys.Browser.storage){case Ag.StorageType.dom:return this.$_storage.getItem(c)!=null;case Ag.StorageType.userData:this.$_storage.load("xmlStore");return this.$_storage.getAttribute(c)!=null;case Ag.StorageType.gears:var b=this.$_storage.execute("select ItemKey from AppState where ItemKey = ?",[c]);var a=b.isValidRow();b.close();return a}},encode:function(){var d={};switch(Sys.Browser.storage){case Ag.StorageType.dom:for(var f=0;f<this.$_storage.length;f++){var c=this.$_storage.getItem(this.$_storage.key(f));if(c!=null){d[this.$_storage.key(f)]=Sys.Serialization.JavaScriptSerializer.deserialize(c.toString())}}break;case Ag.StorageType.userData:this.$_storage.load("xmlStore");for(var f=0;f<this.$_storage.XMLDocument.childNodes[0].attributes.length;f++){var a=this.$_storage.XMLDocument.childNodes[0].attributes[f];if(a!=null){d[a.nodeName]=Sys.Serialization.JavaScriptSerializer.deserialize(a.nodeValue)}}break;case Ag.StorageType.gears:var b=this.$_storage.execute("select * from AppState");while(b.isValidRow()){d[b.field(0)]=Sys.Serialization.JavaScriptSerializer.deserialize(b.field(1));b.next()}b.close();break}var e=Sys.Serialization.JavaScriptSerializer.serialize(d);return encodeURIComponent(e)},getItem:function(d){var c=null;switch(Sys.Browser.storage){case Ag.StorageType.dom:var b=this.$_storage.getItem(d);c=(b==null?null:b.toString());break;case Ag.StorageType.userData:this.$_storage.load("xmlStore");c=this.$_storage.getAttribute(d);break;case Ag.StorageType.gears:var a=this.$_storage.execute("select ItemValue from AppState where ItemKey = ?",[d]);if(a.isValidRow()){c=a.field(0)}a.close();break}return c==null?null:Sys.Serialization.JavaScriptSerializer.deserialize(c)},remove:function(a){if(this.$_autoPromote){delete this[a]}switch(Sys.Browser.storage){case Ag.StorageType.dom:this.$_storage.removeItem(a);break;case Ag.StorageType.userData:this.$_storage.removeAttribute(a);this.$_storage.save("xmlStore");break;case Ag.StorageType.gears:this.$_storage.execute("delete from AppState where ItemKey = ?",[a]);break}},setItem:function(b,c){var a=Sys.Serialization.JavaScriptSerializer.serialize(c);switch(Sys.Browser.storage){case Ag.StorageType.dom:this.$_storage.setItem(b,a);break;case Ag.StorageType.userData:this.$_storage.setAttribute(b,a);this.$_storage.save("xmlStore");break;case Ag.StorageType.gears:if(this.contains(b)){this.$_storage.execute("update AppState set ItemValue = ? where ItemKey = ?",[a,b])}else{this.$_storage.execute("insert into AppState values (?, ?)",[b,a])}break}if(this.$_autoPromote){this[b]=c}},setItems:function(b,a){for(var d in b){var c=b[d];if(a||this.getItem(d)==null){this.setItem(d,c)}}},get_autoPromote:function(){return this.$_autoPromote},set_autoPromote:function(a){this.$_autoPromote=a}};Ag.AppState.registerClass("Ag.AppState",Sys.Component);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();