function getXhr(){
	var xhr = null;
	if(window.XMLHttpRequest){ // Firefox et autres
	  xhr = new XMLHttpRequest();
	}else if(window.ActiveXObject){ // Internet Explorer
	  try {
	    xhr = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
	    xhr = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}else { // XMLHttpRequest non supporte par le navigateur
	  alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	  xhr = false;
	}
	return xhr;
}

function trim(str){
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	return s;
}

function initCarousel(num){
	if(!hCarousel1){
		hCarousel1 = new UI.Carousel("room1");
	}
	/*
	var i = 1;
	while(document.getElementById('room'+i)){
		new UI.Carousel("room"+i);
		i = parseInt(i) + 1;
	}
	*/
}

function centerDiv(elem){
	var my_width  = 0;
	var my_height = 0;
	if ( typeof( window.innerWidth ) == 'number' ){
		my_width  = window.innerWidth;
		my_height = window.innerHeight;
	}else if ( document.documentElement && 
			 ( document.documentElement.clientWidth ||
			   document.documentElement.clientHeight ) ){
		my_width  = document.documentElement.clientWidth;
		my_height = document.documentElement.clientHeight;
	}
	else if ( document.body && 
			( document.body.clientWidth || document.body.clientHeight ) ){
		my_width  = document.body.clientWidth;
		my_height = document.body.clientHeight;
	}
	elem.style.position = 'absolute';
	elem.style.zIndex   = 99;
	
	var scrollY = 0;
	if ( document.documentElement && document.documentElement.scrollTop ){
		scrollY = document.documentElement.scrollTop;
	}else if ( document.body && document.body.scrollTop ){
		scrollY = document.body.scrollTop;
	}else if ( window.pageYOffset ){
		scrollY = window.pageYOffset;
	}else if ( window.scrollY ){
		scrollY = window.scrollY;
	}
	//alert(my_width + '@' + my_height);

	//var dimensions = Element.getDimensions(elem);
	//alert(dimensions.width + '@' + dimensions.height);
	var setX = ( my_width  - 538 ) / 2;
	var setY = ( my_height - 360 ) / 2 + scrollY;
	
	setX = ( setX < 0 ) ? 0 : setX;
	setY = ( setY < 0 ) ? 0 : setY;
	
	elem.style.left = setX + "px";
	elem.style.top  = setY + "px";
}

function initZoom(roomImgLen){
	//document.getElementById("imgRoomZoom").src = imgRooms[0]["imgRoom"+roomNb][imgRoomsIdx[0]["imgRoomZoom"]];
	if(imgRoomsIdx[0]["imgRoomZoom"] == 0){
		document.getElementById("prevImageZoom").className+="_disabled";
	}else if(imgRoomsIdx[0]["imgRoomZoom"] == (roomImgLen - 1)){
		document.getElementById("nextImageZoom").className+="_disabled";
	}else{
		document.getElementById("prevImageZoom").className = "previous_button";
		document.getElementById("nextImageZoom").className = "next_button";
	}
}

function zoom(roomNb){
//	new UI.Carousel("viewer_carousel");
	//document.getElementById('view').style.display = "none";
	document.getElementById('view').style.display = "block";
	imgRoomsIdx[0]["imgRoomZoom"] = imgRoomsIdx[0]["imgRoom"+roomNb];
	imgRoomsIdx[0]["imgRoomNb"] = roomNb;
	document.getElementById("imgRoomZoom").src = imgRooms[0]["imgRoom"+roomNb][imgRoomsIdx[0]["imgRoomZoom"]].src;
	/*
	if(imgRoomsIdx[0]["imgRoomZoom"] == 0){
		document.getElementById("prevImageZoom").className = "previous_button_disabled";
	}else if(imgRoomsIdx[0]["imgRoomZoom"] == imgRooms[0]["imgRoom"+roomNb].length - 1){
		document.getElementById("nextImageZoom").className = "next_button_disabled";
	}else{
		document.getElementById("prevImageZoom").className = "previous_button";
		document.getElementById("nextImageZoom").className = "next_button";
	}
	*/
	if(imgRooms[0]["imgRoom"+roomNb].length < 2){
		document.getElementById("prevImageZoom").className+="previous_button_disabled";
		document.getElementById("nextImageZoom").className+="next_button_disabled";
	}else{
		document.getElementById("prevImageZoom").className = "previous_button";
		document.getElementById("nextImageZoom").className = "next_button";
	}
	//document.getElementById('view').style.display = "block";
	centerDiv(document.getElementById('view'));
	//new Effect.BlindDown('view');
}

function close(){
	document.getElementById('view').style.display = "none";
	/*
	document.getElementById("prevImageZoom").className = "previous_button";
	document.getElementById("nextImageZoom").className = "next_button";
	*/
	document.getElementById("imgRoomZoom").src = "/2006/ResaB2C/img/dot.gif";
	//new Effect.SlideUp('view');
}

function preLoadImg(){
	//alert('nbCarousel: '+nbCarousel);
	//initImgVar();
	//if(typeof nbCarousel == 'undefined' || nbCarousel == null || nbCarousel == ""){
	var elem = document.getElementById('nbCarousel');
		//alert('nbCarousel: '+nbCarousel);
	//}
	var imgTmp;
	//alert('ICI1: '+(typeof elem));
	if(typeof elem != "undefined" && elem != null){
		//alert('ICI2');
		var nbCarousel = parseInt(elem.innerHTML);
		for(var i = 1; i <= nbCarousel; i++){
			//alert('ICI3');
			for(var j = 0; j < imgRooms[0]["imgRoom"+i].length; j++){
				imgTmp = imgRooms[0]["imgRoom"+i][j];
				imgRooms[0]["imgRoom"+i][j] = new Image();
				imgRooms[0]["imgRoom"+i][j].src = imgTmp;
				//alert('imgTmp.src: '+imgTmp.src);
			}
			elem = document.getElementById("imgRoom"+i);
			if(typeof elem != "undefined" && elem != null){
				document.getElementById("imgRoom"+i).src = imgRooms[0]["imgRoom"+i][imgRoomsIdx[0]["imgRoom"+i]].src;
				if(imgRooms[0]["imgRoom"+i].length < 2){
					document.getElementById("prevImage"+i).className+="previous_button_disabled";
					document.getElementById("nextImage"+i).className+="next_button_disabled";
				}
			}
		}
	}
}

function prevImage(roomNb, inZoom){
	var idx = 0;
	if(inZoom){
		idx = imgRoomsIdx[0]["imgRoomZoom"];
		roomNb = imgRoomsIdx[0]["imgRoomNb"];
	}else{
		idx = imgRoomsIdx[0]["imgRoom"+roomNb];
	}
	idx--;
	if(idx >= 0 && idx < imgRooms[0]["imgRoom"+roomNb].length){
		setImageRoom(roomNb, idx, inZoom, false);
	}else{
		idx = imgRooms[0]["imgRoom"+roomNb].length - 1;
		setImageRoom(roomNb, idx, inZoom, false);
	}
}

function nextImage(roomNb, inZoom){
	var idx = 0;
	if(inZoom){
		idx = imgRoomsIdx[0]["imgRoomZoom"];
		roomNb = imgRoomsIdx[0]["imgRoomNb"];
	}else{
		idx = imgRoomsIdx[0]["imgRoom"+roomNb];
	}
	idx++;
	if(idx < imgRooms[0]["imgRoom"+roomNb].length){
		setImageRoom(roomNb, idx, inZoom, true);
	}else{
		idx = 0;
		setImageRoom(roomNb, idx, inZoom, true);
	}
}

function setImageRoom(roomNb, idx, inZoom, isLeft){
	if(inZoom){
		document.getElementById("imgRoomZoom").src = imgRooms[0]["imgRoom"+roomNb][idx].src;
		imgRoomsIdx[0]["imgRoomZoom"] = idx;
		/*
		if(idx == 0){
			document.getElementById("prevImageZoom").className+="_disabled";
		}else if(idx == imgRooms[0]["imgRoom"+roomNb].length - 1){
			document.getElementById("nextImageZoom").className+="_disabled";
		}else{
			document.getElementById("prevImageZoom").className = "previous_button";
			document.getElementById("nextImageZoom").className = "next_button";
		}
		*/
	}else{
		if(isLeft){
			
		}
		document.getElementById("imgRoom"+roomNb).src = imgRooms[0]["imgRoom"+roomNb][idx].src;
		imgRoomsIdx[0]["imgRoom"+roomNb] = idx;
		/*
		if(idx == 0){
			document.getElementById("prevImage"+roomNb).className+="_disabled";
		}else if(idx == imgRooms[0]["imgRoom"+roomNb].length - 1){
			document.getElementById("nextImage"+roomNb).className+="_disabled";
		}else{
			document.getElementById("prevImage"+roomNb).className = "previous_button";
			document.getElementById("nextImage"+roomNb).className = "next_button";
		}
		*/
	}
}

function getPopup(divNum, url, isRoom){
	//alert('divId: '+divId+' url: '+url);
	blockScreen();
	var divElem = document.getElementById('iframe'+divNum);
	//alert('divElem.style.display: '+divElem.style.display);
	//alert('innerHTML: '+divElem.innerHTML);
	if(trim(divElem.innerHTML) == ""){
		var xhr = getXhr()
		xhr.onreadystatechange =  function(){
		                            if(xhr.readyState == 4 && xhr.status == 200){
		                              	var popupHTML = xhr.responseText;
		                              	//alert(popupHTML);
		                              	if(popupHTML != ""){
											divElem.innerHTML = popupHTML;
											document.getElementById('waitImg'+divNum).style.display = "none";
											if(typeof isRoom != "undefined" && isRoom){
												new Effect.BlindDown('iframe'+divNum, {duration: 0.5, afterFinish: function(){unblockScreen();}});
												//alert('nbCarousel: '+document.getElementById('nbCarousel').innerHTML);
												//new UI.Carousel("room"+document.getElementById('nbCarousel').innerHTML);
											}else{
												new Effect.BlindDown('iframe'+divNum, {duration: 0.5, afterFinish: function(){unblockScreen();}});
											}
										}
		                            }
		                          }
		document.getElementById('waitImg'+divNum).style.display = "block";
		setTimeout("unblockScreen()", 5000);
		//window.location.protocol
		xhr.open("GET","http://"+document.domain+url,true);
		xhr.send(null);
	}else if(divElem.style.display == "" || divElem.style.display == "block"){
		new Effect.SlideUp('iframe'+divNum, {duration: 0.5, afterFinish: function(){unblockScreen();}});
	}else{
		new Effect.BlindDown('iframe'+divNum, {duration: 0.5, afterFinish: function(){unblockScreen();}});
	}
}

function closePopup(divNum){
	blockScreen();
	new Effect.SlideUp('iframe'+divNum, {duration: 0.5, afterFinish: function(){unblockScreen();}});
}

function toggleDivDisplay(divId){
	var divElem = document.getElementById(divId);
	if(divElem){
		if(divElem.style.display == "none"){
			divElem.style.display = "block";
		}else{
			divElem.style.display = "none";
		}
	}
}

function closeInsidePopup(popupId) {
   var pop = document.getElementById(popupId || "insidePopup");
   if (pop) pop.style.display="none";
}

function getCookieVal(name) {
	var nameEQ = name + "=" ;
	var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) { 
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0){
				return unescape(c.substring(nameEQ.length,c.length));
			}
		}
	return null;
}