﻿var gmapsLink = "http://maps.google.nl/maps?saddr=&daddr=";

function initializeGmaps(mapHeight, mapWidth, mapZoomFactor, address, iconUrl, popupImgUrl, getDirectionsLabel, toHereLabel, fromHereLabel) {

    var divMap = document.getElementById('googleMaps');
    
    if(divMap != null)
    {
        var geoCoder = new GClientGeocoder();
        geoCoder.getLatLng(address, function(point) { 
            if(point != null)
            {
                iconUrl = iconUrl.replace("~", "");
                divMap.style.height = mapHeight + 'px';
                divMap.style.width = mapWidth + 'px';
                
                var mapObj = new GMap2(divMap);
                mapObj.setCenter(point, mapZoomFactor);  
                mapObj.setUIToDefault();
                var marker = new GMarker(point);
                mapObj.addOverlay(marker);
		marker.openInfoWindow('<div><div><b>Entree Afval Energie Bedrijf</b></div><div>Australiëhavenweg 21</div><div>1045 BA Amsterdam</div></div>');
                
		// Gebied van AEB
		var ll = [14];
		ll[0] = new GLatLng(52.401889, 4.790007);
		ll[1] = new GLatLng(52.401921, 4.793236);
		ll[2] = new GLatLng(52.402209, 4.793644);
		ll[3] = new GLatLng(52.402792, 4.793698);
		ll[4] = new GLatLng(52.402825, 4.800081);
		ll[5] = new GLatLng(52.402268, 4.800103);
		ll[6] = new GLatLng(52.401653, 4.799341);
		ll[7] = new GLatLng(52.401175, 4.798193);
		ll[8] = new GLatLng(52.400907, 4.79683);
		ll[9] = new GLatLng(52.400534, 4.795392);
		ll[10] = new GLatLng(52.400115, 4.794652);
		ll[11] = new GLatLng(52.399598, 4.794341);
		ll[12] = new GLatLng(52.399637, 4.790028);
		ll[13] = new GLatLng(52.401889, 4.790007);

		//var hl = new GPolygon(ll,"#000055",2,1,"#0000FF",0.5);
		//mapObj.addOverlay(hl);

		var sw = ll[12];
		var ne = ll[4];
		var bnds = new GLatLngBounds(sw, ne);
		var gover = new GGroundOverlay("/resources/images/aeboverlay.png", bnds);
		mapObj.addOverlay(gover);

                if(popupImgUrl != null && popupImgUrl != "")
                {
                    var maxW = 50;
                    var html = getPopupHtml(address, popupImgUrl, getDirectionsLabel, toHereLabel, fromHereLabel); //"<img src=\"" + popupImgUrl + "\" alt=\"popup image\" />";
                    GEvent.addListener(marker, "click", function() {
                      mapObj.openInfoWindowHtml(point, html);
                     });
                }

            }
         });
    }
}

function getPopupHtml(address, popupImgUrl, getDirectionsLabel, toHereLabel, fromHereLabel)
{
    var text = "<div style=\"font-size: 9pt;\"><img src=\"" + popupImgUrl + "\" alt=\"popup image\" />";
    text = text.replace(/~/, "");

    text += "<br />";
    
    if(getDirectionsLabel == null || getDirectionsLabel == "")
        getDirectionsLabel = "Get directions:";
        
    text += getDirectionsLabel + "<br />";
    
    if(toHereLabel == null || toHereLabel == "")
        toHereLabel = "To here";

    var toLink = gmapsLink.replace(/daddr=/, "daddr=" + address);
    text += "<a href=\"" + toLink + "\" target=\"_blank\">" + toHereLabel + "</a>" + " - ";

    if(fromHereLabel == null || fromHereLabel == "")
        fromHereLabel = "From here";

    var fromLink = gmapsLink.replace(/saddr=/, "saddr=" + address);
    text += "<a href=\"" + fromLink + "\" target=\"_blank\">" + fromHereLabel + "</a>" + "</div>";

    return text;
}

function GoogleMapUnload()
{
    if (typeof(GUnload) == "function") GUnload();
}
