// JavaScript Document
//alert ("ok");

function rollBouton() {
	$(".bt_over").mouseenter(function() {
		/*$(this).parents(".bouton").children("div").addClass("actif"); attention ça pête sous ie6*/
		$(this).parents(".bouton").find(".bt_jaun_bg_gch").addClass('actif1');
		$(this).parents(".bouton").find(".bt_jaun_bg_ctr").addClass('actif2');
		$(this).parents(".bouton").find(".bt_jaun_bg_dt").addClass('actif3');
		$(this).parents(".bouton").find(".bt_roug_bg_gch").addClass('actif4');
		$(this).parents(".bouton").find(".bt_roug_bg_ctr").addClass('actif5');
		$(this).parents(".bouton").find(".bt_roug_bg_dt").addClass('actif6');
		$(this).parents(".bouton").find(".bt_gris_bg_gch").addClass('actif7');
		$(this).parents(".bouton").find(".bt_gris_bg_ctr").addClass('actif8');
		$(this).parents(".bouton").find(".bt_gris_bg_dt").addClass('actif9');
		$(this).addClass('actif10');
		$(this).addClass('actif11');
		$(this).addClass('actif12');
	});

	$(".bt_over").mouseleave(function() {
		/*$(this).parents(".bouton").children("div").removeClass("actif"); attention ça pête sous ie6*/
		$(this).parents(".bouton").find(".bt_jaun_bg_gch").removeClass('actif1');
		$(this).parents(".bouton").find(".bt_jaun_bg_ctr").removeClass('actif2');
		$(this).parents(".bouton").find(".bt_jaun_bg_dt").removeClass('actif3');
		$(this).parents(".bouton").find(".bt_roug_bg_gch").removeClass('actif4');
		$(this).parents(".bouton").find(".bt_roug_bg_ctr").removeClass('actif5');
		$(this).parents(".bouton").find(".bt_roug_bg_dt").removeClass('actif6');
		$(this).parents(".bouton").find(".bt_gris_bg_gch").removeClass('actif7');
		$(this).parents(".bouton").find(".bt_gris_bg_ctr").removeClass('actif8');
		$(this).parents(".bouton").find(".bt_gris_bg_dt").removeClass('actif9');
		$(this).removeClass('actif10');
		$(this).removeClass('actif11');
		$(this).removeClass('actif12');
	});
}

function initMenus() {
	$(".menu").mouseover(function() {
		$(this).find(".ssmenu").show();
	});

	$(".menu").mouseleave(function() {
		$(this).find(".ssmenu").hide();
	});
}

function initMenusNav2() {

	$(".nav2_item_cont").mouseenter(function() {
		$(this).find(".ssmenu2_cont").show();
	});

	$(".nav2_item_cont").mouseleave(function() {
		$(this).find(".ssmenu2_cont").hide();
	});

	$(".nav2_item_cont").mouseover(function() {
		var tmp=$(this).children(".nav2_item").attr('class');
		var tmp_len=tmp.length;
		if(tmp.substr(tmp_len-3,3) !="_ov"){
			$(this).children(".nav2_item").attr('class',tmp+'_ov');
		}
	});

	$(".nav2_item_cont").mouseleave(function() {
		var tmp=$(this).children(".nav2_item").attr('class');
		var tmp_len=tmp.length;
		if(tmp.substr(tmp_len-3,3) =="_ov"){
			$(this).children(".nav2_item").attr('class',tmp.substr(0,tmp_len-3));
		}
	});

	/*$(".nav2").mouseover(function() {
		$(this).find(".ssmenu2").show();
	});

	$(".nav2").mouseleave(function() {
		$(this).find(".ssmenu2").hide();
	});

	$(".nav2").mouseenter(function() {
		$(this).find(".nav2_compte").addClass("nav2_compte_ov");
		$(this).find(".nav2_annonce").addClass("nav2_annonce_ov");
		$(this).find(".nav2_agenda").addClass("nav2_agenda_ov");
		$(this).find(".nav2_offre").addClass("nav2_offre_ov");
		$(this).find(".nav2_alerte").addClass("nav2_alerte_ov");
		$(this).find(".nav2_comptepro").addClass("nav2_comptepro_ov");
		$(this).find(".nav2_selection").addClass("nav2_selection_ov");
		$(this).find(".nav2_mabonner").addClass("nav2_mabonner_ov");
		$(this).find(".nav2_visit").addClass("nav2_visit_ov");
		$(this).find(".nav2_archive").addClass("nav2_archive_ov");
		$(this).find(".nav2_bibliotheque").addClass("nav2_bibliotheque_ov");
		$(this).find(".nav2_gestion_offre").addClass("nav2_gestion_offre_ov");
	});

	$(".nav2").mouseleave(function() {
		$(this).find(".nav2_compte").removeClass("nav2_compte_ov");
		$(this).find(".nav2_annonce").removeClass("nav2_annonce_ov");
		$(this).find(".nav2_agenda").removeClass("nav2_agenda_ov");
		$(this).find(".nav2_offre").removeClass("nav2_offre_ov");
		$(this).find(".nav2_alerte").removeClass("nav2_alerte_ov");
		$(this).find(".nav2_comptepro").removeClass("nav2_comptepro_ov");
		$(this).find(".nav2_selection").removeClass("nav2_selection_ov");
		$(this).find(".nav2_mabonner").removeClass("nav2_mabonner_ov");
		$(this).find(".nav2_visit").removeClass("nav2_visit_ov");
		$(this).find(".nav2_archive").removeClass("nav2_archive_ov");
		$(this).find(".nav2_bibliotheque").removeClass("nav2_bibliotheque_ov");
		$(this).find(".nav2_gestion_offre").removeClass("nav2_gestion_offre_ov");
	});*/
}
//Fonction plus utilisée car gérée dans la page et en fonction du contenu des champs ville et référence
function rollBoutonCherch() {
	$(".bt_cherch").mouseenter(function() {
		$(this).addClass('ov');
	});

	$(".bt_cherch").mouseleave(function() {
		$(this).removeClass('ov');
	});
}
function diapoAccueil() {
	$(window).load(function(){
		$(".overflow_diaporama").icecarousel({
			btnNext: ".btn_avance",
			btnPrev: ".btn_retour",
			speed : 500,
			rotate : false,
			rotate_speed : 2000
		});
	});
}
function diapoFlash() {
	$(window).load(function(){
		$(".overflow_flash_diaporama").icecarousel_h({
			btnNext: ".flash_btn_avance",
			btnPrev: ".flash_btn_retour",
			speed : 2000,
			rotate : true,
			rotate_speed : 2000
		});
	});
}
function initAgenda() {
	$(".parametre").click(function(event) {
		event.preventDefault();
		$('#parametre_agenda').show();
	});

	$(".fermer_agenda").click(function(event) {
		event.preventDefault();
		$('#parametre_agenda').hide();
	});

	$('#fermer_param_agenda').click(function(e){
		e.preventDefault();
		$(this).parents('#parametre_agenda').hide();
	});

}
function initMessage() {
	$(".ouvrir").click(function(event) {
		event.preventDefault();
		$('.commentaire').show();
	});

	$(".fermer_message").click(function(event) {
		event.preventDefault();
		$('.commentaire').hide();
	});
}
function tbFermer() {
	$(".tb_fermer").click(function() {
		parent.tb_remove();
	});
}
function imgAffich() {
	$(".fenetre .col_dte img").each(function() {
		$(this).click(function() {
			$("#imag_principale img").attr("src", $(this).attr("src"));
		});
	});
}

function initFermerAlert() {
	$(".fermer_alert").click(function(event) {
		event.preventDefault();
		$('.planning_alert').hide();
	});
}

function initAlert_annonce() {
	$(".ajouter").click(function(event) {
		event.preventDefault();
		$(this).parents(".ann_liens").find('.alert_ajout').show();
	});

	$(".alert_ajout_close").click(function(event) {
		event.preventDefault();
		$(this).parents(".ann_liens").find('.alert_ajout').hide();
	});
}



function initEnchere() {

	$(".enchere").click(function(event) {
		event.preventDefault();
		$('.faire_une_enchere').show();
		$('.historique').hide();
		$(this).parents(".pag_encart_ctr").children('.detail_descriptif,.detail_info').addClass('display_none');

		id_civilite = $('#civilite_offre').val();

		displayFormCiv(id_civilite);
	});

	$(".fermer_enchere").click(function(event) {
		event.preventDefault();
		$('.faire_une_enchere').hide();
		$(this).parents(".pag_encart_ctr").children('.detail_descriptif,.detail_info').removeClass('display_none');
	})
}
function initHistorique() {

	$(".histo").click(function(event) {
		event.preventDefault();
		$('.historique').show();
		$('.faire_une_enchere').hide();
		$(this).parents(".pag_encart_ctr").children('.detail_descriptif,.detail_info').addClass('display_none');
	});

	$(".fermer_histo").click(function(event) {
		event.preventDefault();
		$('.historique').hide();
		$(this).parents(".pag_encart_ctr").children('.detail_descriptif,.detail_info').removeClass('display_none');
	});
}
function initCommentaireFiche() {

	$(".message").click(function(event) {
		event.preventDefault();
		$('.commentaire').show();
	});

	$(".fermer_commentaire").click(function(event) {
		event.preventDefault();
		$('.commentaire').hide();
	});
}



function initUtilisateur() {

}

/****************************************GESTION DU PLANNING***************************************/
function initPlanningInfo() {
	$(".plann_bt_info").click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		$(".planning_info_dt,.planning_info_gch").hide();
		$('.rdv').css('z-index','100');
		$(this).parents('.rdv').css('z-index','200');
		$(this).next().show();
	});

	$(".terminer").click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		//$(this).next().hide();
		var objet_courant=$(this).parents('form');
		var tmp_deb=$(objet_courant).find('.heure_courante').val();

		var heure = tmp_deb.substring(0,2);
		var minute = tmp_deb.substring(3,5);
		var time_deb=parseInt(heure)*60+parseInt(minute);
		heure=$(objet_courant).find('select[name=heure]').val();
		minute=$(objet_courant).find('select[name=minute]').val();

		var time_fin=parseInt(heure)*60+parseInt(minute);

		if (time_fin-time_deb<60){
			//alert('Les rendez vous doivent faire au moins une heure !')
			//return false;
		}

		if($(objet_courant).find('input[type=radio][checked]').length==0){
		  alert('Merci d\'accepter ou non le rendez vous');
		  return false;
		}
		var macible=$(objet_courant).serialize()+"&cible="+$(this).attr('rel');

		$.ajax({
			type: "POST",
			url: "a_oui_non_visite.php",
			data: macible,
			success: function(msg){
				if (msg==1){
					refresh_planning(false,true);
				}else{
					alert(msg);
				}
			}
		});



	});

	$('.planning_suivant a,.planning_precedent a').click(function(event){
		event.preventDefault();
		event.stopPropagation();
		if ($(this).parent().hasClass('planning_suivant')){
			var choix="jsuivant";
		}else{
			var choix="jprecedent";
		}
		refresh_planning(choix,true);
	});

	$('.plann_bt_fermer').click(function(event){
		event.preventDefault();
		event.stopPropagation();
		if (confirm('Attention, la suppression est irr\351versible !')){
			$.ajax({
				type: "POST",
				url: "a_delete_visite.php",
				data: "cible="+$(this).attr('rel'),
				success: function(msg){
					if (msg==1){
						refresh_planning(false,true);
					}else{
						alert('Une erreur est survenue lors de la suppression !');
					}
				}
			});
		}

	});

	$('input[name=choix_rdv]').click(function(event){
		event.stopPropagation();
		$(this).parents("table").find('textarea').show().prev().prev().show();
	});

	$('.calendrier_precedent,.calendrier_suivant').click(function(event){
		event.preventDefault();
		event.stopPropagation();
		if ($(this).hasClass('calendrier_suivant')){
			var choix="msuivant";
		}else{
			var choix="mprecedent";
		}
		refresh_planning(choix,$(this).attr('rel'));

	});

	$('.click_calendrier').unbind('click');
	$('.click_calendrier').click(function(event){
		event.preventDefault();
		event.stopPropagation();
		refresh_planning(false,$(this).attr('rel'));

	});
	$('.planning_info_gch,.planning_info_fr,.rdv').click(function(event){
		event.stopPropagation();
	});



	$(".planning_info_gch .fermer_alert,.planning_info_dt .fermer_alert").click(function(event) {
		event.preventDefault();
		event.stopPropagation();
		$('.planning_info_gch,.planning_info_dt').hide();
	});


}

function refresh_calendrier($choix,$date){
		$.ajax({
			type: "POST",
			url: "refresh_calendrier_planning.php",
			data: "choix="+$choix+"&date="+$date,
			success: function(msg){
				$('#calendrier').html(msg);
				initPlanningInfo();
			}
		});
}

function refresh_planning($choix,$date){
		if ($date==true){
			$date=$("input.date_courante").val()
		}
		$.ajax({
			type: "POST",
			url: "refresh_planning.php",
			data: "choix="+$choix+"&date="+$date,
			success: function(msg){
				$('.visite').html(msg);
				refresh_calendrier($choix,$date)
			}
		});
}
/****************************************FIN GESTION DU PLANNING***************************************/
function initBulle() {
	$(".point").mouseover(function() {
		$(this).find(".bulle").show();
	});

	$(".point").mouseleave(function() {
		$(this).find(".bulle").hide();
	});
}
function initAlertList() {
	$(".offre_alerte").mouseover(function(){
		$(this).children().find(".planning_info_gch").show();
	});
}

/*************************** Autocomplétion des villes et cp ******************************/
function initAutocomplete() {
	//Autocomplétion du moteur de recherche
	$('.ville').autocomplete('a_liste_ville_cp.php', {
													maxItemsToShow:30,
													delay:10,
													minChars:2,
													matchSubset:1,
													matchContains:1,
													cacheLength:10,
													onItemSelect:selectItem,
													onFindValue:findValue,
													formatItem:formatItem,
													autoFill:false,
													width:200
													});
													
	//Autocomplétion de la création d'annonce
	//Champ ville
	$('#ville').autocomplete('a_liste_ville.php', { minChars:2,
													maxItemsToShow:30,
													autoFill:true,
													onItemSelect:selectItemVille,
													onFindValue:findValueVille,
													formatItem:formatItem,
													matchContains:1,
													inputClass:"ac_input",
													resultsClass:"ac_results",
													width:200
													});
				
	//Champ CP
	$('#cp').autocomplete('a_liste_cp.php', { minChars:2,
													maxItemsToShow:30,
													autoFill:true,
													onItemSelect:selectItemCp,
													onFindValue:findValueCp,
													formatItem:formatItem,
													matchContains:1,
													inputClass:"ac_input",
													resultsClass:"ac_results",
													width:200
													});
	
}

function findValue(li) {
	if( li == null ) return alert("No match!");
	

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;


}

//Recherche de la ville en fonction du cp
function findValueCp(li) {
	if( li == null ) return false;//alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	$('#ville').val(ucwords(sValue));

}

//Recherche du cp en fonction de la ville
function findValueVille(li) {
	if( li == null ) return false;//alert("No match!");
	

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	$('#cp').val(ucwords(sValue));

}

function selectItem(li) {
	findValue(li);
}

function selectItemCp(li) {
	findValueCp(li);
}
function selectItemVille(li) {
	findValueVille(li);
}

//Format d'affichage de la liste
function formatItem(row) {
	return ucwords(row[1])+" -- "+ucwords(row[0]);
}

function lookupAjax(){
	var oSuggest = $(".ville")[0].autocompleter;

	oSuggest.findValue();

	return false;
}
/*************************** FIN Autocomplétion ******************************/

function ucwords(str) {
	// Uppercase the first character of every word in a string 
	//
	// version: 909.322
	// discuss at: http://phpjs.org/functions/ucwords
	// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +   improved by: Waldo Malqui Silva
	// +   bugfixed by: Onno Marsman
	// *     example 1: ucwords('kevin van zonneveld');
	// *     returns 1: 'Kevin Van Zonneveld'
	// *     example 2: ucwords('HELLO WORLD');
	// *     returns 2: 'HELLO WORLD'
	return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase( ); } );
}

