//layerPanel.js
//Panel for display and control of map layer groups.

LayerPanel = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject, newMapId)
{
  if (arguments.length > 0)
    this.init(newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject, newMapId);
};

LayerPanel.prototype = new GuiWidget();
LayerPanel.constructor = LayerPanel;
LayerPanel.superclass = GuiWidget.prototype;

LayerPanel.prototype.init = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newMapObject, newMapId)
{
  LayerPanel.superclass.init.call(this, newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, "LayerPanel");
  if (newMapId!=null)
  {
    this.map = OBJECT_MANAGER.getControl(newMapId);
    this.mapId = newMapId;
  }
  else
  {
    this.map = newMapObject;
    this.mapId = newMapObject.id;
  }
  if (this.map)
    this.map.layerControl = this;
  this.groupWidgetList = new Object;
  this.activeThemeWidget = null;
  this.altLegendWidgets = new Array();
};
 
LayerPanel.prototype.draw = function()
{
  this.width(parseInt(this.element.style.width)-4);
  var yPosition = 0;
  var maxHeight = 0;
  for (var groupName in this.map.config["themeGroups"])
  {
    this.groupWidgetList[groupName] = new Object;
    this.groupWidgetList[groupName]["groupName"] = groupName;
    this.groupWidgetList[groupName]["widget"] = new GuiWidget(this.id+"groupHeader", this.element, 0, yPosition, 0, this.width(), 20, true, "themeGroupHeader");
    this.groupWidgetList[groupName]["widget"].element.cbe.innerHtml(escapeHTML(groupName));
    yPosition += 20;
    this.groupWidgetList[groupName]["layers"] = new Object;
    for (var layerName in this.map.config["themeGroups"][groupName])
    {
      if (!this.map.config["themes"][layerName]["hideTheme"])
      {
        xPosition = 0;
        this.groupWidgetList[groupName]["layers"][layerName] = new Object;
        this.groupWidgetList[groupName]["layers"][layerName]["layerName"] = layerName;
        this.groupWidgetList[groupName]["layers"][layerName]["toggleButton"] = new ToggleButton(this.id+"layer"+layerName+"toggle", this.element, xPosition, yPosition, 0, 20, 20, true, "displayToggle", "Set Layer Invisible", "Set Layer Visible",false);
        xPosition += this.groupWidgetList[groupName]["layers"][layerName]["toggleButton"].width();
  
        this.groupWidgetList[groupName]["layers"][layerName]["toggleButton"].mapObject = this.map;
        this.groupWidgetList[groupName]["layers"][layerName]["toggleButton"].themeName = layerName;
        this.groupWidgetList[groupName]["layers"][layerName]["toggleButton"].clickOn = function(e)
        {
          this.mapObject.setThemeState(this.themeName, true);
        };
        this.groupWidgetList[groupName]["layers"][layerName]["toggleButton"].clickOff = function(e)
        {
          this.mapObject.setThemeState(this.themeName, false);
        };
  
        if(this.map.config["themes"][layerName].altLegends.length > 0)
        {
          var altButton = new ImageButton(this.id+"layer"+layerName+"AltLegendButton", this.element, xPosition, yPosition, 0, 20,20,true,'showAltLegendsButton','Show Alternate Legends');
          altButton.layerPanel = this;
          altButton.thisLayerName = layerName;
          altButton.clickEvent = function(e)
          {
            this.layerPanel.showThemeAltLegends(this.thisLayerName);
          };
          /* make the altLegend widget */
          altButton.form = new GuiWidget(this.id+"layer"+layerName+"AltLegendForm", this.element, xPosition, yPosition, 1,this.width()-xPosition-1, 200, false, "AltLegendsForm");
          altButton.form.element.cbe.zIndex(250);
          this.populateAltLegendForm(altButton.form,layerName);
          xPosition += altButton.width();
          var calcMaxHeight = yPosition+altButton.form.height();
          if (calcMaxHeight > maxHeight) maxHeight = calcMaxHeight;
        }
        
        this.groupWidgetList[groupName]["layers"][layerName]["themeLabel"] = new GuiWidget(this.id+"layer"+layerName, this.element, xPosition, yPosition, 0, this.width()-xPosition, 20, true, (this.map.config["themes"][layerName]["selectOptions"]!=null)?"themeGroupLayer":"themeGroupLayerNonSelectable");
        this.groupWidgetList[groupName]["layers"][layerName]["themeLabel"].element.cbe.innerHtml(escapeHTML(this.map.config["themes"][layerName]["themeName"]));
        this.groupWidgetList[groupName]["layers"][layerName]["themeLabel"].element.cbe.addEventListener('click',EVENT_LISTENER, false);
        if (this.map.config["themes"][layerName]["selectOptions"]!=null)
        {
          this.groupWidgetList[groupName]["layers"][layerName]["themeLabel"].themeName = layerName;
          this.groupWidgetList[groupName]["layers"][layerName]["themeLabel"].mapObject = this.map;
          this.groupWidgetList[groupName]["layers"][layerName]["themeLabel"].clickEvent = function(e)
          {
            this.mapObject.setActiveTheme(this.themeName);
          };
        }
        yPosition += 20;
        
      }
    }
  }
  if (maxHeight > yPosition)
    this.height(maxHeight);
  else
    this.height(yPosition);
};

LayerPanel.prototype.populateAltLegendForm = function(altform,layerName)
{
  var x = 0;
  var h = 0;
  /* caption section */
  var w = altform.width();
  altform.mainCaptionElement = new GuiWidget(this.id+"layer"+layerName+"AltLegendFormMainCaptionElement", altform.element, x, h, 0, w, 20, true, "AltLegendsHeader");
    altform.mainCaptionElement.element.cbe.innerHtml(this.map.config["themes"][layerName]["themeName"]);
  h += altform.mainCaptionElement.height();
  
  /* one for each default and alt legend */
  this.altLegendWidgets[layerName] = new Object;
  this.altLegendWidgets[layerName].radioGrp = new RadioButtonGroup(this.id+"layer"+layerName+"RadioButtonGroup");
  this.setAltLegendRadio(layerName,'');  // default. TODO: change this to handle reinit
  var altLegends = this.map.config["themes"][layerName].altLegends;
  var defaultLegend = new Object;
  defaultLegend.legendName = '';
  defaultLegend.legendLabel = '<i>Default</i>';
  altLegends[-1] = defaultLegend; // add the default
  for(var lcv=-1;lcv < altLegends.length;lcv++)
  {
    var radioButton = new RadioButton(this.id+"layer"+layerName+"RadioButtonGroupButton"+lcv, altform.element, x, h, 0, 20, 20, true, "setAltLegendButton", "Set Legend", this.altLegendWidgets[layerName].radioGrp);
    radioButton.layerName = layerName;
    radioButton.legendName = altLegends[lcv].legendName;
    radioButton.layerPanel = this;
    radioButton.clickEvent = function(e)
    {
      this.layerPanel.setAltLegendRadio(this.layerName,this.legendName);
    };
    var radioLabel = new GuiWidget(this.id+"layer"+layerName+"RadioButtonGroupLabel"+lcv, altform.element, x+radioButton.width(), h+3, 0, w-radioButton.width(), 20, true, "");
    radioLabel.element.cbe.innerHtml(escapeHTML(altLegends[lcv].legendLabel));
    h += radioButton.height();
    if(altLegends[lcv].legendName == this.altLegendWidgets[layerName].currentRadioLegend)
    {
      this.altLegendWidgets[layerName].radioGrp.setActiveButton(lcv+1);
      radioButton.setActive();
    }
  }
  
  x=0;
  /* apply button */
  altform.applyButton = new ImageButton(this.id+"layer"+layerName+"AltLegendFormApplyButton", altform.element, x, h, 0, 50,20,true,'applyButton','Apply new Alternate Legend');
    altform.applyButton.layerName = layerName;
    altform.applyButton.layerPanel = this;
    altform.applyButton.clickEvent = function(e)
    {
      this.layerPanel.applyAltLegend(this.layerName);
    };
    x+=altform.applyButton.width();
    altform.closeButton = new ImageButton(this.id+"layer"+layerName+"AltLegendFormCloseButton", altform.element, x, h, 0, 50,20,true,'hideAltLegendsButton','Close');
    altform.closeButton.altform = altform;
    altform.closeButton.clickEvent = function(e)
    {
      this.altform.hide();
    };
  h += altform.applyButton.height();
  
  h += 10;
  altform.height(h);
};

LayerPanel.prototype.setAltLegendRadio = function(layerName,legendName)
{
  this.altLegendWidgets[layerName].currentRadioLegend = legendName;
};

LayerPanel.prototype.applyAltLegend = function(layerName)
{
  var legendName = this.altLegendWidgets[layerName].currentRadioLegend;
  var xmlrpcResponse = makeSyncPostRequest(XMLRPC_URL,'GIS.Theme.activeLegend.set',this.map.sessionID,0,layerName,legendName);
  var data = getXMLRPCResponseObject(xmlrpcResponse);
  this.map.legendSync = false;
  this.map.redraw();
};

LayerPanel.prototype.showThemeAltLegends = function(layerName)
{
  var form = document.getWidgetById(this.id+"layer"+layerName+"AltLegendForm");
  if (form)
    form.show();
};

LayerPanel.prototype.update = function()
{
  for (var groupName in this.map.config["themeGroups"])
  {
    for (var layerName in this.map.config["themeGroups"][groupName])
    {
      if (!this.map.config["themes"][layerName]["hideTheme"])
      {
        this.groupWidgetList[groupName]["layers"][layerName]["toggleButton"].setState(this.map.config["themes"][layerName]["visibility"]);
        this.groupWidgetList[groupName]["layers"][layerName]["themeLabel"].element.cbe.innerHtml(this.map.config["themes"][layerName]["themeName"]+(((this.map.config.activeTheme == layerName)&&(this.map.selectionsExist))?" <i>(Active)</i>":""));
      }
    }
  }     
};

