//LayerControl.js

LayerControl = function (newId, newMapObject, newMapId)
{
  if (arguments.length > 0)
    this.init(newId, newMapObject, newMapId);
};

LayerControl.prototype = new Object();
LayerControl.constructor = LayerControl;
LayerControl.superclass = Object.prototype;

LayerControl.prototype.init = function (newId, newMapObject, newMapId)
{
  this.id = newId;
  OBJECT_MANAGER.addControl(this,'labelControl',this.id);
  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.layerPanel = null;
};

LayerControl.prototype.drawGroups = function(targetElement)
{
  var htmlString='';
  var groupIndex = 0;
  for (var groupName in this.map.config["themeGroups"])
  {
    htmlString+='<div class="rightPanelFormHeaderActive" style="margin-bottom: 0px;" onmouseover="setClass(this,\'rightPanelFormHeaderHighlight\');" onmouseout="setClass(this,\'rightPanelFormHeaderActive\');"; onclick="togglePanel(\''+this.id+'['+groupIndex+']'+'layerControlForm\');">'+escapeHTML(groupName)+'</div>';
    htmlString +='<div class="layerControlForm" id="'+this.id+'['+groupIndex+']'+'layerControlForm">';
    htmlString += '<table width="100%" cellpadding="0" cellspacing="0">';
    for (var layerName in this.map.config["themeGroups"][groupName])
    {
      if (!this.map.config["themes"][layerName]["hideTheme"])
      {
        htmlString+='<tr><td width="25px" class="layerControlLeftColumn"><input id="'+this.id+'.'+layerName+'.visibility" type="checkbox" onclick="document.getGuiControl(\''+this.id+'\').toggleTheme(\''+layerName+'\');" '+((this.map.config["themes"][layerName]['visibility']==1)?(' checked="true"'):(''))+'></td><td class="layerControlLayerName">'+escapeHTML(this.map.config["themes"][layerName]["themeName"])+'</td></tr>';
      }
    }
    htmlString+='</table>';
    htmlString+='</div>';
    groupIndex++;
  }
  if (targetElement!=null)
    targetElement.innerHTML = htmlString;
  else
    this.layerPanel.innerHTML = htmlString;
};

LayerControl.prototype.toggleTheme = function (themeId)
{
  if (_MAP_INITIALIZED)
    this.map.setThemeState(themeId,!this.map.config["themes"][themeId]["visibility"]);
};

LayerControl.prototype.drawLayers = function (targetElement)
{
  var htmlString = '<table>';
  for (var currentLayer in this.map.config.layers)
  if (!this.map.config["themes"][currentLayer]["hideTheme"])
  {
    htmlString+='<tr><td><input type="checkbox"></td><td><span class="layerControlLayerName">'+currentLayer+'</span></td></tr>';  
  }
  htmlString+='</table>';
  targetElement.innerHTML = htmlString;
};

LayerControl.prototype.update = function()
{
  this.drawGroups(null);
  return;
};

LayerControl.prototype.updateFromGroupPanel = function()
{
  if (_MAP_INITIALIZED)
    this.map.redraw();
};
