// $Id: yandex_map.js
// dev by boh@RT
var map, geoResult, placemark, placemark_options;

window.onload = function () {
 if(Drupal.settings.yandex_map.start) {
    placemark_options = {
        draggable: Drupal.settings.yandex_map.placemark_options.draggable,
        hasBalloon: Drupal.settings.yandex_map.placemark_options.hasBalloon,
        hintOptions: {
            maxWidth: 150,
            maxHeight: 150
        }
    };
    balloon_options = {
        maxWidth: 150,
        maxHeight: 150,
        hasCloseButton: true,
        mapAutoPan: true
    }
    
    // build map
    map = new YMaps.Map(document.getElementById("yandex_map"));

    // set map type
    if(Drupal.settings.yandex_map.maptype == 'HYBRID') map.setType(YMaps.MapType.HYBRID);
    if(Drupal.settings.yandex_map.maptype == 'SATELLITE') map.setType(YMaps.MapType.SATELLITE);

    // add map option
    if(Drupal.settings.yandex_map.mapcontrol_options.TypeControl)
      map.addControl(new YMaps.TypeControl(), new YMaps.ControlPosition(YMaps.ControlPosition.TOP_RIGHT));
    
    if(Drupal.settings.yandex_map.mapcontrol_options.ToolBar)
      map.addControl(new YMaps.ToolBar());
    
    if(Drupal.settings.yandex_map.mapcontrol_options.Zoom)
      map.addControl(new YMaps.Zoom());
    
    if(Drupal.settings.yandex_map.mapcontrol_options.ScaleLine)
      map.addControl(new YMaps.ScaleLine(), new YMaps.ControlPosition(YMaps.ControlPosition.BOTTOM_LEFT));
    
    if(Drupal.settings.yandex_map.mapcontrol_options.MiniMap)
      map.addControl(new YMaps.MiniMap(), new YMaps.ControlPosition(YMaps.ControlPosition.BOTTOM_RIGHT));
    
    if(Drupal.settings.yandex_map.mapcontrol_options.copyright)
      map.addCopyright(Drupal.settings.yandex_map.copyright);
      
    if(Drupal.settings.yandex_map.mapcontrol_options.DblClickZoom) map.enableDblClickZoom();
    else map.disableDblClickZoom();
      
    if(Drupal.settings.yandex_map.mapcontrol_options.Dragging) map.enableDragging();
    else map.disableDragging();
    
    if(Drupal.settings.yandex_map.mapcontrol_options.ScrollZoom) map.enableScrollZoom();
    else map.disableScrollZoom();
      
    if(Drupal.settings.yandex_map.mapcontrol_options.HotKeys) map.enableHotKeys();
    else map.disableHotKeys();
      
    if(Drupal.settings.yandex_map.mapcontrol_options.Magnifier) map.enableMagnifier();
    else map.disableMagnifier();
    
  //check for "add/edit" or "view"
  if(Drupal.settings.yandex_map.show_options.edit) {
    $('#yandex_map').mousemove(function(e){ yandex_map_update(); });
    
    // set map center
    // prepare & build placemark
    placemark = new YMaps.Placemark(new YMaps.GeoPoint(25.33, 50.75), placemark_options);
    if (Drupal.settings.yandex_map.item) {
        placemark.setGeoPoint(new YMaps.GeoPoint(Drupal.settings.yandex_map.item.lng, Drupal.settings.yandex_map.item.lat));
        if (Drupal.settings.yandex_map.center)
            map.setCenter(new YMaps.GeoPoint(Drupal.settings.yandex_map.center.lng, Drupal.settings.yandex_map.center.lat), Drupal.settings.yandex_map.center.zoom);
    } else if(Drupal.settings.yandex_map.default_place.city) {
        var geocoder = new YMaps.Geocoder(Drupal.settings.yandex_map.default_place.city + ' Якутск', {results: 1});
        YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
            if (this.length()) {
                placemarks = this.get(0);
                map.setCenter(placemarks.getGeoPoint(), Drupal.settings.yandex_map.default_place.zoom);
                placemark.setGeoPoint(placemarks.getGeoPoint());
                map.panTo(placemark.getGeoPoint());
            }
        });
    } else {
        map.setCenter(new YMaps.GeoPoint(25.33, 50.75), 13);
        alert("Enter the default city to show on Yandex Map setting forms!");
    }
    map.addOverlay(placemark);
    
    // actions
    YMaps.Events.observe(map, map.Events.Click, function (mEvent) {
        placemark.setGeoPoint(mEvent.getGeoPoint());
        placemark.closeBalloon();
        yandex_map_update();
    }, this);
  } else {
    // set map center
    map.setCenter(new YMaps.GeoPoint(Drupal.settings.yandex_map.center.lng, Drupal.settings.yandex_map.center.lat), Drupal.settings.yandex_map.center.zoom);

    // prepare & build placemark
    placemark = new YMaps.Placemark(new YMaps.GeoPoint(Drupal.settings.yandex_map.item.lng, Drupal.settings.yandex_map.item.lat));
    placemark.setIconContent(null);
    placemark.setOptions(placemark_options);
    placemark.setBalloonOptions(balloon_options);
    placemark.setBalloonContent('<b>' + Drupal.settings.yandex_map.item.title + '</b>'
           + '<br />' + Drupal.settings.yandex_map.item.description);
    map.addOverlay(placemark);
    if ((Drupal.settings.yandex_map.item.title || Drupal.settings.yandex_map.item.description) && Drupal.settings.yandex_map.ballon_on_view)
      placemark.openBalloon();
  }
 }
}

function yandex_map_update() {
    placemark.setIconContent(null);
    placemark.setOptions(placemark_options);
    placemark.setBalloonOptions(balloon_options);
    update_ballon();
    update_placemark();
    update_center();
}

function update_ballon() {
    content = '<b>' + document.getElementById("edit-yandex-map-item-title").value + '</b>'
           + '<br />' + document.getElementById("edit-yandex-map-item-description").value;
    placemark.setBalloonContent(content);
}

function update_placemark() {
    document.getElementById("edit-yandex-map-item-lat").value = placemark.getGeoPoint().getLat();
    document.getElementById("edit-yandex-map-item-lng").value = placemark.getGeoPoint().getLng();
    document.getElementById("edit-yandex-map-item-zoom").value = map.getZoom();
}

function update_center() {
    document.getElementById("edit-yandex-map-map-center-lat").value = map.getCenter().getLat();
    document.getElementById("edit-yandex-map-map-center-lng").value = map.getCenter().getLng();
    document.getElementById("edit-yandex-map-map-center-zoom").value = map.getZoom();
}

function findAddress(value, print, ballon) {
    value = Drupal.settings.yandex_map.default_place.city + ' ' + value;
    var geocoder = new YMaps.Geocoder(value, {results: 1});
    YMaps.Events.observe(geocoder, geocoder.Events.Load, function () {
        if (this.length()) {
            placemarks = this.get(0);
            if (print) {
                placemark.setGeoPoint(placemarks.getGeoPoint());
                placemark.setBalloonContent(placemarks.getBalloonContent());
                if (ballon) placemark.openBalloon();
            }
            map.panTo(placemark.getGeoPoint());
        } else {
            alert('Не удалось найти. Попробуйте найти на карте вручную');
        }
    });
    return placemark;
}

function findAddress_form() {
    return findAddress(document.getElementById("edit-yandex-map-last-search").value, true, true);
}