# Source: bemap-model/bemap-polygon.js

    /**
     * BeNomad BeMap JavaScript API - Polygon class
     */

    /**
     * @classdesc
     * Base class for Polygon.
     * @public
     * @constructor
     * @param {bemap.Coordinate} coordinates Array of bemap.Coordinate.
     * @param {Object} options See below the available values.
     * @param {bemap.LineStyle} options.style Style of poyline used by the renderer.
     * @param {String} options.name Name of polygon.
     * @param {Object} options.properties custom object.
     */
    bemap.Polygon = function(coordinates, options) {
        var opts = options || {};

        /**
         * @type {Object}
         * @protected
         */
        this.native = null;

        /**
         * @type {Object}
         * @protected
         */
        this.map = null;

        /**
         * @type {Object}
         * @protected
         */
        this.layer = null;

        /**
         * @type {bemap.Coordinate[]}
         * @protected
         */
        this.coords = coordinates ? coordinates : [];

        /**
         * @type {
         bemap.LineStyle
     }
         * @protected
         */
        this.style = opts.style ? opts.style : null;

        /**
         * @type {String}
         * @protected
         */
        this.id = opts.id ? opts.id : null;

        /**
         * @type {String}
         * @protected
         */
        this.name = opts.name ? opts.name : null;

        /**
         * @type {Array}
         * @protected
         */
        this.events = [];

        /**
         * @type {Function}
         * @protected
         */
        this.callback = [];

        /**
         * @type {Object}
         * @protected
         */
        this.properties = opts.properties ? opts.properties : null;
    };

    /**
     * Add a coordinate.
     * @public
     * @param {bemap.Coordinate} coordinate bemap.Coordinate.
     * @return {bemap.Polygon} this
     */
    bemap.Polygon.prototype.addCoordinate = function(coordinate) {
        if (coordinate === null || !bemap.inheritsof(coordinate, bemap.Coordinate)) {
            throw new bemap.Exception("coordinate is not an bemap.Coordinate object");
        }
        this.coords.push(coordinate);
        return this;
    };

    /**
     * Add a coordinate.
     * @public
     * @param {double} lon Longitude in degres deciaml (WGS84).
     * @param {double} lat Latitude in degres deciaml (WGS84).
     * @return {bemap.Polygon} this
     */
    bemap.Polygon.prototype.addLonLat = function(lon, lat) {
        this.coords.push(new bemap.Coordinate(lon, lat));
        return this;
    };

    /**
     * Return an array with longitude and latitude in degres decimal (WGS94).
     * @public
     * @param {int} index Index of coordinate in polygon.
     * @return {array} Return an array with longitude and latitude in degres decimal (WGS94).
     */
    bemap.Polygon.prototype.getLonLatArray = function(index) {
        return this.coords[index].getLonLatArray();
    };

    /**
     * Return an array with latitude and longitude in degres decimal (WGS94).
     * @public
     * @param {int} index Index of coordinate in polygon.
     * @return {array} Return an array with latitude and longitude in degres decimal (WGS94).
     */
    bemap.Polygon.prototype.getLatLonArray = function(index) {
        return this.coords[index].getLatLonArray();
    };

    /**
     * Return an array with longitude and latitude in degres decimal (WGS94).
     * Example: [ [lon, lat], [lon, lat], ... ]
     * @public
     * @return {array} Return an array with longitude and latitude in degres decimal (WGS94).
     */
    bemap.Polygon.prototype.getLonLatArrays = function() {
        var i = 0,
            iLen = this.coords.length,
            rets = [],
            c;
        for (; i < iLen; i++) {
            c = this.coords[i];
            rets.push([c.lon, c.lat]);
        }
        return rets;
    };

    /**
     * Return an array with latitude and longitude in degres decimal (WGS94).
     * Example: [ [lat, lon], [lat, lon], ... ]
     * @public
     * @return {array} Return an array with latitude and longitude in degres decimal (WGS94).
     */
    bemap.Polygon.prototype.getLatLonArrays = function() {
        var i = 0,
            iLen = this.coords.length,
            rets = [],
            c;
        for (; i < iLen; i++) {
            c = this.coords[i];
            rets.push([c.lat, c.lon]);
        }
        return rets;
    };

    /**
     * Return the id of the polygon.
     * @public
     * @return {String} Return the id of the polygon.
     */
    bemap.Polygon.prototype.getId = function() {
        return this.id;
    };

    /**
     * Define the id of polygon
     * @public
     * @param {String} id the new id to set.
     * @return {bemap.Polygon} this
     */
    bemap.Polygon.prototype.setId = function(id) {
        this.id = id;
        return this;
    };

    /**
     * Return the name of the polygon.
     * @public
     * @return {String} Return the name of the polygon.
     */
    bemap.Polygon.prototype.getName = function() {
        return this.name;
    };

    /**
     * Define the name of polygon
     * @public
     * @param {String} name the new name to set.
     * @return {bemap.Polygon} this
     */
    bemap.Polygon.prototype.setName = function(name) {
        this.name = name;
        return this;
    };

    /**
     * Return the coordinate. See bemap.Coordinate.
     * @public
     * @param {int} index Index of coordinate in polygon.
     * @return {bemap.Coordinate} Return the coordinate. See bemap.Coordinate.
     */
    bemap.Polygon.prototype.getCoordinate = function(index) {
        return this.coords[index];
    };

    /**
     * Return an array of coordinate. See bemap.Coordinate.
     * @public
     * @return {bemap.Coordinate[]} Return an array of the coordinate. See bemap.Coordinate.
     */
    bemap.Polygon.prototype.getCoordinates = function() {
        if(this.map && this.native) {
          this.map.updatePolygonCoordinates(this);
        }
        return this.coords;
    };

    /**
     * Return the style of the polygon.
     * @public
     * @return {bemap.LineStyle} Return the style of the polygon.
     */
    bemap.Polygon.prototype.getStyle = function() {
        return this.style;
    };

    /**
     * Remove the Polygon from the layer.
     * @public
     * @return {bemap.Polygon} this
     */
    bemap.Polygon.prototype.remove = function() {
        if (this.map !== null && bemap.inheritsof(this.map, bemap.Map)) {
            this.map.removePolygon(this);
        }
        return this;
    };

    /**
     * Set the listner when an specified eventType occur on bemap.Polygon.
     * @public
     * @param {bemap.Map.EventType} eventType Event type.
     * @param {function} callback Function will be called when the specified eventType is occur.
     * @param {object} options options.
     * @return {bemap.Polygon} this.
     */
    bemap.Polygon.prototype.on = function(eventType, callback, options) {
        if (this.map !== null && bemap.inheritsof(this.map, bemap.Map)) {
            this.map.onPolygon(this, eventType, callback, options);
        }
        return this;
    };

    /**
     * Define the draggable capability for bemap.Polygon.
     * @protected
     * @param {function} callback Function will be called when the specified eventType is occur.
     * @param {object} options Options.
     * @param {bemap.Layer} options.layerFilter set the bemap layer used as filter.
     * @return {bemap.Listener} bemap.listener.
     */
    bemap.Polygon.prototype.draggable = function(callback, options) {
        if (this.map !== null && bemap.inheritsof(this.map, bemap.Map)) {
            return this.map.draggablePolygon(this, callback, options);
        }
        return new bemap.listener();
    };