////////////////////////////
//                        //
//        SCREEN          //
//                        //
////////////////////////////

// Fonction pour avoir la taille des scroll de la page.
function getPageScroll(){
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop){ // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) { // all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;   
    }
    arrayPageScroll = new Array(xScroll,yScroll)
    return arrayPageScroll;
}

// Fonction qui retourne les tailles de la page.
function getPageSize(){
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {   
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }   
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){   
        pageWidth = xScroll;       
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
    return arrayPageSize;
}

// Fonction pour savoir si le navigateur est IE.
function detectionIE(){
	if (navigator.appName == 'Microsoft Internet Explorer') return true;
	else return false;
}


////////////////////////////
//                        //
//      FORMULAIRES       //
//                        //
////////////////////////////

// Fonction pour indiquer ce qu'il faut faire dans le champ de recherche de texte.
function onText(champ, text){
	if(champ.value == text){
		champ.value = '';
	}
}
function outText(champ, text){
	if(champ.value == ''){
		champ.value = text;
	}
}

// Fonction test de champ vide.
function testVide(champ){
	if (champ.value.length == 0){
		champ.focus();
		highlightChamp(champ); // on met le champ en avant.
		return 'non';
    }
    if (champ.value.length > 0){
        var tout_espaces='oui';
    	for (var i = 0; i < champ.value.length; i++){
        	if (champ.value.substring(i, i + 1) != ' '){ tout_espaces='non'; }
      	}
      	if (tout_espaces=='oui'){
			champ.focus();
			highlightChamp(champ); // on met le champ en avant.
			return 'non';
      	}
    } 
	shadowChamp(champ); // on remet le champ par défaut.
	return 'ok';
}

// Fonction de vérification de saisie d'email au bon format.
function verifMail(email){
	if (email.value != ''){
		var reg_mail = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
		if (!(reg_mail.exec(email.value)!=null)){
			email.focus(); // on selectionne le champ concerné.
			highlightChamp(email); // on met le champ en avant.
			return 'non';
		}
		else {
			shadowChamp(email); // on remet le champ par défaut.
			return 'ok';
		}
	}
	else {
		email.focus(); // on selectionne le champ concerné.
		highlightChamp(email); // on met le champ en avant.
		return 'non';
	}
}

// Fonction pour mettre en surbrillance un champ quand il n'est pas rempli.
function highlightChamp(champ){
	if (champ.tagName.toLowerCase() == 'select'){
		champ.className = 'selectOn';
	} else {
		champ.className = champ.type.toLowerCase()+'On';	
	}
}

// Fonction pour enlever la surbrillance a un champ.
function shadowChamp(champ){
	if (champ.tagName.toLowerCase() == 'select'){
		champ.className = 'select';
	} else {
		champ.className = champ.type.toLowerCase();	
	}
}

// Verification question.
function verifAmi(form){
	if (testVide(form.EmailE) != 'ok') {return 'non'}
	if (verifMail(form.EmailE) != 'ok') {return 'non'}
	if (testVide(form.EmailD) != 'ok') {return 'non'}
	if (verifMail(form.EmailD) != 'ok') {return 'non'}
	return 'ok';
} 
function valAmi(){
	if (verifAmi(document.ami)=='ok') {
		document.ami.submit();
	}
}


////////////////////////////
//                        //
//          NAV           //
//                        //
////////////////////////////

// Fonction pour montrer une sous nav.
function montreNav(id){
	cacheToutNav();
	resetClock();
	var d = document.getElementById('snav_'+id);
	if (d){
		d.style.display = 'block';
	}
}

// Fonction pour cacher une sous nav.
function cacheNav(id){
	var d = document.getElementById(id);
	if (d){
		d.style.display = 'none';
	}
}

// Fonction pour cacher toutes les sous nav.
function cacheToutNav(){
	var nav = document.getElementById('nav');
	var objBody = nav.getElementsByTagName("*");
	for (i=0; i<objBody.length; i++){
		if (objBody[i].id.substring(0,5) == 'snav_'){
			cacheNav(objBody[i].id);
		}
	}
}

// Fonction de timer pour tout cacher ou remettre le timer a 0.
var clockHide;
function hideAll(){
	if (clockHide) resetClock();
	clockHide = setTimeout("cacheToutNav()",500);
}
function resetClock(){
	if (clockHide){
		clearTimeout(clockHide);
		clockHide = null;
	}
}


////////////////////////////
//                        //
//        IMAGES          //
//                        //
////////////////////////////

// Fonction pour savoir sur quelle image on se trouve.
function mediaSelect(id){
	val = id.split('_');
	allOff(); // on passe tout en off.
	selectBtn(val[0],val[1]); // on passe le numero en On.
	afficheBlock('illus',val[1]); // on fait apparaitre l'illustration.
	// on garde en memoire l'image selectionnée.
	obj = document.getElementById('remote');
	obj.index = parseInt(di[val[1]]);
}

// Fonction pour lancer le diaporama.
function playDiapo(){
	btn = document.getElementById('diapo');
	btn.className = 'diaOn'; // on passe le bouton sur On.
	btn.title = 'arrêter le diaporama'; // on change le titre du bouton.
	btn.onclick = pauseDiapo; // on change l'action sur le bouton pour une pause.
	runDiapo(); // on lance l'animation.
}

// Fonction pour mettre en pause le diaporama.
function pauseDiapo(){
	btn = document.getElementById('diapo');
	btn.className = 'diaOff'; // on passe le bouton sur Off.
	btn.title = 'lancer le diaporama'; // on change le titre du bouton.
	btn.onclick = playDiapo; // on change l'action sur le bouton pour une lecture.
	obj = document.getElementById('remote');
	clearTimeout(obj.timeout); // on arrete le timeout.
	obj.timeout = null;
}

// Fonction de lancement du fondu.
var newOp = 0;
function runFondu(pref,id){
	var incr = 0.05;
	newOp = parseFloat(newOp) + parseFloat(incr);
	if (newOp > 1){ newOp = 1; clearTimeout(timer); } else { newOp = newOp; }
	fondu(pref,id);
	if (newOp < 1){ var timer = setTimeout("runFondu('"+pref+"',"+id+")", 50); }
}

// Fonction de fondu.
function fondu(pref,id){
	var elmt = document.getElementById(pref+'_'+id);
	if (elmt){
		if (detectionIE() == true){ elmt.style.filter = 'alpha(opacity:'+(newOp*100)+')'; }
		else { elmt.style.opacity = newOp; }
	}
}

// Fonction d'animation du diaporama.
function runDiapo(){
	obj = document.getElementById('remote');
	obj.index += 1;
	if (obj.index > (obj.lengthDiapo-1)){ obj.index = 0; }
	var numId = id[obj.index];
	allOff(); // on passe tout en off.
	selectBtn('num',numId); // on passe le numero en On.
	newOp = 0; // initialisation de l'opacité.
	if (detectionIE() == true){ var pref = 'media'; }
	else { var pref = 'illus'; }
	fondu(pref,numId); // on met l'opacité a 0.
	afficheBlock('illus',numId); // on fait apparaitre l'illustration.
	runFondu(pref,numId); // on lance la gestion du fondu.
	obj.timeout = setTimeout("runDiapo()", (obj.transPause*1000)); // timer entre les illustrations.
}

// Fonction pour tout cacher et tout mettre en Off.
function allOff(){
	var remote = document.getElementById('remote');
	var elmt = remote.getElementsByTagName("*");
	for (i=0; i<elmt.length; i++){
		if (elmt[i].id.substring(0,4) == 'num_'){
			elmt[i].className = 'numOff';
		}
		if (elmt[i].id.substring(0,4) == 'vid_'){
			elmt[i].className = 'vidOff';
		}
		if (elmt[i].id.substring(0,4) == 'pan_'){
			elmt[i].className = 'panOff';
		}
		if (elmt[i].id.substring(0,6) == 'illus_'){
			elmt[i].style.display = 'none';
		}
	}
}

// Fonction pour selectionner un numéro ou un extra.
function selectBtn(pref,id){
	var elmt = document.getElementById(pref+'_'+id);
	if (elmt){
		elmt.className = pref+'On';
	}
}

// Fonction pour déselectionner un numéro ou un extra.
function deselectBtn(pref,id){
	var elmt = document.getElementById(pref+'_'+id);
	if (elmt){
		elmt.className = pref+'Off';
	}
	document.getElementById(pref+'_'+id).className = pref+'Off';
}

// Fonction pour faire apparaitre une illustration.
function afficheBlock(pref,id){
	var elmt = document.getElementById(pref+'_'+id);
	if (elmt){
		elmt.style.display = 'block';
	}
}

// Fonction pour faire disparaitre une illustration.
function cacheBlock(pref,id){
	var elmt = document.getElementById(pref+'_'+id);
	if (elmt){
		elmt.style.display = 'none';
	}
}

// Ajout d'évènement window et fonction d'initialisation.
addEvent(window, 'load', initialise);
function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	}
	else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	else {
		return false;
	}
}

function initialise(){
	obj = document.getElementById('remote');
	obj.transPause = 5;
	obj.lengthDiapo = media['image'].length;
	// taille de l'affichage et donc hauteur max de l'image.
	var arrayPageSize = getPageSize();
	var haut = arrayPageSize[3] - 255;
	// initialisation des images.
	mediaSelect('num_'+id[0]); // on selectionne par défaut la premiere image.
	for (i=0; i<media['image'].length; i++){
		var numId = id[i];
		var elmt = document.getElementById('media_'+numId);
		if (elmt){
			elmt.src = "generethumb.php?thumb=documents/"+media['image'][i]+"&haut="+haut+"&qual=80";
		}
	}
	// initialisation des videos.
	for (; i<media['video'].length; i++){
		var numId = 'media_'+id[i];
		var elmt = document.getElementById(numId);
		if (elmt){
			var flashvars = false;
			var params = {menu: "false", flashvars: "file=documents/"+media['video'][i], allowfullscreen: "true", allowscriptaccess: "always", wmode: "transparent"}; // , quality: "high", wmode: "transparent"
			var attributes = {};
			swfobject.embedSWF("player.swf", numId, (haut*1.25), haut, "9.0.0", "expressInstall.swf", flashvars, params, attributes);
		}
	}
	// initialisation des panorama 360.
	/*for (; i<media['panorama'].length; i++){
		
	}*/
	// initialisation de la carte des couvertures.
	for (; i<media['plan'].length; i++){
		var numId = 'media_'+id[i];
		var elmt = document.getElementById(numId);
		if (elmt){
			var zoom = 6;
			if (haut < 300){ zoom = 5; }
			else if (haut > 780){ zoom = 7; }
			elmt.innerHTML = "<iframe width=\""+(haut*1.5)+"\" height=\""+haut+"\" frameborder=\"0\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\"http://maps.google.com/maps/ms?ie=UTF8&amp;hl=fr&amp;oe=UTF8&amp;start=-191&amp;num=200&amp;t=p&amp;s=AARTsJovCxn3sEiP5zF4rkJKf4gt-64HSg&amp;msa=0&amp;msid=104375427385331384443.000463443f4ec08403cbe&amp;ll=48.980217,2.834473&amp;spn=7.212056,19.775391&amp;z="+zoom+"&amp;output=embed\"></iframe>";
		}
	}
}

