// AJOUT D'UN ITEM DANS LA SELECTION
var suppress = function(id, user){
	new Ajax.Request('menu.php?act=del&id='+id+'&user='+user, {
		onComplete: function(transport) {
			new Effect.Fade('li'+id,{duration:0.3, afterFinish:
				function(){
					calculItem();
				}
			});
		}
	});
}

var masssuppress = function(id){
	new Ajax.Request('menu.php?act=delall', {
		onComplete: function(transport) {
			document.getElementById('selection_droite').innerHTML = '<ul></ul>';
			calculItem();
		}
	});
}

var ajoutSelection = function(id, user){
	if ((user == undefined) || (user=='')) {
		myLightWindow.activateWindow({href: 'login.php?id='+id, width: '610', height:'320'});
	} else {
		ajoutSelection2(id, user);
	}
}

var ajoutSelection2 = function(id, user){
	if (document.getElementById('carte')) getMovieName("carte").setUser(user);
	new Ajax.Request('menu.php?id='+id+'&user='+user, {
		onComplete: function(transport) {
			document.getElementById('selection').innerHTML = transport.responseText;
			deploy_sel();
			calculItem();
		}
	});
}

function deploy_sel() {
	var couleur1 = '#AFC376';
	var couleur2 = '#607F09';
	var vitesse = '1.5';
	var li_selection = $$('div.liste_selection ul li');
	var premier = li_selection[0].id;
	new Effect.ScrollTo('selection_droite',{duration:0.7,afterFinish:
		function(){
			new Effect.BlindDown('selection_droite',{duration:0.3,afterFinish:
				function(){
					new Effect.Highlight(premier,{duration:vitesse,startcolor:couleur1,endcolor:couleur2,afterFinish:
						function(){
							new Effect.Highlight(premier,{duration:vitesse,startcolor:couleur2,endcolor:couleur1,afterFinish:
								function(){
									new Effect.BlindUp('selection_droite',{duration:0.3});
								}
							});
						}
					});	
				}
			});
		}
	});
}

// AFFICHE / MASQUE LA SELECTION
var afficheSelection = function(){
	var etat = $('selection_droite').style.display;
	if(etat=='none'){
		new Effect.BlindDown('selection_droite',{duration:0.3});
	}else{
		new Effect.BlindUp('selection_droite',{duration:0.3});
	}
}
Event.observe(window, 'load', function(evt){
	Event.observe('ma_selection','click',function(){
		afficheSelection();
	});								   
	if(isId('fermer_selection')){
		Event.observe('fermer_selection','click',function(){
			afficheSelection();
		});
	}
});

// ROLLOVER D'UN ITEM DANS LA SELECTION (DROITE)
Event.observe(window, 'load', function(evt){
	var li = $$('.liste_selection li');
	li.each( function(e){
		Event.observe(e,'mouseover',function(){
			e.addClassName("on");
		});								   
		Event.observe(e,'mouseout',function(){
			e.removeClassName("on");
		});								   
	});
});

// CALCUL DU NB D'ITEM DANS LA SELECTION + UPDATE
var calculItem = function (){
	var nb = 0;
	var li = $$('.liste_selection li');
	li.each( function (e,index){
		if(e.style.display != 'none'){
			nb++;	
		}
	});
	if(nb>1){
		var nb_chambre = nb+' '+w_room+'s';
		var selection = ' '+w_select+'s';
	}else if(nb==0){
		var nb_chambre = w_none;
		var selection = ' '+w_selection;
	}else{
		var nb_chambre = nb+' '+w_room;
		var selection = ' '+w_select;
	}
	$('nb_chambre').update(nb_chambre);
	if(isId('nb_chambre2')){
		$('nb_chambre2').update(nb_chambre+selection);
	}
}

// SUPPRESSION D'UN ITEM DANS LA SELECTION (PAGE)
Event.observe(window, 'load', function(evt){
	var a = $$('.f_liste a.supprimer');
	a.each( function(e,index){
		var a_parent = e.parentNode;
		a_parent.setAttribute("id","item_selection"+index);
		Event.observe(e,'click',function(){
			new Effect.Fade(a_parent,{duration:0.3});
		});								   
	});
	
	if(isId("supprimer_tous")){
		var a2 = $('supprimer_tous');
		Event.observe(a2,'click',function(){
			a.each( function(e,index){
				var a_parent = e.parentNode;
				new Effect.Fade(a_parent,{duration:0.3});
				// Suppression
				masssuppress();
			});
		});
	}
});

// "ACCORDEON" FICHE
var initFiche = function(type){
	var t_accordeon = $$('.t_accordeon');
	var a_accordeon = $$('.t_accordeon a');
	var d_accordeon = $$('.accordeon');
	
	// MISE EN PLACE DES ID
	t_accordeon.each( function(e,index){
		e.setAttribute("id","t_accordeon"+index);
	});
	a_accordeon.each( function(e,index){
		e.setAttribute("id","a_accordeon"+index);
		Event.observe(e,'click',function(){
			return false;
			e.blur();
		});
	});
	d_accordeon.each( function(e,index){
		e.setAttribute("id","d_accordeon"+index);
		e.style.display='none';
	});
	
	// ACTIONS
	switch(type){
		case 'unique' :
			t_accordeon.each( 
				function(e,index){
					Event.observe(e,'click',function(){
						if($('d_accordeon'+index).style.display=='none'){
							new Effect.ScrollTo('t_accordeon'+index,{duration:0.7,afterFinish:
								function(){
									new Effect.BlindDown('d_accordeon'+index,{duration:0.3});
									a_accordeon[index].update(w_close);
								}
							});
						}else{
							new Effect.BlindUp('d_accordeon'+index,{duration:0.3});
							a_accordeon[index].update(w_open);
						}
					});
				}
			);
		break;
		case 'deplier' :
			t_accordeon.each( 
				function(e,index){
					new Effect.BlindDown('d_accordeon'+index,{duration:0.3});
					a_accordeon[index].update(w_close);
				}
			);
		break;
		case 'replier' :
			t_accordeon.each( 
				function(e,index){
					new Effect.BlindUp('d_accordeon'+index,{duration:0.3});
					a_accordeon[index].update(w_open);
				}
			);
		break;
		default :
			new Effect.ScrollTo('t_accordeon'+type,{duration:0.7,afterFinish:
				function(){
					new Effect.BlindDown('d_accordeon'+type,{duration:0.3});
					a_accordeon[type].update(w_close);
				}
			});
		break;
	}
}
Event.observe(window, 'load', function(evt){
	if(isId("fiche")){
		initialize();
		initFiche('unique');
	}
});

// ENVOI AMI
var ajoutDestinataire = function(){
	if($('liste_destinataire').style.display=='none'){
		$('liste_destinataire').style.display='block';
		$('ajout_destinataire').update(w_supdest);
		$('ajout_destinataire').addClassName("supprimer");
		$('ajout_destinataire').blur();
	}else{
		$('liste_destinataire').style.display='none';
		$('ajout_destinataire').update(w_adddest);
		$('ajout_destinataire').removeClassName("supprimer");
		$('ajout_destinataire').blur();
	}
}
var initEnvoiAmi = function(){
	$('envoi_ami').style.display='none';
	var boutons = new Array('bt_envoyer_ami','bt_fermer_ami');
	// ACTIONS
	boutons.each( 
		function(e,index){
			Event.observe(boutons[index],'click',function(){
				$(boutons[index]).blur();
				if($('envoi_ami').style.display=='none'){
					$(boutons[0]).addClassName('on');
					new Effect.BlindDown('envoi_ami',{duration:0.3,afterFinish:
						function(){
							new Effect.ScrollTo('envoi_ami',{duration:0.7});
						}
					});
				}else{
					$(boutons[0]).removeClassName('on');
					new Effect.BlindUp('envoi_ami',{duration:0.3});
					$('confirm_ami').style.display = 'none';
					$('erreur_ami').style.display = 'none';					
				}
			});
		}
	);
}
Event.observe(window, 'load', function(evt){
	if(isId("envoi_ami")){
		initEnvoiAmi();							   
	}
});

// AVIS
var initAvis = function(){
	var boutons = new Array('bt_donner_avis2','bt_donner_avis');
	// ACTIONS
	boutons.each( 
		function(e,index){
			Event.observe(boutons[index],'click',function(){
				$(boutons[index]).blur();
				if(boutons[index] == boutons[0]){
					var div_accordeon_parent = $('depot_avis').parentNode.parentNode.id; // Retrouve le parent du div "depot_avis"
					$(div_accordeon_parent).style.display = 'block';	
				}
				if($('depot_avis').style.display=='none'){
					var div_accordeon_parent = $('depot_avis').parentNode.parentNode.id; // Retrouve le parent du div "depot_avis"
					$(div_accordeon_parent).style.display = 'block';	
					$(boutons[0]).addClassName('on');
					new Effect.ScrollTo('depot_avis',{duration:0.7});
				}else{
					$(boutons[0]).removeClassName('on');
				}
			});
		}
	);
}
Event.observe(window, 'load', function(evt){
	if(isId("depot_avis")){
		initAvis();							   
	}
});

// PRINT FICHE
var printFiche = function(){
	initFiche('deplier');
	window.print();
}

// SMENU DHTML
var efTout = function(){
	new Effect.Fade("dhtml",{duration:0.3});
	isWorking = false;
}

Event.observe(window, 'load', InitPage, false);
function InitPage(){
	if($('smenu')) var menuTop = new menuDHTML('smenuTop', {etat:'on'});
}


/*var initDhtml = function(type){
	var pause = 500;
	var racine = $$('#smenu .smenu li.'+type);
	var lien = racine[0].firstChild;
	var smenu = racine[0].childNodes[2];
	smenu.setAttribute("id","dhtml");
	var smenu_lien = $$('#dhtml li');
	var isWorking=false;
	var delai;
	
	if(isWorking==false){
		Event.observe(lien, 'mouseover', function(evt){
			if(delai){
				clearTimeout(delai);
			}
			new Effect.Appear("dhtml",{duration:0.3});
			isWorking = true;
		});
	}
	Event.observe(lien, 'mouseout', function(evt){
		delai=setTimeout('efTout()',pause);
	});
	Event.observe("dhtml", 'mouseout', function(evt){
		delai=setTimeout('efTout()',pause);
		lien.removeClassName("on");
	});
	Event.observe("dhtml", 'mouseover', function(evt){
		clearTimeout(delai);
		lien.addClassName("on");
	});
	smenu_lien.each(
		function(e,index){
			Event.observe(e, 'mouseover', function(evt){
				clearTimeout(delai);
			});
		}
	);

}



Event.observe(window, 'load', function(evt){
	if(isId("smenu")){
		initDhtml("cadeaux");						   
	}
	
});*/

// ROLLOVER INPUT IMAGE
Event.observe(window, 'load', function(evt){
	var input = $$('input[type=image], img.rollover');
	input.each( function(e,index){
		var chemin = input[index].src;
		var pos_debut = chemin.lastIndexOf('/')+1;
		var pos_fin = chemin.lastIndexOf('.');
		var chemin_fichier = chemin.substring(0,pos_debut);
		var nom_fichier = chemin.substring(pos_debut,pos_fin);
		var ext_fichier = chemin.substr(pos_fin);
		Event.observe(e, 'mouseover', function(evt){
			e.src = chemin_fichier+nom_fichier+'_on'+ext_fichier;
		});			 
		Event.observe(e, 'mouseout', function(evt){
			e.src = chemin_fichier+nom_fichier+ext_fichier;
		});			 
	});
});

// FERMETURE DE LA LIGHTWINDOW
var fermerLightwindow = function(){
	myLightWindow.deactivate();
}

// VERIF RECHERCHE
var verifRecherche = function(){
	if ($F('reference').length>0) {
//		$('message').update('Cette rfrence n\'existe pas.');
//		new Ajax.Request('_ajax_reference.php', {
//			method: 'post',
//			parameters: {
//				reference: $F('reference')
//			},
//			onComplete: function(transport) {
//				eval(transport.responseText);
//				return false;
//			}
//		});
//		return false;
		new Effect.Fade('erreur_recherche',{duration:0.3});
		callExternalInterface();
		return false;
	} else if ($F('charme')==null  && $F('chateau')==null && $F('gite_charme')==null) {
		$('message').update(err_type_heb);
		new Effect.Appear('erreur_recherche',{duration:0.3});
		return false;
	}else if($F('region')==0 && $F('commune')=='' && $F('reference')==''){
		$('message').update(err_geo);
		new Effect.Appear('erreur_recherche',{duration:0.3});
		return false;
	}else{
		new Effect.Fade('erreur_recherche',{duration:0.3});
		callExternalInterface();
		return false;
	}
}



var afficheItineraire = function(type){
	if($('erreur_gmap')){
		$('erreur_gmap').remove();
	}
	
	// Itineraire
	var langue =  $F('langue');
	var from = $F('from');
	var to = [$F('to_latitude'),$F('to_longitude')];

	new Ajax.Request('fiche/_itineraire.php', {
		onComplete: function(transport){
			if(!$('route')){
				$('gmap_detail').insert({bottom:transport.responseText});
			}else{
				$('route').update('');
			}
			
			map = new GMap2($("map_localisation1"));
			map.addControl(new GMapTypeControl());
			map.addControl(new GSmallMapControl());
			directionsPanel = $("route");
			directions = new GDirections(map, directionsPanel);
			directions.load('from:'+from+' to:'+to,{"locale":langue});
			
			//TRAITEMENT DES ERREURS
			GEvent.addListener(directions, "error", function() {
				//db(directions.getStatus().code);
				if(!$('erreur_gmap')){
					$('gmap_detail').insert({before:'<div class="h_confirmation" id="erreur_gmap"><div class="b_confirmation"><div class="f_confirmation"><p>'+erreur_gmap+'</p></div></div></div>'});
				}
				$('map_localisation1').setStyle({'width':'0px','height': '0px'});
				$('route').hide();
			});
			// PAS D'ERREUR
			GEvent.addListener(directions, "load", function() {
				//db(directions.getStatus().code);
				if(!$('retour_geoloc')){
					$('gmap_detail').insert({before:'<div id="retour_geoloc"><a href="javascript:initialize();">'+retour_localisation+'</a></div>'});				
				}
				$('map_localisation1').setStyle({'float':'left','width':'519px','height': '393px'});
				$('route').show();
			});
		
		}
	});
	return false;
};

var zoomLB = function(num){
	var imgLB = $$('.zoomLB');
	LB.start(imgLB[num]);
}