//============================================================================//
//========                  GOOGLE MAPS                             ==========//
//============================================================================//
var map;
var geocoder;
var mgr;
var markerIcon;
var markerOptions;
var user_city;

function initGeoSystem() {
    if (GBrowserIsCompatible()) {
        function CategoryControl() {}
        CategoryControl.prototype            = new GControl();
        CategoryControl.prototype.initialize = function(map) {
             var container = document.createElement("div");
            var categoryDiv = document.createElement("div");
            categoryDiv.innerHTML = $('#inmaps_map_filter').html();
            $('#inmaps_map_filter').remove();
            this.setButtonStyle_(categoryDiv);
            container.appendChild(categoryDiv);
            map.getContainer().appendChild(container);
            return container;
        }

        CategoryControl.prototype.getDefaultPosition = function() {
          return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 35));
        }

        CategoryControl.prototype.setButtonStyle_ = function(button) {
          button.style.backgroundColor = "white";
          button.style.font = "small Arial";
          button.style.border = "1px solid black";
          button.style.padding = "15px";
          button.style.marginBottom = "3px";
        }

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

        map         = new GMap2(document.getElementById("citymap"));
        geocoder    = new GClientGeocoder();
        mgr         = new GMarkerManager(map);

        if (user_city) { centerAddress(user_city); }

        map.setUIToDefault();

        map.addControl(new CategoryControl());

        // Create our "tiny" marker icon
        markerIcon = new GIcon();
        markerIcon.image = "/components/maps/images/markers/default.png";
        markerIcon.shadow = "/components/maps/images/markers/marker-shadow.png";
        markerIcon.iconSize = new GSize(12, 20);
        markerIcon.shadowSize = new GSize(22, 20);
        markerIcon.iconAnchor = new GPoint(6, 20);
        markerIcon.infoWindowAnchor = new GPoint(5, 1);

        // Set up our GMarkerOptions object literal
        markerOptions = { icon:markerIcon };

        getPlaces();

    }

}

function unloadGeoSystem(){
    GUnload();
}

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

function initPlaceMap(address){

    small_map             = new GMap2(document.getElementById("placemap"));
    geocoder              = new GClientGeocoder();

    var customUI = small_map.getDefaultUI();
    customUI.controls.maptypecontrol = false;
    customUI.controls.smallzoomcontrol3d = false;
    customUI.controls.largezoomcontrol3d = false;

    small_map.setUI(customUI);

    // Create our "tiny" marker icon
    markerIcon = new GIcon();
    markerIcon.image = "/components/maps/images/markers/default.png";
    markerIcon.shadow = "/components/maps/images/markers/marker-shadow.png";
    markerIcon.iconSize = new GSize(12, 20);
    markerIcon.shadowSize = new GSize(22, 20);
    markerIcon.iconAnchor = new GPoint(6, 20);
    markerIcon.infoWindowAnchor = new GPoint(5, 1);

    // Set up our GMarkerOptions object literal
    markerOptions = { icon:markerIcon };

    geocoder.getLatLng(address,
    function(point){
            small_map.setCenter(point, 15);
            var marker = new GMarker(point, markerOptions);
            small_map.addOverlay(marker);
       }
    );
}

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

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

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

function centerAddress(address){
    geocoder.getLatLng(address,
        function(point){
            map.setCenter(point, 3);
        }
    );
}

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

function addMarker(place_id, address, icon) {
    if (!icon) { icon = 'default.png'; }
    geocoder.getLatLng(address,
        function(point){
            if (point) {
                  markerOptions.icon.image = "/components/maps/images/markers/"+icon;
                  var marker = new GMarker(point, markerOptions);
                  GEvent.addListener(marker, "click", function(data) {
                        clickMarker(place_id, marker);
                  });
                  map.addOverlay(marker);
            }
        }
    );
}

function clickMarker(place_id, marker){
    marker.openInfoWindowHtml('<div class="loading" style="display:block">Загрузка...</div>');

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

}
