/*----------------    FILE HEADER ---------------------------------------
 This file is part of Geoide.
 Copyright (C) 2005-2006 by:
 IDgis B.V.
 http://www.idgis.nl

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

 Contact:

 Herman Assink
 IDgis bv
 P.O. Box 15
 7450 AA Holten
 The Netherlands
 E-Mail: herman.assink@idgis.nl

 * @version 1.4.0
 * @author IDgis team
 * 
 ------------------------------------------------------------------------*/
import nl.idgis.giclient.geoma.Circle;
import nl.idgis.giclient.geoma.Ellipse;
import nl.idgis.giclient.geoma.Geometry;
import nl.idgis.giclient.geoma.GeometryCollection;
import nl.idgis.giclient.geoma.LineString;
import nl.idgis.giclient.geoma.Point;
import nl.idgis.giclient.geoma.Polygon;
import nl.idgis.giclient.geoma.Square;
import nl.idgis.giclient.gui.mapviewer.MapGeometry;
import nl.idgis.giclient.gui.mapviewer.MapViewer;
import nl.idgis.giclient.gis.Feature;
import nl.idgis.giclient.gui.mapviewer.MapIcon;

class nl.idgis.giclient.gui.mapviewer.GeometryLayer extends MovieClip { // StageListener

    private var mapViewer:MapViewer = null; // Set by init object.

    private var mapGeometries:Array = null;
    private var numMapGeometries:Number = 0;
    
    private var mapIcons:Array = null;
    private var numMapIcons:Number = 0;

    private var selectedMapGeometry:MapGeometry = null;

    function GeometryLayer() {
        mapGeometries = new Array();
        mapIcons = new Array();
    }

    function getMapViewer():MapViewer {
        return mapViewer;
    }
    
    function addMapIcon(feature:Feature, mapGeometry:MapGeometry):MapIcon {
    	var initObject:Object = new Object();
    	initObject["feature"] = feature;
    	initObject["mapGeometry"] = mapGeometry;
    	initObject["gism"] = mapViewer.ruler.getGIS();
    	initObject["geometryLayer"] = this;
    	var mapIcon:MapIcon = MapIcon(mapGeometry.attachMovie("MapIcon", "mapIcon" + numMapIcons++, mapGeometry.getNextHighestDepth(), initObject));
    	mapIcons.push(mapIcon);
    	mapGeometry.setMapIcon(mapIcon);
    	return mapIcon;
    }

    function addMapGeometry(geometry:Geometry, type:Number, color:Number, labelText:String,
    	annotation:String):MapGeometry {
        var mapGeometry:MapGeometry = null;
        var initObject:Object = new Object();
        initObject["geometry"] = geometry;
        initObject["type"] = type;
        initObject["color"] = color;
        initObject["labelText"] = labelText;
        initObject["annotation"] = annotation;
        initObject["gism"] = mapViewer.ruler.getGIS();
        initObject["geometryLayer"] = this;
        if (geometry instanceof GeometryCollection) {
            mapGeometry = MapGeometry(attachMovie("MapGeometryCollection", "mapGeometryCollection" + numMapGeometries, numMapGeometries, initObject));
        } else if (geometry instanceof Point) {
            mapGeometry = MapGeometry(attachMovie("MapPoint", "mapPoint" + numMapGeometries, numMapGeometries, initObject));
        } else if (geometry instanceof LineString) {
            mapGeometry = MapGeometry(attachMovie("MapLineString", "mapLineString" + numMapGeometries, numMapGeometries, initObject));
        } else if (geometry instanceof Square) {
            mapGeometry = MapGeometry(attachMovie("MapSquare", "mapSquare" + numMapGeometries, numMapGeometries, initObject));
        } else if (geometry instanceof Ellipse) {
            mapGeometry = MapGeometry(attachMovie("MapEllipse", "mapEllipse" + numMapGeometries, numMapGeometries, initObject));
        } else if (geometry instanceof Circle) {
            mapGeometry = MapGeometry(attachMovie("MapCircle", "mapCircle" + numMapGeometries, numMapGeometries, initObject));
        } else if (geometry instanceof Polygon) {
            mapGeometry = MapGeometry(attachMovie("MapPolygon", "mapPolygon" + numMapGeometries, numMapGeometries, initObject));
        }
        mapGeometries.push(mapGeometry);
        numMapGeometries++;

        return mapGeometry;
    }

    function removeMapGeometry(mapGeometry:MapGeometry):Void {
        for (var i:Number = 0; i < mapGeometries.length; i++) {
            if (mapGeometries[i] == mapGeometry) {
                mapGeometries.splice(i, 1);
            }
        }
        mapGeometry.removeMovieClip();
    }
    
    function removeMapIcon(mapIcon:MapIcon):Void {
    	for (var i:Number = 0; i < mapIcons.length; i++) {
            if (mapIcons[i] == mapIcon) {
                mapIcons.splice(i, 1);
            }
        }
        mapIcon.removeMovieClip();
    }

    function moveMapGeometryFrontward(mapGeometry:MapGeometry):Void {
        var movieClip:MovieClip = getInstanceAtDepth(numMapGeometries - 1);
        mapGeometry.swapDepths(movieClip);
    }

    function setSelectedMapGeometry(selectedMapGeometry:MapGeometry):Void {
        this.selectedMapGeometry = selectedMapGeometry;
    }

    function getSelectedMapGeometry():MapGeometry {
        return selectedMapGeometry;
    }

}