if(typeof(CenterCollectionController) == 'undefined')
    CenterCollectionController = new Object();


Object.extend(CenterCollectionController,
{
	
	
	_containerId: null,
	_map: null,
	_geocoder: null,
	_markers: $H(),
	_defaultPosition: null,
	_zoom: 12,
	
	initGoogleMap: function(containerId, defaultLatitude, defaultLongitude)
	{
		this._containerId = containerId;
		if (GBrowserIsCompatible()) 
		{
			this._map = new GMap2($(this._containerId));
			this._geocoder = new GClientGeocoder();
			this._map.addControl(new GSmallMapControl());
			this._defaultPosition = new GLatLng(defaultLatitude, defaultLongitude);
			this._map.setCenter(this._defaultPosition, this._zoom);

			var icon = new GIcon();
			icon.image = "/dynamic/centerCollection/images/intradel.png";
			icon.iconSize = new GSize(20, 20);
			icon.iconAnchor = new GPoint(15, 20);
			icon.infoWindowAnchor = new GPoint(15, 1);
			var marker = new GMarker(this._defaultPosition, icon);
			
			this._map.addOverlay(marker);			
		}
	},
	
	displayMarkers: function()
	{
		$$('input.googlemap-marker').each(function(el)
		{
			var data = $H();
			var lat = $F('googlemap-' + $F(el) + '-lat');
			var lng = $F('googlemap-' + $F(el) + '-lng');
			var type = $F('googlemap-' + $F(el) + '-type');
			
			
			data.set('lat',lat);
			data.set('lng',lng);
			data.set('type',type);
			if (type != 'searchplace') 
			{
				var icon = new GIcon();
				icon.image = "/dynamic/centerCollection/images/" + type + ".png";
				icon.iconSize = new GSize(20, 20);
				icon.iconAnchor = new GPoint(15, 20);
				icon.infoWindowAnchor = new GPoint(15, 1);
				var marker = new GMarker(new GLatLng(lat, lng), icon);
				this._map.addOverlay(marker);
				this._markers.set($F(el), data);
			}
			if (type == 'searchplace')
				this._map.setCenter(new GLatLng(lat, lng), this._zoom);
			
				
		}.bind(this));
	},
	
	
	centerMap: function(latitude, longitude)
	{
		//if (!this._map) return;		
		this._map.setCenter(new GLatLng(latitude, longitude), this._zoom);
	}
	  
	/*
	_apiKey: null,
	_latitude: null,
	_longitude: null,
	_map: null,
	_geocoder: null,
	_map: null,
	_latitudeFormFieldKey: null,
	_longitudeFormFieldKey: null,
	_latitudeFormFieldKey: null,
	
	loadGoogleMap: function(ContainerId, apiKey, defaultLatitude, defaultlongitude, latitude, longitude, latitudeFormFieldKey, longitudeFormFieldKey, addressFormFieldKey, localityFormFieldKey )
	{
		
		this._ContainerId = ContainerId;
		this._apiKey = apiKey;
		this._defaultLatitude = defaultLatitude;
		this._defaultLongitude = defaultlongitude;
		this._latitude = latitude;
		this._longitude = longitude;
		this._latitudeFormFieldKey = latitudeFormFieldKey;
		this._longitudeFormFieldKey = longitudeFormFieldKey;
		
		var point;
		var address = $F(addressFormFieldKey);
		var locality = $F(localityFormFieldKey);
		
		this._map = new GMap2($(ContainerId));
		this._map.setCenter(new GLatLng(50.545949, 5.674215), 13);
		this._geocoder = new GClientGeocoder();
		
		if (GBrowserIsCompatible()) 
		{
			this._map.setCenter(new GLatLng(defaultLatitude, defaultlongitude), 13);
			this._geocoder = new GClientGeocoder();
			
			if ( latitude > 0 && longitude > 0 )
			{
				this.addMarkerToMap(latitude, longitude, address + ' ' + locality);
			}			
			else
			{ 
				this.findLocation(address + ', ' + locality);
			}
		}
	},
	addMarkerToMap: function(lat, lng, InfoWindowHtml)
	{
		var point = new GLatLng(lat, lng);
		var pointlat = lat;
		var pointlng = lng;
		this._map.setCenter(point, 13);
		
		var marker = new GMarker(point, {draggable: true});
	
        
		//var icon = new GIcon();
		//icon.image = "http://dev.emod.be/images/pictos/dome_map.gif";
		//icon.shadow = "http://dev.emod.be/images/pictos/dome_map.gif";
		//icon.iconSize = new GSize(20, 25);
		//icon.shadowSize = new GSize(20, 25);
		//icon.iconAnchor = new GPoint(15, 20);
		//icon.infoWindowAnchor = new GPoint(15, 1);
		//var marker = new GMarker(point, icon);
		
	
		this._map.addControl(new GSmallMapControl());
        this._map.addControl(new GMapTypeControl());
	    
		this._map.addOverlay(marker);
		//marker.openInfoWindowHtml(InfoWindowHtml);
		
		GEvent.addListener(marker, "dragstart", function() {
			//this._map.closeInfoWindow();
		}.bind(this));
	
		GEvent.addListener(marker, "dragend", function() {
			point = marker.getPoint();
			if ( document.getElementById(this._latitudeFormFieldKey) )
				document.getElementById(this._latitudeFormFieldKey).value = point.lat();
			if ( document.getElementById(this._longitudeFormFieldKey) )
				document.getElementById(this._longitudeFormFieldKey).value = point.lng();
			//marker.openInfoWindowHtml(InfoWindowHtml);
		}.bind(this));
	},
	// addAddressToMap() is called when the geocoder returns an
	// answer.  It adds a marker to the map with an open info window
	// showing the nicely formatted version of the address and the country code.
	addAddressToMap: function(response) 
	{
		var place;
		this._map.clearOverlays();
		if (!response || response.Status.code != 200) {
			//alert("Désolé, il est impossible de localiser l'adresse :<br>");
			this.addMarkerToMap(this._defaultLatitude, this._defaultLongitude, translation.get('sorry__the_location_could_not_be_found') );
		} 
		else {
			place = response.Placemark[0]; 		
			this.addMarkerToMap(place.Point.coordinates[1], place.Point.coordinates[0], place.address);
		}
	},
	findLocation: function(address) 
	{
		this._geocoder.getLocations(address, this.addAddressToMap.bind(this));
	},
	// showLocation() is called when you click on the Search button
	// in the form.  It geocodes the address entered into the form
	// and adds a marker to the map at that location.
	showLocation: function(addressFormFieldKey, localityFormFieldKey) 
	{
		var address = $F(addressFormFieldKey) + ', ' + $F(localityFormFieldKey);
		this._geocoder.getLocations(address, this.addAddressToMap.bind(this));
	}
	*/
});