/**
 * A panel containing Legends (see Legend.js) for all of the visible layers. This panel
 * is used in the Legend tab in the east pane.
 */
LegendPanel = function() {
   
   LegendPanel.superclass.constructor.call(this, {
      border: false
      ,id: 'legend-panel'
      ,layout: 'fit'
      ,buttonAlign: 'center'
      ,autoScroll: true
      ,buttons: [{
         text: 'Apply'
         ,id: 'lp-apply'
         ,disabled: true
         ,handler: function() {
            var visibleLayers = tree.getChecked('id');

            for (var i=0; i < visibleLayers.length; i++) {
               
               // Copy local styling changes for each layer into the JSON
               layerArray[visibleLayers[i]].getLegend().apply();

               // Apply the changes to the server
               layerArray[visibleLayers[i]].applyStyle(); 
            }
            var theLPResetButton = Ext.getCmp('lp-reset');
            theLPResetButton.enable();
         }
      },{
         text: 'Reset All Layers'
         ,id: 'lp-reset'
         ,disabled: false
         ,handler: function() {
            var visibleLayers = tree.getChecked('id');
            for (var i=0; i < visibleLayers.length; i++) {
               layerArray[visibleLayers[i]].reset(); 
            }
         }
      }]
   });
   this.doLayout();
}
Ext.extend(LegendPanel, Ext.Panel, {
   legendItems: [],      // List of legend objects in the panel

   /**
    * Called when the legend tab is activated. Hides all of the legends, then selectively
    * shows the visible ones
    */
   activatePanel: function() {
      // Get all of the currently visible layers
      var visibleLayers = tree.getChecked('id');

      // hide all of the legendItems
      for (var i=0; i < this.legendItems.length; i++) {
         this.legendItems[i].hide();
      }

      // Show visible layers if in legendItems, if not, add it
      for (var i=0; i < visibleLayers.length; i++) {
         var legend = layerArray[visibleLayers[i]].getLegend();//.getElement();
         legend.hideButtons();
         var found = false;

         for (var j=0; j < this.legendItems.length; j++) {
            if (this.legendItems[j] === legend) {
               this.legendItems[j].show();
               found = true;
            }
         }

         // If we didn't find this layer, add it to the array
         if (!found) {
            this.legendItems.push(legend);
         }
         // Add the legend to the panel
         this.add(legend.getElement());
      }
      this.doLayout();
   },

   /**
    * Called when the Legend panel is deactivated (switched away from). Shows all
    * of the legend items. This is necessary because the legend item objects are 
    * shared between the legend panel and the context menu. If we didn't do this,
    * any legend that we left hidden above wouldn't appear in the context menu.
    */
   deactivatePanel: function() {
      // Show all of the legends
      for (var i=0; i < this.legendItems.length; i++) {
         this.legendItems[i].show();
         this.legendItems[i].showButtons();
         
         // Synchronize the legend with the layer JSON in case the user made styling
         // changes but didn't apply them.
         this.legendItems[i].synchronize();
      }
      this.doLayout();
   }
});
var legendPanel = new LegendPanel();

