var ordreGlobal = "asc";
var triGlobal = "date";

var oldDiv = "dateUp";
var oldOrdre = "asc";
var connectivite = false;

function saveNewsletter() {
	var xhr = getXhr();

	var adresse = document.getElementById('adresseNewsletter').value;

	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			if (xhr.responseText == "") alert ("Votre adresse électronique a bien été enregistrée.");
		}
	}

	var params = "adresse=" + adresse;

	xhr.open("GET", "/ajax/fonctions.php?action=saveNewsletter&" + params + "&d="+ (new Date()).getTime(), true);
	xhr.send(null);
}

function getTarifs(residenceId, tri, ordre) {

	var xhr = getXhr();
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			document.getElementById("tarifs").innerHTML = xhr.responseText;

			// Méthode babare : on met toutes les flèches au noir, et on colore la bonne en rouge
			// En fait non.

			if (oldOrdre == "asc") document.getElementById(oldDiv).src = "images/up_ltl.gif";
			else document.getElementById(oldDiv).src = "images/down_ltl.gif";

			if (ordre == "asc") { var tmp = "Up"; var src = "images/up.gif"; }
			else { tmp = "Down"; var src = "images/down.gif"; }
			var imgId = tri + tmp;
			document.getElementById(imgId).src = src;

			oldDiv = imgId;
			oldOrdre = ordre;

			ordreGlobal = ordre;
			triGlobal = tri;

			getPanier();

		}
	}

	var params = "residenceId=" + residenceId + "&ordre=" + ordre + "&tri=" + tri;

	xhr.open("GET", "/ajax/fonctions.php?action=getTarifs&" + params + "&d="+ (new Date()).getTime(), true);
	xhr.send(null);
}

/* DEPLACE DANS js/fonctions.js
function reserve(hebergeId, occupeId, dateId, residenceId, request) {
	var xhr = getXhr();
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			//alert(xhr.responseText);
			getTarifs(residenceId, triGlobal, ordreGlobal);
		}
		document.getElementById("picto_verif_prix").style.visibility = 'hidden';
	}
	document.getElementById("picto_verif_prix").style.visibility = 'visible';
	var params = "occupeId=" + occupeId + "&hebergeId=" + hebergeId + "&dateId=" + dateId + "&request=" + request;

	xhr.open("GET", "/ajax/fonctions.php?action=addPanier&" + params + "&d="+ (new Date()).getTime(), true);
	xhr.send(null);
}
*/

function getRotatif(rotatif, aAfficher, max, maxReel, type) {
	document.getElementById("rotatif_" + rotatif + "_load").style.visibility = "visible";
	var div = "rotatif_" + rotatif;
	var xhr = getXhr();
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			document.getElementById(div).innerHTML = xhr.responseText;
		}
	}

	var params = "rotatif=" + rotatif + "&aAfficher=" + aAfficher + "&max=" + max + "&maxReel=" + maxReel + "&type=" + type;

	xhr.open("GET", "/ajax/fonctions.php?action=getRotatif&" + params + "&d="+ (new Date()).getTime(), true);
	xhr.send(null);
}

function rotatifNext(leRotatif) {
	// Le rotatif
	var rotatif = document.getElementById('rotatif_' + leRotatif + '_content');
	//rotatifActual++;
	eval('var left = rotatif_' + leRotatif + '_left;');
	eval('var max = rotatif_' + leRotatif + '_max;');
	eval('var actual = rotatif_' + leRotatif + '_actual;');
	eval('var width = rotatif_' + leRotatif + '_width;');

	eval('var auto = rotate_' + leRotatif + '_auto;');
	eval('var sens = rotate_' + leRotatif + '_sens;');

	max = max - 2; // Parce qu'on en affiche pas 1 mais 3

	if (max <= 1) return;

	if (auto == false) {
		if (actual == max) {
			rotatifRembobine(leRotatif);
			return;
		}
	} else if (auto == true) {
		if (actual == max) {
			sens = "prev";
			eval('rotate_' + leRotatif + '_sens = sens;');
			rotatifPrev(leRotatif);
			return;
		}
	}

	actual++;
	//alert (actual);
	eval('rotatif_' + leRotatif + '_actual = actual;');

	if (document.getElementById('rotatif_' + leRotatif + '_actual')) document.getElementById('rotatif_' + leRotatif + '_actual').innerHTML = actual;

	var leftFinal = left - width;
	eval('rotatif_' + leRotatif + '_left = leftFinal;');

	rotatif.move = function() {
		if (left > leftFinal) {
			if (left - 30 > leftFinal) left = left - 20;
			else left = left - 5;
			if (left < leftFinal) left = leftFinal;
			rotatif.style.left = left + "px";
			setTimeout(function() {
					rotatif.move();
			} , 10);

		}
	}

	rotatif.move();
}

function rotatifDebobine(leRotatif) {
	// Le rotatif
	var rotatif = document.getElementById('rotatif_' + leRotatif + '_content');
	//rotatifActual++;
	eval('var left = rotatif_' + leRotatif + '_left;');
	eval('var max = rotatif_' + leRotatif + '_max;');
	eval('var actual = rotatif_' + leRotatif + '_actual;');
	eval('var width = rotatif_' + leRotatif + '_width;');

	// On récupère de combien il faut rembobiner
	var largeur = width * (max - 1);

	actual = max;

	eval('rotatif_' + leRotatif + '_actual = actual;');
	if (document.getElementById('rotatif_' + leRotatif + '_actual')) document.getElementById('rotatif_' + leRotatif + '_actual').innerHTML = actual;

	var leftFinal = left - largeur;
	eval('rotatif_' + leRotatif + '_left = leftFinal;');

	rotatif.move = function() {
		if (left > leftFinal) {
			if (left - 30 > leftFinal) left = left - 100;
			else left = left - 20;
			if (left < leftFinal) left = leftFinal;
			rotatif.style.left = left + "px";
			setTimeout(function() {
					rotatif.move();
			} , 10);

		}
	}

	rotatif.move();
}






function rotatifPrev(leRotatif) {
	// Le rotatif
	var rotatif = document.getElementById('rotatif_' + leRotatif + '_content');
	//rotatifActual++;
	eval('var left = rotatif_' + leRotatif + '_left;');
	eval('var max = rotatif_' + leRotatif + '_max;');
	eval('var actual = rotatif_' + leRotatif + '_actual;');
	eval('var width = rotatif_' + leRotatif + '_width;');

	eval('var auto = rotate_' + leRotatif + '_auto;');
	eval('var sens = rotate_' + leRotatif + '_sens;');

	if (max <= 1) return;

	if (auto == false) {
		if (actual == 1) {
			rotatifDebobine(leRotatif);
			return;
		}
	} else if (auto == true) {
		if (actual == 1) {
			sens = "next";
			eval('rotate_' + leRotatif + '_sens = sens;');
			rotatifNext(leRotatif);
			return;
		}
	}

	actual--;

	eval('rotatif_' + leRotatif + '_actual = actual;');
	if (document.getElementById('rotatif_' + leRotatif + '_actual')) document.getElementById('rotatif_' + leRotatif + '_actual').innerHTML = actual;

	var leftFinal = left + width;
	eval('rotatif_' + leRotatif + '_left = leftFinal;');

	rotatif.move = function() {
		if (left < leftFinal) {
			if (left + 30 < leftFinal) left = left + 20;
			else left = left + 5;
			if (left > leftFinal) left = leftFinal;
			rotatif.style.left = left + "px";
			setTimeout(function() {
					rotatif.move();
			} , 10);

		}
	}

	rotatif.move();
}

function rotatifRembobine(leRotatif) {
	// Le rotatif
	var rotatif = document.getElementById('rotatif_' + leRotatif + '_content');
	//rotatifActual++;
	eval('var left = rotatif_' + leRotatif + '_left;');
	eval('var max = rotatif_' + leRotatif + '_max;');
	eval('var actual = rotatif_' + leRotatif + '_actual;');
	eval('var width = rotatif_' + leRotatif + '_width;');

	// On récupère de combien il faut rembobiner
	var largeur = width * (max - 1);

	actual = 1;

	eval('rotatif_' + leRotatif + '_actual = actual;');
	if (document.getElementById('rotatif_' + leRotatif + '_actual')) document.getElementById('rotatif_' + leRotatif + '_actual').innerHTML = actual;

	var leftFinal = left + largeur;

	eval('rotatif_' + leRotatif + '_left = leftFinal;');

	rotatif.move = function() {
		if (left < leftFinal) {
			if (left + 30 < leftFinal) left = left + 100;
			else left = left + 20;
			if (left > leftFinal) left = leftFinal;
			rotatif.style.left = left + "px";
			setTimeout(function() {
					rotatif.move();
			} , 10);

		}
	}
	rotatif.move();
}

function rotate(rotatifId) {
	eval('var auto = rotate_' + rotatifId + '_auto;');
	eval('var sens = rotate_' + rotatifId + '_sens;');
	if (auto == true) {
		if (sens == "next") rotatifNext(rotatifId); else if (sens == "prev") rotatifPrev(rotatifId);
		setTimeout("rotate('" + rotatifId + "')", 5000);
	}
}

function showInfos(hid) {
	var div = "infos_" + hid;
	if (document.getElementById(div).style.display == "none") document.getElementById(div).style.display = "";
	else document.getElementById(div).style.display = "none";
}

function getBodyY(item) {
	var res = 0;
	var e = item;
	while (e) {
		res += e.offsetTop;
		e = e.offsetParent;
	}
	if (typeof document.body.style.maxHeight != "undefined") {
	} else {
		//res -= 172;
	}
	if (connectivite == true) res += 164;

	return res;
}

function scrolldiv(adivid,astartx,astarty,scrollparentid) {
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var d = document;
	var el = d.getElementById ? d.getElementById(adivid) : d.all ? d.all[adivid] : d.layers[adivid]; // el = élément à faire bouger
	if (!el) {
		return; // Pas d'élément à faire bouger trouvé : on s'en va
	}

	if (!scrollparentid) {
		scrollparentid="scrollzone"; // Pas de zone parente spécifiée : on prend l'id "scrollzone"
	}

	el.scrollpId = scrollparentid;
	el.sP = function(x,y){ // Fonction pour replacer l'élément
		this.style.left= x + "px";
		this.style.top= y + "px";
	}

	el.bodyY = function() {
		return getBodyY(this);
	}

	el.scrollParent = function() {
		var res;
		if (this.scrollpId) {
			res = d.getElementById ? d.getElementById(this.scrollpId) : d.all ? d.all[this.scrollpId] : d.layers[this.scrollpId];
		}
		if (!res) {
			res = document.documentElement;
		}
		return res;
	}

	el.stayvisible=function() {
		var el = this;
		var prevY = el.y;
		var bodyYact= el.bodyY();
		var sY = ns ? pageYOffset : document.documentElement.scrollTop;
		var p = el.scrollParent();
		var maxy = getBodyY(p) + p.offsetHeight - el.offsetHeight;
		if (( maxy > 0) && (maxy < sY)) {
			sY = maxy;
		}
		if (bodyYact < sY) {
			el.y = sY-el.startBY;
		} else if ((bodyYact > el.startBY) && (bodyYact > sY)) {
			el.y = sY-el.startBY;
			if (el.y < el.startY) {el.y = el.startY;}
		}
		var tdelay = 300;
		if (el.y != prevY) {
			if (Math.abs(el.y - prevY) > 8) {
				el.y = Math.round( prevY + ((el.y - prevY) / 8));
			}
			el.sP(el.x, el.y);
			tdelay = 10;
		}
		setTimeout(function() {
			el.stayvisible();
		} , tdelay);
	}
	el.startX=astartx;
	el.startY=astarty;
	el.style.position='relative';
	el.x = el.startX;
	el.y = el.startY;
	el.sP(el.x, el.y);
	el.startBY = el.bodyY();
	el.stayvisible();
}