//uid must be unique within a page.
//constructor
function StateObject(uid) {
	this.$uid=uid;	
	this.load();
}

function getPageName() {
	 var pageUrl = window.location.href.toLowerCase();
	 var orgUrlPos = pageUrl.indexOf('nroriginalurl=');
	 if (orgUrlPos>0)
	 {
		pageUrl = pageUrl.substr(orgUrlPos + 14);
		var endPos = pageUrl.indexOf('&');
		if (endPos>0)
		{
			pageUrl = pageUrl.substring(0,endPos);			
		}
		pageUrl = unescape(pageUrl);
	 }
	 var last=pageUrl.indexOf("?");
	 if (last==-1) {
		  last=pageUrl.length;
	 }
	 var first=pageUrl.lastIndexOf("/")+1;
	 if (first==-1) {
		  //		  first=window.location.href.lastIndexOf('\\');
	 }
	 var pageName = pageUrl.substring(first,last).toLowerCase()
	 return pageName;
}

StateObject.prototype.$cookie=new Cookie(document,getPageName(),1/6,'/');
StateObject.prototype.$lastAccess=new Object();
//static function.
//Makes sure the cookie for the page does not grow out of bounds.
StateObject.maintain=function() {};
StateObject.prototype.save=function() {
	 var globalKey;
	 var mydate=new Date();
	 //First load whatever has been saved before.
	 this.$cookie.load();

	 for (var key in this) {
		  if (this.isTrueKey(key)) {
				globalKey=this.getGlobalKey(key);
				this.$cookie[globalKey]=this[key];
		  }

	 }	
	 this.$cookie.store();
}
StateObject.prototype.load=function() {

	 this.$cookie.load();
	 for (var key in this) {
		  if (this.isTrueKey(key)) {
				var globalKey=this.getGlobalKey(key);
				if (this.$cookie[globalKey]) {
					 this[key]=this.$cookie[globalKey];
				}
		  }
	 }
	 
}
StateObject.prototype.isTrueKey=function(key) {
	 if (key.charAt(0)!="$" && !(this[key] instanceof  Function) ) {
		  return true;
	 }
	 else {
		  return false;
	 }
}
StateObject.prototype.getGlobalKey=function(key) {
	 return this.$uid+"__"+key;
}

