﻿var map = null;
var geocoder = null;
var bounds = null;
var steyrIcon = null;
var markerIcon = null;
var steyrIconPath = null;
var iconPath = null;
var bounds = null;
var markers = new Array();
var mCount = 0;
var zoomIn = true;
var zoomOut = true;

function load() {
	if (!map && GBrowserIsCompatible()) {
	    map = new GMap2(document.getElementById("map"));

		map.addControl(new GLargeMapControl());
		
		steyrPoint = new GLatLng(48.160141, 14.513084);
		geocoder = new GClientGeocoder();
		bounds = new GLatLngBounds();

		markerIcon = new GIcon();
		markerIcon.image = iconPath;
		markerIcon.iconAnchor = new GPoint(5, 5);
		markerIcon.infoWindowAnchor = new GPoint(5, 5);
		markerIcon.iconSize = new GSize(11, 11);

		steyrIcon = new GIcon();
		steyrIcon.image = steyrIconPath;
		steyrIcon.iconAnchor = new GPoint(24, 28);
		steyrIcon.infoWindowAnchor = new GPoint(48, 0);
		steyrIcon.iconSize = new GSize(48, 28);
		
		var optMarker = { title: 'STEYR', icon: markerIcon };
		var steyrMarker = new GMarker(steyrPoint, optMarker);
		
		map.setCenter(steyrPoint, 10);
		map.addOverlay(steyrMarker);

		GEvent.addListener(map, "click", function(overlay, point) {
			if (overlay) {
				if (overlay.myhtml) {
					overlay.openInfoWindowHtml(overlay.myhtml);
				}
			}
        });
	}
}

function setMarkerImage(newLevel) {
    window.setTimeout("setMarkerImageAsync(" + newLevel + ")", 50);
}

function setMarkerImageAsync(newLevel) {
    if (newLevel >= 9) {
        if (zoomIn) {
            map.clearOverlays();

            for (var i = 1; i < markers.length; ++i) {
                var markerOptions = { title: markers[i].getTitle(), icon: steyrIcon };
                var marker = new GMarker(markers[i].getLatLng(), markerOptions);
                marker.myhtml = markers[i].myhtml;
                map.addOverlay(marker);
            }
            zoomOut = true;
            zoomIn = false;
        }
    }
    else {
        if (zoomOut) {
            map.clearOverlays();

            for (var i = 1; i < markers.length; ++i) {
                var markerOptions = { title: markers[i].getTitle(), icon: markerIcon };
                var marker = new GMarker(markers[i].getLatLng(), markerOptions);
                marker.myhtml = markers[i].myhtml;
                map.addOverlay(marker);
            }
            zoomIn = true;
            zoomOut = false;
        }
    }
}

function setMarkerWithAddress(address, name, nr, language) {
	if (!geocoder) {
		load();
	}

	if (geocoder) {
		//use getLocations to get status of request
	    geocoder.getLocations(address, function(response) {
	        if (!response || response.Status.code != 200) {
	            //alert("\"" + address + "\" nicht gefunden (Statuscode: " + response.Status.code + ")");

	            // address not found --> try city and country only
	            if (response.Status.code == 602) {
	                var piece = address.split(',');
	                if (piece.length >= 4) {
	                    window.setTimeout("setMarkerWithAddress('" + piece[1] + "," + piece[3] + "', '" + name + "')", 100);
	                }
	            }
	            //too many queries
	            if (response.Status.code == 620) {
	                window.setTimeout("setMarkerWithAddress('" + address + "', '" + name + "')", 300);
	            }
	        }
	        else {
	            var place = response.Placemark[0];
	            var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

	            bounds.extend(point);
	            var markerOptions = { title: name, icon: markerIcon };
	            var marker = new GMarker(point, markerOptions);

	            marker.myhtml = getMarkerHtml(name, address.replace(', ', ''), nr, language);
	            
	            mCount++;
	            markers[mCount] = marker;
	        }
	    });
	}
}

function setMarker(latitude, longitude, name, address, nr, language) {
	if (!geocoder) {
		load();
	}

	var point = new GLatLng(latitude, longitude);
	bounds.extend(point);
	
	var markerOptions = { title: name, icon: markerIcon };
	var marker = new GMarker(point, markerOptions);
	marker.myhtml = getMarkerHtml(name, address, nr, language);

	mCount++;
	markers[mCount] = marker;
}

function displayMarkers() {
    for (var i = 1; i < markers.length; ++i) {
        map.addOverlay(markers[i]);
    }
    if (map.getZoom() >= 9) {
        setMarkerImage(map.getZoom());
    }
}

function resetMap() {
    if (!geocoder) {
        load();
    }

	bounds = new GLatLngBounds();
	markers = new Array();
	zoomIn = true;
	zoomOut = true;
	
	mCount = 0;
	if (map) {
	    map.clearOverlays();
	}
}

function getMarkerHtml(name, address, nr, language) {
    return '<b>' + name + '</b><br />' + address + '<br/><br/><a href="javascript:openDetailsWindow(\'' + nr + '\',\'' + language + '\')" >détails</a>';
}

function openDetailsWindow(nr, language) {
    window.open('Detail.aspx?c=' + language + '&Nr=' + nr ,'', 'width=680,height=470,menubar=no,scrollbars=no,status=no,toolbar=no,location=no');
}


