/*
 * jQuery Timer Plugin
 * http://www.evanbot.com/article/jquery-timer-plugin/23
 *
 * @version      1.0
 * @copyright    2009 Evan Byrne (http://www.evanbot.com)
 */ 

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	if(typeof(a) != 'undefined')
		clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};


(function($) {
	$.fn.slideShow = function(settings) {
		settings = jQuery.extend({
			fadeTime:		1000,
			fadeDelay:		3000,
			slideShowGo:''
		},settings);
		
		var jQueryMatchedObj = this;
		pics = $(this).data('diaporama');
		dossier = $(this).data('dossier');
		
		looped = false;
		if(pics.length > 1 )
		{
			current = 0;
			$(this).append('<img class="active" src="' + dossier + '/' + pics[0] + '" alt="" />');
			_showNext();
		}
			
			
		function _showNext()
		{
			$.clearTimer(settings.slideShowGo);
			
			settings.slideShowGo = $.timer(settings.fadeDelay,function(){
				var actuelle = jQueryMatchedObj.children('img.active');
				current++;
				if(current >= pics.length)
				{
					looped = true;
					current = 0;
					
				}
				var data = pics[current];
				jQueryMatchedObj.children('img').removeClass('activeFin');
				$(actuelle).removeClass('active').addClass('activeFin');
				
				//Image deja chargée ?
				if(!looped)
				{
					$('<img class="active" src="' + dossier+'/'+data + '" alt="" style="display:none;" />').insertAfter(actuelle).load(function () {
						$(this).fadeTo(settings.fadeTime,1);
					});
				}else{
					//Image existe deja
					if($(actuelle).next('img').length==0)
						var anime = jQueryMatchedObj.children('img:first').first();
					else
						var anime = $(actuelle).next('img');
					$(anime).hide().removeClass('activeFin').addClass('active').fadeTo(settings.fadeTime,1);
				}
				_showNext();
			});
		}
	}
})(jQuery);



current_photo = 'p1';
loading = false;
slideShowGo = '';
fadeDelay = 5000;
killSlide = false;

$(document).ready(function() {

	$('#lieux a').click(function(event){
		killSlide = true;
		loadPage(this, true);
		event.preventDefault();
		return false;			
	});	
	if($('#lieux a').length>0)
		SlideIntro();
	
	$('#menu2').hover(	
		function () {
			if(!killSlide)
				$.clearTimer(slideShowGo);
		},
		function () {
			if(!killSlide)
				SlideIntro();
		}
	);
	
});

function SlideIntro()
{
	$.clearTimer(slideShowGo);			
	slideShowGo = $.timer(fadeDelay,function(){
			
		var tgt = $('#lieux a.menuactive').parent().next('li').find('a');
		if(tgt.length==0)		
			var tgt = $('#lieux a').first();
		loadPage(tgt, false);
		SlideIntro();
	});
}

function showImage(img_url)
{
	if(img_url!='' && typeof(img_url)!='undefined')
	{
		//Load photo
		if(current_photo == 'p1')
			do_photo = 'p2';
		else
			do_photo = 'p1';
		
		$('#'+current_photo).css('z-index', 1);
		$('#'+do_photo)
			.hide()
			.css('z-index', 2)
			.html('<img src="'+img_url+'" alt="" />');
	
		$('#'+do_photo + ' > img').load(function () {
			$('#'+do_photo).fadeIn('slow');
		});
		current_photo = do_photo;						
	}
}

function loadPage(id_link, show_text)
{
	if(!loading)
	{
		loading = true;
		$(id_link).addClass('menuactive').parent().siblings().find('a').removeClass('menuactive');
		
		lieu = $(id_link).attr('id');
		$.ajax({
			url: $(id_link).attr('href'),
			data: {print: true},
			success: function(data) {
				loading = false;
				data = data.split('/////');					
				if(show_text)
				{
					$('#c1').stop().fadeOut('fast', function() 
					{
						$('#c1')
							.hide()
							.html('<div class="bg_lieu" style="background-image:url(imgs/' + lieu + '.png);">'+data[0]+'</div>')
							.slideDown('slow');										
					});	
				}					
				showImage(data[1]);				
				return false;
			}
		});
	}
}

