﻿/*!
* Простая яндекс карта
*/

function SimpleYandexMap() {
    this.map = {};
    this.zoom = {};
    this.titles = {};
    this.coords = {};
    this.showBallons = {};
    this.placemarkStyle = { style: "default#redPoint" };


    this.initMapControls = function() {
        this.map.addControl(new YMaps.Zoom({ noTips: true }));
        this.map.enableScrollZoom();
        var center = this.getCenter();
        this.map.setCenter(new YMaps.GeoPoint(center[0], center[1]), this.zoom, YMaps.MapType.MAP);
    };

    this.initLables = function() {
        for (var i = 0; i < this.coords.length; i++) {
        	var point = new YMaps.GeoPoint(this.coords[i][0], this.coords[i][1]);

            var placemark = new YMaps.Placemark(point, this.placemarkStyle);
            placemark.description = this.titles[i];
            //placemark.setIconContent(this.titles[i]);

            this.map.addOverlay(placemark);
        }
    };

    this.getCenter = function() {
        var ret = [37.609218, 55.753559]; // Moscow

        if (this.coords != undefined && this.coords != null && this.coords.length > 0) {
            if (this.coords.length > 1) {
                var xSum = 0.0;
                var ySum = 0.0;

                for (var i = 0; i < this.coords.length; i++) {
                    xSum += this.coords[i][0];
                    ySum += this.coords[i][1];
                }

                ret = [xSum / this.coords.length, ySum / this.coords.length];
            } else {
                ret = [this.coords[0][0], this.coords[0][1]];
            }
        }
        return ret;
    };
}

SimpleYandexMap.prototype.initMap = function(mapHolderId, zoom, titles, coords) {
    this.map = new YMaps.Map($("#" + mapHolderId)[0]);
    this.zoom = zoom;
    this.titles = titles;
    this.coords = coords;
    this.initMapControls();
    this.initLables();
}