var clickHandler;
var map;
var locations;
var bounds;
var geocoder = null;
$(document).ready(function()
{
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng( 52.1874047455997 , 18.984375 ), 6);
        geocoder = new GClientGeocoder();
        $.get('/_ajax/locations.php', processLocations );
    }    
});

function processLocations(content)
{
    eval("locations = "+content);
  
    $(locations).each(function()
    {
        var marker = new GMarker(new GLatLng(this.latitude, this.longitude), {title: this.name});
        map.addOverlay(marker);
        var punkt = this;
        GEvent.addListener(marker, 'click', function()
        {
            marker.openInfoWindowHtml('<b>'+punkt.name+'</b><br />'+punkt.opis);       
        });
    });

    zoomShowAll();
}

function zoomShowAll()
{
    bounds = new GLatLngBounds();
    map.setCenter(new GLatLng( 52.1874047455997 , 18.984375 ), 6);
    
    $(locations).each(function()
    {
        bounds.extend(new GLatLng(this.latitude, this.longitude));
    });
    map.setZoom(map.getBoundsZoomLevel(bounds));
    var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
    var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
    map.setCenter(new GLatLng(clat,clng));
}