/*
	- voir mise à jour des tableaux dans diaporama.inc.php :
	  évaluation javascript dans httprequest.js->htmlobj_onload()
	
	- diaporama_onload -> appel dans htmlobj_onload() : after_onload()
*/
var photo = new Array();
var vignette = new Array();

var diaporama_onload = function(){
	// gestion prototype pour les boutons
	// mise à jour onclick des boutons : affichage des types de vignette
	proto_ie_diaporama();

	// fin de chargement
	hide_waitwhileloading();
	
	// sélection par défaut
	if (getById("main_image_thumb_type")) {
		switch(getById("main_image_thumb_type").value) {
			case "1":
				getById("btInterior").onclick();
				break;
			case "2":
				getById("btPlan").onclick();
				break;
			default:
				getById("btExterior").style.color = "#FF6600";
				break;
		}
	}
	else {
		getById("btExterior").style.color = "#FF6600";
	}
};

/*  ----------------------------------------------------
	affichage du diaporama
*/
var diaporama_opacity_speed = 20;
var diaporama_opacity_offset = 0.1;
	
var show_diaporama = function() {
	var diaporama = getById("diaporama_container");
	/*
		opacité
	*/
	if (isNaN(diaporama.style.opacity))
		setOpacity(diaporama, 0);
	if (diaporama.style.display != "block") {
		/* 
			height = SA : 298px; FF - IE : 311px -> voir style : img.photo 
		*/
		if (nav.safari) {
			//getById("photo_diaporama").style.height = px(298);
			getById("btPlay_diaporama").style.top = px(352);
			getById("label_btPlay_diaporama").style.top = px(354);
			getById("btPlay_diaporama").style.left = px(5);
			getById("diaporama_height").style.height = px(17);
		}
		if (nav.explorer) {
			//getById("photo_diaporama").style.height = px(310);
			getById("btPlay_diaporama").style.top = px(350);
			getById("label_btPlay_diaporama").style.top = px(352);
			getById("btPlay_diaporama").style.left = px(5);
			getById("diaporama_height").style.height = px(16);
		}
		diaporama.style.display = "block";	
	}
	
	var opacity = Number(diaporama.style.opacity) + diaporama_opacity_offset;
	if (opacity < 1) {
		// boucle
		setOpacity(diaporama, opacity);
		setTimeout(show_diaporama, diaporama_opacity_speed);
	}
	else {
		opacity = 1;
		setOpacity(diaporama, opacity);
		show_diaporama.positionned = 0;
	}
	getById("btClose_diaporama").disabled = (opacity != 1);
};

var hide_diaporama = function() {
	var diaporama = getById("diaporama_container");

	if (isNaN(diaporama.style.opacity))
		setOpacity(diaporama, 1);
	if (diaporama.style.display != "block")
		diaporama.style.display = "block";	
	
	// opacité
	var opacity = Number(diaporama.style.opacity) - diaporama_opacity_offset;
	if (opacity > 0) {
		// boucle
		setOpacity(diaporama, opacity);
		setTimeout(hide_diaporama, diaporama_opacity_speed / 2);
	}
	else {
		opacity = 0;
		setOpacity(diaporama, opacity);
		diaporama.style.display = "none";
	}
	getById("btClose_diaporama").disabled = (opacity != 0);
};

/*  ----------------------------------------------------
	affichage du groupe type de photo 
	sur click des boutons extérieur, intérieur, plan
*/
// gestion prototype ie pour les boutons
var proto_ie_diaporama = function() {
	proto_ie(getById("btExterior"));
	getById("btExterior").cvalue = 0;
	getById("btExterior").onclick = show_photo_type;

	proto_ie(getById("btInterior"));
	getById("btInterior").cvalue = 1;
	getById("btInterior").onclick = show_photo_type;

	proto_ie(getById("btPlan"));
	getById("btPlan").cvalue = 2;
	getById("btPlan").onclick = show_photo_type;
};

var show_photo_type = function() {
	// arrêt de l'animation
	if (diaporama_animation_started) getById("btPlay_diaporama").onclick();
	
	var type = this.cvalue;
	// voir animate_diaporama()
	diaporama_source_type = type;

	if (this.blur) this.blur();
	for (i = 0; i < this.parentNode.getElementsByTagName("button").length; i++)
		this.parentNode.getElementsByTagName("button")[i].style.color = (type == i ? "#FF6600" : "");

	for (var i = 0; i < vignette.length; i++) {
		for (var j = 0; j < vignette[i].length; j++) {
			if (i == type) {
				if (j == 0) {
					// première photo par défaut
					change_photo(getById(photo[i][j]).src, getById(photo[i][j]).style.width, getById(photo[i][j]).style.height);
				}
				// les vignettes du type de photo, voir diaporama.css->img.vignette
				getById(vignette[i][j]).style.display = "inline";
			}
			else {
				// les autres vignettes
				getById(vignette[i][j]).style.display = "none";
			}
		}
	}

};

/*  ----------------------------------------------------
	affichage d'une photo sur click d'une vignette
*/
var change_photo = function(new_src, width, height){
	setOpacity(getById("photo_diaporama"), 0.2);
	getById("photo_diaporama").src = new_src;

	getById("photo_diaporama").style.width = width;
	getById("photo_diaporama").style.height = height;
	show_changed_photo();
};

var show_changed_photo = function() {
	var photo_diaporama = getById("photo_diaporama");
	// opacité
	var opacity = Number(photo_diaporama.style.opacity) + diaporama_opacity_offset;
	if (opacity < 1) {
		// boucle
		setOpacity(photo_diaporama, opacity);
		setTimeout(show_changed_photo, diaporama_opacity_speed);
	}
	else {
		opacity = 1;
		setOpacity(photo_diaporama, opacity);
	}
};

/*  ----------------------------------------------------
	animation diaporama
*/
// voir bouton play->onclick()
var diaporama_animation_started = false;

// voir show_photo_type()
var diaporama_source_type = 0;

var animate_diaporama = function(diapo) {
	if (!diaporama_animation_started) return;
	change_photo(getById(photo[diaporama_source_type][diapo]).src, getById(photo[diaporama_source_type][diapo]).style.width, getById(photo[diaporama_source_type][diapo]).style.height);
	diapo = (diapo < (photo[diaporama_source_type].length - 1) ? (diapo + 1) : 0);
	setTimeout(function(){animate_diaporama(diapo)}, 3000);
};


