
jQuery(function($){
	$.datepicker.regional['fr'] = {clearText: 'Effacer', clearStatus: '',
		closeText: 'Fermer', closeStatus: 'Fermer sans modifier',
		prevText: '&lt;Préc', prevStatus: 'Voir le mois précédent',
		nextText: 'Suiv&gt;', nextStatus: 'Voir le mois suivant',
		currentText: 'Courant', currentStatus: 'Voir le mois courant',
		monthNames: ['janvier','février','mars','avril','mai','juin',
		'juillet','août','septembre','octobre','novembre','décembre'],
		monthNamesShort: ['Jan','Fév','Mar','Avr','Mai','Jun',
		'Jul','Aoû','Sep','Oct','Nov','Déc'],
		monthStatus: 'Voir un autre mois', yearStatus: 'Voir un autre année',
		weekHeader: 'Sm', weekStatus: '',
		dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
		dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
		dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
		dayStatus: 'Utiliser DD comme premier jour de la semaine', dateStatus: 'Choisir le DD, MM d',
		dateFormat: 'dd/mm/yy', firstDay: 0, 
		initStatus: 'Choisir la date', isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['fr']);
});
$(window).load(function() {
	if($('.jslider-label-to')){
		$('.jslider-label-to').html("<span>&gt;</span>"+$('.jslider-label-to').html());
	}
});
$(document).ready(function() {

	
	$('#cdt35-search-box .form-text').each(function()
	{
		if($(this).val()=="")
		{
			$(this).val('Recherche');		
		}
		$(this).blur(function(){
			if($(this).val()=="")
			{
				$(this).val('Recherche');		
			}
		});
		$(this).focus(function(){
			if($(this).val()=="Recherche")
			{
				$(this).val('');		
			}
		});
	});
	$('a.oter_brochure').click(function(){
   		$('body').fadeOut(1000);   		
   });
   $('.formulaire_commande form #edit-submit').click(function(){
   		$('body').fadeOut(1000);   		
   });
	$(".diveBlock").hover(
      function () {
	       if(!$('.diveCloser').hasClass("deployed")){
		       $('.diveCloser').addClass("deployed");
		       $('.diving').slideToggle('slow',function(){});
	       }
      },
      function () {
	      if($('.diveCloser').hasClass("deployed")){
	       $('.diveCloser').removeClass("deployed");
	       $('.diving').slideToggle('slow',function(){	});
	      }
      }
    );
   $('.diving').hide();
   jQuery('.jcarousel-skin-home-univers').jcarousel();
   window.setTimeout("init_select_OS()",4000);
   window.setTimeout("init_loader_OS()",4000);
   window.setTimeout("init_select_jourmois()",4200);
   initDisplayCurrentActiviteSlider();
   initDisplayCurrentHebergementSlider();
   $(".newsletter a").click(function () {
      if ($("div.bloc-inscription-newsletter").is(":hidden")) {
        $("div.bloc-inscription-newsletter").slideDown("slow");
      } else {
        $("div.bloc-inscription-newsletter").slideUp();
      }
    });
	if($('.RootMenuContainer').length>0)
	{
		var userAgent = navigator.userAgent.toLowerCase();
		 
		// Figure out what browser is being used
		jQuery.browser = {
			version: (userAgent.match( /.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/ ) || [])[1],
			chrome: /chrome/.test( userAgent ),
			safari: /webkit/.test( userAgent ) && !/chrome/.test( userAgent ),
			opera: /opera/.test( userAgent ),
			msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
			mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
		};
		
		if(jQuery.browser.chrome )
		{
			$('.RootMenuContainer').width( parseInt($('.RootMenuContainer ul').width())+120);
		}
		else
		{
			$('.RootMenuContainer').width( parseInt($('.RootMenuContainer ul').width())+50);
		}
	}
	if($('.with_notabs').length>0)
	{
		reinit_select('.with_notabs');
	}
	/*if($('.select_liste_div').length>0)
	{
		reinit_select('.select_liste_div');
	}*/

	if($('.recherche-menu li a').length>0)
	{
		$('.tab_content').css('display','none');
		$($('.recherche-menu li.active a').attr('href')).show();
		reinit_select($('.recherche-menu li.active a').attr('href'));
		$('.link_tabs a').click(function(e){
			e.stopPropagation();
			$('.tab_content').css('display','none');
			$('.link_tabs').removeClass('active');
			$(this).parent().addClass('active');
			//$($(this).attr('href')).css('display','inline');
			var tabref = $(this).attr('href');
			
			$( $(this).attr('href') ).show();				

			// réinitialise les sliders sur le change
			if( tabref == "#form_hebergement" )
			{
				displayCurrentHebergementSlider();
			}
			else if ( tabref == "#form_activite" )
			{
				displayCurrentActiviteSlider();
				
			}
			else if ( tabref == "#form_evenement" )
			{
				
			}
			else if ( tabref == "#form_sejour" )
			{
				$("#sejour_pricefieldmin").slider("redraw");
			}
			else
			{
				
			}

			
			reinit_select($(this).attr('href'));
			return false;
		});
	}
	if($('#mergedtype_hebergement').length>0 || $('#mergedtype_decouverte').length>0 )
	{
		$('#mergedtype_hebergement').change(function(){
		    $('.dynamic_field').hide();
			$('.dynamic_field_'+$(this).val()).show();
			displayCurrentHebergementSlider();
		});
		
		
		
		$('.dynamic_field').hide();

		type=$('#mergedtype_hebergement').val();		
		//$('.dynamic_field_'+type).show();		
				
		
		$('#mergedtype_decouverte').change(function(){
		    $('.dynamic_field').hide();
			$('.dynamic_field_'+$(this).val()).show();

			displayCurrentActiviteSlider();

		});		
		
		type=$('#mergedtype_decouverte').val();
		
		//$('.dynamic_field_'+type).show();
		
		
		$('#mergedtype_evenement').change(function(){
		    $('.dynamic_field').hide();
			$('.dynamic_field_'+$(this).val()).show();
		});		
		
		type=$('#mergedtype_evenement').val();
			
		//$('.dynamic_field_'+type).show();
		

		setTimeout( function(){ 
			$('.dynamic_field').hide('0', function(){
				//on reaffiche ce qu'on vient de cacher (comme la zone géographique)
				type=$('#mergedtype_hebergement').val();		
				$('.dynamic_field_'+type).show();
				type=$('#mergedtype_decouverte').val();
				$('.dynamic_field_'+type).show();
				type=$('#mergedtype_evenement').val();
				$('.dynamic_field_'+type).show();
				
				displayCurrentHebergementSlider();			
			}); });

		
		
		// ajoute le redraw sur le clic pour plus d'infos
		// nécessaire car le script jSlider ne supporte pas encore d'être affecté dans un élément qui passe en display:none
		/*
		$("#camping-additionalfields a").click( function(){
			setTimeout( function(){ $("#campings_hebergement_pricefieldmin").slider("redraw"); },1000);
		});
		$("#hotel-additionalfields a").click( function(){
			setTimeout( function(){ $("#hotel_hebergement_pricefieldmin").slider("redraw"); },1000);
		});
		$("#residence-additionalfields a").click( function(){
			setTimeout( function(){ $("#residence_hebergement_pricefieldmin").slider("redraw"); },1000);
		});
		$("#chambres-additionalfields a").click( function(){
			setTimeout( function(){ $("#chambres_hebergement_pricefieldmin").slider("redraw"); },1000);
		});
		$("#location-additionalfields a").click( function(){
			setTimeout( function(){ $("#location_hebergement_pricefieldmin").slider("redraw"); },1000);
		});
		

		$("#activite-additionalfields a").click( function(){
			setTimeout( function(){ $("#activite_decouverte_pricefieldmin").slider("redraw"); },1000);
		});
		$("#culture_patrimoine-additionalfields a").click( function(){
			setTimeout( function(){ $("#culture_patrimoine_decouverte_pricefieldmin").slider("redraw"); },1000);
		});*/
	}

	// ajoute le redraw sur le clic pour plus d'infos
	$("#restaurant-additionalfields legend a").click( function(){
		setTimeout( function(){ $("#restaurant_restaurant_pricefieldmin").slider("redraw"); },1000);
	});
	


	/*function datepicker page recherche*/
	if(document.getElementById('field_Date_debut_evenement'))	{
		$('#field_Date_debut_evenement').datepicker();
		$('#field_Date_debut_evenement').datepicker('option', 'dateFormat', 'dd/mm/y');
		$('#field_Date_debut_evenement').datepicker('option', 'monthNames', ['Janvier','F&eacute;vrier','Mars','Avril','Mai','Juin','Juillet','Ao&ucirc;t','Septembre','Octobre','Novembre','D&eacute;cembre'] );
		$('#field_Date_debut_evenement').datepicker('option', 'dayNamesMin', ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa']);
		$('#field_Date_debut_evenement').datepicker('option', 'firstDay', 0);
	}
	if(document.getElementById('field_Date_fin_evenement'))	{
		$('#field_Date_fin_evenement').datepicker();
		$('#field_Date_fin_evenement').datepicker('option', 'dateFormat', 'dd/mm/y');
		$('#field_Date_fin_evenement').datepicker('option', 'monthNames', ['Janvier','F&eacute;vrier','Mars','Avril','Mai','Juin','Juillet','Ao&ucirc;t','Septembre','Octobre','Novembre','D&eacute;cembre'] );
	    $('#field_Date_fin_evenement').datepicker('option', 'dayNamesMin', ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa']);
	   	$('#field_Date_fin_evenement').datepicker('option', 'firstDay', 0);
	 }

	reinit();
	node_photos();
	$('#roll-debug').removeAttr("href");
	$('#roll-debug').click(function()
	{		
		$('#node-debug').slideToggle();
	});
	$('#type_emploi_filter').change(function (elt) {
		var val = $(this).val();
		if (val == "Tous")
		{
			window.location.href=('?field_type_emploi_value=');
		}
		else
		{
			window.location.href=('?field_type_emploi_value='+val);
		}
	});
	 $(".fast_admin_links_container").parent().hover(
      function () {
        $(this).children('.fast_admin_links_container').animate({'opacity':1},500);
      },
      function () {
        $(this).children('.fast_admin_links_container').animate({'opacity':0.1},500);
      }
    );
    $(".fast_admin_links_container").hover(
      function () {
       $(this).parent().addClass('admin_link_hover');
      },
      function () {
       $(this).parent().removeClass('admin_link_hover');
      }
    );
    
    
    // gestion des photos sur le diaporama / slider
	if($('.offre_photo_diapos').length>0)
	{		
		$('.offre_photo_diapos_container a:not(:first)').hide();
		$('.offre_photo_diapos_container a:first').fadeIn(1000);

		$('.offre_photo_diapos #carousel_diapo li a').click(function(e){

			e.stopPropagation();
			
			currentID = $(this).attr('id');

			$('.gallery-photo-medium-sized').hide()
			$('#'+currentID.substr(5) ).fadeIn('1500');

			return false;
		});
	}
	
	$('.recherche-container form').submit(function(){
		if($('#hebergement_communefield-wrapper #autocomplete').length==0){
		//$('.recherche-container form input.edit-submit-1').click(function(){
			$('.recherche-content').after('<div class="recherche-waiter"></div>');
			$('.recherche-waiter').css('opacity',0);
			$('.recherche-waiter').height($('.recherche-content').height());
			$('.recherche-waiter').width($('.recherche-content').width());
			$('.recherche-waiter').css('margin-top',-($('.recherche-content').height()));
			$('.recherche-waiter').css('opacity',0.5);
		}	
	});
	$('div.offre-emploi').click(function () {
	  	if ($('.bloc-candid-form').css('display') == "none")$('.bloc-candid-form').show();
	    else $('.bloc-candid-form').hide();
		});
});
function init_select_OS()
{
	reinit_select('#OsBarreOnglet div.ui-tabs-panel:visible');
	$("#OsBarreOnglet .ui-tabs-nav li a").click(function(){
		reinit_select($(this).attr('href'));
	});
}

function init_loader_OS(){
	$("#OSRecherche #OsCritCont_btnrech a").click(function(){
		$(this).css("display", "none");
		$("#OSRecherche").fadeTo("slow", 0.33);
		$("#OSRecherche-loader").css("display", "block");
		return false;
	});
}
function reinit_select(selecteur)
{
		$(selecteur+" select").each(function(){
			if(!$(this).hasClass('selectInit'))
			{
				//{'defaultCss':'qsdsqd'}
				if($(this).hasClass('select_liste') || $(this).hasClass('OsDureeNuit') || $(this).hasClass('OsJourDebut') || $(this).hasClass('OsMoisDebut') || $(this).hasClass('OsAnDebut'))
				{
					if($(this).hasClass('OsMoisDebut') || $(this).hasClass('OsDureeNuit') || $(this).hasClass('select_liste'))
					$(this).sSelect({'defaultCss':'newListSelected newListSelectedSm'});
					else
					$(this).sSelect({'defaultCss':'newListSelected newListSelectedLt'});

				}
				else
				{
					$(this).sSelect({'defaultCss':'newListSelected'});
				}
				$(this).addClass('selectInit');
			}
		});
}
function node_photos()
{	
	$('.bloc_node_photo .switch_photo').each(function()
	{
		
		$(this).removeAttr("href");
		$(this).click(function()
		{			
			pic=$(this).attr('rel');
			alt=$(this).attr('alt');
			$('#big-pic').attr({ src: pic, alt: alt });
		});
	});
}

function changeCreditPhoto(credit){	
	$('#credit_photo').html(credit);	
}

function replaceAfterTimer(selector,content){
	$(selector).prev().toggle();
	$(selector).replaceWith(content);
	
}

function reinit()
{

	/** Initialisation Bloc Top newsletter **/
/*

	$('.newsletter-submit').removeAttr("href");

	$('.newsletter-submit').live('click', function(elt){

		var post_mail = 	$('.newsletter-subscribe').val();
		var post_todo = $('#edit-todo').val();
		$.post("http://cdt35-web2.groupereflect.net/helper/ajaxrefresh",
					 { ajax : 1 , arg : 'newsletter' , mail : post_mail , todo : post_todo },
					  function(data){
						$('.error').hide();
						$('.newsletter-form').replaceWith(data.result);

					  }, "json");
		return false;

	});
*/
/************ GESTION de la SELECTION ***************/
	$('.node .selection_add , .offre .selection_add , .node-idee .selection_add ').click(function()
	{

		url=$(this).attr('href')+'?ajax=1&modif=1';

		$(this).replaceWith('<span id="processing" class="processing_selection">Traitement en cours</span>');
		$.ajax({
			url: url,
			type: "GET",
				success: function(result){
					$('#processing').replaceWith('<span id="processing" class="processing_selection"></span>');
					$('#processing').prev().toggle();
					window.setTimeout(function () {$('#processing').prev().remove();replaceAfterTimer('#processing',result);reinit();},5000);
				}
			});
		return false;
	});
	$('.node .selection_remove , .offre .selection_remove , .node-idee .selection_remove ').click(function()
	{
		url=$(this).attr('href')+'?ajax=1&modif=1';
		$(this).replaceWith('<span id="processing" class="processing_selection">Traitement en cours</span>');
		$.ajax({
			url: url,
			type: "GET",
				success: function(result){
					$('#processing').replaceWith('<span id="processing" class="processing_selection"></span>');
					$('#processing').prev().toggle();
					window.setTimeout(function () {$('#processing').prev().remove();replaceAfterTimer('#processing',result);reinit();},5000);
				}
			});
		return false;
	});
};

/* On enregistre dans un cookie de domaine */

function scookie(name,value,days) {
	var expire=new Date();
	
	var path="/";
	var domain=null;
	expire.setDate(expire.getDate()+days);

	document.cookie=name+"="+escape(value)+
		((expire==null) ? "" : ("; expires="+expire.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain));
	return true;
}
function gcookie(name) {
	if(document.cookie.length>0){
		start=document.cookie.indexOf(name+'=');
		pos = start+name.length+1;
		if(start!=0) {
			start=document.cookie.indexOf('; '+name+'=');
			pos = start+name.length+3;
		}
		if(start!=-1) {
			start=pos;
			end=document.cookie.indexOf(';',start);
			if(end==-1){
				end=document.cookie.length;
			}
			return unescape(document.cookie.substring(start,end));
		}
	}
	return '';
}

/* On affecte les données du cookie aux champs du formulaire de commande des brochures touristiques */
function getDataCommandeBrochures(){
	var infos = gcookie("CDT35-InfosCommandeBrochures");
	position_fin = infos.indexOf('||');
	
	// Civilite
	donnees = infos.substring((infos.indexOf('='))+1,infos.indexOf('||'));
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-ID-CIVILITE").selectedIndex = donnees;
	// Nom - edit-NOM
	donnees = infos.substring((infos.indexOf('='))+1,infos.indexOf('||'));
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-NOM").value = donnees;
	// Prenom
	donnees = infos.substring((infos.indexOf('='))+1,infos.indexOf('||'));
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-PRENOM").value = donnees;
	// Age
	donnees = infos.substring((infos.indexOf('='))+1,infos.indexOf('||'));
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-AGE").value = donnees;
	// Situation
	donnees = infos.substring((infos.indexOf('='))+1,infos.indexOf('||'));
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-PROSP-FAMILLE").selectedIndex = donnees;
	// Adresse
	donnees = infos.substring((infos.indexOf('='))+1,infos.indexOf('||'));
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-ADRESSE1").value = donnees;
	// CP
	donnees = infos.substring((infos.indexOf('='))+1,infos.indexOf('||'));
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-CP").value = donnees;
	// Commune
	donnees = infos.substring((infos.indexOf('='))+1,infos.indexOf('||'));
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-COMMUNE").value = donnees;
	// Pays
	donnees = infos.substring((infos.indexOf('='))+1,infos.indexOf('||'));
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-ID-PAYS").selectedIndex = donnees;
	// Email
	donnees = infos.substring((infos.indexOf('='))+1,infos.indexOf('||'));
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-TYPCOMM-C4").value = donnees;
	// Motivation
	donnees = infos.substring((infos.indexOf('='))+1);
	infos = infos.substring((infos.indexOf('||'))+2);
	document.getElementById("edit-PROSP-MOTIVATION").selectedIndex = donnees;
	
	
}

// On recupère les données du formulaire de commande des brochure et on les met dans un cookie
function saveDataCommandeBrochures(nom, prenom, civilite, age, situation, adresse, cp, commune, pays, email, motivation){
	//alert('civilite : '+civilite+' nom : '+nom+" prenom : "+prenom+" age : "+age+" situation : "+situation+" adresse : "+adresse+" cp : "+cp+" comm : "+commune+"  pays : "+pays+" email : "+email+" motivation : "+motivation);
	var infos = "civilite="+civilite+"||nom="+nom+"||prenom="+prenom+"||age="+age+"||situation="+situation+"||adresse="+adresse+"||cp="+cp+"||commune="+commune+"||pays="+pays+"||email="+email+"||motivation="+motivation;
	scookie("CDT35-InfosCommandeBrochures",infos,100);
	
	parent.document.location.href='/brochures-touristiques';
	
}

function init_select_jourmois() {	
	var reg=new RegExp("(OsRadioDuree)", "g");
	$('input.OsRadioDuree').click(function(){
		var thisId = $(this).attr('id');
	    var thisCutId = thisId.replace(reg, '');
	    var indexDuree = thisId.substring(thisId.lastIndexOf("_")+1);
	    var indexMoteur = thisCutId.substr(0, thisCutId.length-2);
	    
	    var newIndexDuree = Math.abs(parseInt(indexDuree) - 1);
	    $('#OsBlocSel'+thisCutId).show();
	    
	    //$('#OsBlocSel'+thisCutId).css('display', 'inline')
	    if (newIndexDuree == 0) {
	    	 $('#OsBlocSel'+thisCutId).css('display', 'block');
	    }
	    if (newIndexDuree == 1) {
	    	 $('#OsBlocSel'+thisCutId).css('zoom','1');
	    }
	    $('#OsBlocSel'+indexMoteur+'_'+newIndexDuree).hide();    

	})
}

function displayCurrentHebergementSlider()
{
	
	$('#residence_pricefieldmin-wrapper').parent().hide();
	$('#camping_pricefieldmin-wrapper').parent().hide();
	$('#location_pricefieldmin-wrapper').parent().hide();
	$('#chambres_pricefieldmin-wrapper').parent().hide();
	$('#hotel_pricefieldmin-wrapper').parent().hide();
	
	var toDisplay = $('#mergedtype_hebergement-wrapper > select').val();
	$('#'+toDisplay+'_pricefieldmin-wrapper').parent().show();
	$('#'+toDisplay+'_pricefieldmin').slider('redraw');
}

function initDisplayCurrentHebergementSlider()
{
	$('#residence_pricefieldmin-wrapper').parent().hide();
	$('#location_pricefieldmin-wrapper').parent().hide();
	$('#chambres_pricefieldmin-wrapper').parent().hide();
	$('#hotel_pricefieldmin-wrapper').parent().hide();
}


function initDisplayCurrentActiviteSlider()
{
	$("#culture_patrimoine_pricefieldmin-wrapper").parent().hide();
}

function displayCurrentActiviteSlider()
{
	$('#activite_pricefieldmin-wrapper').parent().hide();
	$("#culture_patrimoine_pricefieldmin-wrapper").parent().hide();
	var toDisplay = $('#mergedtype_decouverte-wrapper > select').val();
	$('#'+toDisplay+'_pricefieldmin-wrapper').parent().show();
	$('#'+toDisplay+'_pricefieldmin').slider('redraw');
}

