SelectionPanel = function (newId, newIndexFormContainer, newDetailFormContainer, newSelectionControl, newSelectionControlId)
{
  if (arguments.length > 0)
    this.init(newId, newIndexFormContainer, newDetailFormContainer, newSelectionControl, newSelectionControlId);
};

SelectionPanel.prototype = new Object;
SelectionPanel.constructor = SelectionPanel;

SelectionPanel.prototype.init = function (newId, newIndexFormContainer, newDetailFormContainer, newSelectionControl, newSelectionControlId)
{
  this.id = newId;
  if (newSelectionControlId != null)
  {
    this.selectionControl = OBJECT_MANAGER.getControl(newSelectionControlId);
    this.selectionControlId = newSelectionControlId;
  }
  else
  {
    this.selectionControl = newSelectionControl;
    this.selectionControlId = newSelectionControl.id;
  }
  this.selectionControl.selectionPanel = this;
  OBJECT_MANAGER.addControl(this,'selectionControl', newId);
  this.indexPanel = newIndexFormContainer;
  this.displayPanel = newDetailFormContainer;
};

SelectionPanel.prototype.initialize = function()
{
  this.redrawIndex();
  this.indexPanel.style.display = 'block';
  this.displayPanel.style.display = 'none';
};

SelectionPanel.prototype.redrawIndex = function()
{
  var indexHtml = '<table width="100%" cellpadding="0" cellspacing="0">';
  if (!this.selectionControl.mapObject.selectionsExist)
  {
    indexHtml += '<tr class="selectionIndexHeader"><td colspan="2">Selection Index Not Available</td></tr>'
    + '<tr><td width="5px"></td><td width="*"  class="selectionIndexEmptyTheme">No selectable themes exist.</td></tr>'
    + '</table>';
  }
  else
  {
    for (var currentTheme in this.selectionControl.selections)
    {
      indexHtml += '<tr class="selectionIndexHeader">'
      + '  <td colspan="4" class="selectionIndexHeader">'
      + this.selectionControl.mapObject.config.themes[currentTheme].themeName
      + '  </td>'
      + '  <td style="text-align:  right;" class="selectionIndexHeader">'
      + '    <img src="./'+GuiWidget.THEME_PATH+'/'+GuiWidget.THEME+'/images/selectionClearTheme.png" title="Clear All Selections" onclick="document.getGuiControl(\''+this.selectionControl.id+'\').clearTheme(\''+currentTheme+'\');" style="cursor:pointer;">'
      + '  </td>'
      + '</tr>';
      if (this.selectionControl.selections[currentTheme].list == null)
      {
        indexHtml += '<tr>'
        + '  <td width="5px">'
        + '  </td>'
        + '  <td width="*">'
        + 'No items selected'
        + '  </td>'
        + '  <td></td>'
        + '  <td></td>'
        + '  <td></td>'
        + '</tr>';
      }
      else
      {
        var currentSelection = this.selectionControl.selections[currentTheme].list;
        var rowcount = 0;
        while (currentSelection != null)
        {
          indexHtml+= '<tr style="'+(((rowcount%2)==0)?'background: #FEFEFF;':'')+'">'
          + '<td width="5px"></td>'
          + '<td style="font-size:  8pt; border-left:  1px #b0c4de solid; border-bottom:  1px #b0c4de solid; padding-left:  2px;">'
          + '<span class="pseudolink" onclick="document.getGuiControl(\''+this.id+'\').displaySelection(\''+currentTheme+'\','+currentSelection.handle+');">'+currentSelection.identifier+'</span>'
          + '</td>'
          + '<td style="text-align:right; border-bottom:  1px #b0c4de solid;">';
          indexHtml+= '<img src="./'+GuiWidget.THEME_PATH+'/'+GuiWidget.THEME+'/images/selectionPrint.png" title="Print Selection" onclick="document.getGuiControl(\''+this.id+'\').printSelection(\''+currentTheme+'\','+currentSelection.handle+');" style="cursor:pointer;"></td>'
          + '<td style="text-align:right; border-bottom:  1px #b0c4de solid;"><img src="./'+GuiWidget.THEME_PATH+'/'+GuiWidget.THEME+'/images/selectionZoom.png"  title="Zoom To Selection" onclick="document.getGuiControl(\''+this.selectionControl.id+'\').zoomTo(\''+currentTheme+'\','+currentSelection.handle+');" style="cursor:pointer;"></td>'
          + '<td style="text-align:right; border-bottom:  1px #b0c4de solid;"><img src="./'+GuiWidget.THEME_PATH+'/'+GuiWidget.THEME+'/images/selectionClear.png" title="Clear Selection" onclick="document.getGuiControl(\''+this.selectionControl.id+'\').clearSelection(\''+currentTheme+'\','+currentSelection.handle+');" style="cursor:pointer;"></td>'
          + '</tr>';
          currentSelection = currentSelection.next;
          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.indexPanel.innerHTML = indexHtml;
};

SelectionPanel.prototype.displaySelection = function(newThemeIndex, newSelectionHandle)
{
  this.selection = this.selectionControl.getSelection(newThemeIndex, newSelectionHandle);
  this.currentSelectionTheme = newThemeIndex;
  this.currentSelectionHandle = newSelectionHandle;
  if (this.selection == null)
    alert('Application Error in\n'+this.id+'.displaySelection('+newThemeIndex+','+newSelectionHandle+'):\nSelection '+newSelectionHandle+' on theme '+newThemeIndex+' not found\nUnable to display selection');
  else
  {
    this.selection.renderTarget = this.displayPanel;
    this.selection.render();
    this.indexPanel.style.display = 'none';
    this.displayPanel.style.display = 'block';
    document.getElementById("selectionIndexToggle").style.display = 'block';
    if (this.onSelectionDisplay)
      this.onSelectionDisplay();
  }
};

SelectionPanel.prototype.displayIndex = function()
{
  this.indexPanel.style.display = 'block';
  this.displayPanel.style.display = 'none';
  document.getElementById("selectionIndexToggle").style.display = 'none';
  if (this.onIndexDisplay)
    this.onIndexDisplay();
};

SelectionPanel.prototype.zoomTo = function(newThemeIndex, newSelectionHandle)
{
  this.selectionControl.zoomTo(newThemeIndex, newSelectionHandle);
  if (this.onZoomTo)
    this.onZoomTo();
};

SelectionPanel.prototype.clearSelection = function(newThemeIndex, newSelectionHandle)
{
  this.selectionControl.clearSelection(newThemeIndex, newSelectionHandle);
  this.displayIndex();
};

SelectionPanel.prototype.clearTheme = function(newThemeIndex)
{
  this.selectionControl.clearTheme(newThemeIndex);
  this.displayIndex();
};

SelectionPanel.prototype.printSelection = function(newThemeIndex, newSelectionHandle)
{
  this.selectionControl.printSelection(newThemeIndex, newSelectionHandle);
};

SelectionPanel.prototype.getAttachmentElem = function(newThemeIndex, newSelectionHandle)
{
  return(this.selectionControl.getAttachmentElem(newThemeIndex, newSelectionHandle));
};

SelectionPanel.prototype.createBuffer = function (newTargetTheme, newBufferDistance)
{
  //based on the selection specified by this.selection, which is the currently visible selection.
  newBufferDistance = isNaN(Math.abs(parseFloat(newBufferDistance)))?(1):(Math.abs(parseFloat(newBufferDistance)));
  document.getElementById('selectionBufferDistanceInput').value = newBufferDistance;
  this.selection.createBuffer(newTargetTheme, newBufferDistance);
};
