﻿var map = null;
var geocoder = null;
var showmap = false;
var bounds = null;
var addresses = null;

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) {
    var description = null;
    var currAddress = null;
    var currPoint = null;
    var lat = null;
    var lang = null;
    var Exp1 = /(.+)\|(.+)/;
    var Exp2 = /\((.+),(.+)\)/;

    /* fill an array with all locations */
    addresses = gmapAddress.split(/\n/);
    for (i = 0; i < addresses.length; i++) {
        /*check on a descripive part (format: [description]|[adress])  */
        if (Exp1.exec(addresses[i]) != null) {
            description = RegExp.$1;
            currAddress = RegExp.$2;
        }
        else {
            description = null;
            currAddress = addresses[i];
        }

        /* check if this is an address or a pair of coordinates (must match format ([latitude], [longitude]) ) */
        if (Exp2.exec(currAddress) != null) {
            lat = RegExp.$1;
            lang = RegExp.$2;
            currPoint = new GLatLng(parseFloat(lat), parseFloat(lang));

            /* add an geocoded point to the map */
            addGeocodedPoint(currPoint, description);
        }
        else {
            /* add an address to the map */
            addAddress(currAddress, description);
        }

    }
}

function addGeocodedPoint(point, description) {
    map.setCenter(point);
    var marker = new GMarker(point);
    map.addOverlay(marker);

    GEvent.addListener(marker, "click",
		function() {
		    marker.openInfoWindowHtml(description);
		});

    bounds.extend(new GLatLng(point.y, point.x));
    redraw();
}

function addAddress(currAddress, description) {
    if (geocoder) {
        geocoder.getLatLng(currAddress,
			function(point) {
			    if (!point) {
			        /* document.getElementById("map").style.display = 'none'; */
			    } else {
			        map.setCenter(point);
			        var marker = new GMarker(point);
			        map.addOverlay(marker);
			        if (description != null) {
			            GEvent.addListener(marker, "click",
						function() {
						    marker.openInfoWindowHtml("<b>" + description + "</b><br/>" + currAddress);
						});
			        }
			        else {
			            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();
}

