/* === BASOH Scripts === */

// Fonction Menu BASOH
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// Fonction affichage panel thumbs
function HideAndShowPanel(layerId1,layerId2,styleId,btnId) {
document.getElementById(layerId1).style.display = 'none';
	if (document.getElementById(styleId).className == 'pageOff') {
		document.getElementById(styleId).className = 'pageOn';
		document.getElementById(btnId).className = 'pageOff';
		}
	else {
		document.getElementById(styleId).className = 'pageOff';
		document.getElementById(btnId).className = 'pageOn';
	}
document.getElementById(layerId2).style.display = 'block';
}

// Fonction Preload images
function preloadImage()
{
	if (document.images) {
		tabImages = new Array;
		for (var i=0; i<preloadImage.arguments.length; i++) {
			tabImages[i] = new Image();
			tabImages[i].src = preloadImage.arguments[i];
			//alert(tabImages[i].src);
		}
		document.getElementById('loader').style.visibility = 'hidden';
	}
}
var tabImages = new Array;

// Fonction initShowPic
var photoIndex = 1;	// Variable globale correspondant à la mini en cours
function initShowPic(){
	document.getElementById('photo-' + photoIndex).className = 'thumbOn';	// Met la 1ere mini sur ON
	document.getElementById('prevBtn').style.visibility = 'hidden';	// on cache le btn prev
	
	// Detection du type (H, V, C) de l'image src
	var type = document.getElementById('pict-' + photoIndex).childNodes[0].nodeValue;
	//var position = typePict.length;
	//var type = typePict.substr((position-1),1);
	//document.getElementById('test').innerHTML = type;
	
	// Initialisation à 'none'
	document.getElementById('horizontalPicture').style.display = 'none';
	document.getElementById('squarePicture').style.display = 'none';
	document.getElementById('verticalPicture').style.display = 'none';
	
	if (type) {	// On detecte le type d'image
		if (type == 'H') {
			document.getElementById('horizontalPicture').style.display = 'block';
		}
		else if (type == 'C') {
			document.getElementById('squarePicture').style.display = 'block';
		}
		else if (type == 'V') {
			document.getElementById('verticalPicture').style.display = 'block';
		}
	}
	else {	// par défaut
		document.getElementById('horizontalPicture').style.display = 'block';
	}
	showPic(document.getElementById('photo-1'),'1',type);	// On affiche la 1ère photo
}

// Fonction ShowPic
function showPic (whichpic,index,type) {
	
	if (document.getElementById) {	
		// Initialisation à 'none'
		document.getElementById('horizontalPicture').style.display = 'none';
		document.getElementById('squarePicture').style.display = 'none';
		document.getElementById('verticalPicture').style.display = 'none';
		
		if (type) {	// On detecte le type d'image
			if (type == 'H') {
				document.getElementById('horizontalPicture').style.display = 'block';
			}
			else if (type == 'C') {
				document.getElementById('squarePicture').style.display = 'block';
			}
			else if (type == 'V') {
				document.getElementById('verticalPicture').style.display = 'block';
			}
		}
		else {	// par défaut
			document.getElementById('horizontalPicture').style.display = 'block';
		}
		document.getElementById('mainPicture-' + type).src = whichpic.href;
		
		// Traitement du lien Lightbox
		var lightBoxUrl = whichpic.href.replace(/basohPhotos/, "lightbox");
		document.getElementById('mainSrc-' + type).href = lightBoxUrl;
		
		//document.getElementById('test').innerHTML = lightBoxUrl;
			
		//if (whichpic.title) {  
		 
		   var titre = document.getElementById('title-' + index).childNodes[0].nodeValue;
		   var para = document.getElementById('text-' + index).childNodes[0].nodeValue;
		   
		   //document.getElementById('mainTitle').childNodes[0].nodeValue = whichpic.title;	// on injecte le contenu de 'title'
		   document.getElementById('mainTitle-' + type).childNodes[0].nodeValue = titre;	// on injecte le contenu du <h4>
		   document.getElementById('mainText-' + type).childNodes[0].nodeValue = para;	// on injecte le contenu du <p>
		   document.getElementById('mainSrc-' + type).title = titre;	// Lightbox title
		   document.getElementById('mainPicture-' + type).alt = titre;
	   
		   // Ajoute la bordure sur la mini en cours
		   var gallery = document.getElementById('thumbsList');
		   var links = gallery.getElementsByTagName('a');
		   var linksTotal = links.length;
		   for (k=0; k<links.length; k++) {
				links[k].className = 'thumbOff';	// initialise la bordure
			}
			whichpic.className = 'thumbOn';
			
		   // Mise à jour de la variable globale
		   photoIndex = index;
		   //alert ('photo en cours n°' + photoIndex);
		   
		   // Affichage ou non des boutons prev et next
		   if (photoIndex == 1) {
				document.getElementById('prevBtn').style.visibility = 'hidden';	// on cache le btn prev
			}
		   else if (photoIndex == linksTotal) {
				document.getElementById('nextBtn').style.visibility = 'hidden';	// on cache le btn next
			}
			else {
				document.getElementById('prevBtn').style.visibility = 'visible';	// les 2 boutons sont affichés
				document.getElementById('nextBtn').style.visibility = 'visible';
			}
		//}	
		//else {	// sinon utilise le contenu du texte du lien
		//	document.getElementById('mainTitle-' + type).childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;   
		//}
		//return false;
	} 
	else {
		return true;
	}
}

// Fonction showNext
function showNext() {
	photoIndex++;	// On incremente la variable globale pour afficher la photo suivante
	var localIndex = photoIndex;
	//alert(localIndex);
	var NbMiniPanel = 15;	// Nbre de mini par panel
	
	// Detection du type (H, V, C) de l'image src
	var type = document.getElementById('pict-' + localIndex).childNodes[0].nodeValue;
	//var position = typePict.length;
	//var type = typePict.substr((position-1),1);
	//document.getElementById('test').innerHTML = type;
	
	// Initialisation à 'none'
	document.getElementById('horizontalPicture').style.display = 'none';
	document.getElementById('squarePicture').style.display = 'none';
	document.getElementById('verticalPicture').style.display = 'none';
	
	if (type) {	// On detecte le type d'image
		if (type == 'H') {
			document.getElementById('horizontalPicture').style.display = 'block';
		}
		else if (type == 'C') {
			document.getElementById('squarePicture').style.display = 'block';
		}
		else if (type == 'V') {
			document.getElementById('verticalPicture').style.display = 'block';
		}
	}
	else {	// par défaut
		document.getElementById('horizontalPicture').style.display = 'block';
	}
	
	var gallery = document.getElementById('thumbsList');
	var links = gallery.getElementsByTagName('a');
	var linksTotal = links.length;
	
	if ( (localIndex <= linksTotal) && (localIndex >= 1) ) {
		
		// Afichage photo princ + liens
		document.getElementById('mainPicture-' + type).src = document.getElementById('photo-' + localIndex).href;	// On change l'image principale
		
		// Traitement du lien Lightbox
		var lightboxLink = document.getElementById('photo-' + localIndex).href;
		var lightBoxUrl = lightboxLink.replace(/basohPhotos/, "lightbox");
		document.getElementById('mainSrc-' + type).href = lightBoxUrl;
		
		 var titre = document.getElementById('title-' + localIndex).childNodes[0].nodeValue;
		 var para = document.getElementById('text-' + localIndex).childNodes[0].nodeValue;
	   
		 //document.getElementById('mainTitle').childNodes[0].nodeValue = whichpic.title;	// on injecte le contenu de 'title'
		 
		 document.getElementById('mainTitle-' + type).childNodes[0].nodeValue = titre;	// on injecte le contenu du <h4>
		 document.getElementById('mainText-' + type).childNodes[0].nodeValue = para;	// on injecte le contenu du <p>
		 document.getElementById('mainSrc-' + type).title = titre;	// Lightbox title
		 document.getElementById('mainPicture-' + type).alt = titre;	 
		 
		 // Ajoute la bordure sur la mini en cours	  
		 for (k=0; k<links.length; k++) {
			links[k].className = 'thumbOff';	// initialise la bordure
		  }
		  document.getElementById('photo-' + localIndex).className = 'thumbOn';
		  
		  // Affichage ou non des btns
		  if (localIndex == linksTotal){
				document.getElementById('nextBtn').style.visibility = 'hidden';
				document.getElementById('prevBtn').style.visibility = 'visible';
		  }
		  else {  // On fait apparaitre les 2 boutons
			  document.getElementById('nextBtn').style.visibility = 'visible';	
			  document.getElementById('prevBtn').style.visibility = 'visible';
		  }
		  
		  // Affichage page 1 ou 2
		  if (localIndex > NbMiniPanel) {	// on affiche la page 2
			  	document.getElementById('p1').style.display = 'none';
				document.getElementById('link2').className = 'pageOn';	// on change le style du bouton
				document.getElementById('link1').className = 'pageOff';					
				document.getElementById('p2').style.display = 'block';			
		  }
		  else {		// on est tj sur la page 1
			 	document.getElementById('p2').style.display = 'none';
				document.getElementById('p1').style.display = 'block';
		  }
		}
	else {
		document.getElementById('nextBtn').style.visibility = 'hidden';	// On cache le bouton
	}	 
}

// Fonction showPrev
function showPrev() {
	photoIndex--;
	var localIndex = photoIndex;
	//alert(localIndex);
	var NbMiniPanel = 15;	// Nbre de mini par panel
	
	// Detection du type (H, V, C) de l'image src
	var type = document.getElementById('pict-' + localIndex).childNodes[0].nodeValue;
	//var position = typePict.length;
	//var type = typePict.substr((position-1),1);
	//document.getElementById('test').innerHTML = type;
	
	// Initialisation à 'none'
	document.getElementById('horizontalPicture').style.display = 'none';
	document.getElementById('squarePicture').style.display = 'none';
	document.getElementById('verticalPicture').style.display = 'none';
	
	if (type) {	// On detecte le type d'image
		if (type == 'H') {
			document.getElementById('horizontalPicture').style.display = 'block';
		}
		else if (type == 'C') {
			document.getElementById('squarePicture').style.display = 'block';
		}
		else if (type == 'V') {
			document.getElementById('verticalPicture').style.display = 'block';
		}
	}
	else {	// par défaut
		document.getElementById('horizontalPicture').style.display = 'block';
	}
	
	var gallery = document.getElementById('thumbsList');
	var links = gallery.getElementsByTagName('a');
	var linksTotal = links.length;	
	
	if ( (localIndex >= 1) && (localIndex <= linksTotal) ){		
		
		// Afichage photo princ + liens
		document.getElementById('mainPicture-' + type).src = document.getElementById('photo-' + localIndex).href;	// On change l'image principale
		
		// Traitement du lien Lightbox
		var lightboxLink = document.getElementById('photo-' + localIndex).href;
		var lightBoxUrl = lightboxLink.replace(/basohPhotos/, "lightbox");
		document.getElementById('mainSrc-' + type).href = lightBoxUrl;
		
		 var titre = document.getElementById('title-' + localIndex).childNodes[0].nodeValue;
		 var para = document.getElementById('text-' + localIndex).childNodes[0].nodeValue;
	   
		 //document.getElementById('mainTitle').childNodes[0].nodeValue = whichpic.title;	// on injecte le contenu de 'title'
		 
		 document.getElementById('mainTitle-' + type).childNodes[0].nodeValue = titre;	// on injecte le contenu du <h4>
		 document.getElementById('mainText-' + type).childNodes[0].nodeValue = para;	// on injecte le contenu du <p>
		 document.getElementById('mainSrc-' + type).title = titre;	// Lightbox title
		 document.getElementById('mainPicture-' + type).alt = titre;	 
		 
		 // Ajoute la bordure sur la mini en cours	  
		  for (k=0; k<links.length; k++) {
			links[k].className = 'thumbOff';	// initialise la bordure
		  }
		  document.getElementById('photo-' + localIndex).className = 'thumbOn';
		  
		  // Affichage ou non des btns
		  if (localIndex == 1){
				document.getElementById('prevBtn').style.visibility = 'hidden';
				document.getElementById('nextBtn').style.visibility = 'visible';
		  }
		  else {  // On fait apparaitre les 2 boutons
			  document.getElementById('nextBtn').style.visibility = 'visible';	
			  document.getElementById('prevBtn').style.visibility = 'visible';
		  }
		  
		  // Affichage page 1 ou 2
		  if (localIndex > NbMiniPanel) {	// on affiche la page 2
			  //alert(localIndex);
			  	document.getElementById('p1').style.display = 'none';
				document.getElementById('link2').className = 'pageOn';	// on change le style du bouton
				document.getElementById('link1').className = 'pageOff';					
				
				if (document.getElementById('p2')) {	// sinon erreur
					document.getElementById('p2').style.display = 'block';
				}
		  }
		  else { 		// on est tj sur la page 1
		  
			 	document.getElementById('p2').style.display = 'none';
				
				if (document.getElementById('link2')) {	// sinon erreur					
					document.getElementById('link2').className = 'pageOff';	// on change le style du bouton
				}
				
				document.getElementById('link1').className = 'pageOn';					
				document.getElementById('p1').style.display = 'block';
		  }
		}
	else {
		document.getElementById('prevBtn').style.visibility = 'hidden';
	}	 
}

// CONTACT
var nbMax = 600; //nbre max caracteres contenu
//var formName = "basohContactForm";
function verifierLongeur() {
	var txt = document.basohContactForm.contenuMessage.value;
	if (txt.length>nbMax) {		
		document.basohContactForm.contenuMessage.value = txt.substring(0,nbMax);
		txt = document.basohContactForm.contenuMessage.value;
	}
document.basohContactForm.nbCar.value = nbMax-txt.length;
setTimeout("verifierLongeur()",100);
}

function emptySmartInput(obj,string){
	if (obj.value == string){
		obj.value = '';
	}
}

function focusedInput(obj,newclass){
	obj.className = newclass;
}
function bluredInput(obj,newclass){
	obj.className = newclass;
}

function checkContactForm(f) {

	var regMail = new RegExp("^[0-9a-z._-]+@{1}[0-9a-z. -]{2,}[.]{1}[a-z]{2,5}$", "i");     // Verification adresse email
	//var verif = /^[0-9]+$/;     // Verif birthyear -> integer

 if(f.email.value == "") {
   alert("Veuillez entrer votre email");
   f.email.focus();
   return false;
  }
 if(!regMail.test(f.email.value)) {
    alert("Merci de nous indiquer une adresse email valide");
    f.email.focus();
    return false;
   }
   if((f.objet.value == "") || (f.objet.value == "Objet de votre mail")) {
   alert("Veuillez entrer l'objet de votre email");
   f.objet.focus();
   return false;
  }
  else {
	//Hide("submit-btn");
	f.submit();
	f.email.value = '';	
	f.objet.value = '';
	f.contact.value = '';
	f.contenuMessage.value = '';
    //alert("formulaire valide");    
  }
}

function emptyContactForm(f) {
	f.email.value = '';	
	f.objet.value = '';
	f.contact.value = '';
	f.contenuMessage.value = '';
}