var timer, t1;
var actif = 1; // Photo active

var col = document.getElementsByTagName('body')[0];
var nb = 12;
//var nb = (window.location.pathname.search('index2') > -1 ? 18 : 12);

//for(var i in col)
//	if(c[i].className == 'piscine_bois')
//		nb++;
//alert('' + ' - ' + col[0]);
var tab = new Array();
var delai = 3; // Délai entre 2 photos en secondes

function shuffle(o){
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}

function fadeout()
{
	var i = document.getElementById(tab[0]);
	i.style.opacity -= 0.035;
	i.style.filter = 'al(opacity=' + (i.style.opacity * 100) + ')';

	if(i.style.opacity < 0 || i.style.opacity < 0)
	{
		clearInterval(t1);
		
		actif++;
		if(actif > nb)
			actif = 1;
		
		clearTimeout(timer);
		timer = setTimeout('sw()', delai * 1000);
	} 
}

function sw() 
{
	for(var j = 0; j < nb; j++)
	{
		tab[j] = 'piscine_bois_' + ( (actif + j) > nb ? (actif + j) - nb : (actif + j) );
		document.getElementById(tab[j]).style.opacity = '1';
		document.getElementById(tab[j]).style.filter = 'alpha(opacity=100)';
		
		if(actif == (j + 1))
		{

			if(document.getElementById(tab[j]).src.match('tarifs'))
			{
				document.getElementById(tab[j]).src = document.getElementById(tab[j]).src;
			}

		}
		
		if(j == 0)
			document.getElementById(tab[j]).style.zIndex = '99';
		else
			document.getElementById(tab[j]).style.zIndex = nb + 10 - j;
	}
//	alert(actif);
	t1 = setInterval('fadeout()', 30);
}


window.onload = function()
{
	if(document.getElementById('piscine_bois_1'))
		timer = setTimeout('sw()', delai * 1000);
}

window.onunload = function()
{
	clearInterval(timer);
}


/* Diaporama en pop-in */
var nb_diaporama = 47; // Nombre d'images du diaporama
var i, j; // i : # image en cours ; j : #image à précharger
var img; // Image à précharger
var w_max = $(window).width() - 80;
var h_max = $(window).height() - 200;
/*
var tmr;
function diaporama_slide()
{
	$('#div-diaporama').parent().show();
	$('#div-diaporama img').fadeOut();
	$('#div-diaporama img').remove();
	
	var tmp_img = new Image();
	tmp_img.src = 'images/diaporama/piscine-' + (i < 10 ? '0' : '') + i + '.jpg';
	
	$('#div-diaporama').append($('<img />').attr('src', 'images/diaporama/piscine-' + (i < 10 ? '0' : '') + i + '.jpg').css('display', 'none'));
	
	// Redim si besoin
	while($('#div-diaporama img').first().width() == 0)
	{
		$('#div-diaporama').parent().hide();
		usleep(100);
	}
	
	var w = $('#div-diaporama img').first().width();
	var h = $('#div-diaporama img').first().height();
	if(w > w_max || h > h_max)
	{
		if((w / h) > (w_max / h_max))
		{
			// on cale la largeur
			$('#div-diaporama img').first().css('width', w_max);
			$('#div-diaporama img').first().css('height', parseInt(h * w_max / w));
		}
		else
		{
			// on cale la hauteur
			$('#div-diaporama img').first().css('height', h_max);
			$('#div-diaporama img').first().css('width', parseInt(w * h_max / h));
		}
	}

	
	if(i == '02' && false)
	{
		alert("w_max : " + w_max + "\nh_max : " + h_max + "\nw : " + w + "\nh : " + h + "\nwidth : " + $('#div-diaporama img').first().css('width') + "\nheight : " + $('#div-diaporama img').first().css('height'));
	}
	// Position
	$('#div-diaporama').parent().css('top', parseInt(($(window).height() - parseInt($('#div-diaporama img').first().css('height'))) / 2));

	$('#div-diaporama').parent().css('left', parseInt(($(window).width() - parseInt($('#div-diaporama img').first().css('width'))) / 2));
	

	$('#div-diaporama img').first().fadeIn();
	
	i = (parseInt(i) + 1);
	if(i > nb_diaporama)
	{
		i = 1;
	}

}

function diaporama_show()
{
	$('embed').hide();
	
	$('#div-diaporama').dialog({
		modal : true,
		resizable : false,
		close : function()
		{
			diaporama_hide();
		}
	});
	
	$('.ui-widget-overlay').css('opacity', 0.9).css('moz-opacity', 0.9).css('text-align', 'center').append($('<img />').attr('src', "extranet/_imgs/loading.gif").css('position', 'relative').css('top', $(window).height() / 2)).click(function() { diaporama_hide()});
	
	$('.ui-dialog, .ui-dialog-titlebar').css('background', 'black').css('border', 'none').css('width', 'auto').css('height', 'auto').css('top', 'auto').css('left', 'auto')
	$('#div-diaporama').css('color', 'white').css('max-width', $(window).width()).css('max-height', $(window).height() - 30).css('height', 'auto');
	
	diaporama_slide();
	tmr = setInterval('diaporama_slide()', 10000);
}

function diaporama_hide()
{
	// clearInterval(tmr);
	$('#div-diaporama').dialog('destroy');
	$('embed').show();
}


function diaporama_pause()
{
	// clearInterval(t)

}
*/

function sleep(delay) 
{
	var q;
	var start = new Date().getTime();
	while (new Date().getTime() < start + delay) { q = 1;}
}

function diaporama_init()
{
	i = 1;
	$('embed').hide();
	if(navigator.appName.substr(0, 18) == 'Microsoft Internet')
		$("#home2").hide();
	$('#div-overlay').show();
	$('#div-diaporama').show();
	img = new Image();
	img.src = "images/diaporama/piscine-02.jpg";
	img.onload = function() { $('#div-diaporama-menu').fadeIn(); };
	diaporama_show();
	$.ajax({
		url:'index-diaporama.php',
		data:'ajax=diaporama&init=1'
	});

}

function diaporama_appear()
{
	$('#div-diaporama-photo img').first().hide();
	var w = $('#div-diaporama img').first().width();
	var h = $('#div-diaporama img').first().height();
	if(w == 0)
		return setTimeout('diaporama_appear()', 500);
	if(w > w_max || h > h_max)
	{
		if((w / h) > (w_max / h_max))
		{
			// on cale la largeur
			$('#div-diaporama img').first().css('width', w_max);
			$('#div-diaporama img').first().css('height', parseInt(h * w_max / w));
		}
		else
		{
			// on cale la hauteur
			$('#div-diaporama img').first().css('height', h_max);
			$('#div-diaporama img').first().css('width', parseInt(w * h_max / h));
		}
	}

	$('#div-diaporama').width($('#div-diaporama-photo img').first().width());
	$('#div-diaporama').height($('#div-diaporama-photo img').first().height()+10);
	$('#div-diaporama').css('top', 25);
	$('#div-diaporama').css('left', parseInt((w_max - $('#div-diaporama').width()) / 2));
	// Le lien "Fermer" se place au bon endroit horizontalement
	$('#a_diaporama_close').css('margin-left', parseInt(($('#div-diaporama').width()) / 2) - 12);
	// Fade-in de la photo
	$('#div-diaporama-photo img').first().fadeIn('slow');
	
	
}

function diaporama_disappear()
{
	$('#div-diaporama-menu').hide();
	$('#div-diaporama-photo img').first().fadeOut('normal', function() { 	$('#div-diaporama-photo img').remove();
	diaporama_show();
 });
}

function diaporama_show()
{
	if($('#div-diaporama-photo img').size() > 0)
	{
		return diaporama_disappear();
	}
	else
	{
		$('#div-diaporama-photo').append($('<img />').attr('src', 'images/diaporama/piscine-' + (i < 10 ? '0' : '') + i + '.jpg'));
		$('#div-diaporama-photo').click(function() { diaporama_next(); });
	// while($('#div-diaporama-photo img').first().width() == 0) { sleep(500);}
	
		diaporama_appear();
	}
}

function diaporama_prev()
{
	i -= 1;
	if(i == 0)
		i = nb_diaporama;
	j = i - 1;
	if(j == 0)
		j = nb_diaporama;
	img = new Image();
	img.src = "images/diaporama/piscine-" + (j < 10 ? '0' : '') + j + ".jpg";
	img.onload = function() { $('#div-diaporama-menu').show(); }
	diaporama_show();
}

function diaporama_next()
{
	i += 1;
	if(i > nb_diaporama)
		i = 1;
	j = i + 1;
	if(j >  nb_diaporama)
		j = 1;
	img = new Image();
	img.src = "images/diaporama/piscine-" + (j < 10 ? '0' : '') + j + ".jpg";
	img.onload = function() { $('#div-diaporama-menu').show(); }
	diaporama_show();
}

function diaporama_hide()
{
	$('#div-diaporama-photo img').remove();
	$('#div-overlay').hide();
	$('#div-diaporama').hide();
	$('embed').show();
	if(navigator.appName.substr(0, 18) == 'Microsoft Internet')
		$("#home2").show();
}

function diaporama_partage()
{
	$.ajax({
		url:'index-diaporama.php',
		data:'ajax=diaporama&partage=' + arguments[0]
	});
}

function diaporama_c2a()
{
	$.ajax({
		url:'index-diaporama.php',
		data:'ajax=diaporama&c2a=1'
	});
}

