var initPage = function(){
	// FONCTIONS COMMUNES
	RollOverImage();
	focusInput();
	
	/* HOME */
	if($('home')){
		if($('ss')){
			if($$('#ss .item').length >1){
				var ss = new slideShow('ss','cont_ss','item',{
					slideAutostart : true,
					slideDuree:1,
					slideTempo : 5
				});
			}
		}
		initCarte();	
	}
};

/* FONCTIONS */
var initCarte = function(){
	new Ajax.Request('_ajax.php?ACT=carte1',{
		parameters : {},
		onComplete : function(transport){
			var data = transport.responseJSON;
			$$('body')[0].insert({top:'<div id="etiquette" display:none;></div>'});
			$$('#mapCarte area').each(function(e){
				Event.observe(e,'mouseout',function(){
					Event.stopObserving(document,'mousemove',coordMouse);
					pX=0;
					pY=0;
					$('etiquette').hide();
				});
				Event.observe(e,'mouseover',function(){
					$('etiquette').show();
					var tpl = '';
					tpl+= '<h4>'+e.alt.substr(5)+'</h4>';
					data[e.hash.substr(1)].each(function(centre,i){
						tpl+='<p>'+centre+'</p>';								 
					});
					$('etiquette').update(tpl);
					Event.observe(document,'mousemove',coordMouse);				
				});
				e.stopObserving('click');
				e.observe('click',function(event){
					carteGmap(e.hash.substr(1));
					event.stop();
				});
			});
		}
	});
	$('bt_autre_recherche').observe('click',function(event){
		$('liste').update('');
		$('carte1').show();
		$('recherche_carte').show();
		$('resultat_carte').hide();
		event.stop();
	});
	$('frm_centre').observe('submit',function(event){
		tab_param = $('frm_centre').serialize(true);
		if(tab_param.cp_centre != '' && tab_param.localisation_centre != ''){
			carteGmap(tab_param.cp_centre,tab_param.localisation_centre);
		}
		event.stop();										  
	});
};

var map;
var baseIcon = new GIcon(G_DEFAULT_ICON);

var carteGmap = function(id,type){
	baseIcon.iconSize = new GSize(20,30);
	baseIcon.shadow = 'images/commun/ombre.png';
	baseIcon.shadowSize = new GSize(0,0);
	baseIcon.iconAnchor = new GPoint(10,15);
	baseIcon.infoWindowAnchor = new GPoint(0, 0);
	new Ajax.Request('_ajax.php?ACT=carte2',{
		parameters: {id : id},
		onComplete: function(transport){
			$('carte1').hide();
			tab_point = transport.responseText.evalJSON();
			if(tab_point.size()>0){
				map = new GMap2($("gmap"));
				map.clearOverlays();
				map.enableScrollWheelZoom();
				creerPoint();
				$('recherche_carte').hide();
				$('resultat_carte').show();
				if(!type){
					$('message_resultat').update('Vous cherchez un centre du <span>'+id+'</span>');
				}else{
					tab_type={
						1 : 'domicile',
						2 : 'travail'
					};
					$('message_resultat').update('Vous cherchez un centre proche du <span>'+id+'</span>, code postal de votre <span>'+tab_type[type]+'</span>');
				}
			}
		}
	});
}
var centrerCarte = function(){
	var sw = new GLatLng(calculCoord('lat_min'), calculCoord('lng_min'));
	var ne = new GLatLng(calculCoord('lat_max'), calculCoord('lng_max'));
	var bounds = new GLatLngBounds(sw, ne);
	level = map.getBoundsZoomLevel(bounds)-1;
	map.setCenter(new GLatLng(calculCoord('lat_moy'), calculCoord('lng_moy')), level);	
};
var tri_nombres = function(a,b){
	return a-b;
};
var calculCoord = function(calcul){
	var lat_min = parseFloat(tab_lat.sort(tri_nombres).first());
	var lat_max = parseFloat(tab_lat.sort(tri_nombres).last());
	var lng_min = parseFloat(tab_lng.sort(tri_nombres).first());
	var lng_max = parseFloat(tab_lng.sort(tri_nombres).last());
	
	if(calcul=='lat_moy'){return (lat_min+lat_max)/2;}
	if(calcul=='lng_moy'){return (lng_min+lng_max)/2;}
	if(calcul=='lat_min'){return lat_min;}
	if(calcul=='lat_max'){return lat_max;}
	if(calcul=='lng_min'){return lng_min;}
	if(calcul=='lng_max'){return lng_max;}
};
var creerPoint = function(){
	$('liste').update('');
	tab_lat = new Array();
	tab_lng = new Array();
	tab_point.each( function(e,i){
		if(e.lat != 0 && e.lng != 0){
			var point = new GLatLng(e.lat,e.lng);
			map.addOverlay(createMarker(point, i));
			tab_lat.push(e.lat);
			tab_lng.push(e.lng);
			
			tpl = '<div class="item">';
			tpl+= '		<div class="f_item'+parseInt(i+1)+'">';
			tpl+= '			<h2><a href="'+e.url+'">'+e.nom+'</a></h2>';
			tpl+= '			<p class="adresse">'+e.adresse+'</p>';
			tpl+= '			<p class="tel">T&egrave;l : '+e.tel+'</p>';
			tpl+= '			<p class="info">'+e.info+'</p>';
			tpl+= '			<a href="'+e.url+'" class="lien">Site internet du centre</a>';
			tpl+= '		</div>';
			tpl+= '</div>';
			$('liste').insert({bottom:tpl});
		}
	});
	centrerCarte();
};
var createMarker = function(point,i) {
	var IconType = new GIcon(baseIcon);
	IconType.image = 'images/commun/marker'+parseInt(i+1)+'.png';
	markerOptions = {
		clickable : false,
		icon:IconType
	};
	var marker = new GMarker(point, markerOptions);
	return marker;
};
var coordMouse = function(e){
	pX = Event.pointerX(e);
	pY = Event.pointerY(e)-30;
	w = $('etiquette').getWidth()/2;
	h = $('etiquette').getHeight()-20;
	$('etiquette').setStyle({display:'block', top:pY-h+'px', left:pX-w+'px'});
};
