nb_fois = 0;
arreter = 0;


/* 
JQuery 
19 dec 2010
Patrick D.
*/

// variables pour slideshow

// duration between slides
animDuration = 1200;

// display time of each image
durationForEach = 3000;

// pause before animation
pauseBefore = Math.round(durationForEach*0.1); 

// pause after animation
pauseAfter = Math.round(durationForEach*0.9);

// animation duration
translationEach = Math.round(durationForEach*0.8);

waitBeforeChange = 4000;
initialWait = 500;

slideHeight = 460;

function animateSlider() {
	currentImg = $('#slider img:eq('+$('#slider').data('current')+')');
	if (currentImg.height() > slideHeight) {
		diff = (currentImg.height()-slideHeight);
		$('#slider').animate({top: $('#slider').position().top-diff}, translationEach,	function(){ setTimeout('goNext()', pauseAfter); });
	} else {
		setTimeout('goNext()', pauseAfter+durationForEach);
	}
	/*setTimeout('goNext()', pauseAfter+durationForEach);	*/
}
function doneSwitching() {
	currentImg = $('#slider img:eq('+$('#slider').data('current')+')');
	setTimeout("animateSlider()", pauseBefore);
}

function goNext() {
	curr = $('#slider').data('current');
	max = $('#slider').data('max');
	if ((curr+1) == max) {
		next = 0;
	} else {
		next = curr+1;
	}
	nextImg = $('#slider img:eq('+next+')');
	
	if (nextImg.height() < slideHeight) {
		diff = (nextImg.height()-slideHeight)/2;
		nextImgTop = nextImg.position().top+diff;
	} else {
		nextImgTop = nextImg.position().top;
	}
	
	
	slideTo(curr, next, -nextImgTop, doneSwitching);
	$('#slider').data('current', next);
}

function slideTo(currElmIndex, nextElmIndex, pos, callback) {
	var options = {
		duration: animDuration,
		complete: callback
	}
	fadeDuration = Math.round(animDuration*0.5);
	//$("#info_"+currElmIndex).fadeOut(fadeDuration, function(){ $("#info_"+nextElmIndex).fadeIn(fadeDuration) } );
	$("#info_"+currElmIndex).animate({right: -250, opacity: 0}, {duration: fadeDuration, complete: function(){ 
			$("#info_"+currElmIndex).hide();
			$("#info_"+nextElmIndex).css("right", -250).show();
			$("#info_"+nextElmIndex).animate({right: 0, opacity: 1}, {duration: fadeDuration}); 
		} 
	});
	$('#slider').animate({top: pos}, options);
}







$(function(){
	// Form inscription ==========================================
	$("form#inscription").submit(function(){
		if ($('#response').length == 0) {
			$("form#inscription").after('<div id="response"></div>');
		}
		
		// disparait le form
		$("form#inscription").fadeTo(200, 0);		
		// apparait le wait/reponse
		$("#response").fadeTo(1, 0)
				.css('width', $('.box').width() )
				.css('height', $('.box').height() )
				.html("<table><tr><td><img src='/futur-anterieur/images/loader.gif' alt=''/></td></tr></table>")
				.animate({opacity: 1}, 200);
		
		$.ajax({
			url: baseHrefJava+'addnewsletter.php',
			data: {name: $('form#inscription #name').val(), email: $('form#inscription #email').val(), prenom: $('form#inscription #prenom').val()},
			success: function(data){
				if (data == 'true') {
					msg = "<table><tr><td>"+msg_added+"</td></tr></table>";
				} else {
					msg = "<table><tr><td>"+msg_error+" <a id='retry' href='#'>"+msg_retry+"</a>.</td></tr></table>";
				}
				
				$('#response img').fadeTo(200, 0, function(){
					$('#response').fadeTo(1, 0, function(){
						$('#response').html(msg).fadeTo(1000, 1);
						$('#retry').click(function(e){
							e.preventDefault();
							$('#response').fadeTo(200, 0, function(){ 
								$('#response').remove();
								$('form#inscription').fadeTo(200, 1);
							});
						});
					});
				});
				
			},
			dataType: 'text'
		});
		return false;
	});
	$("form#inscription #submit").click(function(e){ 
		e.preventDefault(); 
		$("form#inscription").submit(); 
	})
	$("form#inscription :input").bind('focus', function(){
		if (this.value == this.title) this.value = '';
	});
	$("form#inscription :input").bind('blur', function(){
		if (this.value == '') this.value = this.title;
	});
	
	// Slider
	$('#slider').data('current', 0);
	$('#slider').data('max', $('#slider img').length);
	$('#slider').each(function(){
		//setTimeout('goNext()', initialWait);
		setTimeout("animateSlider()", initialWait);
	});
	
	
	// Patch
	$("#bottom_content").append('<span id="patch"></span>');
	
	// Pour albums photos
	$('#album_navigation').data('current', 'id1');
	
	$("#album_navigation a").click(function(e){
		targetID = $(this).attr('class');
		$('#'+$('#album_navigation').data('current')).hide();
		$('#'+targetID).show();
		$("#imgdesc").text($('#'+targetID+" img").attr('alt'));
		$('#album_navigation').data('current', targetID);
		e.preventDefault();
	});
	
	// Pour les listes produits (met les colonnes a la meme longueur)
	var maxHeight = 0;
	$('.child_long').each(function(i, e){
		if ((i+1)%3 == 0) {
			$(this).addClass('end_row');
		}
		
		if ($(this).height() > maxHeight) {
			maxHeight = $(this).height();	
		}
	});
	$('.child_long').height(maxHeight);
	
	
});	




//function onLoader
function roundNumber(rnum, rlength) {
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber;
}
	
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}
function getVars()
	{
		var info ="";
		if(location.href.indexOf('?Erreur=') != -1)
		{
			var laVar = location.href.substring(location.href.indexOf('?Erreur=') +8, location.href.length);
			
			//alert(laLangue);
			if(laLangue == "fr")
			{
				if(laVar == "login")
				{
					info = "Problème de connexion: mot de passe ou utilisateur incorrect";
				}
				if(laVar == "exp")
				{
					info = "Vous avez tentez d'accèder à l'administration et le temps est écoulé";
				}
				if(laVar == "vide")
				{
					info = "Vous devez entrer votre nom d'utilisateur et mot de passe";
				}
			}
			else
			{
				if(laVar == "login")
				{
					info = "Connexion problem: incorrect password or user";
				}
				if(laVar == "exp")
				{
					info = "You tried to access to the administration section but the time as expired";
				}
				if(laVar == "vide")
				{
					info = "You have to enter your user and password";
				}
			}
		}
		return info;
	}

function pages(adress){
	
		window.open(adress,"_self");	
	}
	function clearAllImgs(totalImages,itemID)
	{
		for (var a=1; a<=totalImages; a++)
			{
				document.getElementById("id"+itemID+a).style.display = 'none';
			}
	}
	function afficherImage(id,totalImages,itemID)
	{
		clearAllImgs(totalImages,itemID);		
		document.getElementById("id"+itemID+id).style.display = 'block';
		/*document.getElementById('img_centre').src = image;
		document.getElementById('img_centre').alt = imageName;
		document.getElementById('img_centre').title = imageName;
		*/
	}
	function changeImg(id,totalImages,itemID)
	{
		clearAllImgs(totalImages,itemID);
		document.getElementById("id"+itemID+id).style.display = 'block';
	};
	function retourneImage()
	{
		/* Retourne le NOM de l'image dans le URL 
		
		if(location.href.indexOf('#') != -1 && location.href.indexOf('&') != -1 && location.href.indexOf('#id') == -1)
		{
		var lesrc = location.href.substring(location.href.indexOf('#') +1, location.href.indexOf('&'));
		var leEt = location.href.substring(location.href.indexOf('&') +1, location.href.length);
		var lesrc = lesrc.toString();

		clearAllImgs(leEt,lesrc);	
		

		var leId = "id"+document.images[lesrc].id;
		document.getElementById(leId).style.display = 'block';
		}
		*/
		/* Retourne le ID de l'image dans le URL */
		if(location.href.indexOf('#id') != -1)
		{
			var leId = location.href.substring(location.href.indexOf('#') +1, location.href.indexOf('&'));
			var itemID = location.href.substring(location.href.indexOf('#') +3, location.href.indexOf('&')-1);
			var leEt = location.href.substring(location.href.indexOf('&') +1, location.href.length);

			clearAllImgs(leEt,itemID);	
			document.getElementById(leId).style.display = 'block';
		}
		else if(location.href.indexOf('photos') != -1)
		{
			//document.getElementById("id1").style.display = 'block';
		}
	}
	function openFormLog(laBase)
	{
		if(document.getElementById('formlog').style.display=='inline')
		{
			document.getElementById('formlog').style.display='none';
		}
		else
		{
			document.getElementById('formlog').style.display='inline';
		}		
		
		var newImg = '';
		if(document.getElementById('imgLog').src==laBase+'images/fleche_bas_right.gif')
		{
			newImg =laBase+'images/fleche_haut_right.gif';
		}
		else
		{
			newImg =laBase+'images/fleche_bas_right.gif';
		}
		document.getElementById('imgLog').src = newImg;
	}
	
	function openDesc(base,id)
	{	
		var lacolor="black";
		var lafleche = id + "_fleche";
		//alert(document.getElementById(lafleche).src);
		if(document.getElementById(id).style.display == 'block')
		{

			document.getElementById('show_hide_'+id).style.backgroundImage="url('"+base+"images/"+lacolor+"_arrow_show.png')";
			document.getElementById(id).style.display = 'none';
		}
		else
		{
			document.getElementById('show_hide_'+id).style.backgroundImage="url('"+base+"images/"+lacolor+"_arrow_hide.png')";
			document.getElementById(id).style.display = 'block';
		}
	}
	function openId(id)
	{	
		if(document.getElementById(id).style.display == 'block')
		{
			document.getElementById(id).style.display = 'none';
		}
		else
		{
			//document.getElementById(lafleche).style.backgroundimage.src='images/fleche_haut.gif';
			document.getElementById(id).style.display = 'block';
		}
	}


	
	function clearAllDivs(totalItems)
	{
		document.getElementById("chaletstart").style.display = 'none';
		for (var a=1; a<=totalItems; a++)
			{				
				document.getElementById("chalet"+a).style.display = 'none';
			}
	}
	function aff_hebergement(id,totalItems)
	{
		clearAllDivs(totalItems);
		document.getElementById(id).style.display = 'inline';
	}
	function hebergement_img(image,alt,num)
	{			
		image_src = base + "gros_img_chalet/" + image + ".jpg";
		document.getElementById('heb_big_img'+num).src = image_src;		
		document.getElementById('heb_big_img'+num).alt = alt;
		document.getElementById('heb_big_img'+num).title = alt;
	}
	function positionsCheck(id,totalresults)
	{
		leReturn = true;
		for (var a=1; a<=totalresults; a++)
		{				
			if(a!=id && document.getElementById("position"+id).value==document.getElementById("position"+a).value)
			{
				//alert(document.getElementById("position"+id).value+"=="+document.getElementById("position"+a).value);
				leReturn=false;
			}
		}
		return leReturn;
	}
	function positions(totalresults)
	{
		leReturn=true;
		//alert(laLangue);
		if(laLangue=="fr")
		{
			positionNum = "Une position n'est pas un chiffre";	
			positionMin = "Une position est plus petit que 1";	
			positionMax = "Une position est trop élevée par rapport à la plus haute position disponible";	
			positionDouble = "Deux positions ou plus sont les mêmes";
		}
		else
		{
			positionNum = "One position is not a number";	
			positionMin = 'A position is under 1';	
			positionMax = 'One of the position is over the maximum available spot';	
			positionDouble = 'Two or more positions are the same';	
		}
		for (var a=1; a<=totalresults; a++)
		{				
			valuePositionA = document.getElementById("position"+a).value;
			if(isNaN(valuePositionA))
			{
				alert(positionNum);	
				leReturn=false;
				break;	
			}
			if(valuePositionA<1)
			{
				alert(positionMin);	
				leReturn=false;
				break;
			}
			if(valuePositionA>totalresults)
			{
				alert(positionMax);	
				leReturn=false;
				break;
			}
			if(positionsCheck(a,totalresults)==false)
			{
				alert(positionDouble);
				leReturn=false;
				break;
			}
		}
		return leReturn;
	}
	function confirmation()
	{
		answer = confirm('Voulez-vous vraiment supprimer?'); if(answer){return true;}else{return false;}	
	}
	
