/**
 * DenkXweb, Version 1.0.0
 * Copyright (c) 2005 rjm business solutions GmbH
 * All rights reserved, license restrictions apply
 *
 * $Id$
 *
 * Messwerkzeug auf Kartenanzeige
 */


/** 
 *
 * Class MeasureLine
 *
 * Eine Mess-Linie zum Bestimmen von Abständen zwischen zwei Punkten.
 *
 * @attr canvas   Zeichenfläche zur Anzeige der Linie, vom Typ jsGraphics
 * @attr scaling  Skalierungsfaktor
 * @attr startPt  Startpunkt für die Messlinie, vom Typ "Point"
 * @attr endPt    Endpunkt für die Messlinie, vom Typ "Point"
 * @see Point
 */

/**
 * Standardkonstruktur für eine neue Mess-Linie.
 * @param aCanvas  Zeichenfläche, auf der die Mess-Linie erscheinen soll.
 * @param aScaling Skalierungsfaktor
 * @param aStartPt Ausgangspunkt für die Messung.
 */
function MeasureLine(canvas, scaling, startPt) {
  this.canvas  = canvas;
  this.scaling = scaling;
  this.startPt = (new Point()).assign(startPt);
  this.endPt   = (new Point()).assign(this.startPt);
  

  this.drawStringInBox = function(txt, x, y) {
    var bgColor = 'white';
    with (this.canvas) {
      // zunächst transparentes Hintergrund-Rechteck rendern,
      // darauf angegebenen Text darstellen
      htm += '<div style="position:absolute;white-space:nowrap;'+ 
        'left:' + (x-1) + 'px;'+ 
        'top:' + (y-1) + 'px;'+ 
          'font-family:' +  ftFam + ';'+ 
          'font-size:' + ftSz + ';'+ 
          '-moz-opacity:.7;'+  // Mozilla > 1.6
          'filter:progid:DXImageTransform.Microsoft.Alpha(opacity=70);'+ // IE
          '-khtml-opacity:.7;'+ // Konqueror > 3.3
          'opacity:0.7;'+ // Safari > 1, Mozilla >= 1.7b 
          'border:2px solid '+bgColor+';' +
          'background-color:' + bgColor + ';' +  
          'color:' + bgColor + ';' + ftSty + '">'+ 
          txt + 
        '<\/div>'+
        '<div style="position:absolute;white-space:nowrap;'+
          'left:' + x + 'px;'+
          'top:' + y + 'px;'+
          'font-family:' + ftFam + ';'+
          'font-size:' + ftSz + ';'+
          'border-width:1px;'+
          'border-style:solid;'+
          'color:' + color + ';' + ftSty + '">'+
          txt +
        '<\/div>';
    }
  }

  /** 
   * Die Mess-Linie anzeigen, d.h. eine Linie zwischen den Start- und 
   * End-Punkten ziehen. 
   */
  this.show = function() {
    with (this.canvas) {
      setStroke(3);
      setColor("yellow");
      drawLine(this.startPt.x, this.startPt.y, 
	       this.endPt.x,   this.endPt.y);

      setFont("arial","12px",Font.BOLD); 
      setColor("navy"); 

      var ofsX;
      if (this.endPt.x >= this.startPt.x) ofsX = 10
        else if (this.endPt.x < this.startPt.x) ofsX = -50;

      var ofsY;
      if (this.endPt.y >= this.startPt.y) ofsY = 20
        else if (this.endPt.y < this.startPt.y) ofsY = -10;

      this.drawStringInBox(this.calcDist().toFixed(2) + "m",
		 this.endPt.x + ofsX,
		 this.endPt.y + ofsY); 


      paint();
    }
  }; 

  /**
   * Die Mess-Linie ausblenden. 
   */
  this.hide = function() {
    this.canvas.clear();
  };
    
  /**
   * Berechnet die Länge der Mess-Linie (in m)
   * 
   * @return Abstand Startpunkt-Endpunkt (in m)
   */
  this.calcDist = function() {
    var distPixels = Math.sqrt(Math.pow(this.startPt.x-this.endPt.x, 2) + 
			       Math.pow(this.startPt.y-this.endPt.y, 2));
    return (distPixels * this.scaling);
  };
    
  /** 
   * Verschiebt den Endpunkt der Mess-Linie und aktualisiert die Anzeige.
   * @param newEndPt  Neuer Endpunkt
   * @see hide()
   * @see show()
   */
  this.setEndPt = function(newEndPt) {
    this.endPt.assign(newEndPt);
    this.hide();
    this.show();
  };
}


/**
 * Class MeasureManager
 *
 * Factory-Klasse für Measuring Line.
 * 
 * @attr map    Objekt vom Typ "Map" als Repräsentation der darunterliegenden
 *              Karte
 * @attr line   Mess-Linie, die gerade mit der Maus aufgezogen wird
 */
function MeasureManager(map, canvasId) {
  this.map = map;
  this.canvasId = canvasId;
  this.line = null;

  
  /**
   * Das Drücken einer Maustaste verarbeiten: Sofern sich die Maus auf dem
   * Bild-Element befindet, wird eine neue Mess-Linie angelegt, mit Start-
   * Koordinate an der aktuellen Mausposition (relativ zum Ursprung des
   * Bildes - also die Position der linken oberen Kartenecke)
   * Es wird erwartet, dass dem Bildelement zur Anzeige der Karte die 
   * HTML-ID "map" zugewiesen wurde.
   * Es wird erwartet, dass eine Zeichenfläche vom Typ "jsGraphics" in 
   * der globalen Variable "canvas" zur Verfügung gestellt wird.
   * @param e  Mausereignis vom Typ "MouseEvent",  das in Reaktion zur
   *           letzten Mausbewegung erzeugt wurde
   */
  this.setStartPt = function(mouseEvent) {
    if (this.map.isMouseOver(mouseEvent)) {
      var startPt = this.map.getMapPos(mouseEvent);
      this.line = new MeasureLine(this.canvas, this.map.getScaling(), 
                                  startPt);
      
      return true;
    } else
      return false;
  }

  /**
   * Eine neue Endposition für die aktuelle Mess-Linie aus der aktuellen
   * Mausposition berechnen. Ungültige Positonen ausserhalb der Karte 
   * werden korrigiert.
   *
   * @param e  Mausereignis vom Typ "Event" wie vom Browser geliefert.
   */

  this.setNewEndPt = function(mouseEvent) {
    var newEndPt = this.map.getMapPos(mouseEvent);
    newEndPt.checkBounds(new Point(0, 0), 
                         new Point(map.mapSize, map.mapSize));
    this.line.setEndPt(newEndPt);
  }


  this.initCanvas = function() {
    this.canvas = new jsGraphics(this.canvasId);


  }


}


