// Create the namespace object if not already created
if (TimemapNS == null || typeof(TimemapNS) != "object") { var TimemapNS = new Object();}



TimemapNS.StatePersistence = function() {
}
TimemapNS.StatePersistence.prototype.write = function(model, map) {
	this.put('tm_year', model.getCurrentYear());
	if (model.hasCurrentPeriod()) this.put('tm_period_id', model.getCurrentPeriod().id);
	this.put('tm_map_center_lat', map.gMap.getCenter().lat());
	this.put('tm_map_center_lng', map.gMap.getCenter().lng());
	this.put('tm_map_zoom', map.gMap.getZoom());
}
TimemapNS.StatePersistence.prototype.read = function(model, map) {
	var year = this.get('tm_year', true); if (year) model.setYear(parseInt(year));
	var period = this.get('tm_period_id', true); if (period) model.setPeriod(parseInt(period));
	var lat = this.get('tm_map_center_lat', true);
	var lng = this.get('tm_map_center_lng', true);
	if (lat && lng) map.gMap.setCenter(new GLatLng(lat, lng));
	var zoom = this.get('tm_map_zoom', true);
	if (zoom) map.gMap.setZoom(parseInt(zoom));
}
// Abstract method
TimemapNS.StatePersistence.prototype.put = function(key, value) {
	throw new Error('TimemapNS.ModelPersistence must be subclassed '+
		'and subclasses must define the "put" method');
}
// Abstract method
TimemapNS.StatePersistence.prototype.get = function(key, remove) {
	throw new Error('TimemapNS.ModelPersistence must be subclassed '+
		'and subclasses must define the "get" method');
}



TimemapNS.CookieStatePersistence = function() {
	this.writeCookie = function(name, value, hours) {
		if (hours) {
			var date = new Date();
			date.setTime(date.getTime()+(hours*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = '';
		document.cookie = name+"="+value+expires+"; path=/";
	};
	this.readCookie = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0; i<ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) {
				return c.substring(nameEQ.length, c.length);
			}
		}
		return null;
	};
	this.deleteCookie = function(name) {
		this.writeCookie(name, "", -1);
	}
}
TimemapNS.CookieStatePersistence.prototype = new TimemapNS.StatePersistence();
TimemapNS.CookieStatePersistence.prototype.put = function(key, value) {
	this.writeCookie(key, value, 1);
}
TimemapNS.CookieStatePersistence.prototype.get = function(key, remove) {
	var val = this.readCookie(key);
	if (remove) this.deleteCookie(key);
	return val;
}
