﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("SpatialTest");

SpatialTest.StateManager = function(id) {
    this._id = id;
    //console.log("State constructor");
    //console.log("id " + id);
    this._isValid = false;
    this._data = null;
    this._timeout = 1;
}

SpatialTest.StateManager.prototype = {
    initialize: function() {
        //console.log("State initialize");


        // Add custom initialization here
    },
    dispose: function() {
        //Add custom dispose actions here
        //console.log("State dispose");
    },
    validate: function(nick) {

        var stringRep = this._readCookie(this._id);
        var ok = false;
        if (stringRep != null) {
            try {
                var data = Sys.Serialization.JavaScriptSerializer.deserialize(stringRep);
                if (data != null) {
                    if (data['n'] == nick) {
                        this._data = data;
                        ok = true;
                    }
                }
            }
            catch (exc) {
                //console.log("Error deserializing " + stringRep);
            }
        }
        if (!ok) {
            this._data = this._createData(nick);
            stringRep = Sys.Serialization.JavaScriptSerializer.serialize(this._data);
        }
        this._createCookie(this._id, stringRep, this._timeout);
        this._isValid = true;
    },
    _createData: function(nick) {
        return { n: nick };
    },
    getValue: function(key, defVal) {
        if (this._isValid) {
            var res = this._data[key];
            res = res == null ? defVal : res;
            return res;
        }
        else {
            return defVal;
        }
    },
    setValue: function(key, value) {
        if (this._isValid) {
            if (key == 'n')
                return;
            this._data[key] = value;
            var stringRep = Sys.Serialization.JavaScriptSerializer.serialize(this._data);
            this._createCookie(this._id, stringRep, this._timeout);
        }
        //        else
        //            console.log("State data is not valid.");
    },
    _createCookie: function(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + escape(value) + expires + "; path=/";
    },
    _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 unescape(c.substring(nameEQ.length, c.length));
        }
        return null;
    },
    _eraseCookie: function(name) {
        createCookie(name, "", -1);
    }
}
SpatialTest.StateManager.registerClass('SpatialTest.StateManager');

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

