/***
AltLegendControl.js
library for handling the map markup functions.

Dependencies:
MapLib/map.js
MapLib/mapImage.js

***/

AltLegendControl = function (newId, newMapObject, newMapId)
{
  if (arguments.length > 0)
    this.init(newId, newMapObject, newMapId);
};

AltLegendControl.prototype = new Object();
AltLegendControl.constructor = AltLegendControl;

AltLegendControl.prototype.init = function (newId, newMapObject, newMapId)
{
  this.id = newId;
  if (newMapId!=null)
  {
    this.mapObject = OBJECT_MANAGER.getControl(newMapId);
    this.mapId = newMapId;
  }
  else
  {
    this.mapObject = newMapObject;
    this.mapId = newMapObject.id;
  }
  this.mapObject.altLegendControl = this;
  this.altLegendPanel = null;
  this.activeLegends = new Array();  // index on themeID, retrieves index of alt legend 0 (default) ... n
  this.themeLegends = new Array();  // index on themeID, array of 0..n { legendName, legendLabel }
  this.configured = false;
  this.haveAltLegends = false; // default
};

AltLegendControl.prototype.initialize = function()
{
  if (!this.configured)
  {
    this.setConfig();
    this.configured = true;
  }
  if (this.altLegendPanel)
    this.drawAltLegendPanel();
};

AltLegendControl.prototype.setConfig = function()
{
  var config = this.mapObject.config;
  for (var themeID in config.themes)
  {
    if (config.themes[themeID].altLegends.length > 0)
    {
      this.haveAltLegends = true;
      this.activeLegends[themeID] = 0;
      this.themeLegends[themeID] = new Array();
      this.themeLegends[themeID][0] = { legendName:'', legendLabel:'Default Legend'};
      for(var idx in config.themes[themeID].altLegends)
        this.themeLegends[themeID][this.themeLegends[themeID].length] = config.themes[themeID].altLegends[idx];
    }
  }
};

AltLegendControl.prototype.getThemeNameByThemeID = function(themeID)
{
  for(var id in document.mapObject.config.themes)
    if (id == themeID)
      return(document.mapObject.config.themes[id].themeName);
  return('');
};

AltLegendControl.prototype.assignAltLegendPanel = function(elem)
{
  this.altLegendPanel = elem;
};

AltLegendControl.prototype.getActiveLegend = function(themeID)
{
  if (!this.themeLegends[themeID])
    return(null);
  return(this.themeLegends[themeID][this.activeLegends[themeID]]);
};
AltLegendControl.prototype.getActiveLegendIdx = function(themeID)
{
  if (!this.themeLegends[themeID])
    return(-1);
  return(this.activeLegends[themeID]);
};

AltLegendControl.prototype.setActiveLegend = function(themeID,altLegendIdx)
{
  this.activeLegends[themeID] = altLegendIdx;
  var legend = this.getActiveLegend(themeID);
  if (!legend)
  {
    alert('No defined active legend for theme id: '+themeID);
    return;
  }
  var requestInfo = new Object();
  requestInfo.mode = 'setActiveLegend';
  requestInfo.themeID = themeID;
  requestInfo.altLegendIdx = altLegendIdx;
  requestInfo.newLegend = legend;
  this.mapObject.setWaiting(true);
  var doc = makeASyncPostRequest(this,requestInfo,XMLRPC_URL,'GIS.Theme.activeLegend.set',this.mapObject.sessionID,0,themeID,legend.legendName);
};

AltLegendControl.prototype.callback = function(serverReplyDoc, pendingOperation)
{
  this.mapObject.setWaiting(false);
  if (!validateXMLDoc(serverReplyDoc))
    return null;

  var clientReply = new XMLRPCResponse();
  clientReply.setResponseByDoc(serverReplyDoc);
  if(clientReply.isFault())
  {
    alert('Fault '+clientReply.getFaultCode()+': '+clientReply.getFaultString());
    var data = null;
  }
  else
    var data = clientReply.getObject();

  if(data != null)
  {
    switch (pendingOperation.mode)
    {
      case 'setActiveLegend':
        this.mapObject.legendSync = false;
        this.mapObject.redraw();
        break;
    }
  }
};

AltLegendControl.prototype.drawAltLegendPanel = function()
{
  var indexHtml = '<table width="100%" cellpadding="0" cellspacing="0">';
  for (var themeID in this.themeLegends)
  {
    indexHtml += '<tr class="selectionIndexHeader">'
     +  '  <td colspan="4" class="selectionIndexHeader">'
     +  this.getThemeNameByThemeID(themeID)
     +  '  </td>'
     +  '  <td style="text-align:  right;" class="selectionIndexHeader">'
     +  '    '
     +  '  </td>'
     +  '</tr>';
    var rowcount = 0;
    for(var altLegendIdx in this.themeLegends[themeID])
    {
      indexHtml+= '<tr style="'+(((rowcount%2)==0)?'background: #FEFEFF;':'')+'">'
       +  '  <td width="5px">'
       +  '  </td>'
       +  '  <td width="100%" style="font-size:  8pt; border-left:  1px #b0c4de solid; border-bottom:  1px #b0c4de solid; padding-left:  2px;">'
       +      '<span class="pseudolink" onclick="document.altLegendControl.setActiveLegend(\''+themeID+'\','+altLegendIdx+');this.getElementsByTagName(\'INPUT\')[0].checked=true;"><INPUT type="radio" name="alternateLegendRadio'+themeID+'" '+((altLegendIdx == this.activeLegends[themeID])?'checked="checked"':'')+'>';
      indexHtml += escapeHTML(this.themeLegends[themeID][altLegendIdx].legendLabel)+'</span>'
       +  '  </td>'
       +  '  <td width="1px" style="background:  #FEFEFF;"></td>'
       +  '  <td width="1px" style="background:  #FEFEFF;"></td>'
       +  '  <td width="1px" style="background:  #FEFEFF;"></td>'
       +  '</tr>';
      rowcount++;
    }
  }
  indexHtml+='<tr height="4px" class="selectionIndexFooter">'
   +  '<td width="5px"></td>'
   +  '<td width="*" style="border-left:  1px black solid; border-top:  1px black solid;"></td>'
   +  '<td width="15px" style="border-top:  1px black solid;"></td>'
   +  '<td width="15px" style="border-top:  1px black solid;"></td>'
   +  '<td width="15px" style="border-top:  1px black solid;"></td>'
   +'</tr>'
   + '</table>';
  this.altLegendPanel.innerHTML = indexHtml;
};
