/*Requires MooTools*/
GoogleMapSelector = new Class({	
	
	Implements: Options,	
	options: {
		edit	: false,
		zoom	: 13
	},
		
	
	initialize: function(mapContainer,mCenter,callBackElement,myForm,options) {		
		
		this.setOptions(options);
		
		if (GBrowserIsCompatible()) 
		{
			
			this.map = new GMap2(mapContainer);
			
			var mCenter = mCenter.split(",");
			
			this.mapCenter = new GLatLng(mCenter[0].toFloat(), mCenter[1].toFloat());
			
			this.map.setCenter(this.mapCenter, this.options.zoom);
			
			this.marker = new GMarker(this.mapCenter, {draggable: true});				
			
			if(this.options.edit)
			{
				this.map.openInfoWindow(this.map.getCenter(),document.createTextNode("Sleep mij naar de nieuwe locatie of klik met de rechtermuisknop."));
				
				GEvent.addListener(this.marker, "dragstart", function() {
				this.map.closeInfoWindow();
				
				}.bind(this));
				
				GEvent.addListener(this.marker, "dragend", function(point) {
				
				callBackElement.value = point.y+','+point.x;
				});
				
				
				GEvent.addListener(this.map,"singlerightclick",function(pixel,tile) {
				point = this.map.fromContainerPixelToLatLng(pixel);
				this.marker.setPoint(point);		
				callBackElement.value = point.y+','+point.x;
				}.bind(this));
			
			}
			else
			{
				this.marker.disableDragging();	
			}
			
			
			this.map.addOverlay(this.marker);	
			
			this.map.setUIToDefault();
			
			if(myForm)
			{
				myForm.addEvent('click', function(e) {
					
					//Prevents the default submit event from loading a new page.
					//e.stop();						
					
					if(!$('googleSearch'))
					{
						alert('Cannot find input type text with name "googleSearch".');
						return;
					}
					
					var address = $('googleSearch').value;
					var geocoder = new GClientGeocoder();
					geocoder.getLatLng(address, function(point) {
			
						if(!point)
						{
							alert("Adres \"" + address + "\" is niet gevonden.");
						}
						else
						{
							this.map.setCenter(point, this.options.zoom);
							this.marker.setPoint(point);
							callBackElement.value = point.y+','+point.x;
						}					
					
					}.bind(this));
				
				}.bind(this));
			}	
			
		}		
	}
	
});