$(document).ready(function() {
	
	jQuery.easing.def = 'easeInOutQuad';
	
	//####################################################
	//####################################################
	// GENERAL
	//####################################################
	//####################################################
	
	// Menu principal
	$("#menu-principal li").hover(function(e) {
		$(this).find("ul.sub-menu").hoverFlow(e.type, {height: 'show', opacity: '1'},{queue:false}, 400, "swing");
		
		// culture
		if( $(this).hasClass('menu-culture')) {
			$(this).addClass('culture-active');
		}
		// sport
		if( $(this).hasClass('menu-sport') ) {
			$(this).addClass('sport-active');
		}
		// loisirs
		if( $(this).hasClass('menu-loisirs') ) {
			$(this).addClass('loisirs-active');
		}
		// jeunes
		if( $(this).hasClass('menu-jeunes') ) {
			$(this).addClass('jeunes-active');
		}
		// cinema
		if( $(this).hasClass('menu-cinema') ) {
			$(this).addClass('cinema-active');
		}
		
	}, function(e) {
		$(this).find("ul.sub-menu").hoverFlow(e.type, {height: 'hide', opacity: '0'},{queue:false}, "swing");
		
		// culture
		if( $(this).hasClass('menu-culture') && !$(this).hasClass('is-single') ) {
			$(this).removeClass('culture-active');
		}
		// sport
		if( $(this).hasClass('menu-sport') && !$(this).hasClass('is-single') ) {
			$(this).removeClass('sport-active');
		}
		// loisirs
		if( $(this).hasClass('menu-loisirs') && !$(this).hasClass('is-single') ) {
			$(this).removeClass('loisirs-active');
		}
		// jeunes
		if( $(this).hasClass('menu-jeunes') && !$(this).hasClass('is-single') ) {
			$(this).removeClass('jeunes-active');
		}
		// cinema
		if( $(this).hasClass('menu-cinema') && !$(this).hasClass('is-single') ) {
			$(this).removeClass('cinema-active');
		}
		
	});
	
	
	// Gestion du lien pour le haut de la page
	if($('#back-top').length) {
		
		var actif = 0;
		
		// on le cache au chargement
		$("#back-top").hide();
		
		if(actif==1) {
		
			// affichage en fade
			$(function () {
				$(window).scroll(function () {
					if ($(this).scrollTop() > 100) {
						$('#back-top').fadeIn();
					} else {
						$('#back-top').fadeOut();
					}
				});
		
				// on scroll jusqu'en haut au click
				$('#back-top a').click(function () {
					$('body,html').animate({
						scrollTop: 0
					}, 800);
					return false;
				});
			});
			
		}
		
	}

	
	// PROTEXTION DES IMAGES PAR CLIC DROIT (...)
	$("img").rightClick( function(e) {
		
	});

	// Diaporama de la page d'accueil
	if($('#diaporama').length) {
		$('#diaporama').coinslider({
			width: 910, // width of slider panel
			height: 300, // height of slider panel
			spw: 1, // squares per width 7
			sph: 1, // squares per height 5
			delay: 5000, // delay between images in ms
			sDelay: 20, // delay beetwen squares in ms
			opacity: 0.80, // opacity of title and navigation
			titleSpeed: 1500, // speed of title appereance in ms
			effect: 'straight', // random, swirl, rain, straight
			navigation: true, // prev next and buttons
			links : false, // show images as links 
			hoverPause: true // pause on hover
		});
		
	}
	

	// Diaporama de la page SINGLE
	if($('#single-diaporama').length) {
	
		$('#single-diaporama').coinslider({
			width: 620, // width of slider panel
			height: 300, // height of slider panel
			spw: 1, // squares per width 7
			sph: 1, // squares per height 5
			delay: 5000, // delay between images in ms
			sDelay: 20, // delay beetwen squares in ms
			opacity: 0.95, // opacity of title and navigation
			titleSpeed: 1500, // speed of title appereance in ms
			effect: 'straight', // random, swirl, rain, straight
			navigation: true, // prev next and buttons
			links : true, // show images as links 
			hoverPause: true // pause on hover
		});
		

		$("#single-diaporama a").fancybox({
			'titleShow'		: false
		});
		
	}



//####################################################
//####################################################
// PAGE CINEMA
//####################################################
//####################################################

	if($('#cinema-list').length) {
		
		hoverHoraires();
		
		// DOMContentLoaded
		$(function() {
		
		  // bind 
		  var $filterCinema = $('#navig_salles li.filtresalle');

		  // recuperation de la liste d'affiches
		  var $affiche = $('#destination');

		  // clone de la liste d'affiche
		  var $afficheClone = $affiche.clone();
		
			// bouton pour voir toutes les affiches
		  $('#navig_salles li.all').click(function(e) {
		  
				var $afficheCloneFiltre = $afficheClone.find('li');
				
		    // on appel le plugin
		    $affiche.quicksand($afficheCloneFiltre, {
		      duration: 800,
		      easing: 'easeInOutQuad',
		      attribute: 'data-type'
		    }, function() {
		    	hoverHoraires();
		    });
		
		  });

		  // appel du plugins sur les clics filtre du menu cine
			$filterCinema.click(function(e) {
				
				var $afficheCloneFiltre = $afficheClone.find('li[data-id=' + $(this).attr('data-id') + ']');
								
		    // on appel le plugin
		    $affiche.quicksand($afficheCloneFiltre, {
		      duration: 800,
		      easing: 'easeInOutQuad',
		      attribute: 'data-type'
		    }, function() {
		    	hoverHoraires();
		    });
		    
		    
		
		  });
		
		});
	
	
	}


function hoverHoraires() {
	/* Horaires du jours en hover */
	$('.linkAffiche').hover(function(){
		$('.todayHour', this).stop(true, true).slideDown('fast');
	}, function() {
		$('.todayHour', this).stop(true, true).slideUp('fast');
	});
}

//####################################################
//####################################################
// WIDGETS
//####################################################
//####################################################

	// widget RECHERCHE
	// datepickers
	var dates = $('#search-date-debut, #search-date-fin').datepicker({
		dateFormat: 'dd-mm-yy',
		prevText: '',
		nextText: '',
		numberOfMonths: 1,
		minDate: 0,
		onSelect: function(selectedDate) {
			var option = this.id == "search-date-debut" ? "minDate" : "maxDate";
			var instance = $(this).data("datepicker");
			var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
			dates.not(this).datepicker("option", option, date);
		}
	});


	$('#wpsb_fld_2').watermark('Votre Commune', {className: 'inputWatermark'});
	$('#wpsb_email').watermark('Votre adresse E-mail', {className: 'inputWatermark'});

	// valeur par defaut a l'interieur des champs
	$('#search-mot-cle').watermark('Saisissez un mot clé', {className: 'inputWatermark'});
	$('#search-date-debut').watermark('Entrez une date de début', {className: 'inputWatermark'});
	$('#search-date-fin').watermark('Entrez une date de fin', {className: 'inputWatermark'});

	$('#search-field-top').watermark('Recherche', {className: 'inputWatermark'});

	// CARTE GOOGLE
	if($('#map_canvas').length) {
		var map = null;
		var geocoder = null;

		initialize();
		
	}
	
	
	// widget de connexion
	if($('#log2').length) $('#log2').watermark('Nom utilisateur', {className: 'inputWatermark'});
	if($('#pwd2').length) $('#pwd2').watermark('Mot de passe', {className: 'inputWatermark'});
	
	
	
//####################################################
//####################################################
// PAGE SINGLE
//####################################################
//####################################################

	/* BOUTONS POUR SWITCHER ENTRE LA VIDEO ET LES IMAGES */
	if($('.switch-button').length) {
		
		$('.switch-button-video').click(function() {
		
			if($('#single-diaporama').length) {
				$('#single-diaporama').hide();
			} else {
				$('#single-diaporama-nop').hide();
			}
			$('#single-video').fadeIn();
			
			$('.switch-button-images').removeClass('bt-actif');
			$('.switch-button-video').addClass('bt-actif');
		});
		
		$('.switch-button-images').click(function() {
		
			$('#single-video').hide();
			
			if($('#single-diaporama').length) {
				$('#single-diaporama').fadeIn();
			} else {
				$('#single-diaporama-nop').fadeIn();
			}
			
			$('.switch-button-video').removeClass('bt-actif');
			$('.switch-button-images').addClass('bt-actif');

		});
		
	}
	
	
	
//####################################################
//####################################################
// FORMULAIRES
//####################################################
//####################################################
	
	// formulaire de connexion
	if($('#form-connexion').length) {
		if($('#log').length) $('#log').watermark('Nom utilisateur', {className: 'inputWatermark'});
		if($('#pwd').length) $('#pwd').watermark('Mot de passe', {className: 'inputWatermark'});
	}
	
	// formulaire d'inscription
	if($('#register-form').length) {
	
		// valeur par defaut a l'interieur des champs
		if($('#user_login').length) $('#user_login').watermark('Choisissez un nom utilisateur', {className: 'inputWatermark'});
		if($('#user_email').length) $('#user_email').watermark('Votre adresse e-mail', {className: 'inputWatermark'});
		
		if($('#firstname').length) $('#firstname').watermark('Prénom', {className: 'inputWatermark'});
		if($('#lastname').length) $('#lastname').watermark('Nom', {className: 'inputWatermark'});
		
		if($('#website').length) $('#website').watermark('Site internet', {className: 'inputWatermark'});
		if($('#tel_reservation').length) $('#tel_reservation').watermark('Téléphone réservation', {className: 'inputWatermark'});
		if($('#tel_renseignements').length) $('#tel_renseignements').watermark('Téléphone renseignements', {className: 'inputWatermark'});
		
		if($('#nom_association').length) $('#nom_association').watermark('Nom de l\'association', {className: 'inputWatermark'});
		
		if($('#pass1').length) $('#pass1').watermark('Choisissez un mot de passe', {className: 'inputWatermark'});
		if($('#pass2').length) $('#pass2').watermark('Saisissez le une seconde fois', {className: 'inputWatermark'});
		
		if($('#adresse').length) $('#adresse').watermark('Adresse', {className: 'inputWatermark'});
		if($('#code_postal').length) $('#code_postal').watermark('Code Postal', {className: 'inputWatermark'});
		if($('#ville').length) $('#ville').watermark('Ville', {className: 'inputWatermark'});
		
		if($('#infos_supplementaires').length) $('#infos_supplementaires').watermark('Informations supplémentaire (si vous souhaitez donner des précisions)', {className: 'inputWatermark'});
		
				
		// VALIDATION
		//$("#register-form").validationEngine();

	}
	
	// formulaire de l'annonce
	if($('#post-form').length) {
		
		
		// parametres de l'uploader
	  /*
	  $('#formUpload').uploadify({
	    'uploader'  : '/uploadify/uploadify.swf',
	    'script'    : '/uploadify/uploadify.php',
	    'cancelImg' : '/uploadify/cancel.png',
	    'folder'    : '/',
	    'auto'      : true,
			'fileExt'     : '*.jpg;*.gif;*.png',
			'buttonText': 'Choisir une image',
			'multi'			: false
		});
		*/
		
		
		// agrandissement auto de la zone de texte
		$('#texte_form').elastic();
		
		// effacement du message de validation au bout de 10 sec
		$("#msgForm").show().delay(10000).fadeOut();
		
		// valeur par defaut a l'interieur des champs
		if($('#titre_form').length) $('#titre_form').watermark('Titre', {className: 'inputWatermark'});
		if($('#soustitre_form').length) $('#soustitre_form').watermark('Sous-titre', {className: 'inputWatermark'});
		if($('#texte_form').length) $('#texte_form').watermark('Votre texte', {className: 'inputWatermark'});
		if($('#lieu_form').length) $('#lieu_form').watermark('Lieu', {className: 'inputWatermark'});
		if($('#ville_form').length) $('#ville_form').watermark('Ville', {className: 'inputWatermark'});
		if($('#date_form').length) $('#date_form').watermark('Date', {className: 'inputWatermark'});
		if($('#tag_form').length) $('#tag_form').watermark('Tag', {className: 'inputWatermark'});
		
				
		// datepicker
		var dates = $('#dateDeb_form, #dateFin_form').datepicker({
			dateFormat: 'dd-mm-yy',
			prevText: '',
			nextText: '',
			numberOfMonths: 1,
			minDate: 0,
			onSelect: function(selectedDate) {
				var option = this.id == "dateDeb_form" ? "minDate" : "maxDate";
				var instance = $(this).data("datepicker");
				var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
				dates.not(this).datepicker("option", option, date);
			}
		});
		
		// VALIDATION
		$("#formul").validationEngine();
		
	}
	
});



/////////////////////////////////////////
// GOOGLE MAPS
var geocoder;
var map;

function initialize() {
	geocoder = new google.maps.Geocoder();
	var myLatlng = new google.maps.LatLng(46.780556,4.852778);
	var myOptions = {
	  zoom: 12,
	  center: myLatlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function showAddress(address) {
	geocoder.geocode( { 'address': address}, function(results, status) {
	  if (status == google.maps.GeocoderStatus.OK) {
	    map.setCenter(results[0].geometry.location);
	    var marker = new google.maps.Marker({
	        map: map, 
	        position: results[0].geometry.location
	    });
	  } else {
	    //alert("Geocode was not successful for the following reason: " + status);
	  }
	});
}
// FIN GOOGLE MAPS
/////////////////////////////////////////


/* Menu quand on est sur un single ou une sous cat */
function addMenuHighlight(posttype) {
	
	$(".menu-"+posttype).addClass(posttype+'-active');
	$(".menu-"+posttype).addClass('is-single');

}


/* tab pour le cinema */
function switch_tabs(obj)
{
	$('.tab-content').hide();
	$('.navig_salles a').removeClass("selected");
	var id = obj.attr("rel");
 
	$('#'+id).fadeIn();
	obj.addClass("selected");
}
