//domain+='/vazy';

function calendar(div)
{
	var base = $(div).parents('#calendar_header').attr('class');
	var ar_base = base.split('/');
	var mois = ar_base[0];
	var year = ar_base[1];
	
	if ($(div).hasClass('plus'))
	{
		var lang = $(div).prev('#calendarLang').attr('rel');
		if (mois==12)
		{
			mois=1;
			year++;		
		}
		else
		{
			mois++;
		}			
	}
	else
	{
		var lang = $(div).next('#calendarLang').attr('rel');
		if (mois==1)
		{
			mois=12;
			year--;
		}
		else
		{
			mois--;
		}
	}
	
	var data = "month="+mois+"&year="+year+'&lang='+lang;
	
	//alert( domain+"/do/calendarMonth.php");
	$.ajax({
			type: "POST",
			url: domain+"/do/calendarMonth.php",
			data: data,
			
			success: function(msg){
				msg.replace(/^\s+/g,'').replace(/\s+$/g,'');
				$('#calendar').html(msg);
				calendarTooltip();
			}
		});		
}


// Calendrier (Sidebar), au passage sur les évènements liés
function calendarTooltip()
{
	$('#calendar tr td a').tooltip({
	track: false, 
	delay: 0, 
	showURL: false, 
	showBody: " - ",
	extraClass: "bottom", 
	top : 10,
	left : -100, 
	fade: 250,
	bodyHandler: function() { return $(this).next('.calendar_tooltip').html(); }
});	
}

function timeLineTooltip()
{
// Timeline, au passage sur les carrés de couleurs
	$('.timeline_days li ul li a').tooltip({
		track: false, 
		delay: 0, 
		showURL: false, 
		showBody: " - ",
		extraClass: "right", 
		top : 15,
		left : 0, 
		fade: 250,
		bodyHandler: function() { return $(this).next('.timeline_tooltip').html(); }
	});	
}
	
$(document).ready(function(){//on attend que la page soit chargée
		
	calendarTooltip();
	timeLineTooltip();
				   
	// Header, animation du slider en jquery
	$('.showcase-content img').show();
	$('.showcase-content iframe').show();
	$("#showcase").awShowcase(
	{
		content_width:			710,
		content_height:			470,
		hundred_percent:		false,
		auto:					true,
		interval:				3000,
		continuous:				true,
		loading:				true,
		tooltip_width:			200,
		tooltip_icon_width:		32,
		tooltip_icon_height:	32,
		tooltip_offsetx:		18,
		tooltip_offsety:		0,
		arrows:					true,
		buttons:				false,
		btn_numbers:			true,
		keybord_keys:			true,
		mousetrace:				false,
		pauseonover:			true,
		transition:				'hslide', /* hslide/vslide/fade */
		transition_delay:		0,
		transition_speed:		500,
		show_caption:			'onload', /* onload/onhover/show */
		thumbnails:				false,
		thumbnails_position:	'outside-last', /* outside-last/outside-first/inside-last/inside-first */
		thumbnails_direction:	'vertical', /* vertical/horizontal */
		thumbnails_slidex:		1, /* 0 = auto / 1 = slide one thumbnail / 2 = slide two thumbnails / etc. */
		dynamic_height:			false,
		speed_change:			true,
		viewline:				false
	});
	
	// Afficher la recherche avancée
	$('#search_form a#advanced').click(function(){ 
		$('#search_form #form_advanced').slideToggle();
		return false;
	});
	$('*').click(function(event){
		if ($(event.target).parents('#form_advanced').length == 0 && $(event.target).attr('id') != 'advanced' && $(event.target).attr('id') != 'form_advanced' && $(event.target).parents('#ui-datepicker-div').length == 0) $('#search_form #form_advanced').slideUp();
	});
	
	// Mettre en grisé les types d'évènements dans la recherche
	$('.search_form input[type=checkbox]').change(function(){ 
		var infoCheck = $(this).attr('checked');
		var idCheckbox = $(this).attr('id');
		if (infoCheck == true) /* Si on vient de cliquer sur une checkbox, on doit afficher en couleur le label lié à la checkbox */
		{ 
			$('.search_form label[for='+idCheckbox+']').removeClass('disabled'); 
		}
		else /* sinon on grise le label */
		{ 	
			$('.search_form label[for='+idCheckbox+']').addClass('disabled'); 
		}
	});
	
	// jQuery UI : datepicker recherche avancée
	$('.datepicker').datepicker({
		closeText: 'Fermer',
		prevText: '&#x3c;Préc',
		nextText: 'Suiv&#x3e;',
		currentText: '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'],
		dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
		dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
		dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
		weekHeader: 'Sm',
		dateFormat: 'dd/mm/yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''
	});
	
	
	// Sidebar droite, au passage sur les miniatures des photos
	$('#galerie_photos ul li a').tooltip({
		track: true, 
		delay: 0, 
		showURL: false, 
		showBody: " - ",
		extraClass: "right", 
		top : 10,
		left : 0, 
		fade: 250 
	});	
	
	// Timeline mode OPEN, pour dérouler en version détaillée
	$('#timeline_derouler a').toggle(function(){
		var hauteurTimeLine = $('#timeline_week .timeline_days').height(); // on recherche la largeur de la timeline
		$('#timeline_week .timeline_days > li').css('height', hauteurTimeLine+'px');
		$('#timeline_week').animate({ height: hauteurTimeLine+'px' },200);	
		$('#timeline_derouler a').removeClass().addClass('open');
	}, function(){ 
		$('#timeline_week').animate({ height: '50px' },200);	
		$('#timeline_derouler a').removeClass().addClass('close');
	});
	
	

	function animateTimeLine(whatDirection,NavPrevNum,NavNextNum,aJaxUse)
	{
		// recherche la largeur de la timeline
		var incremenTation = (parseInt($('#timeline_week .timeline_days li:first').css('width')))*5;
		
		// recherche la position en left
		var positionTimeLine = parseInt($('#timeline_week .timeline_days').css('left')); 
		
		// calcule la nouvelle position de la timeline en left et le nouveau rel des liens de navigation
		if (whatDirection == 'prev')
		{ 
			if (aJaxUse == 1) // recadre manuellement si ajax utilisé pour ajouter des élements en prepend
			{
					$('#timeline_week .timeline_days').css('left','-650px'); 
					$('#timeline_week .timeline_days').animate({ left: '0px'},300);
			}
			else
			{
					var newPosition = positionTimeLine + incremenTation;
					$('#timeline_week .timeline_days').animate({ left: newPosition+'px'},300);
			}
			
			var newNavPrevNum = parseInt(NavPrevNum) - parseInt(1);
			var newNavNextNum = parseInt(NavNextNum) - parseInt(1);
		}
		
		if (whatDirection == 'next')
		{
			var newPosition = positionTimeLine - incremenTation;
			$('#timeline_week .timeline_days').animate({ left: newPosition+'px'},300);
			
			var newNavPrevNum = parseInt(NavPrevNum) + parseInt(1);
			var newNavNextNum = parseInt(NavNextNum) + parseInt(1);
		}
		
		$('.timeline_nav#prev').attr('rel',newNavPrevNum);
		$('.timeline_nav#next').attr('rel',newNavNextNum);
	}
	
	// timeline
	if ($('ul.timeline_days').length)
	{
		// hightlight the current day
		var idTodayIs = $('ul.timeline_days').attr('id'); 
		var splitBegin = idTodayIs.split('_');
		var idCurrentTimeLine = splitBegin[1]; // timestamp original
			
		$('ul.timeline_days li#day_'+idCurrentTimeLine).addClass('current');
		
		// Timeline, slider horizontal
		$('#timeline_vazy a.timeline_nav').click(function(){
				
			var whatDirection = $(this).attr('id'); // prev or next
			var NavPrevNum = $('.timeline_nav#prev').attr('rel');
			var NavNextNum = $('.timeline_nav#next').attr('rel');
			var hauteurTimeLine = $('#timeline_week .timeline_days > li:first').height();
			
			var NavNum = $(this).attr('rel'); // niveau à afficher (1 = 5 jours suivants, -1 = 5 jours précédents)
			var idTodayIs = $('ul.timeline_days').attr('id'); // timestamp original
			var splitBegin = idTodayIs.split('_');
			var BeginWhere = splitBegin[1];
			
			var timeStampBegin = parseInt(BeginWhere) + NavNum*5*24*3600; // timestamp du 1er jour à afficher
			
			// récupération en ajax des évènements s'ils ne sont pas déjà chargés
			if (!$('li#day_'+timeStampBegin).length)
			{
				// on cache les flèches de navigations et on affiche les loading
				$('#timeline_vazy a.timeline_nav').hide();
				$('#timeline_vazy .loading').show();
			
				$.ajax({
				   type: "POST",
				   url: domain+"/do/timeline.php",
				   data: "timestamp=" + timeStampBegin,
				   success: function(msg){
					 if (whatDirection == 'next') { $('ul.timeline_days').append(msg); }
					 if (whatDirection == 'prev') {	$('ul.timeline_days').prepend(msg);}
					 animateTimeLine(whatDirection,NavPrevNum,NavNextNum,1);
					 timeLineTooltip();
				   }
				});
			}
			else
			{	
				animateTimeLine(whatDirection,NavPrevNum,NavNextNum,0);
			}
			
						
			$("#timeline_vazy a.timeline_nav").ajaxStop(function(){
			 	$('#timeline_vazy a.timeline_nav').show();
				$('#timeline_vazy .loading').hide();
				var hauteurTimeLine2 = 0;
				
				// Fonction debug pour adapter la hauteur de la timeline en mode OPEN
				if ($('#timeline_derouler a').hasClass('open'))
				{
					// On checke la hauteur des nouveaux li
					$('#timeline_week .timeline_days li.bloc_'+timeStampBegin).each(function() 
					{
						var hauteurLi = $(this).height(); 
						if (hauteurLi > hauteurTimeLine2) { hauteurTimeLine2 = hauteurLi; }
					});			
					// alert(hauteurTimeLine2);	
					if (hauteurTimeLine2 > hauteurTimeLine)
					{ hauteurTimeLine = hauteurTimeLine2 }
						
					$('#timeline_week .timeline_days > li').css('height', hauteurTimeLine+'px');
					$('#timeline_week').animate({ height: hauteurTimeLine+'px' },200);
				}
			});
			
			
			
			return false;
		});
	}
	
	
	// Galerie photos
	if ($('.galerie').length)
	{
		$(".galerie a.photo").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	true,
			'cyclic'		: 	true,
			'titlePosition'	:	'inside'
		});
	}
	
	
	// Homepage menu_semaine : aujourd'hui / demain / ce week-end (jQuery Ui Tabs)
	$( "#menu_semaine" ).tabs({ fx: { opacity: 'toggle' } });
	
	var $tabs = $('#menu_semaine').tabs({
    add: function(event, ui) {
        $tabs.tabs('select', '#' + ui.panel.id);
    }
});

	// Google maps
	if ($('#googlemaps').length)
	{
		makeGoogleMap('googlemaps',13);
	}

													  
});


$(window).load(function(){ //on attend que la page soit chargée	

});
