google.load("maps", "2.x");
google.load("jquery","1");


var map = null;
    var geocoder = null;






function initialize() {

$('.tx-tcgooglemaps-pi1').prepend('<form class="search" action="" method="post"><input type="text" /><input type="submit" value="søg" /></form>');
$('form.search').submit(function(){

showAddress($('form.search > input[type=text]').val()+' denmark');
return false;
});




function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, parseInt(setup.zoomLevel));
           /*   var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);*/
            }
          }
        );
      }
    }



	var points = new Array();

	var map = new google.maps.Map2(document.getElementById("map"));
	
	map.setCenter(new google.maps.LatLng(55.662469, 12.541924), 13);
	
	geocoder = new GClientGeocoder();

		for(i=0;i<setup.markers.length;i++){
			
			var latlng = new GLatLng(parseFloat(setup.markers[i].lat),parseFloat(setup.markers[i].lng));
			points[i] = latlng;
			
			var iconArray = setup.icons[setup.markers[i].type];
			
			var iconFile = iconArray.path;
			var iconHeight = iconArray.height;
			var iconWidth = iconArray.width;
			centerPoint = new GPoint((iconWidth/2),(iconHeight/2));
			stdGSize = new GSize(iconWidth,iconHeight);
			
			customIcon = new GIcon();
			customIcon.image = iconFile;
			customIcon.iconSize = stdGSize
			customIcon.shadow = iconFile;
			customIcon.shadowSize = stdGSize
			customIcon.iconAnchor = centerPoint;
			customIcon.infoWindowAnchor = centerPoint;
			customIcon.infoShadowAnchor = centerPoint;
			
			var options = {icon:customIcon};							
			var marker = new GMarker(latlng,options);
			
			
			var html = '';
			if(setup.markers[i].image){
				html += setup.markers[i].image;
			}
			if(setup.markers[i].info){
				html += setup.markers[i].info;
			}
			if(html != ''){	
				addListener(marker,html);
			}
		
			
			map.addOverlay(marker);	
		}				
		fitMap(map,points);

	function addListener(markerobj,infoContent){
		GEvent.addListener(markerobj, "click", function() {
			if(infoContent){
				this.openInfoWindowHtml(infoContent);
			}
		});
		
	}

	map.removeMapType(G_SATELLITE_MAP);

	map.addControl(new GMapTypeControl());
	map.addControl(new GLargeMapControl());

	function fitMap( map, points ) {
		var bounds = new GLatLngBounds();
		for (var i=0; i< points.length; i++) {
			bounds.extend(points[i]);
		}
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
	}
}
google.setOnLoadCallback(initialize);
