/* SCRIPTS */

// init parametres slideshow
var myphotos = new Array;
var currentphoto = 1;
var state = 1;

function init(mycontext){
	if(mycontext == "text"){
		fadeIn("topleft",0);
		fadeIn("topright",0);
		fadeIn("text",0);
	} else {
		for(x=1;x<nbphotos+1;x++){
			myphotos[x] = x+".jpg";
		}
		var randomphoto = Math.floor((Math.random()*nbphotos)+1);
		currentphoto = randomphoto;
		fadeIn("topleft",0);
		fadeIn("topright",0);
		fadeIn("image",0);
		autoswitch(randomphoto);
		if(mygallery=="observations" || mygallery=="intimate" || mygallery=="time"){
			fadeIn("textobservations");
			document.getElementById('image').style.width = "800px";
		} else {
			fadeIn("text",0);
			document.getElementById('image').style.height = "560px";
		}
	}
}

function switchphoto(){
	if(state == 1){
		stopautoswitch();
	} else {
		autoswitch(currentphoto++);
	}
}

function showhide(myId){
	if ( document.getElementById(myId).style.display == "" || document.getElementById(myId).style.display == "none" ) { 
		show(myId);
	} else {
		hide(myId);
	}
}

function show(myId){
	document.getElementById(myId).style.display = "block";
}

function hide(myId){
	document.getElementById(myId).style.display = "none";
}

function fadeIn(myDiv,myOpacite) {
	var opacitefinal = 100;
	var vitesse = 1;
	var step = 3;
	var oldOpacite = parseInt(myOpacite);
	var newOpacite = oldOpacite + step;
	
	if (newOpacite < opacitefinal) { 
		var startFade = setTimeout("fadeIn('"+myDiv+"',"+newOpacite+")", vitesse);
		if(navigator.appName != "Microsoft Internet Explorer"){
			document.getElementById(myDiv).style.opacity = newOpacite/100;
		} else {
			document.getElementById(myDiv).style.filter = "alpha(opacity:"+newOpacite+")";
		}
	} else {
		clearTimeout(startFade); 
		if(navigator.appName != "Microsoft Internet Explorer"){
			document.getElementById(myDiv).style.opacity = 1;
		} else {
			document.getElementById(myDiv).style.filter = "alpha(opacity:100)";
		}
	}
}

function fadeOut(myDiv,myOpacite) {
	var opacitefinal = 0;
	var vitesse = 1;
	var step = 1;
	var oldOpacite = parseInt(myOpacite);
	var newOpacite = oldOpacite - step;
	
	if (newOpacite > opacitefinal) { 
		var endFade = setTimeout("fadeOut('"+myDiv+"',"+newOpacite+")", vitesse);
		if(navigator.appName != "Microsoft Internet Explorer"){
			document.getElementById(myDiv).style.opacity = newOpacite/100;
		} else {
			document.getElementById(myDiv).style.filter = "alpha(opacity:"+newOpacite+")";
		}
	} else {
		clearTimeout(endFade); 
		if(navigator.appName != "Microsoft Internet Explorer"){
			document.getElementById(myDiv).style.opacity = 0;
		} else {
			document.getElementById(myDiv).style.filter = "alpha(opacity:0)";
		}
	}
}

var compteurphotos;
function autoswitch(numphoto){
	if(numphoto>nbphotos){ numphoto = 1; }
	document.getElementById("image").innerHTML = "<img src='photos/"+mygallery+"/"+myphotos[numphoto]+"' alt='' id='mainphoto' />";
	preloadphotos(numphoto);
	fadeIn("image",0);
	newnumphoto = numphoto+1;
	compteurphotos = setTimeout("autoswitch("+ newnumphoto +")",5000);
	currentphoto = newnumphoto;
	state = 1;
	document.getElementById("switch").innerHTML = "<a href='javascript:;' onclick=\"switchphoto("+newnumphoto+");\">photos <img src='img/photostop.png' border='0' alt='play photos' /></a>";
}

function stopautoswitch(){
	clearTimeout(compteurphotos);
	state = 0;
	document.getElementById("switch").innerHTML = "<a href='javascript:;' onclick=\"switchphoto("+currentphoto+");\">photos <img src='img/photoplay.png' border='0' alt='stop photos' /></a>";
}

function preloadphotos(numphoto){
	nextphoto = numphoto + 1;
	if(nextphoto > nbphotos){
		nextphoto = 1;
	}
	document.getElementById("preload").innerHTML = "<img src='photos/"+mygallery+"/"+myphotos[nextphoto]+"' />";
}

function showtext(mytext){
	document.getElementById("text").innerHTML = "<a href='javascript:;' onClick=\"fadeOut('text',50);\" class='close'>close | x</a>";
	document.getElementById("text").innerHTML += document.getElementById(mytext).innerHTML;
	show("text");
	fadeIn("text",50);
}

/* formulaire de contact */
function validateForm(){
	var myform = document.formContact;
	var continu = 1;
	
	// controle des champs
	if(myform.fname.value == "")								{ continu=0; }
	if(myform.lname.value == "")								{ continu=0; }
	if(myform.email.value == "")									{ continu=0; }
	if(myform.email.value.indexOf("@",0) == -1)	{ continu=0; }
	if(myform.email.value.indexOf(".",0) == -1)		{ continu=0; }
	if(myform.email.value.indexOf(" ",0) == 1)		{ continu=0; }
	if(myform.message.value.length<50)					{ continu=0; }
	
	// message d'erreur
	if(continu==0){
		alert('Attention: please fill in every mandatory field properly.\n\nThank you for your understanding.\nStephane');
	} else {
		alert('Thank you for your message, I willl answer ASAP.\nStephane');
		myform.submit();
	}
}

