var eMap, eInfos;
var oMap, oGLatLngBounds, oIcon;
var aPoints, aTypes;
var sDate;
var fRefreshMap;

aPoints = [
	{
		'nom': 'Mairie de Gouesnou',
		'addresse': 'Place des Fusillés',
		'telephone': '02 98 07 86 90',
		'latitude': 48.45039955732798,
		'longitude': -4.465792179107666,
		'registre': true,
		'commissaire': {
			'2009-3-9': '9 mars 2009 - 14h00 - 17h00',
			'2009-4-8': '8 avril 2009 - 9h00 - 12h00',
			'2009-4-17': '17 avril 2009 - 9h00 - 12h00'
		}
	},
	{
		'nom': 'Mairie de Bohars',
		'addresse': '1, rue Prosper Salaun',
		'telephone': '02 98 03 59 63',
		'latitude': 48.42887592509323,
		'longitude': -4.514973163604736,
		'registre': true,
		'commissaire': false
	},
	{
		'nom': 'Mairie de Guilers',
		'addresse': '16, rue Charles de Gaulle',
		'telephone': '02 98 07 61 52',
		'latitude': 48.42370718756201,
		'longitude': -4.557137489318848,
		'registre': true,
		'commissaire': false
	},
	{
		'nom': 'Mairie de Guipavas',
		'addresse': 'Place de la Mairie',
		'telephone': '02 98 84 75 54 ',
		'latitude': 48.43423063839376,
		'longitude': -4.4018107652664185,
		'registre': true,
		'commissaire': {
			'2009-3-9': '9 mars 2009 - 14h00 - 17h00',
			'2009-3-20': '20 mars 2009 - 9h00 - 12h00',
			'2009-4-17': '17 avril 2009 - 9h00 - 12h00'
		}
	},
	{
		'nom': 'Mairie du Relecq-Kerhuon',
		'addresse': 'Place de la Libération',
		'telephone': '02 98 28 14 18 ',
		'latitude': 48.40765654542511,
		'longitude': -4.396569728851318,
		'registre': true,
		'commissaire': false
	},
	{
		'nom': 'Mairie de Plougastel-Daoulas',
		'addresse': 'Place Jean Fournier',
		'telephone': '02 98 37 57 57 ',
		'latitude': 48.37169156017345,
		'longitude': -4.370412826538086,
		'registre': true,
		'commissaire': false
	},
	{
		'nom': 'Mairie de Plouzané',
		'addresse': 'ZAC Castel Nevez',
		'telephone': '02 98 31 95 30',
		'latitude': 48.38023198273052,
		'longitude': -4.598078727722168,
		'registre': true,
		'commissaire': false
	},
	{
		'nom': 'Mairie de Bellevue',
		'addresse': '25, place Napoléon III',
		'telephone': '02 98 00 85 00 ',
		'latitude': 48.404605,
		'longitude': -4.506648,
		'registre': true,
		'commissaire': false
	},
	{
		'nom': 'Mairie de Brest centre',
		'addresse': '2, rue Frézier',
		'telephone': '02 98 00 82 30',
		'latitude': 48.39160083594044,
		'longitude': -4.484546184539795,
		'registre': true,
		'commissaire': {
			'2009-3-9': '9 mars 2009 - 9h00 - 12h00',
			'2009-3-19': '19 mars 2009 - 14h00 - 17h00',
			'2009-3-28': '28 mars 2009 - 9h00 - 12h00',
			'2009-4-7': '7 avril 2009 - 14h00 - 17h00',
			'2009-4-17': '17 avril 2009 - 14h00 - 17h00'
		}
	},
	{
		'nom': 'Mairie de l’Europe',
		'addresse': '31, rue Saint-Jacques',
		'telephone': '02 98 34 26 30 ',
		'latitude': 48.4087504856973,
		'longitude': -4.4733452796936035,
		'registre': true,
		'commissaire': {
			'2009-3-18': '18 mars 2009 - 14h00 - 17h00',
			'2009-3-24': '24 mars 2009 - 9h00 - 12h00',
			'2009-3-30': '30 mars 2009 - 14h00 - 17h00',
			'2009-4-16': '16 avril 2009 - 14h00 - 17h00'
		}
	},
	{
		'nom': 'Mairie de Lambézellec',
		'addresse': '25, rue Robespierre',
		'telephone': '02 98 00 85 20 ',
		'latitude': 48.4089221239121,
		'longitude': -4.490940570831299,
		'registre': true,
		'commissaire': false
	},
	{
		'nom': 'Mairie de Saint-Marc',
		'addresse': '124, rue de Verdun',
		'telephone': '02 98 00 85 70 ',
		'latitude': 48.39848247480434,
		'longitude': -4.4544196128845215,
		'registre': true,
		'commissaire': {
			'2009-3-27': '27 mars 2009 - 14h00 -17h00 ',
			'2009-4-16': '16 avril 2009 - 14h00 - 17h00'
		}
	},
	{
		'nom': 'Mairie de Saint-Pierre',
		'addresse': '26, rue Jean-François Tartu',
		'telephone': '02 98 00 81 70 ',
		'latitude': 48.38296541349848,
		'longitude': -4.529886245727539,
		'registre': true,
		'commissaire': {
			'2009-3-18': '18 mars 2009 - 9h00 - 12h00',
			'2009-3-27': '27 mars 2009 - 9h00 - 12h00',
			'2009-4-8': '8 avril 2009 - 14h00 - 17h00',
			'2009-4-16': '16 avril 2009 - 9h00 - 12h00'
		}
	},
	{
		'nom': 'Mairie de Quatre-Moulins',
		'addresse': '200, rue Anatole France',
		'telephone': '02 98 00 85 40',
		'latitude': 48.38272314454519,
		'longitude': -4.51563835144043,
		'registre': true,
		'commissaire': {
			'2009-3-20': '20 mars 2009 - 14h00 - 17h00',
			'2009-3-24': '24 mars 2009 - 14h00 - 17h00',
			'2009-3-31': '31 mars 2009 - 9h00 - 12h00',
			'2009-4-16': '16 avril 2009 - 9h00 - 12h00'
		}
	},
	{
		'nom': 'Hôtel de communauté de Brest métropole océane',
		'addresse': '200, rue Anatole France',
		'telephone': '02 98 33 50 50',
		'latitude': 48.39390906700129,
		'longitude': -4.483022689819336,
		'registre': true,
		'commissaire': {
			'2009-3-9': '9 mars 2009 - 9h00 - 12h00',
			'2009-3-17': '17 mars 2009 - 14h00 - 17h00',
			'2009-3-23': '23 mars 2009 - 14h00 - 17h00',
			'2009-3-31': '31 mars 2009 - 14h00 - 17h00',
			'2009-4-17': '17 avril 2009 - 14h00 - 17h00'
		}
	}
];

window.addEvent('domready', function(){
	sDate = '';
	eInfos = $('gmaps-infos');
	eMap = $('gmaps-map');
	aTypes = [];
	
	if(GBrowserIsCompatible()){
		fRefreshMap = function(){
			eInfos.setStyle('display', 'none').getElement('.content').empty();
			//oMap.clearOverlays();
			oGLatLngBounds = new GLatLngBounds();
			$$('#gmaps-form input[name^=type]').each(function(item, i){
				if(item.getProperty('checked')) aTypes.push(item.value);
				else aTypes.erase(item.value); 
			});
			aPoints.each(function(oPoint, i){
				if(oPoint.commissaire) oCommissaire = new Hash(oPoint.commissaire);
				if((oPoint.registre && aTypes.contains('registre'))  || (oPoint.commissaire && aTypes.contains('commissaire') && (sDate == '' || oCommissaire.has(sDate)))){
					if(oPoint.registre && oPoint.commissaire && !aTypes.contains('registre')) oPoint.marker.setImage('/img/gmaps/commissaire.png');
					else if(oPoint.registre && oPoint.commissaire && !aTypes.contains('commissaire')) oPoint.marker.setImage('/img/gmaps/registre.png');
					else oPoint.marker.setImage('/img/gmaps/'+(oPoint.commissaire && oPoint.registre ? 'registre+commissaire' : (oPoint.commissaire ? 'commissaire' : 'registre'))+'.png');
					oPoint.marker.show();
					oGLatLngBounds.extend(oPoint.marker.getLatLng());
				}else oPoint.marker.hide();
			});
			if(!oGLatLngBounds.isEmpty()){
				oMap.setZoom(oMap.getBoundsZoomLevel(oGLatLngBounds));
				oMap.panTo(oGLatLngBounds.getCenter());
				oMap.savePosition();
			}
		}
		
		oMap = new GMap2(eMap);
		G_SATELLITE_3D_MAP.getName = function() {return '3D'} 
		G_SATELLITE_3D_MAP.getAlt = function() {return 'Afficher la carte en 3D (requiert l\'installation du plugin-in Google Earth)'} 
		oMap.addMapType(G_SATELLITE_3D_MAP)
		var uiOptions = oMap.getDefaultUI();
		uiOptions.zoom.scrollwheel = false;
		uiOptions.controls.scalecontrol = false;
		uiOptions.controls.maptypecontrol = false;
		uiOptions.controls.menumaptypecontrol = false;
		oMap.setUI(uiOptions);
		oMap.addControl(new GHierarchicalMapTypeControl());

		oMap.setCenter(new GLatLng(48.39160083594044, -4.484546184539795), 13);
		
		oIcon = new GIcon(G_DEFAULT_ICON);
		oIcon.shadow = null;
		oIcon.iconSize = new GSize(30, 43);
		oIcon.iconAnchor = new GPoint(15, 38);
		
		aPoints.each(function(oPoint, i){
			oPoint.marker = new GMarker(new GLatLng(oPoint.latitude, oPoint.longitude), {icon: new GIcon(oIcon, '/img/gmaps/'+(oPoint.commissaire && oPoint.registre ? 'registre+commissaire' : (oPoint.commissaire ? 'commissaire' : 'registre'))+'.png'), title: oPoint.nom+' ('+(oPoint.commissaire && oPoint.registre ? 'Dossiers, registres et permanence de la commission d\'enquête' : (oPoint.commissaire ? 'Permanence de la commission d\'enquête' : 'Dossiers et registres'))+')'});
			GEvent.bind(oPoint.marker, 'click', oPoint, function(){
				eInfos.setStyle('display', 'block').getElement('h4').set('html', this.nom).highlight('#ffab00');
				var ul = new Element('ul').inject(eInfos.getElement('.content').empty());
				new Element('a', {'text': 'Zoomer', 'href': '#'}).inject(new Element('li', {'class': 'address', 'html': this.addresse+' - '}).inject(ul)).addEvent('click', function(event){
					event.stop();
					oMap.setZoom(17);
					oMap.panTo(this.marker.getLatLng());
				}.bind(this));
				new Element('li', {'class': 'phone', 'html': this.telephone}).inject(ul);
				if(oPoint.registre) new Element('li', {'class': 'file', 'html': 'Dossiers et registres disponibles'}).inject(ul);
				if(oPoint.commissaire){
					html = '';
					oCommisaire = new Hash(oPoint.commissaire).each(function(horaire, date){
						if(sDate && sDate == date) html += '<br/><strong>'+horaire+'</strong>';
						else html += '<br/>'+horaire;
					});
					new Element('li', {'class': 'commissioner', 'html': 'Permanence de la commission d\'enquête : '+html}).inject(ul);
				}
			});
			oMap.addOverlay(oPoint.marker);
			oPoint.marker.hide();
		});
		
		$$('#gmaps-form input').addEvents({
			'change': function(){
				fRefreshMap();
			},
			'click': function(){
				this.blur();
			}
		});
		
		$$('#gmaps-form select').addEvent('change', function(){
			sDate = this.getSelected().get('value').toString();
			if(sDate != '') $$('#gmaps-form input[value=registre]').removeProperty('checked');
			fRefreshMap();
		}).fireEvent('change');
	}
});

window.addEvent('unload', function(){
	GUnload();
});
