
/*extern ERDAS*/

ERDAS.defns('ic');


/**
 * @constructor
 *
 * @class
 * A simple holder for a feature being edited. Will make sure
 * only one panel is opened at a time.
 *
 * @param {HTMLElement, string} elt The HTML element where
 * to build the holder
 */
ERDAS.ic.FeaturePanelHolder = function(elt, map, config) {

        var dh = ERDAS.ui.DomHelper.forDocument (document);

        this.elt = dh.get (elt);
        this.map = map;

        this.noFeatureElt = this.elt.childNodes [0];

        //<log>
        this.debug = ERDAS.Loader.getInstance ().debug;
        if (this.debug) {

                this.logger = ERDAS.misc.createDedicatedLogger (
                        "FeaturePanelHolder");
        }
        //</log>

        this.panel = null;
};


ERDAS.misc.addToPrototype (
        ERDAS.ic.FeaturePanelHolder,
{
        withNewPanel : function (featureTypePointer,
                                 callbacks,
                                 config) {

                //<log>
                if (this.debug) {

                        this.logger.debug ("withNewPanel() called");
                }
                //</log>

                if (this.panel) {

                        this.panel.withReleasedFeature (
                                true,
                                {
                                        success  : this.cbFeatureReleasedAndCreatePanel,
                                        failure  : this.cbFeatureFailedReleasing,
                                        scope    : this,
                                        argument : [featureTypePointer,
                                                    callbacks,
                                                    config]
                                });
                } else {

                        this.doCreatePanel.apply (this, arguments);
                }
        },



        cbFeatureReleasedAndCreatePanel : function (unused, arg) {

                this.doCreatePanel.apply (this, arg);
        },



        cbFeatureFailedReleasing : function () {

                alert (ERDAS.misc.i18n ("ic.featurepanelholder.errors.releasepreviousfeaturefailed"));
        },



        doCreatePanel : function (featureTypePointer,
                                  callbacks,
                                  config) {

                //<log>
                if (this.debug) {

                        this.logger.debug ("doCreatePanel() called");
                }
                //</log>

                this.destroyPanel ();

                //<log>
                if (this.debug) {

                        this.logger.debug ("instanciating new SimpleFeaturePanel2");
                }
                //</log>

                this.panel = new ERDAS.feature.SimpleFeaturePanel2 (
                        this.elt,
                        this.map,
                        featureTypePointer,
                        config);

                this.panel.onFeatureReleased.subscribe (
                        this.destroyPanel,
                        this,
                        true);

                //<log>
                if (this.debug) {

                        this.logger.debug ("calling user callbacks");
                }
                //</log>

                // Remove the "no features" thingy
                if (this.noFeatureElt.parentNode === this.elt) {

                        this.elt.removeChild (this.noFeatureElt);
                }

                callbacks.success.call (
                        callbacks.scope || this,
                        this.panel,
                        callbacks.argument);
        },



        destroyPanel : function (type, args, obj) {

                //<log>
                if (this.debug) {

                        this.logger.debug ("destroyPanel() called");
                }
                //</log>

                // Destroy previous one if needed. We are sure the
                // feature was already released.
                var dh;
                if (this.panel) {

                        //<log>
                        if (this.debug) {

                                this.logger.debug ("Panel is present. Destroying");
                        }
                        //</log>

                        this.panel.destroy ();
                        this.panel = null;

                        dh = ERDAS.ui.DomHelper.forDocument (document);
                        dh.clr (this.elt);
                        this.elt.appendChild (this.noFeatureElt);
                }
        }
});


