//============================================================================//
//========                  YANDEX MAPS                             ==========//
//============================================================================//

var map;
var user_city;
var s;

//============================================================================//

function initGeoSystem() {

    if ($('#inmaps_map_filter').length){
        function OfficeNavigator () {

            // Обработчик добавления элемента на карту
            this.onAddToMap = function (map, position) {
                this.container = $("#inmaps_map_filter");
                this.map = map;
                this.position = new YMaps.ControlPosition(YMaps.ControlPosition.TOP_RIGHT, new YMaps.Size(6, 36));

                // CSS-свойства, определяющие внешний вид элемента
                this.container.css({
                    position: "absolute",
                    right: '0px',
                    zIndex: YMaps.ZIndex.CONTROL,
                    background: '#fff',
                    listStyle: 'none',
                    padding: '15px',
                    margin: 0,
                    border: 'solid 1px gray',
                    cursor: 'default',
                });

                // Располагает элемент управления в верхнем правом углу карты
                this.position.apply(this.container);

                // Добавляет элемент управления на карту
                this.container.appendTo(this.map.getContainer());

            }

            this.onRemoveFromMap = function () {};
        }
    }

    user_city = $('input[name=user_city]').val();

    map = new YMaps.Map(YMaps.jQuery("#citymap")[0]);

    if (user_city) { centerAddress(user_city); }

    map.addControl(new YMaps.TypeControl());
    map.addControl(new YMaps.Zoom());
    map.addControl(new YMaps.ScaleLine());

    if ($('#inmaps_map_filter').length){
        map.addControl(new OfficeNavigator());
    }

    s                   = new YMaps.Style();
    s.iconStyle         = new YMaps.IconStyle();
    s.iconStyle.href    = "/components/maps/images/markers/default.png";
    s.iconStyle.size    = new YMaps.Point(12, 20);
    s.iconStyle.offset  = new YMaps.Point(-6, -20);

    s.iconStyle.shadow          = new YMaps.IconShadowStyle();
    s.iconStyle.shadow.href     = "/components/maps/images/markers/marker-shadow.png";
    s.iconStyle.shadow.size     = new YMaps.Point(22, 20);
    s.iconStyle.shadow.offset   = new YMaps.Point(-6, -20);

    if (map){
        getPlaces();
    }
    
}

function unloadGeoSystem(){
    GUnload();
}

//============================================================================//

function initPlaceMap(address){

    map = new YMaps.Map(YMaps.jQuery("#placemap")[0]);

    centerAddress(address, 15);

    s                           = new YMaps.Style();
    s.iconStyle                 = new YMaps.IconStyle();
    s.iconStyle.href            = "/components/maps/images/markers/default.png";
    s.iconStyle.size            = new YMaps.Point(12, 20);
    s.iconStyle.offset          = new YMaps.Point(-6, -20);

    s.iconStyle.shadow          = new YMaps.IconShadowStyle();
    s.iconStyle.shadow.href     = "/components/maps/images/markers/marker-shadow.png";
    s.iconStyle.shadow.size     = new YMaps.Point(22, 20);
    s.iconStyle.shadow.offset   = new YMaps.Point(-6, -20);

    var geocoder = new YMaps.Geocoder(address);
    map.addOverlay(geocoder);

}

//============================================================================//

function clearMap(){
    map.removeAllOverlays();
}

//============================================================================//

function centerAddress(address, zoom){

    if (!zoom){ zoom = 11; }

    var geocoder = new YMaps.Geocoder(address);

    YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
        if (this.length()) {
            map.setCenter(this.get(0).getGeoPoint(), zoom)
        }
    })

}

//============================================================================//

function addMarker(place_id, address, icon) {

    var geocoder = new YMaps.Geocoder(address);

    YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
        if (this.length()) {

            if (!icon) {
                icon = 'default.png';
            } else {
                s.iconStyle.href = "/components/maps/images/markers/"+icon;
            }

            var placemark = new YMaps.Placemark(this.get(0).getGeoPoint(), {style: s});

            YMaps.Events.observe(placemark, placemark.Events.Click, function (obj) {
                clickMarker(place_id, obj);
            });

            map.addOverlay(placemark);

        }
    });

}

function clickMarker(place_id, marker){

    marker.openBalloon('<div class="loading" style="display:block">Р—Р°РіСЂСѓР·РєР°...</div>');

    $.ajax({
          type: 'POST',
          url: '/maps/ajax/get-info/'+place_id,
          success: function(msg){
              marker.openBalloon(msg);
          }
    });
    
}

//============================================================================//
