/**
 * Copyright (c) 2008-2009 The Open Source Geospatial Foundation
 *
 * Published under the BSD license.
 * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text
 * of the license.
 */

/** api: (define)
 *  module = GeoExt.ux
 *  class = LoadingStatusBar
 *  base_link = `Ext.StatusBar <http://extjs.com/deploy/dev/docs/?class=Ext.StatusBar>`_
 */
Ext.namespace("GeoExt.ux");

GeoExt.ux.LoadingStatusBar = Ext.extend(Ext.StatusBar, {

    /** api: config[map]
     *  ``OpenLayers.Map`` or :class:`GeoExt.MapPanel`
     */
    map: null,

    /** private: property[counter]
     *  ``Integer``
     */
    counter: 0,


    /** private: method[constructor]
     *  Construct the component.
     */
    constructor: function(config) {
        if (config.map && config.map instanceof GeoExt.MapPanel) {
            config.map = config.map.map;
        }
        if (!config.map) {
           config.map = GeoExt.MapPanel.guess().map;
        }
        GeoExt.ux.LoadingStatusBar.superclass.constructor.call(this, config);
    },

    /** private: method[initComponent]
     *  Initialize the component.
     */
    initComponent: function() {
        GeoExt.ux.LoadingStatusBar.superclass.initComponent.call(this);
        this.bind();
        this.on("beforedestroy", this.unbind, this);
    },

    /** private: method[bind]
     */
    bind: function() {
        if (this.map) {
            this.map.events.on({
                preaddlayer: this.registerLayer,
                removelayer: this.unregisterLayer,
                scope: this
            });
            for (var i = 0, len = this.map.layers.length; i < len; i++) {
                this.registerLayer(this.map.layers[i]);
            }
        }
    },

    /** private: method[unbind]
     */
    unbind: function() {
        if (this.map) {
            this.map.events.un({
                preaddlayer: this.registerLayer,
                removelayer: this.unregisterLayer,
                scope: this
            });
            for (var i = 0, len = this.map.layers.length; i < len; i++) {
                this.unregisterLayer(this.map.layers[i]);
            }
        }
    },

    /** private: method[increaseCounter]
     *  :param evt: ``Event`` Event
     */
    increaseCounter: function(evt) {
        this.counter++;
        this.showBusy();
    },

    /** private: method[decreaseCounter]
     *  :param evt: ``Event`` Event
     */
    decreaseCounter: function(evt) {
        this.counter--;
        if (this.counter == 0) {
            this.clearStatus();
        }
    },

    /** private: method[registerLayer]
     *  :param layer: ``OpenLayers.Layer``
     */
    registerLayer: function(layer) {
        layer.events.on({
            loadstart: this.increaseCounter,
            loadend: this.decreaseCounter,
            scope: this
        });
    },

    /** private: method[unregisterLayer]
     *  :param layer: ``OpenLayers.Layer``
     */
    unregisterLayer: function(layer) {
        layer.events.un({
            loadstart: this.increaseCounter,
            loadend: this.decreaseCounter,
            scope: this
        });
    }
});

/** api: xtype = gxux_loadingstatusbar */
Ext.reg('gxux_loadingstatusbar', GeoExt.ux.LoadingStatusBar);

