RedlineTool = function(mapviewObject, lineStyle, areStyle, tol, distanceUnit, redliningInfoDivName, markerUrl, factoryObj) {
  this.mapviewObject = mapviewObject;
  this.lineStyle = lineStyle;
  this.areStyle = areStyle;
  this.tol = tol;
  this.distanceUnit = distanceUnit;
  
  if (lineStyle == null)
    this.lineStyle = "TELEATLAS.L.HISTORY";

  if (areStyle == null)
    this.areStyle = "TELEATLAS.C.RED";
  
  if (tol == null)
    this.tol = 0.0;
  
  if (distanceUnit == null)
    this.distanceUnit = "M";

  this.totalDistance = 0.0;
  this.redlineObj = null;
  this.infoHandler = null;
  this.noInfoHandler = null;
  
  this.fois = new Array();
  this.newFOICenterX = null;
  this.newFOICenterY = null;
  this.redliningInfoDivName = redliningInfoDivName;
  this.markerUrl = markerUrl;
  
  this.factoryObj = factoryObj;
}

RedlineTool.prototype.createRedlining = function() {
  this.redlineObj = new MVRedlineTool(this.lineStyle, this.areStyle);
  if (this.markerUrl != null)
    this.redlineObj.setMarkerImage(this.markerUrl, 5, 5);
  this.mapviewObject.addRedLineTool(this.redlineObj);
}

RedlineTool.prototype.startRedlining = function() {
  if (this.redlineObj == null) {
    this.clearRedlining();
    this.createRedlining();
  }
  
  var instance = this;
  this.infoHandler = function() {
    instance.getCoordinateDistance(instance);
  }
  
  if (this.infoHandler!=null) {
    this.mapviewObject.addEventListener("mouse_click", this.infoHandler);
  }
  
  this.redlineObj.init();
  this.totalDistance = 0.0;
}

RedlineTool.prototype.clearRedlining = function() {
  if (this.redlineObj != null) {
    this.redlineObj.clear();
    
    var instance = this;
    this.noInfoHandler = function() { 
      instance.doNothing();
    }
    
    if (this.noInfoHandler!=null) {
      this.mapviewObject.addEventListener("mouse_click", this.noInfoHandler);
    }
  }
  
  this.clearDistanceLabels();
  this.totalDistance = 0.0;
  this.clearRedliningInfoDiv();
}

RedlineTool.prototype.getOrdinates = function() {
  if (this.redlineObj != null) {
    return this.redlineObj.getOrdinates();
  }
}

RedlineTool.prototype.getTol = function() {
  return this.tol;
}

RedlineTool.prototype.getDistanceUnit = function() {
  return this.distanceUnit;
}

RedlineTool.prototype.doNothing = function() {
}

RedlineTool.prototype.getDistanceLabels = function() {
  return this.fois;
}

RedlineTool.prototype.clearDistanceLabels = function() {
  for (var foiIndex in this.fois) {
    this.mapviewObject.removeFOI(this.fois[foiIndex]);
  }
}

RedlineTool.prototype.addDistanceLabel = function(distanceString) {
  // Distance string might be too long.
  var distance = parseFloat(distanceString);
  var maptext = "";

  if (distance > 1000) 
    maptext = this.factoryObj.formatNumber(distance/1000, 2) + " km.";
  else
    maptext = this.factoryObj.formatNumber(distance, 1) + " m.";

  var foiLength = this.fois.length;
  var foiLabelName = "distancelabel_" + foiLength;
  var pt = MVSdoGeometry.createPoint(this.newFOICenterX, this.newFOICenterY, 8307);
  var hf = MVFOI.createHTMLFOI(foiLabelName, pt, 
        "<p style=\"color: #000000; background-color: #98FFFF; font-size:9px; font-family:tahoma;\"><B>" + maptext + "</B></p>", 5, 5);
  this.mapviewObject.addFOI(hf);
  this.fois[foiLength] = hf;

  this.totalDistance = this.totalDistance + distance;
  this.updateRedliningInfoDiv();
}

RedlineTool.prototype.updateRedliningInfoDiv = function() {
  if (this.redliningInfoDivName != null) {
    var div = document.getElementById(this.redliningInfoDivName);
    if (this.totalDistance > 1000) 
      div.innerHTML = this.factoryObj.formatNumber(this.totalDistance/1000, 2) + " km.";
    else
      div.innerHTML = this.factoryObj.formatNumber(this.totalDistance, 1) + " m.";
  }
}

RedlineTool.prototype.clearRedliningInfoDiv = function() {
  if (this.redliningInfoDivName != null) {
    var div = document.getElementById(this.redliningInfoDivName);
    div.innerHTML = "0.0 m.";
  }
}

RedlineTool.prototype.getCoordinateDistance = function(instance) {
  var ordinates = instance.getOrdinates();
  var tol = instance.getTol();
  var distanceUnit = instance.getDistanceUnit();

  if (ordinates.length >= 4) {
    var coords = new Array();
    coords[0] = ordinates[ordinates.length - 4];
    coords[1] = ordinates[ordinates.length - 3];
    coords[2] = ordinates[ordinates.length - 2];
    coords[3] = ordinates[ordinates.length - 1];
    instance.requestCoordDistance(coords, tol, distanceUnit);
  }
}

RedlineTool.prototype.requestCoordDistance = function (coords, tol, distanceUnit) {
  var extraParams = new Array();
  extraParams["from_x"] = coords[0];
  extraParams["from_y"] = coords[1];
  extraParams["to_x"] = coords[2];
  extraParams["to_y"] = coords[3];
  extraParams["tol"] = tol;
  extraParams["unit"] = distanceUnit;
  var xml = this.factoryObj.prepareGenericRequestData("COORD_DISTANCE", extraParams);
  xml = "xmlDoc=" + xml;

  this.factoryObj.getListenerRequestBaseObj().doRequestAsynchronous(xml, this.parseCoordDistance, this);

  this.newFOICenterX = (parseFloat(coords[0]) + parseFloat(coords[2]))/2;
  this.newFOICenterY = (parseFloat(coords[1]) + parseFloat(coords[3]))/2;
}

RedlineTool.prototype.parseCoordDistance = function(status, statusText, responseText, responseXML, caller) {
  responseXML = caller.factoryObj.convertStringToXml(responseText);
  var results = responseXML.getElementsByTagName("distance");
  var coordDistance = converterFactory.getObjectArray(responseXML, com.infotech.services.entity.common.DataDistance, "distance");
  var distanceString = "";
  for(var i = 0; i < coordDistance.length; i++) {
    distanceString = coordDistance[i].getValue(); 
  }
  caller.addDistanceLabel(distanceString);
}

RedlineTool.prototype.getTotalDistance = function() {
  return this.totalDistance;
}

RedlineTool.prototype.getOrdinates = function() {
  if (this.redlineObj != null)
    return this.redlineObj.getOrdinates();
  
  return "";
}

RedlineTool.prototype.generateArea = function() {
  if (this.redlineObj != null)
    return this.redlineObj.generateArea();
}
