function BienGMapManager()
{
	this.BienGMapObjects = new Array;
	
	this.addBienGMapObject = function(object_id, ajax_script, ajax_params, overwrite)
	{
		if(!this.isBienGMapObjectExisting(object_id) || overwrite)
		{			
			this.BienGMapObjects[object_id] = new BienGMap(object_id, ajax_script, ajax_params);
		}
	}
	
	this.getBienGMapObject = function(object_id)
	{
		if(this.isBienGMapObjectExisting(object_id))
		{
			return this.BienGMapObjects[object_id];
		}
	}
	
	this.isBienGMapObjectExisting = function(object_id)
	{
		if(this.BienGMapObjects[object_id]) return true; else return false;
	}
}

var BienGMapManagerObject = new BienGMapManager();

function BienGMap(object_id, ajax_script, ajax_params)
{
	this.objectId = object_id;
	this.ajaxScript = ajax_script;
	this.ajaxParams = ajax_params || {};
	
	this.gMap = null;
	this.gGeocoder = null;
	
	this.addressArray = $H();
	this.addressOrder = new Array;
	this.centerOnAddressId = null;
	
	this.defaultAdressId = null;
	
	this.navig_container = $(this.objectId+'_navigation');	
	
	if(this.navig_container)
	{
		this.navig_next = $(this.navig_container.id+'_next');
		this.navig_previous = $(this.navig_container.id+'_previous');
	} 
	
	
	this.navig_current_index = 0;
	
	this.init = function()
	{
		Event.observe(window, 'unload', function() {GUnload();});		
		
		this.loadGMap();	
		this.loadAddress();
		
	}
	
	this.loadGMap = function()
	{				
		if(GBrowserIsCompatible()) 
		{
	        this.gMap = new GMap2(document.getElementById(this.objectId));
			
			//this.gMap.enableScrollWheelZoom();
										       						
			this.gMap.addControl(new GMapTypeControl());
			this.gMap.addControl(new GLargeMapControl());
			//this.gMap.addControl(new GOverviewMapControl());
			 
			
			this.gGeocoder = new GClientGeocoder();
      	}
		else
			alert('Votre navigateur n\'est pas compatible !');
	}
	
	this.loadAddress = function()
	{
		AjaxScriptAsync(this.ajaxScript, Object.extend(this.ajaxParams, {object_id:this.objectId}));
	}
	
	this.setDefaultAdresseToId = function(id)
	{
		this.defaultAdressId = id;
	}
	
	this.addAddress = function(id, type, address, description)
	{
		this.addressArray[id] = $H();
		this.addressArray[id]['type'] = type;
		this.addressArray[id]['description'] = description;
		this.addressArray[id]['address'] = address;
		this.addressArray[id]['geolocalized'] = false;
		this.addressArray[id]['point'] = null;
		this.addressArray[id]['marker'] = null;
		
		this.addressOrder.push(id);
		
		this.geolocalizeAddress(id);
	}
	
	this.geolocalizeAddress = function(id)
	{		
		var address = this.addressArray[id]['address'];
		
		
		this.gGeocoder.getLatLng(address, this.onGeolocalizationComplete.bind(this, id));
	}
	
	this.onGeolocalizationComplete = function(id, point)
	{
		if(point)
		{
			var openinfo = false;
			
			if((!this.centerOnAddressId && !this.defaultAdressId) || this.defaultAdressId == id)
			{
				this.navig_current_index = this.addressOrder.indexOf(id);
				this.setupNavigation();
				
				this.centerOnAddressId = id;				
				this.gMap.setCenter(point, 13);
				
				openinfo = true;
			}			
				
			this.addressArray[id]['point'] = point;
			this.addressArray[id]['geolocalized'] = true;
			
			
			this.markOnGMap(id, openinfo);
			
			//dlog('geo ok : '+id);
		}
		else
		{
			//dlog('geo not ok : '+id);
		}
	}
	
	this.markOnGMap = function(id, open_info)
	{
		var datas = this.addressArray[id];
		
		if(datas['geolocalized'])
		{
			switch(datas['type'])
			{
				case 'Maison' :
				break;
				
				case 'Terrain' :
				break;
				
				case 'Appartement' :
				break;
				
				case 'Ecole primaire' :
				break;
				
				case 'Collège' :
				break;
				
				default:
					var marker = new GMarker(datas['point']);					
		        	this.gMap.addOverlay(marker);									
					this.addressArray[id]['marker'] = marker;		
					
					if(open_info)
						marker.openInfoWindowHtml(datas['description']);
					//GEvent.addListener(marker, "mouseover", this.onMouseOverGMarker.bind(this, id));
					//GEvent.addListener(marker, "mouseout", this.onMouseOverGMarker.bind(this, id));
									
				break;
			}
		}
		
	}	
	
	this.onMouseOverGMarker = function(id)
	{
		
	}
	
	this.onAjaxLoadComplete = function()
	{
		this.setupNavigation();
		
	}
		
	this.setupNavigation = function()
	{		
		if(!this.navig_container)
			return;
		var render_navig = false;
		var render_next = false;
		var render_previous = false;
		 

		if(this.addressOrder.length > 1)
		{
			render_navig = true;
			
			if(this.navig_current_index > 0)
			{
				render_previous = true;					
			}			
			
			if(this.navig_current_index < this.addressOrder.length-1)
			{
				render_next = true;
			}
			
		}
		
		if(render_navig)
		{
			if(render_next)
			{				
				this.navig_next.down('a').href="javascript:BienGMapManagerObject.getBienGMapObject('"+this.objectId+"').panTo("+(this.navig_current_index+1)+")";
				this.navig_next.show();
			}
			else
				this.navig_next.hide();
				
			if(render_previous)
			{
				this.navig_previous.down('a').href="javascript:BienGMapManagerObject.getBienGMapObject('"+this.objectId+"').panTo("+(this.navig_current_index-1)+")";
				this.navig_previous.show();
			}
			else
				this.navig_previous.hide();
				
			this.navig_container.show();
			
		}
		else
			this.navig_container.hide();
	}
	
	
	this.centerOn = function(index)
	{
		this.moveOn(index, 'center');
	}
	
	this.centerOnId = function(id)
	{
		this.centerOn(this.addressOrder.indexOf(id));
	}
	
	this.panTo = function(index)
	{	
		this.moveOn(index, 'pan');
	}
	
	this.panToId = function(id)
	{
		this.panTo(this.addressOrder.indexOf(id));
	}
	
	this.moveOn = function(index, type)
	{
		if(!this.addressOrder[index])
			return;
			
		this.navig_current_index = index;
		
		this.setupNavigation();
		
		var datas = this.addressArray[this.addressOrder[index]];
		
		
		if(!datas['geolocalized'])
		{
			//dlog('not geolocalized');
			return;
		}
		
		switch(type)
		{
			case 'pan':
				this.gMap.panTo(datas['point']);
				break;
			case 'center':
			default:			
				this.gMap.setCenter(datas['point']);
				break 
		}
		
		
		datas['marker'].openInfoWindowHtml(datas['description']);
	}
	
	this.init();
}