var map = null;
var geocoder = null;
var showmap = false;
var bounds = null;
var addresses = null;

var icon = new GIcon();
icon.image = "http://apps.vienna.at/tools/apotheken/apotheken_marker.gif";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(27, 27);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(13, 1);

function mapload() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
    bounds = new GLatLngBounds();
  }
}

function showAddress(gmapAddress) {
    addresses = gmapAddress.split(/\n/);
    for (i = 0; i < addresses.length; i++) {
        addAddress(addresses[i]);
    }
}

function addAddress(currAddress) {
    if (geocoder) {
        geocoder.getLatLng(currAddress, 
            function(point) {
                if (!point) {
                    document.getElementById("map").style.display = 'none';
                } else {
                    map.setCenter(point);
                    var marker = new GMarker(point, icon);
                    map.addOverlay(marker);
    
                    GEvent.addListener(marker, "click", 
                        function() {
                            marker.openInfoWindowHtml("<b>" + currAddress.replace(/,/g, '<br>') + "</b>");
                        });
    
                    bounds.extend(new GLatLng(point.y, point.x));
                    redraw();
                }
            });
    }
}

function redraw() {
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
    if (map.getZoom() > 15)
        map.setZoom(15);
}

function mapCallBack(point, currAddress) {
    alert(point);
    if (!point) {
        document.getElementById("map").style.display = 'none';
    } else {
        map.setCenter(point);
        var marker = new GMarker(point);
        map.addOverlay(marker);

        GEvent.addListener(marker, "click", 
            function() {
                marker.openInfoWindowHtml("<b>" + currAddress + "</b>");
            });
        
        bounds.extend(new GLatLng(point.y, point.x));
        redraw();
    }
}

function unloadMap() {
    GUnload();
}


function addAddressWithInfo(currAddress, currInfo) {
	if (geocoder) {
		geocoder.getLatLng(currAddress,
			function(point) {
				if (!point) {
					
				} else {
					map.setCenter(point);
					var marker = new GMarker(point,icon);
					map.addOverlay(marker);
					GEvent.addListener(marker, "click",
						function() {
							marker.openInfoWindowHtml("<b>" + currInfo.replace(/,/g, '<br>') + "</b>");
						});
					bounds.extend(new GLatLng(point.y, point.x));
					map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
				}
			});
	}
}

function showXML(xmlAdress) {
	// Download the data in xmlAdresse and load it on the map. The format we expect is:
	// <markers>
	//   <marker adresse="<Adresse>" info="<Info>"/>
	// </markers>

	document.getElementById("map").style.display = 'none';

	GDownloadUrl(xmlAdress, function(data, responseCode) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		for (var i = 0; i < markers.length; i++) {
			addAddressWithInfo(markers[i].getAttribute("adresse"), markers[i].getAttribute("info"));
		}
	});
	redraw();
	
	document.getElementById("map").style.display = 'block';
}