/**
 * @copyright Copyright Intermesh 2007
 * @author Merijn Schering <mschering@intermesh.nl>
 *
 * This file is part of Group-Office.
 *
 * Group-Office is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * See file /LICENSE.GPL
 */

/**
 * @class Ext.state.HttpProvider
 * @extends Ext.state.Provider
 * The default Provider implementation which saves state via XmlHttpRequest calls to save it in
 * a database.
 *
 * @cfg {String} url The server page that will handle the request to save the state
 * when a value changes it will send 'name' and 'value' to that page.
 *
 * @constructor
 * Create a new HttpProvider
 * @param {Object} config The configuration object
 */
Ext.state.HttpProvider = function(config){
    Ext.state.HttpProvider.superclass.constructor.call(this);
    this.url       = '';
    this.saveDelay = 500;

    this.lastdata  = "";
    Ext.apply(this, config);
    this.state = this.readValues();
};

Ext.extend(Ext.state.HttpProvider, Ext.state.Provider, {
    set: function(name, value) {
        if(typeof value == "undefined" || value === null) {
            this.clear(name);
            return;
        }
        this.setValue(name, value);
        Ext.state.HttpProvider.superclass.set.call(this, name, value);
    },

    clear: function(name) {
        this.clearValue(name);
        Ext.state.HttpProvider.superclass.clear.call(this, name);
    },

    readValues: function() {
        var state = {};
        for (var name in ExtState) {
            if(name!='remove') {
                state[name] = this.decodeValue(ExtState[name]);
            }
        }
        return state;
    },

    setValue: function(name, value) {
        encoded = this.encodeValue(value)
        if(ExtState[name] != undefined && ExtState[name] == encoded) {
            return;
        }
        ExtState[name] = encoded;
        this.queueChanges();
    },

    clearValue: function(name) {
        delete ExtState[name];
        this.queueChanges();
    },

    /* clear all values */
    clearAll: function() {
        for(var key in ExtState) {
            this.clear(key);
        }
        this.saveChanges();
    },

    /* set state from object */
    loadData: function(data) {
        for(var key in data) {
            this.set(key, data[key]);
        }
        this.saveChanges();
    },
    /* queue save changes */
    queueChanges: function() {
        window.clearTimeout(this.interval);
        this.interval = window.setTimeout(Ext.bind(this.saveChanges, this, []), this.saveDelay);
    },
    saveChanges: function(async) {
        if(async == undefined) { async = true; }
        var sum = Ext.JSON.encode(ExtState);
        if(sum == this.lastdata) {
            return;
        }
        this.lastdata = sum;
        var params = { task:  'update' };
        for (var name in ExtState) {
            params[name] = ExtState[name];
        }
        var conn = new Ext.data.Connection();
        conn.request({ url: this.url, params: params, async: async });
    }
});


/* create state provider */
var cp = new Ext.state.HttpProvider({
    url:      '[% url_prefix %]thruk/cgi-bin/panorama.cgi?state',
    lastdata:  Ext.JSON.encode(ExtState)
});
Ext.state.Manager.setProvider(cp);
