window.addEvent('domready', function(){
    $$(".detail_lien_deroulant a").each(function(bt) {
		bt.addEvent('click', init_boutons_select);
	});

	$$(".champ_select").each(function(select) {
    	select.addEvent('click',init_menus_select);
	});
	init_resultat_recherche();
	init_resultat_recherche_annonce();
	
	if($("bt_rechercher")){
	    $("bt_rechercher").addEvent('click', function(){
			rechercher_ajax();
			return false;
		});
	}
    $("global").addEvent('click', function(){
    	show_select('null');
	});
	if( $("mot_clef")){
	    $("mot_clef").addEvent('keyup', function(e){
	    	test_mot_clef(e);
		});
	    $("mot_clef").addEvent('blur', function(){
	    	verifier_champ_text('mot_clef');
		});
	    $("mot_clef").addEvent('focus', function(){
	    	if($("mot_clef").get('value')==$("mot_clef").get('alt')){
	    		 $("mot_clef").set('value','');	
	    	}
		});
	}
});


verifier_champ_text=function(id_champ){
	if($(id_champ)){
    		if($(id_champ).get('value')==''){
    		$(id_champ).set('value',$(id_champ).get('alt'));	
    	}	    	
	} 
}
init_menus_select=function(e){
	new Event(e).stop();
	id_en_cours=this.get('id');
	if(id_en_cours=='detail_region'){
		$$(".champ_select").each(function(select) {
			select.setStyle('visibility','hidden');
		});
	}
	$(id_en_cours).setStyle('visibility','visible');
	lien_enfant=id_en_cours.replace('detail','liste')
	show_select(lien_enfant);

}
init_boutons_select=function(e){
	new Event(e).stop();
	var base=this.get('rel');
	//alert("base="+base);
	var parent='detail_'+base+'_txt';

	if($(parent).get('rel')==null){
		$(parent).set('rel',$(parent).get('html'));
	}
	if(this.get('title')=='aucun'){
		$(parent).set('html',$(parent).get('rel'));
	}else{
		$(parent).set('html',$(parent).get('rel')+' > ' +this.get('html'));
	}
	show_select('null');
	var destination='detail_'+base;
	$(destination).set('rel',this.get('title'));
	rechercher_ajax();
	return false;
}

init_resultat_recherche=function(){
	if($('cache_ajax')){
		$('cache_ajax').fade('0');
	}
    $$(".resultat_recherche").each(function(resultat) {
		resultat.set('opacity','0.80');
    	resultat.addEvent('mouseover', function(e){
    		resultat.addClass('resultat_hover');
    		resultat.fade('in');
		});
		resultat.addEvent('mouseout', function(e){
			resultat.removeClass('resultat_hover');
			resultat.fade('0.80');
		});
		resultat.addEvent('click', function(e){
			var id_etablissement=resultat.getElement('.savoir_plus_annuaire').get('alt');
			rechercher_ajax(id_etablissement);
		});
	});
    $$(".savoir_plus_annuaire").each(function(lien) {
    	lien.addEvent('click', function(){
	    	rechercher_ajax(lien.get('alt'));
	    	//alert(this.get('alt'));
	    	
    		return false;
    	});
    });
    if($('annuaire_liste')){
        SexyLightbox = new SexyLightBox({color:'white', dir: racine+'/sexyimages'});
    }
    precharger_images();
}


function show_select(id_detail){
	$$(".detail_lien_deroulant").each(function(detail_select) {
		detail_select.style.display='none';
	});

	if(id_detail!='null'){
		if($(id_detail).style.display=='none'){
			$(id_detail).style.display='block';
		}else{
			$(id_detail).style.display='none';
		}
	}else{
		$$(".champ_select").each(function(select) {
			select.setStyle('visibility','visible');
		});
	}
}
function afficher_zoom_photo(src_new,zoom_new, suffixe_img) {
	//$("optim_"+suffixe_img).grab("photo_apercu_"+suffixe_img);
	image=$("photo_apercu_"+suffixe_img);
	image.src = src_new;
	image.setStyle('padding','0px');
	image.removeProperties('width','height');
	lien=$("photo_zoom_"+suffixe_img);
	lien.href = zoom_new;
	//lien.injectBefore(image).adopt(image);
	/*
	$$(".div_ombre").each(function(div_ombre) {
		div_ombre.destroy();
	});
	*/
	//shadow_img2();
}

function test_mot_clef(e){
	var mot_clef=$('mot_clef').get('value');
	if(mot_clef==$('mot_clef').get('alt')){
		mot_clef='';
	}
	if(e.key=='backspace'){
		//alert('test='+e.key);
		return false;
	}else{
		if(mot_clef.length>2){
			if($("petiteannonce_liste")){
				//alert('test');
				rechercher_ajax_annonce();
			}else{
				rechercher_ajax();
			}
		}
		return true;
	}
}
function afficher_masquer_recherche(mode){
	valeur_courante=$('content').getElement('h1').getStyle('display');
	//alert('valeur_courante='+valeur_courante+' mode='+mode);
	if(valeur_courante=='none' && mode=='block'){
		$('content').getElement('h1').setStyle('display','block');
		$('content').getElement('.contenu_article').setStyle('display','block');
	}else{
		if(valeur_courante=='block' && mode=='none'){
			$('content').getElement('h1').setStyle('display','none');
			$('content').getElement('.contenu_article').setStyle('display','none');
		}
	}
	
}

function rechercher_ajax(id_etablissement){
	var mot_clef=$('mot_clef').get('value');
	if(mot_clef==$('mot_clef').get('alt')){
		mot_clef='';
	}
	if($('detail_region')){
		var id_region=$('detail_region').get('rel');
		if(id_region==null || id_region=='aucun'){
			id_region='';
		}
	}else{
		id_region='';
	}
	if($('detail_departement')){
		var id_departement=$('detail_departement').get('rel');
		if(id_departement==null || id_departement=='aucun'){
			id_departement='';
		}
	}else{
		id_departement='';
	}
	if($('detail_rubrique')){
		var id_specialite=$('detail_rubrique').get('rel');
		if(id_specialite==null || id_specialite=='aucun'){
			id_specialite='';
		}
	}else{
		id_specialite='';
	}
	
	if(id_etablissement==undefined){
		id_etablissement='';
	}
	
	//var chaine_requete='mot_clef='+mot_clef+'&id_region='+id_region+'&id_departement='+id_departement+'&id_specialite='+id_specialite+'&id_etablissement='+id_etablissement;				
	var chaine_requete='mot_clef='+mot_clef+'&id_specialite='+id_specialite+'&id_etablissement='+id_etablissement;
	var req = new Request({
		url:racine+'recherche_annuaire.htm',
		method:'post',
		autoCancel:true,
		data:chaine_requete,
		onRequest: function() {
			$('cache_ajax').fade('0.90');
		},
		onSuccess: function(response) {
			var tableau_response=response.split("µ");
			if(tableau_response[1]=='ok'){
				$("annuaire_liste").set("html", tableau_response[0]);
				
				if(id_etablissement){
					//afficher_masquer_recherche('none');
					 setTimeout(shadow_img2,500);
				}else{
					//afficher_masquer_recherche('block');
				}
				if(tableau_response[2]='detail_dep'){
					if($("liste_departement")){
						$("liste_departement").set("html", tableau_response[3]);
						$$("#liste_departement a").each(function(bt) {
							bt.addEvent('click', init_boutons_select);
						});
					}
				}					
			}else{
				$("annuaire_liste").set("html",tableau_response[0]);
			}
			setTimeout(init_resultat_recherche,500);
			//document.location.replace(url_en_cours+'#mot_clef-'+mot_clef+'-region-'+id_region+'-departement-'+id_departement+'-id_specialite-'+id_specialite+'-id_etablissement-'+id_etablissement);
		}
			
	}).send();
}
// http://webfreak.no/wp/2007/09/05/get-for-mootools-a-way-to-read-get-variables-with-javascript-in-mootools/ Jens Anders Bakke
function get_url_var(key,url){
	if(arguments.length < 2) url=location.href;
	if(arguments.length > 0 && key != ""){
		if(key == "#"){
			var regex = new RegExp("[#]([^$]*)");
		} else if(key == "?"){
			var regex = new RegExp("[?]([^#$]*)");
		} else {
			var regex = new RegExp("[?&]"+key+"=([^&#]*)");
		}
		var results = regex.exec(url);
		return (results == null )? "" : results[1];
	} else {
		url = url.split("?");
		var results = {};
		if(url.length > 1){
			url = url[1].split("#");
			if(url.length > 1) results["hash"] = url[1];
			url[0].split("&").each(function(item,index){
				item = item.split("=");
				results[item[0]] = item[1];
			});
		}
		return results;
	}
}

