﻿	var map;
	var geocoder;
	var baseIcon;
	var bounds;
	var numPoints = 0;
	var markers = [];

	function createMarker(point) {
		var maxIcon = new GIcon(baseIcon);
		maxIcon.image = "/images/mapicon.png";
		// Set up our GMarkerOptions object
		markerOptions = { icon:maxIcon };
		return new GMarker(point, markerOptions);;
	}

	// Call this function when the page has been loaded
	function initialize() {
	
	    var mapElement = document.getElementById("map");
        if (mapElement) {
	        map = new google.maps.Map2(mapElement);
		    map.setCenter(new google.maps.LatLng(62.754726,17.973633), 4);
    		
		    geocoder = new GClientGeocoder();
		    bounds = new GLatLngBounds();

		    var smallMapControl = new GSmallMapControl();
		    var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5,5));
		    map.addControl(smallMapControl, topLeft);

		    // Create a base icon for all of our markers that specifies the
		    // shadow, icon dimensions, etc.
		    baseIcon = new GIcon();
		    baseIcon.shadow = "/images/mapicon_shadow.png";
		    baseIcon.iconSize = new GSize(30, 38);
		    baseIcon.shadowSize = new GSize(34, 45);
		    baseIcon.iconAnchor = new GPoint(7, 36);
		    //baseIcon.infoWindowAnchor = new GPoint(9, 2);
		    //baseIcon.infoShadowAnchor = new GPoint(18, 25);

            // Load map markers
            for (var i = 0; i < markers.length; i++) {
                addMarker(markers[i][0], markers[i][1], markers[i][2], markers[i][3], markers[i][4], markers[i][5], true);
            }
		}
	}
	
	function setMapAddress(address, pos) {
	    if (pos) {
	        map.setCenter(pos, 15);
	    } else {
	        geocoder.getLatLng(
	            address,
	            function(point) {
	                if (point) {
	                    map.setCenter(point, 15);
	                }
	            }
	        );
	    }
	}

	function defMarker(name, address, postalCode, city, phoneNo, pos) {
	    markers[numPoints] = [];
        markers[numPoints][0] = name;
        markers[numPoints][1] = address;
        markers[numPoints][2] = postalCode;
        markers[numPoints][3] = city;
        markers[numPoints][4] = phoneNo;
        markers[numPoints][5] = pos;
        numPoints++;
    }

	function addMarker(name, address, postalCode, city, phoneNo, pos, zoom) {
	    if (pos) {
            var marker = createMarker(pos);
		    GEvent.addListener(marker, "click", function() {
                map.openInfoWindowHtml(
                    pos,
                    '<b>' + name + '</b><br>' + 
                    address + '<br>' +
                    postalCode + ' ' + city + '<br>' + 
                    phoneNo
                );
            });
            map.addOverlay(marker);
            bounds.extend(pos);
            if (zoom == true && --numPoints == 0) {
                map.setZoom(map.getBoundsZoomLevel(bounds));
                map.setCenter(bounds.getCenter());
            }
	    } else {
	        geocoder.getLatLng(
	            address + ' ' + postalCode + ' ' + city,
	            function(point) {
	                if (point) {
	                    var marker = createMarker(point);
        			    GEvent.addListener(marker, "click", function() {
		                    map.openInfoWindowHtml(
		                        point,
		                        '<b>' + name + '</b><br>' + 
		                        address + '<br>' +
		                        postalCode + ' ' + city + '<br>' + 
		                        phoneNo
		                    );
		                });
	                    map.addOverlay(marker);
	                    bounds.extend(point);
	                    if (zoom == true && --numPoints == 0) {
		                    map.setZoom(map.getBoundsZoomLevel(bounds));
                            map.setCenter(bounds.getCenter());
	                    }
	                }
	            }
	        );
	    }
	}
