var map = null;
var mapCenter = null;
var geocoder = null;
var iconChili = new Array();
var iconChiliSmall = new Array(); 
var markerOwn = null;
var markerUsers = new Array();

function load() {
    if (GBrowserIsCompatible()) {
        map = new GMap2($('GoogleMap'));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        
        if (mapCenter) {
            map.setCenter(mapCenter, 11);
        } else {
            map.setCenter(new GLatLng(51.318928, 9.49601), 5);
        }
        
        for (i=0; i<11; i++) {
            iconChili[i] = new GIcon();
            iconChili[i].image = _siteWebroot + 'img/map.chili.ac' + (i+1) + '.png';
            iconChili[i].shadow = _siteWebroot + 'img/map.chili.shadow.png';
            iconChili[i].iconSize = new GSize(15, 56);
            iconChili[i].shadowSize = new GSize(44, 56);
            iconChili[i].iconAnchor = new GPoint(2, 55);
            iconChili[i].infoWindowAnchor = new GPoint(7, 1);
            iconChili[i].infoShadowAnchor = new GPoint(33, 1);
        }
        
        for (i=0; i<11; i++) {
            iconChiliSmall[i] = new GIcon();
            iconChiliSmall[i].image = _siteWebroot + 'img/map.chili.ac' + (i+1) + '.small.png';
            iconChiliSmall[i].shadow = _siteWebroot + 'img/map.chili.shadow.small.png';
            iconChiliSmall[i].iconSize = new GSize(8, 28);
            iconChiliSmall[i].shadowSize = new GSize(18, 28);
            iconChiliSmall[i].iconAnchor = new GPoint(1, 27);
            iconChiliSmall[i].infoWindowAnchor = new GPoint(4, 1);
            iconChiliSmall[i].infoShadowAnchor = new GPoint(16, 1);
        }
        
        geocoder = new GClientGeocoder();   
        
        showUsers();
    }
}
 
function showUsers() {
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
         
    new Ajax.Request(_siteWebroot + 'Users/getCloseUsers/' + southWest.lat() + '/' + southWest.lng() + '/' + northEast.lat() + '/' + northEast.lng(),
                     {  onLoading: function() {},
                        onComplete: function(request) { 
                            var users = request.responseText.evalJSON();
                            users.each(function(user, index) {
                                
                                markerUsers[index] = new GMarker(new GLatLng(user.Location.lat,
                                                                             user.Location.lon), {icon: iconChiliSmall[user.User.activity-1]});
                                map.addOverlay(markerUsers[index]);
                                GEvent.addListener(markerUsers[index], "click", function() {
                                    if (_user.id) {
                                        markerUsers[index].openInfoWindowHtml('<a href="' + _siteWebroot + 'Users/view/' + user.User.username + '" ' +
                                                                              'onclick="javascript: opener.location.href = \'' + _siteWebroot + 'Users/view/' + user.User.username + '\'; ' +
                                                                              'return false;">' + user.User.username + '</a>');
                                    } else {
                                        markerUsers[index].openInfoWindowHtml(user.User.username);
                                    }
                                });
                            });
                        }
                     });             
}

function showAddress(address) {
    if (geocoder) {
        geocoder.getLocations(
            address,
            function(response) {
                if (!response || response.Status.code != 200) {
                    alert("'" + address + "' nicht gefunden/not found");
                } else {
                    place = response.Placemark[0];
                    point = new GLatLng(place.Point.coordinates[1],
                                        place.Point.coordinates[0]);
                    map.setCenter(point, 11);        
                    if (markerOwn) {
                        map.removeOverlay(markerOwn);       
                    }
                    markerOwn = new GMarker(point, {icon: iconChili[_user.activity-1], zIndexProcess: function(){return 9999;}});
                    map.addOverlay(markerOwn);
                    markerOwn.openInfoWindowHtml(place.address);
                    GEvent.addListener(markerOwn, "click", function() {
                        markerOwn.openInfoWindowHtml(place.address);
                    });
                    $('LocationAddressString').value = place.address;
                }
            }
        );
    }
}

Event.observe(window, 'load', load);