LabelControl = function(newId, newMapObject, newMapId)
{
  OBJECT_MANAGER.addControl(this,'labelControl', newId);
  this.id = newId;
  if (newMapId!=null)
  {
    this.mapObject = OBJECT_MANAGER.getControl(newMapId);
    this.mapId = newMapId;
  }
  else
  {
    this.mapObject = newMapObject;
    this.mapId = newMapObject.id;
  }
  this.indexPanel = null;
  this.labelPanel = null;
  this.mapObject.labelControl = this;
  this.labelList = null;
  this.activeSymbol = null;
  this.activeText = null;
  this.activeLine = null;
  this.activeTheme = null;

  this.LinePoints = new Array();
  this.activeLineStylesheet = '';
  this.activeLineStyleName = '';  // the id for the style, not the same as stylesheet
  this.lineDX = 2;
  this.lineDY = 29;
};

LabelControl.prototype.setConfig = function(newConfigStruct)
{
  this.config = newConfigStruct;
  this.labelPanel.draw();
  this.clearList();
};

LabelControl.prototype.initialize = function()
{
  if (!this.gfx)
    this.gfx = new jsGraphics(this.mapObject.mapImage.drawPlaneNode.id);
  var requestInfo = new Object;
  requestInfo.mode = 'initialize';
  makeASyncPostRequest(this, requestInfo, XMLRPC_URL,'WORKSPACE.data.get',this.mapObject.sessionID,'labelList');
};

LabelControl.prototype.zoomToLabel = function(themeIndex, labelIndex)
{
  this.mapObject.zoomToExtent(this.labelList[themeIndex][labelIndex].extent);
};

LabelControl.prototype.getActiveSymbolString = function()
{
  return this.labelPanel.symbolNameInput.value;
};

LabelControl.prototype.getActiveTextString = function()
{
  return this.labelPanel.textInput.value;
};

LabelControl.prototype.deleteLabel = function(themeIndex, labelIndex)
{
  var requestInfo = new Array('deleteLabel',themeIndex,labelIndex);
  var requestInfo = new Object;
  requestInfo.mode = 'deleteLabel';
  requestInfo.themeIndex = themeIndex;
  requestInfo.labelIndex = labelIndex;
  var mapDimensions = this.mapObject.getMapDimensions();
  this.mapObject.setWaiting(true);
  makeASyncPostRequest(this, requestInfo, XMLRPC_URL,'GIS.DrawPlane.remove.Shape',this.mapObject.sessionID,this.mapObject.mapNumber,this.labelList[themeIndex][labelIndex].themeName,this.labelList[themeIndex][labelIndex].handle, mapDimensions[0][0], mapDimensions[0][1]);
};

LabelControl.prototype.setWorkspaceData = function()
{
  var requestInfo = new Object;
  requestInfo.mode = 'setWorkspaceData';
  makeASyncPostRequest(this, requestInfo, XMLRPC_URL,'WORKSPACE.data.set',this.mapObject.sessionID,'labelList',this.labelList);          
};

LabelControl.prototype.clearList = function()
{
  this.labelList = new Object;
  
  for (var i in this.mapObject.config.themes) 
  {
    this.labelList[i] = new Array(false);
  }
};

LabelControl.prototype.replaceStr = function(orig,lookfor,replacewith,ignorecase)  // JPW::Jan 6, 2003
{
  var str = new String();
  str += orig;
  var type = 'g';
  if (ignorecase) 
    type += 'i';
  var re = new RegExp (lookfor, type);
  return(str.replace(re,replacewith));
};

LabelControl.prototype.callback = function(serverReplyDoc, requestInfo)
{
  var pendingOperation = requestInfo.mode;
  if(serverReplyDoc == null) 
    return(null);
  if(serverReplyDoc.xml == '') 
    return(null);
  if(serverReplyDoc.documentElement.nodeName == 'parsererror')
  {
    this.setWaiting(false);
    return(null);
  }
    
  var clientReply = new XMLRPCResponse();
  clientReply.setResponseByDoc(serverReplyDoc);
  if(clientReply.isFault())
  {
    var data = null;
    alert(this.id+'.callback:  clientReply fault:\n\n'+clientReply.getFaultCode()+'\n'+clientReply.getFaultString());
  }
  else
    var data = clientReply.getObject();

  if((data != null))
  {
    switch (pendingOperation)
    {
      case 'initialize':
        if (data)
        {
          this.labelList = data;
          this.indexPanel.draw();
        }  
        else
        {
          this.clearList();
          this.indexPanel.draw();
          this.setWorkspaceData();
        }
        break;
      case 'setWorkspaceData':
        break;
      case 'LabelPointAdd':
      case 'LabelTextAdd':
      case 'LabelLineSave':
        requestInfo.labelName = stripQuotes(escapeHTML(requestInfo.labelName));
          var newLabel = new Object;
          newLabel.handle =  data[1];
          newLabel.themeName = requestInfo.themeName;
          newLabel.labelName = requestInfo.labelName;
          newLabel.extent =   requestInfo.extentArray;
          newLabel.type = (pendingOperation == 'LabelPointAdd')?'point':(pendingOperation == 'LabelTextAdd')?'text':'line';
          if (newLabel.type == 'line')
            newLabel.style = this.activeLineStyleName;
          else
            newLabel.style = requestInfo.labelConfigIndex;
//        this.labelList[requestInfo.themeName][this.labelList[requestInfo.themeName].length] = newLabel;
        this.addMarkupToIndex(requestInfo.themeName,newLabel);  // now calls common function
        this.mapObject.callback(serverReplyDoc,pendingOperation);
        this.setWorkspaceData();
        break;
      case 'deleteLabel':
        this.labelList[requestInfo.themeIndex][requestInfo.labelIndex] = false;
        this.mapObject.callback(serverReplyDoc,'DeleteLabel');
        this.setWorkspaceData();
        this.indexPanel.draw();
        break;
    }
  }
};

LabelControl.prototype.addMarkupToIndex = function(themeID,markupObj)  // JPW // markupObj: .handle, .themeName, .labelName, .extent, .type, .style
{
  this.labelList[themeID][this.labelList[themeID].length] = markupObj;
  if (this.indexPanel)
    this.indexPanel.draw();
};

/* Line stuff below */
LabelControl.prototype.saveLine = function()
{
  if (this.LinePoints.length == 0)
    return;
  this.mapObject.setWaiting(true);
  /* Make line into nice coords in % */
  var pointArray = new Array();
  for(var lcv=0;lcv < this.LinePoints.length;lcv++)
    pointArray[lcv] = new Array(this.LinePoints[lcv].xp,this.LinePoints[lcv].yp);
  
  /* Save line & Request Redraw */
  var labelName = this.labelPanel.lineNameInput.value;
  var themeName = this.activeTheme;
  var request = 'GIS.DrawPlane.add.Line';
  var labelConfigIndex = this.activeSymbol;
  var stylesheet = this.activeLineStylesheet;
  var extentArray = Array(this.mapObject.extent[0],this.mapObject.extent[1],this.mapObject.extent[2],this.mapObject.extent[3]);
  var requestInfo = new Object();
    requestInfo.mode = 'LabelLineSave';
    requestInfo.labelConfigIndex = labelConfigIndex;
    requestInfo.labelName = labelName;
    requestInfo.themeName = themeName;
    requestInfo.extentArray = extentArray;
  makeASyncPostRequest(this,requestInfo,XMLRPC_URL,request,this.mapObject.sessionID,0,this.mapObject.mapImage.imageNode.cbe.width(),this.mapObject.mapImage.imageNode.cbe.height(),Array(pointArray),false,themeName,stylesheet);
  /* Clear line */
  this.clearLine();
};
LabelControl.prototype.clearLine = function()
{
  if (this.LinePoints.length == 0)
    return;
  delete(this.LinePoints);
  this.LinePoints = new Array();
  this.gfx.clear();
};
LabelControl.prototype.addLinePoint = function(xp,yp,gfxX,gfxY)
{
  var pos = this.LinePoints.length;
  this.LinePoints[pos] = new Array();
  this.LinePoints[pos].xp = xp;
  this.LinePoints[pos].yp = yp;
  this.LinePoints[pos].gfxX = gfxX;
  this.LinePoints[pos].gfxY = gfxY;
  // add the line
  if (pos > 0)
  {
    var lastPt = this.LinePoints[pos-1];
    var dx = this.lineDX;
    var dy = this.lineDY;
    this.gfx.drawLine(lastPt.gfxX-dx,lastPt.gfxY-dy,gfxX-dx,gfxY-dy);
    this.gfx.paint();
  }
};
LabelControl.prototype.setLineType = function(lineType,styleName)
{
  if (!this.gfx)
    this.gfx = new jsGraphics(this.mapObject.mapImage.drawPlaneNode.id);
  this.gfx.setColor(lineType["color"]);
  this.gfx.setStroke(lineType["thickness"]);
  this.activeLineStylesheet = lineType["styleSheet"];
  this.activeLineStyleName = styleName;
  if (this.LinePoints.length > 1)
    this.repaintLine();
};
LabelControl.prototype.repaintLine = function()
{
  if (this.LinePoints.length <= 1)
    return;
  this.gfx.clear();
  for(var lcv=0;lcv < this.LinePoints.length;lcv++)
  {
    var pt = this.LinePoints[lcv];
    if (lcv > 0)
    {
      var lastPt = this.LinePoints[lcv-1];
      var dx = this.lineDX;
      var dy = this.lineDY;
      this.gfx.drawLine(lastPt.gfxX-dx,lastPt.gfxY-dy,pt.gfxX-dx,pt.gfxY-dy);
    }
  }
  this.gfx.paint();
};

