/**
 * @fileOverview Coordinate tool.
 */

/**
 * Coordinate tool constructor.
 *
 * @class Coordinate tool.
 *
 * @param {HMap} hmap Instace of HMap.
 * @param {Function} callback Callback function, should take one arguments that
 *        is the current position in RT90.
 * @constructor
 *
 * @example
 * var coordinateTool = new HCoordinateTool(map, coordinateCallback);
 * 
 * function coordinateCallback(rt90point) {
 *   var coordinateInfo = document.getElementById("coordinateinfo");
 *   var latlng = map.fromRT90ToLatLng(rt90point);
 *   coordinateInfo.innerHTML =
 *       "RT90: " + rt90point.toString() + "<br>" +
 *       "WGS84: " + latlng.toString() + "<br>" +
 *       "Decimal: " + latlng.getLat().toFixed(4) + ", " + latlng.getLng().toFixed(4);
 * }
 */
function HCoordinateTool(hmap, callback) {
    this.hmap = hmap;
    this.callback = callback;

    this.marker = new HMarker(this.hmap.getCenter(), HCoordinateTool.ICON);
    this.marker.enableDragging();

    this.hmap.addOverlay(this.marker);

    // init map event listeners
    var me = this;

    this.listeners = [];    // put event listeners in an array for easy cleanup

    this.listeners.push( HEvent.addListener(this.hmap, HMap.EVENT_MAP_CLICK, function(rt90point) { me.mapClick(rt90point); }) );
    this.listeners.push( HEvent.addListener(this.marker, HMarker.EVENT_DRAG_MOVE, function(rt90point) { me.markerMove(rt90point); }) );
}

HCoordinateTool.ICON = {
    image: "http://www.hitta.se/images/siktebla.png",
    anchor: new HPoint(39, 39),
    infoBoxAnchorTop: new HPoint(39, -2),
    infoBoxAnchorBottom: new HPoint(39, 79)
};

/**
 * Remove listeners and remove marker from map.
 */
HCoordinateTool.prototype.close = function() {
    HEvent.removeListeners(this.listeners);

    this.hmap.removeOverlay(this.marker);
};

/**
 * Event listener for mouse clicks on the map. Move marker to position.
 *
 * @param {HPointRT90} rt90point RT90 coordinate of click.
 * @private
 */
HCoordinateTool.prototype.mapClick = function(rt90point) {
    this.marker.setRT90Point(rt90point);

    this.callback(rt90point);
};

HCoordinateTool.prototype.markerMove = function(rt90point) {
    this.callback(rt90point);
};

