//BufferQueryPanel.js

BufferQueryPanel = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newBufferObject, newThemeId, newQueryId)
{
  if (arguments.length > 0)
    this.init(newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newBufferObject, newThemeId, newQueryId);
};

BufferQueryPanel.prototype = new GuiWidget();
BufferQueryPanel.constructor = BufferQueryPanel;
BufferQueryPanel.superclass = GuiWidget.prototype;
 
BufferQueryPanel.prototype.init = function (newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, newBufferObject, newThemeId, newQueryId)
{
  BufferQueryPanel.superclass.init.call(this, newId, newParent, newXPosition, newYPosition, newZIndex, newWidth, newHeight, newVisibility, "themeGroupHeader");
  this.id           = newId;
  this.bufferObject = newBufferObject;
  this.queryId      = newQueryId;
  this.themeId      = newThemeId;

  this.queryConfig = this.bufferObject.bufferConfig[this.themeId].filterQueries[this.queryId];
  this.formWidgets = new Array;
  this.inputElements = new Array;
  this.captionElement = new GuiWidget(this.id+"captionElement", this.element, 0, 0, 0, this.width(), 20, true, "themeGroupHeader");
  this.captionElement.element.cbe.innerHtml(this.queryConfig.description);
  this.formContainer = new GuiWidget (this.id+"formContainer", this.element, 0, this.captionElement.height(), 0, this.width(), 0, true, "queryObjectBottom");
  var totalHeight = 20;
  for (var currentField in this.queryConfig.fieldList)
  {
    this.formWidgets[currentField] = new GuiWidget(this.id+"."+currentField+"FormElement", this.formContainer.element, 0, this.formContainer.height(), 0, this.width(), 25, true, "queryObjectForm");
    switch (this.queryConfig.fieldList[currentField].fieldType)
    {
      case 'labelField':
        this.formWidgets[currentField].element.cbe.innerHtml(this.queryConfig.fieldList[currentField].value);
        break;
      case 'textField':
        this.formWidgets[currentField].element.cbe.innerHtml(this.queryConfig.fieldList[currentField].caption
             +'<input type="text" id="'+this.id+'['+currentField+'].inputElement" value = "'
             +this.queryConfig.fieldList[currentField].initialValue
             +'" size = "'+this.queryConfig.fieldList[currentField].width+'">');
        this.inputElements[currentField] = document.getElementById(this.id+"["+currentField+"].inputElement");
        break;
      case 'selectField':
        selectBoxHTML = '<select id="'+this.id+'['+currentField+'].inputElement">';
        for (var optionIndex=0; optionIndex< this.queryConfig.fieldList[currentField].optionList.length; optionIndex++)
        {
          selectBoxHTML += this.buildSelectOptionString(this.queryConfig.fieldList[currentField].optionList[optionIndex], currentField);
        }
        selectBoxHTML +='</select>';
        this.formWidgets[currentField].element.cbe.innerHtml(this.queryConfig.fieldList[currentField].caption+selectBoxHTML);
        this.inputElements[currentField] = document.getElementById(this.id+"["+currentField+"].inputElement");
        break;
      default:
        alert('unknown field type in query '+this.id+':'+this.queryConfig.fieldList[currentField].fieldType);
        break;
    }
    this.formContainer.height(this.formContainer.height()+25);
    totalHeight += 25;
  };
  
  this.pointButton = new RadioButton(this.id+"pointButton", this.formContainer.element, 10, this.formContainer.height(), 0, 100,20,true,'bufferPointButton','',this.bufferObject.bufferRadioButtonGroup);
  totalHeight+=25;
  this.formContainer.height(this.formContainer.height()+25);
  this.pointButton.bufferPanel = this.bufferObject;
  this.pointButton.formPanel = this;
  this.pointButton.clickEvent = function(e)
  {
    this.bufferPanel.mapObject.mapPanel.clickMode = 'BufferXY';
    this.bufferPanel.activeQuery= this.formPanel;
  };
  this.pointButton.queryPanel = this;
  
  this.selectionButton = new ImageButton(this.id+"selectionButton", this.formContainer.element, 10, this.formContainer.height(), 0, 100,20,true,'bufferSelectionButton','');
  this.selectionButton.bufferPanel = this.bufferObject;
  this.selectionButton.activeQuery = this;
  this.selectionButton.clickEvent = function(e)
  {
    this.bufferPanel.bufferFromSelection(this.activeQuery);
  };
  totalHeight+=25;
  this.formContainer.height(this.formContainer.height()+25);
  
  this.height(totalHeight);
};

BufferQueryPanel.prototype.buildSelectOptionString = function(optionObject, currentField)
{
  if (optionObject.type == 'optgroup')
  {
    var optionHTML = '<optgroup label='+optionObject.description+'>';
    for (var optionIndex=0; optionIndex < optionObject.value.length; optionIndex++)
    {
      optionHTML += this.buildSelectOptionString(optionObject.value[optionIndex],currentField);
    }
    optionHTML += '</optgroup>';    
  }
  else
    var optionHTML = ('<option value="'+optionObject.value+'"'
      +((this.queryConfig.fieldList[currentField].defaultValue == optionObject.value)?' selected ':'')
      +' >'
      +optionObject.description
      +'</option>'
    );
  return optionHTML;
};

BufferQueryPanel.prototype.getDataPairs = function()
{
  var dataPairs = Array();
  var currentPair = 0;
  for (var currentField in this.inputElements)
  {
    var newFieldValue = escapeHTML(((this.inputElements[currentField].value == '')?(this.queryConfig.fieldList[currentField].defaultValue):(this.inputElements[currentField].value)));
    dataPairs[currentPair] = new Array(this.queryConfig.fieldList[currentField].fieldName,newFieldValue);
    currentPair++;
  }
  return dataPairs;
};

BufferQueryPanel.prototype.resize = function(newWidth, newHeight)
{
  this.element.cbe.resizeTo(newWidth, this.height());  
};

BufferQueryPanel.prototype.isActive = function()
{
  return this.pointButton.isActive();
};

BufferQueryPanel.prototype.setInactive = function()
{
  this.pointButton.setInactive();
};


