// Fonctions nécessaires ŕ la recherche des marchands

function createMarker(point, html) {

    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);

    var marker = new GMarker(point, baseIcon);
    
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    
    /*
    GEvent.addListener(marker, "mouseover", function() {
        marker.openInfoWindowHtml(html, {buttons:{close:{show:0}}});
    });

    GEvent.addListener(marker, "mouseout", function() {
        marker.closeInfoWindow();
    });
    */

    return marker;
}

function showAddress(address, html) {
    geocoder.getLatLng(
        address,
        function(point) {
            if (!point) {
                alert(address + " not found!");
                var mapErrors = document.getElementById("mapErrors");
                mapErrors.innerHTML = mapErrors.innerHTML + "<br>" + address + " not found";
            } else {
                var marker = createMarker(point, html);
                map.addOverlay(marker);
            }
        });
}

function listCenters(baseLongitude, baseLatitude, language) {

	if (map != null) {
	
        var request = null;
		var content = null;
		var longitude = null;
		var latitude = null;

        // Read the data from the webservice file
        request = GXmlHttp.create();

        $mapfile = '../../include/merchant/XmlMerchant.aspx?l=' + language + '&lat=' + baseLatitude + '&lng=' + baseLongitude;
		             
        request.open("GET", $mapfile, true);

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                var xmlDoc = GXml.parse(request.responseText);

                // obtain the array of markers and loop through it
                var markers = xmlDoc.documentElement.getElementsByTagName("marker");

                if (markers.length > 0) {

                    for (var i = 0; i < markers.length; i++) {
                        content = (markers[i].text) ? markers[i].text : (markers[i].textContent) ? markers[i].textContent : "";

                        longitude = markers[i].getAttribute("longitude");
                        latitude = markers[i].getAttribute("latitude");
                        
                        var point = new GLatLng(latitude.replace(",","."), longitude.replace(",","."));
                        var marker = createMarker(point, content);
                        map.addOverlay(marker);
                    }
                }
			}
        }
        request.send(null);
	}
}