//Selection.js
//Selection object.  Initialized from a selection controller.
//
//On initialization,a user-defined function is called (if it exists)
//  allowing customization.

Selection = function(newId,newThemeIndex,newSelectionControl,newMapObject,newData,newPrev,newNext)
{
  if (arguments.length > 0)
    this.init(newId,newThemeIndex,newSelectionControl,newMapObject,newData,newPrev,newNext);
};

Selection.prototype.init = function(newId,newThemeIndex,newSelectionControl,newMapObject,newData,newPrev,newNext)
{
  this.id = newId;
  this.themeIndex = newThemeIndex;
  this.selectionControl = newSelectionControl;
  this.mapObject = newMapObject;
  this.data = newData;
  this.prev = newPrev;
  this.next = newNext;
  this.handle = this.data[0];
  this.identifier = this.data[1][this.mapObject.config.themes[this.themeIndex].selectOptions.idField];
  this.keyField = this.mapObject.config.themes[this.themeIndex].selectOptions.keyField;
  this.keyValue = this.data[1][this.keyField];
  this.renderTarget = null;
  this.resultTemplate = this.mapObject.config.themes[this.themeIndex].selectOptions.resultTemplate;
};

Selection.prototype.getSelectionByKeyValue = function (newKeyValue)
{
  if (this.keyValue == newKeyValue)
    return this;
  else 
    if (this.next == null)
      return null;
    else
      return this.next.getSelectionByKeyValue(newKeyValue);
};

Selection.prototype.getSelectionByHandle = function(newSelectionHandle)
{
  if (this.handle == newSelectionHandle)
    return this;
  else 
    if (this.next == null)
      return null;
    else
      return this.next.getSelectionByHandle(newSelectionHandle);
};

Selection.prototype.render = function()
{
  if (this.renderTarget == null)
    return;
  var source = document.getTemplate(this.resultTemplate);
  if (source)
    this.renderTarget.innerHTML = source.run(this.data);
};

Selection.prototype.printData = function()
{
  var printTarget = document.getPrintTarget();
  var source = document.getTemplate(this.resultTemplate);
  if (source)
    printTarget.innerHTML = source.run(this.data);
  document.printCurrentData();
  return;
};

Selection.prototype.getAttachmentElem = function()
{
  return(this.renderTarget);
};

Selection.prototype.clear = function()
{
  if (this.prev == null)
    this.selectionControl.selections[this.themeIndex].list = this.next;
  else
    this.prev.next = this.next;
  if (this.next != null)
    this.next.prev = this.prev;
  this.next = null;
  this.prev = null; 
};

Selection.prototype.unlink = function()
{
  //unlinks node from selection list.
  //before doing this, verify that the list head is set properly!
  if (this.prev != null)
    this.prev.next = this.next;
  if (this.next != null)
    this.next.prev = this.prev;
  this.prev = null;
  this.next = null;
  
};

Selection.prototype.clearAll = function()
{
  if (this.next != null)
    this.next.clearAll();
  this.next = null;
  return this.clear();
};

Selection.prototype.length = function()
{
  if (this.next == null)
    return 1;
  else
    return this.next.length() + 1;
};

Selection.prototype.lastElement = function()
{
  if (this.next == null)
    return this;
  else
    return (this.next.lastElement());
};

Selection.prototype.createBuffer = function(newTargetTheme, newBufferDistance)
{
  this.mapObject.bufferOnSelection(this.handle, this.themeIndex, newTargetTheme, newBufferDistance, null, null, true);
};
