var __next = 1;

Daedalus.Comment = OpenLayers.Class.create();
Daedalus.Comment.prototype = 
  OpenLayers.Class.inherit(Daedalus.Base, {
    /**
     * @type Array of {Element}
     */
    pages: null,
    
    /**
     * @type int
     */
    page: 0,
    
    /**
     * @type Daedalus.Flag
     */
    flag: null,
    
    /**
     * @type Form
     */
    form: null,
    
    /**
     * @type Daedalus.Gadget
     */
    gadget: null,
    
    /**
     * @type bool
     */
    isFresh: false,
    
    /**
     * @type String
     */
    fldName: null,

    initialize: function(fldName, gadget) {
        this.displayClass = this.CLASS_NAME.replace(".", "");
        this.pages        = [ $('panel_p0'), $('panel_p1'), $('panel_p2') ];
        this.fldName      = fldName;
        this.gadget       = gadget;
        
        this.installFormHook();
        this.buildLayerSwitcher();
    },
    
    installFormHook: function() {
        this.form  = document.forms['q'];
        var self   = this;

        this.form.comment_save.onclick = function() {
            self.saveForm();
            self.showPage(0);
        };
        
        this.form.comment_cancel.onclick = function() {
            self.cancelForm();
            self.showPage(0);
        };

        this.form.comment_vote.onclick = function() {
            self.showPage(0);
        };

        $('comment_trash').onclick = function() {
            self.trashFlag();
            self.showPage(0);
        };
        
        this.loadForm();
    },

    getLayerSwitcher: function(layer, ctl) {
        return function(evt) {
            layer.setVisibility(ctl.checked, true);
        }
    },

    buildLayerSwitcher: function() {
    /*
        var div    = $('panel_layers');
        var map    = gadget.getMap();
        var layers = map.layers;
        for (var i = 0; i < layers.length; i++) {
            var layer   = layers[i];
            if (!layer.isBaseLayer) {
                var el      = document.createElement('input');
                var checked = layer.getVisibility();

                el.type           = 'checkbox';
                el.checked        = true;
                el.defaultChecked = true;
                el.name           = layer.name;
                el.value          = layer.name;
                el.onclick        = this.getLayerSwitcher(layer, el);

                div.appendChild(el);

                var label = document.createElement('span');
                label.innerHTML = layer.name;
                div.appendChild(label);

                div.appendChild(document.createElement("br"));
            }
        }
        */
    },

    clearForm: function() {
        this.setComment('');
    },
    
    setComment: function(text) {
        this.form.comment_body.value = text;
    },
    
    getComment: function() {
        return this.form.comment_body.value;
    },

    setData: function(data) {
        this.form[this.fldName].value = data;
    },
    
    getData: function() {
        return this.form[this.fldName].value;
    },

    formCommit: function() {
        var data = this.gadget.flags.getData();
        this.setData(Object.toJSON(data));
    },

    saveForm: function() {
        // Attach form data to flag
        this.flag.setComment(this.getComment());
        this.formCommit();
    },
    
    trashFlag: function() {
        this.gadget.flags.deleteFlag(
            this.flag.data.id
        );
        this.formCommit();
    },
    
    cancelForm: function() {
        // Delete the flag if it's new
        if (this.isFresh) {
            this.trashFlag();
        }
    },
    
    showPage: function(page) {
        if (this.page != page) {
            for (var p = 0; p < this.pages.length; p++) {
                this.pages[p].style.display 
                    = (p == page) ? "block" 
                                  : "none";
            }
            this.page = page;
        }
    },

    markerSelected: function(flag) {
        this.flag    = flag;
        this.setComment(flag.getComment());
        this.showPage(1);
        this.isFresh = false;
    },

    markerClicked: function(flag) {
        $('panel_comment').innerHTML = '"' + flag.getComment() + '"';
        this.showPage(2);
    },
    
    loadForm: function() {
        var data = this.getData();
        if (data.length) {
            this.gadget.flags.loadData(data);
        }
    },
    
    markerCreated: function(flag) {
        this.flag    = flag;
        this.clearForm();
        this.showPage(1);
        this.isFresh = true;
    },
    
    /** @final @type String */
    CLASS_NAME: "Daedalus.Comment"
});
