﻿
function YOpenInfoWindow(coords, windowText)
{
    ymap.openBalloon(coords, windowText);
}

function YCreateIconStyle(imageHref) {
    var styleObj = new YMaps.Style();
    styleObj.iconStyle = new YMaps.IconStyle();
    styleObj.iconStyle.href = imageHref;
    styleObj.iconStyle.size = new YMaps.Point(26, 34);
    styleObj.iconStyle.offset = new YMaps.Point(-18, -34);
    return styleObj;
}

function YAddMapPoint(coords, title, description, styleObject) {
    var placemark = new YMaps.Placemark(coords, { style: styleObject, hideIcon: false, hasHint: true, balloonOptions: { maxWidth: 200, mapAutoPan: true} });

    placemark.name = title;
    placemark.description = description;

    ymap.addOverlay(placemark);
    return placemark;
}

function YAddMapPointManaged(coords, title, description, styleObject) {
    var placemark = new YMaps.Placemark(coords, { style: styleObject, hideIcon: false, hasHint: true, balloonOptions: {maxWidth: 200, mapAutoPan: true} });

    placemark.name = title;
    placemark.description = description;

    YObjManager.add(placemark);
    return placemark;
}

function YShowAddress(searchString) {
    var geocoder = new YMaps.Geocoder(searchString);
    YMaps.Events.observe(geocoder, geocoder.Events.Load, YShowAddressCallback);
    YMaps.Events.observe(geocoder, geocoder.Events.Fault, YShowAddressError);
}

function YShowAddressCallback() {
    if (this.length()) {
           for (i = 0; i < this.length(); i++) {
            var txt = this.get(0).text;
            var kind = this.get(0).kind;
            var precision = this.get(0).precision;
            //alert(txt + ' : ' + kind + ' : ' + precision);
            //alert(this.get(0).getBounds());
        }
        
        //ymap.addOverlay(this.get(0));
        //ymap.panTo(this.get(0).getGeoPoint());
        //ymap.setZoom(GetZoomValueByKind(this.get(0).kind), { position: this.get(0).getGeoPoint()});

        ymap.openBalloon(this.get(0).getGeoPoint(), this.get(0).text, { maxWidth: 200, mapAutoPan: false });
        //ymap.setBounds(this.get(0).getBounds());
        ymap.setCenter(this.get(0).getGeoPoint(), GetZoomValueByKind(this.get(0).kind));
        MapMoved();
    }
    else {
        alert("Объект не найден");
    }
}
function YShowAddressError(geocoder, errorMessage)
{
    alert("Произошла ошибка: " + errorMessage);
}

function GetZoomValueByKind(kind) {
    switch (kind)
    {
        case 'house': 
            return 17;
        case 'street': 
            return 15;
        case 'metro': 
            return 16;
        case 'district': 
            return 13;
        case 'locality': 
            return 11;            
        case 'province': 
            return 9;            
        case 'country': 
            return 5;            
      default: 
            return 10;            
    }
}


