﻿
        var map;
        var geocoder;
        
        function initialize() {
            if(document.getElementById("map") != undefined)
            {
              map = new GMap2(document.getElementById("map"));
              //map = document.getElementById("map");
              map.setCenter(new GLatLng(37.98839, -122.526909), 12);
              map.addControl(new GLargeMapControl());
              map.addControl(new GMapTypeControl());
              map.addMapType(G_PHYSICAL_MAP);
              geocoder = new GClientGeocoder();
              //Display Map:
              showLocation();
             }
        }

        function addAddressToMap(response) {
          var bounds = new GLatLngBounds();	// The bounds of the map; used for centering and zoom
	      map.clearOverlays();
          var points = [];
          map.clearOverlays();
          if (!response || response.Status.code != 200) {
            var address ="Oslo, Norway";
            //Search again: 
            geocoder.getLocations(address, addAddressToMap);
          } else {
            place = response.Placemark[0];
            point = new GLatLng(place.Point.coordinates[1],
                                place.Point.coordinates[0]);
	        marker = new GMarker(point);
		    bounds.extend(point);		// Add this point to the full bounds
		    points.push(point);
		    //Add event :
            GEvent.addListener(marker, "click", function()
				{
			        var html = '<div>';
			        html += "<h3>" + place.address + "</h3>";
			        /*
			        html += "<img alt='" + m.title + "' src='" + generateImageLink(m.image) + "' style='float: left; margin-right: 1em' />";
			        html += "<ul>";
                    html += "<li><span>Adresse:</span>" + m.address + "</li>";
                    html += "<li><span>Boligtype:</span>" + m.property_type + "</li>";
                    html += "<li><span>Areal:</span>" + m.area + "</li>";
                    html += "</ul>";
			        html += '<p><a href="/properties/show/' + m.ref + '">Se hele annonsen her</a></p>';
			        */
			        html += '<div style="clear:left"></div>';
			        html += '</div>';
                    map.openInfoWindowHtml(point, html);
				});
		        var html = '<div>';
		        html += "<h3>" + place.address + "</h3>";
		        /*
		        html += "<img alt='" + m.title + "' src='" + generateImageLink(m.image) + "' style='float: left; margin-right: 1em' />";
		        html += "<ul>";
                html += "<li><span>Adresse:</span>" + m.address + "</li>";
                html += "<li><span>Boligtype:</span>" + m.property_type + "</li>";
                html += "<li><span>Areal:</span>" + m.area + "</li>";
                html += "</ul>";
		        html += '<p><a href="/properties/show/' + m.ref + '">Se hele annonsen her</a></p>';
		        */
		        html += '<div style="clear:left"></div>';
		        html += '</div>';
                map.openInfoWindowHtml(point, html);
                    map.addOverlay(marker);
				
//			var html = '<div>';
//			html += "<h3>" + place.address + "</h3>";
//			/*
//			html += "<img alt='" + m.title + "' src='" + generateImageLink(m.image) + "' style='float: left; margin-right: 1em' />";
//			html += "<ul>";
//            html += "<li><span>Adresse:</span>" + m.address + "</li>";
//            html += "<li><span>Boligtype:</span>" + m.property_type + "</li>";
//            html += "<li><span>Areal:</span>" + m.area + "</li>";
//            html += "</ul>";
//			html += '<p><a href="/properties/show/' + m.ref + '">Se hele annonsen her</a></p>';
//			*/
//			html += '<div style="clear:left"></div>';
//			html += '</div>';

//            map.openInfoWindowHtml(point, html);
//            
            default_point = points[0];
            map.setCenter(bounds.getCenter());
            var zoomLevel =15;
            var zoom = map.getBoundsZoomLevel(bounds);
            if (zoomLevel < 20) // Hack; Google maps doesn't support this high level - provide any level above 20 to avoid zooming
            {
	            map.setZoom(zoomLevel);
            }
            else
            {
	            map.setZoom(zoom);
            }
          }
        }

        function showLocation() {
          var address = document.getElementById("p_ctl07_hdnAddress").value;
          /*var address1 = document.getElementById("p_ctl02_hdnAddress1").value;*/
          geocoder.getLocations(address, addAddressToMap);
          /*geocoder.getLocations(address1, addAddressToMap);*/
        }

