/*Requires MooTools AYCDICDB
Usage:
mapContainer 	: Element div where the map should be shown.
addresses		: String address or array of addresses where markers should be placed.
addressesText	: Text that should be show for the info window for the addresses. Defaults to the marker address
*/
GoogleMapAddress = new Class({	
	
	Implements: Options,	
	options: {
		zoom		: 13,
		icon		: "",
		iconSize	: "32,32",
		draggable	: false
	},		
	
	initialize: function(mapContainer,addresses,addressesText,options) {		
		
		this.setOptions(options);
		
		if (GBrowserIsCompatible()) 
		{
			addressesText = $splat(addressesText);
			
			this.map = new GMap2(mapContainer);
			this.geocoder = new GClientGeocoder();						
			
			var baseIcon = new GIcon(G_DEFAULT_ICON);			
			if(this.options.icon != "")
			{
				var size = this.options.iconSize.split(",");
				baseIcon.iconSize = new GSize(parseInt(size[0]),parseInt(size[1]));
				baseIcon.shadowSize=new GSize(parseInt(size[0])-15,parseInt(size[1])-15);
				//baseIcon.iconAnchor=new GPoint(50,50);
				baseIcon.infoWindowAnchor=new GPoint(0,0);				
				baseIcon.image = this.options.icon;
				this.markerOptions = { icon:baseIcon,draggable:this.options.draggable };
			}			
			
			$splat(addresses).each(function(address,index){				
				this.geocoder.getLatLng(address, function(point) {					
					if(point)
					{
						if(index == 0)
						{
							this.map.setCenter(point, this.options.zoom);	
						}
						
						marker = new GMarker(point,this.markerOptions);
						
						GEvent.addListener(marker, "click", function() {							
							var infoText = $chk(addressesText[index]) ? addressesText[index] : address ;
							this.map.openInfoWindowHtml(point,infoText);
							this.map.setCenter(point,this.options.zoom);
							//this.map.panTo(point,13);
						}.bind(this));
						
						this.map.addOverlay(marker);
					}
					else
					{						
						alert("Adres \"" + address + "\" is niet gevonden.");
					}					
			
				}.bind(this));
				
			}.bind(this));			
					
			this.map.setUIToDefault();			
		}		
	}
	
});