// загрузка API
google.load("maps", "2", {"language" : "ru"});
var map = null;
var existso = new Array();
var existso2 = new Array();
var existso3 = new Array();
var currentmarker = null;
var gmapzoom = 9;
var mapcity = 1;
var mapSOff = false;
var oIcon = null;
var rIcon = null;
var oIcon2 = null;
var oIcon3 = null;
var oIcon4 = null;

// Инициализация карты
function initialize() {
    map = new google.maps.Map2(document.getElementById("gmap"));
    map.setCenter(new google.maps.LatLng(59.9390390, 30.3157850), gmapzoom); // Санкт-Петербург
    map.addControl(new google.maps.LargeMapControl());
    map.addControl(new google.maps.MenuMapTypeControl());
    map.setMapType(G_HYBRID_MAP);
    map.enableScrollWheelZoom();
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    
    oIcon = new google.maps.Icon(G_DEFAULT_ICON);
    oIcon.image = "/images/alt_blue-dot.png";
    oIcon.iconSize = new google.maps.Size(32, 32); 
    oIcon.iconAnchor = new google.maps.Point(14,36);
    oIcon2 = new google.maps.Icon(G_DEFAULT_ICON);
    oIcon2.image = "/images/bal_nezhiloe.png";
    oIcon2.iconSize = new google.maps.Size(32, 32); 
    oIcon2.iconAnchor = new google.maps.Point(14,36);
    oIcon3 = new google.maps.Icon(G_DEFAULT_ICON);
    oIcon3.image = "/images/bal_zmlya.png";
    oIcon3.iconSize = new google.maps.Size(32, 32); 
    oIcon3.iconAnchor = new google.maps.Point(14,36);
    oIcon4 = new google.maps.Icon(G_DEFAULT_ICON);
    oIcon4.image = "/images/bal_zagorod.png";
    oIcon4.iconSize = new google.maps.Size(32, 32); 
    oIcon4.iconAnchor = new google.maps.Point(14,36);
    
    rIcon = new google.maps.Icon(G_DEFAULT_ICON);
    rIcon.image = "/images/alt_red-dot.png";
    rIcon.iconSize = new google.maps.Size(32, 32); 
    rIcon.iconAnchor = new google.maps.Point(14,36);
    
    var isSetPoints = setSelectedObject();
    if (!isSetPoints) getMapPoints(northEast.lat(), northEast.lng(), southWest.lat(), southWest.lng());
    google.maps.Event.addListener(map, "moveend", function() {
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        //alert(map.getZoom());
        //if (map.getZoom()>12 && !mapSOff) 
        getMapPoints(northEast.lat(), northEast.lng(), southWest.lat(), southWest.lng());
        getMapPointsO(northEast.lat(), northEast.lng(), southWest.lat(), southWest.lng());
    });
    if (mapcity!=1) setCenetreCity(mapcity);
}

// Установка центра карты 
function setCenetreCity(id) {
    var piter=document.getElementById('spb');
    var msk=document.getElementById('msk');
    var krd=document.getElementById('krd');
    $('dealidmap').value='';
    $('searchmap').value='';
    $('mapp_from').value='';
    $('maps_from').value='';
    $('mapp_to').value='';
    $('maps_to').value='';
    $('maps_type').value=1;
    
    var flg = false;
    if (id == 1 && piter.style.fontWeight!='bold') {
        map.setCenter(new google.maps.LatLng(59.9390390, 30.3157850), gmapzoom); // Санкт-Петербург
        piter.style.fontWeight='bold';
        msk.style.fontWeight='normal';
        krd.style.fontWeight='normal';
        mapcity=1;
        flg = true;
    }
    if (id == 2 && msk.style.fontWeight!='bold') {
        map.setCenter(new google.maps.LatLng(55.755786, 37.617633), gmapzoom); // Москва
        piter.style.fontWeight='normal';
        msk.style.fontWeight='bold';
        krd.style.fontWeight='normal';
        mapcity=2;
        flg = true;
    }
    if (id == 3 && krd.style.fontWeight!='bold') {
        map.setCenter(new google.maps.LatLng(45.6415289, 39.7055977), gmapzoom-2); // Краснодарский край
        piter.style.fontWeight='normal';
        msk.style.fontWeight='normal';
        krd.style.fontWeight='bold';
        mapcity=3;
        flg = true;
    }
    if (flg) {
        getMapPointsForm();
        mapSOff = false;
    }
}

// Поиск с учетом формы
function getMapPointsForm() {
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        map.clearOverlays();
        existso = new Array();
        existso2 = new Array();
        existso3 = new Array();
        getMapPoints(northEast.lat(), northEast.lng(), southWest.lat(), southWest.lng());
}

// Получим точки в видимой области
function getMapPoints(lt1, lg1, lt2, lg2)
{
    //alert(map.getZoom());
    $('progressbar').style.display='';
    var did = $('dealidmap').value;
    var sid = $('searchmap').value;
    var mapp_from = $('mapp_from').value;
    var maps_from = $('maps_from').value;
    var mapp_to = $('mapp_to').value;
    var maps_to = $('maps_to').value;
    var maps_type = $('maps_type').value;
    //alert('lt1=' + lt1 + '&lg1=' + lg1 + '&lt2=' + lt2 + '&lg2=' + lg2 + '&did=' + did + '&sid=' + sid + '&mapp_from=' + mapp_from + '&mapp_to=' + mapp_to + '&maps_from=' + maps_from + '&maps_to=' + maps_to + '&maps_type=' + maps_type);
    new Ajax.Request('/map/getMapPoints', {
        asynchronous:true,
        evalScripts:true,
        onComplete:function(transport){
            //alert(transport.responseText);
            setMapPoints();
        },       
        parameters:'lt1=' + lt1 +
                '&lg1=' + lg1 +
                '&lt2=' + lt2 +
                '&lg2=' + lg2 + 
                '&did=' + did + 
                '&sid=' + sid + 
                '&c=' + mapcity +
                '&mapp_from=' + mapp_from + 
                '&mapp_to=' + mapp_to + 
                '&maps_from=' + maps_from + 
                '&maps_to=' + maps_to + 
                '&maps_type=' + maps_type
        });
}
// Нарисуем точки
function setMapPoints() {
    $('progressbar').style.display='none';
}

function getMapPointsFromCheckbox() {
    map.clearOverlays();
    existso = new Array();
    existso2 = new Array();
    existso3 = new Array();
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    //getMapPoints(northEast.lat(), northEast.lng(), southWest.lat(), southWest.lng());
    getMapPointsO(northEast.lat(), northEast.lng(), southWest.lat(), southWest.lng());
}

function getMapPointsO(lt1, lg1, lt2, lg2)
{
    var c1 = 0; var c2 = 0; var c3 = 0;
    if ($('c1').checked) c1=1;
    if ($('c2').checked) c2=1;
    if ($('c3').checked) c3=1;

    $('progressbar').style.display='';
    new Ajax.Request('/map/getMapPointsObjects', {
        asynchronous:true,
        evalScripts:true,
        onComplete:function(transport){
            setMapPointsO();
        },
        parameters:'lt1=' + lt1 +
                '&lg1=' + lg1 +
                '&lt2=' + lt2 +
                '&lg2=' + lg2 +
                '&c1=' + c1 +
                '&c2=' + c2 +
                '&c3=' + c3
        });
}
// Нарисуем точки
function setMapPointsO() {
    $('progressbar').style.display='none';
}

// Установка инициализации на згрузку старницы
google.setOnLoadCallback(initialize);
