﻿
var shost = "http://www.hotel-imperial-palace.com";
var sracine = "/";
var spath = shost + sracine;

var message = "";
var docXML;			// document XML chargé
var docText;		// document texte chargé
var eIMG;			// image  préchargée
var total;
// les éléments à gérer
var ePush;
var sPush;
var span;
var titre;
var img ;
var linkText;
var linkImg;
var link;

// les valeurs à récupérer dans le XML
var imgEvent;		// url de l'image
var idEvent;		// l'id courant
var titleEvent;		// le titre à afficher
var titleUrlEvent;	// le titre à afficher
var textEvent;		// le texte à afficher
var imgActu;		// titre image actuellement/prochainement
var currentDate;	// date courante
var dateDebutEvent;	// date de début de l'évènement
var dateFinEvent;	// date de fin de l'évènement

var last     = 0; 		// id du dernier évènement affiché
var type     = "";		// type de slideshow
var inter    = 5000;	// Intervalle entre changement 5s

var durationIn  = 1500; 					// durée du fadeIn
var durationOut = 1000; 					// durée du fadeOut
var fxFunction  = Fx.Transitions.linear;	// transition
var oFadeIn = {
			duration   : durationIn,
			wait       : false,
			transition : fxFunction
			};
var oFadeOut = {
			duration   : durationOut,
			wait       : false,
			transition : fxFunction
			};


window.addEvent('domready', getAlauneEvent);

function getAlauneEvent (){
	var sfile = spath + "alaune2.php";
	var sMeth = "post";
	var sEncoding = "utf-8";
	
	var request = new Request({
							method    : sMeth,
							url       : sfile,
							encoding  : sEncoding,
							onSuccess : onSuccessHandler,
							onFailure : onFailureHandler
							});
	if (currentPage == "rubrique"){
		request.send('type=actu&idlieu='+idLieu);
		type  = "actu";
		sPush = "index-push";
	}
	else if (currentPage == "homepage"){
		request.send('type=hp&idlieu=0');
		type  = "hp";
		sPush = "accueil-actualites";
	}
	else{
		request.send('type=actu&idlieu=0');
		type  = "actu";
		sPush = "index-push";
	}
}
function onSuccessHandler (responseText, responseXML){
	docXML  = responseXML;
	docText = responseText;
	doSildeShow.delay(5000);
}
function onFailureHandler (xhr){
	//alert("HTTP REQUEST FAILED...");
}
function doSildeShow ()
{
	ePush    = $(sPush);
	span     = $('accroche-event');
	img      = $('img-event');
	linkText = $('link-text');
	linkImg  = $('link-img');
	imgActu  = $('img-actu');
	if (type == "actu") titre = $('titre-event');
	
	var ligneXML  = docXML.getElementsByTagName("evenements");
	var lignesXML = docXML.getElementsByTagName("evenement");
	
	if (lignesXML.length == 0){
		return;
		//alert('XML vide !!!');
	}
	if (ligneXML.length > 0){
		currentDate = ligneXML[0].getAttribute("currentDate");
	}
	total = lignesXML.length;
	var id = 0;
	// si il y a plus d'un évènement
	if (total > 1){
		id = last + 1;
		if (id >= total){
			id = 0;
		}
		last = id;
	}
	else {
		var id = 0;
	}
	// Je récupère les valeurs du XML
	if (total != 0)
	{
		for (i = 0; i < lignesXML[id].childNodes.length; i++)
		{
			if (lignesXML[id].childNodes[i].nodeType == 1)
			{
				var nodeName = lignesXML[id].childNodes[i].nodeName;
				if (nodeName == "titre"){
					titleEvent = lignesXML[id].childNodes[i].firstChild.nodeValue;
				}
				else if (nodeName == "accroche"){
					textEvent = lignesXML[id].childNodes[i].firstChild.nodeValue;
				}
			}
		}
		idEvent        = lignesXML[id].getAttribute("id");
		titleUrlEvent  = lignesXML[id].getAttribute("titleurl");
		imgEvent       = shost + lignesXML[id].getAttribute("url");
		
		dateDebutEvent = lignesXML[id].getAttribute("dateDebut");
		dateFinEvent   = lignesXML[id].getAttribute("dateFin");

		// je précharge l'image
		eIMG = new Asset.image(imgEvent, {
									id      : 'visuel',
									title   : titleEvent,
									onload  : fadeOutEvent,
									onerror : onErrorHandler
									});
	}
}
function onErrorHandler(){
	// je relance en cas d'erreur
	doSildeShow();
}
// losque l'image est chargée
function fadeOutEvent(){
	// je cache
	var fxOutSpan     = new Fx.Morph(span, oFadeOut);
	var fxOutImg      = new Fx.Morph(img, oFadeOut);
	var fxOutLink     = new Fx.Morph(linkText, oFadeOut);
	var fxOutTitreImg = new Fx.Morph(imgActu, oFadeOut);
	if (type == "actu") var fxOutTitre = new Fx.Morph(titre, oFadeOut);

	// à la fin du fadeOut
	fxOutSpan.addEvent ("complete", changeImage);
	// je lance les fade out
	fxOutSpan.start({'opacity': 0});
	fxOutImg.start({'opacity': 0});
	fxOutLink.start({'opacity': 0});
	fxOutTitreImg.start({'opacity': 0});
	if (type == "actu")	{
		fxOutTitre.start({'opacity': 0});
	}
	// j'assigne un délai avant le prochain changement
	if (total > 1) {
		doSildeShow.delay (inter);
	}
}
function fadeInEvent (){
	var fxInSpan     = new Fx.Morph(span, oFadeIn);
	var fxInImg      = new Fx.Morph(img, oFadeIn);
	var fxInLink     = new Fx.Morph(linkText, oFadeIn);
	var fxInTitreImg = new Fx.Morph(imgActu, oFadeIn);
	if (type == "actu") var fxInTitre = new Fx.Morph(titre, oFadeIn);
	
	// je lance les fade in
	fxInSpan.start({'opacity': 1});
	fxInImg.start({'opacity': 1});
	fxInLink.start({'opacity': 1});
	fxInTitreImg.start({'opacity': 1});
	if (type == "actu")	{
		fxInTitre.start({'opacity': 1});
	}
}
function changeImage (){
	// j'assigne les contenus
	link = spath + lang + '/evenements/' + idEvent + '/' + titleUrlEvent + '.html';
	
	ePush.addEvent ("click", function(){window.location.href = link;});
	linkImg.setProperty ('href', link);
	linkText.setProperty ('href', link);
	span.set('text', textEvent);
	img.setProperty ('src', eIMG.src);
	if (type == "actu") titre.set('text', titleEvent);
	
	var dateDebut = _getDateFromString(dateDebutEvent);
	var dateCourante = _getDateFromString(currentDate);
	
	// je change le titre image
	// en fonction de la date
	if (dateDebut <= dateCourante){
		imgActu.setProperty ('src', spath+'/templates/'+lang+'/media/pages/titre-actuellement.gif');
	}
	else{
		imgActu.setProperty ('src', spath+'/templates/'+lang+'/media/pages/titre-prochainement.gif');
	}
	// j'assigne un délai avant de réafficher
	fadeInEvent.delay(500);
}