var OpeningList;
var HotDealsList;
var EarlyBookingList;
var NewsList;
var FocusList;

window.onload = function() {
	// Focus du moteur de recherche
	var valHotelVille = $('hotel_ou_ville').value;
	function initValHotelVille(event){
		var element = event.element();
		if(element.value == valHotelVille){ element.value = ""; }
	}
	function resetValHotelVille(event){
		var element 	= event.element();
		var regex		= new RegExp("(^ +)|( +$)", "g");
		var isEmpty		= element.value.replace(regex, "");
		if(isEmpty == ""){ element.value = valHotelVille; }
	}
	Array.prototype.in_array = function(valeur) {
		for (var i in this) {
			if (this[i] == valeur) return i;
		}
		return -1;
	}
	function sortByDest(a,b){
		if (a[1] != b[1]){
			return (a[1].toUpperCase() > b[1].toUpperCase())? 1 : -1;
		}else{
			return a[2] - b[2];
		}
	}
	Event.observe($('hotel_ou_ville'), 'focus', initValHotelVille, false);
	Event.observe($('hotel_ou_ville'), 'blur', resetValHotelVille, false);
	
	/* injection des offres Opening and renovations */
	if (OpeningList&&OpeningList.Opening&&OpeningList.Opening.length>0) {
		GlobalList= document.createElement("UL");
		Element.extend(GlobalList);
		
		for(i=0; i< OpeningList.Opening.length; i++) {
			if(OpeningList.Opening[i] != null) {
				ItemList= document.createElement("LI");
				Element.extend(ItemList);
				ItemList.style.display='none';
				var LienReserve= "";
				if (OpeningList.Opening[i].lienResa!==''){
					LienReserve = '<p class="more"><a href="'+OpeningList.Opening[i].lienResa+'" target="_top">'+reserver+'</a></p>';
				}
		
				if (OpeningList.Opening[i].lienFiche===''){
					LienHotel = OpeningList.Opening[i].hotel;
				}
				else {
					LienHotel = '<a href="'+OpeningList.Opening[i].lienFiche+'">'+OpeningList.Opening[i].hotel+'</a>';
				}
				ItemList.innerHTML='<strong>'+OpeningList.Opening[i].destination+'</strong><br />'+LienHotel+' <span class="prix"><strong>'+OpeningList.Opening[i].mention+'</strong></span>'+LienReserve;
				GlobalList.appendChild(ItemList);
			}
		}
		
		if(document.getElementById("opening")){
			var myObject = $$("#opening > .content > ul")[0];
			myObject.innerHTML = GlobalList.innerHTML;
			myObjectChildren=myObject.getElementsByTagName("LI");
			for (z=0; z<5 && z<myObjectChildren.length;){
				var random = Math.floor(Math.random()*myObjectChildren.length);
				ItemLisible = myObjectChildren[random];
				if ($(ItemLisible).className != 'doublon'){
					$(ItemLisible).className= 'doublon';
					$(ItemLisible).style.display='block';
					z++;
				}
			}
		} 
	} else {
		$("opening").addClassName('hidden');
	}

	/* injection des offres Hot Deals */
	if (HotDealsList&&HotDealsList.HotDeals&&HotDealsList.HotDeals.length>0) {
		GlobalList= document.createElement("UL");
		Element.extend(GlobalList);
		
		// Sélection des 5 au hasard
		possible = true;
		found_all = false;
		tab_found = new Array();
		tab_destination = new Array();
		tab_try = new Array();
		i = 0;
		while(!found_all && possible){
			// On calcul un entier aléatoire
			var random = Math.floor((Math.random()-0.00000000000000000000000001)*HotDealsList.HotDeals.length);
			if (tab_try.in_array(random) == -1){
				// Le HotDeals[random] n'a pas encore été utilisé
				var temp_tab = new Array();
				temp_tab = [random,HotDealsList.HotDeals[random].destination,HotDealsList.HotDeals[random].prix];
				tab_found.push(temp_tab);
				i++;
				if (tab_found.length == 5){
					found_all = true;
				}
				tab_try.push(random);
			}
			if (!found_all && tab_try.length >= HotDealsList.HotDeals.length){
				possible = false;
			}
		}

		// Classement des 5
		nb_found = tab_found.length;
		tab_found.sort(sortByDest);
		
		// Affichage des 5
		for(i=0; i<nb_found; i++) {
			ItemList= document.createElement("LI");
			Element.extend(ItemList);
			the_devise = HotDealsList.HotDeals[tab_found[i][0]].devise;
			if (the_devise == "EUR"){
				the_devise = "€";
			}else if (the_devise == "USD"){
				the_devise = "$";
			}else if (the_devise == "GBP"){
				the_devise = "£";
			}
			prix_devise = HotDealsList.HotDeals[tab_found[i][0]].prix+' '+the_devise;
			if (lg == "gb"){
				prix_devise = the_devise+' '+HotDealsList.HotDeals[tab_found[i][0]].prix;
			}
			ItemList.innerHTML='<strong>'+HotDealsList.HotDeals[tab_found[i][0]].destination+'</strong><br /><a href="'+HotDealsList.HotDeals[tab_found[i][0]].lienFiche+'">'+HotDealsList.HotDeals[tab_found[i][0]].hotel+'</a> <span class="prix"><a href="'+HotDealsList.HotDeals[tab_found[i][0]].lienResa+'" target="_top">'+prix_devise+'*</a></span>';
			GlobalList.appendChild(ItemList);
		}
		
		if(document.getElementById("hotDeals")){
			var myObject = $$("#hotDeals > .content > ul")[0];
			myObject.innerHTML = GlobalList.innerHTML;
		}
	} else {
		$("hotDeals").addClassName('hidden');
	}
	
	 /* injection des offres Early Booking */
	if (EarlyBookingList&&EarlyBookingList.EarlyBooking&&EarlyBookingList.EarlyBooking.length>0) {
		GlobalList= document.createElement("UL");
		Element.extend(GlobalList);
		
		// Sélection des 5 au hasard		
		possible = true;
		found_all = false;
		tab_found = new Array();
		tab_destination = new Array();
		tab_try = new Array();
		i = 0;
		while(!found_all && possible){
			// On calcul un entier aléatoire
			var random = Math.floor((Math.random()-0.0000000000000000000000000001)*EarlyBookingList.EarlyBooking.length);
			if (tab_try.in_array(random) == -1){
				// Le EarlyBooking[random] n'a pas encore été utilisé
				var temp_tab = new Array();
				temp_tab = [random,EarlyBookingList.EarlyBooking[random].destination,EarlyBookingList.EarlyBooking[random].prix];
				tab_found.push(temp_tab);
				i++;
				if (tab_found.length == 5){
					found_all = true;
				}
				tab_try.push(random);
			}
			if (!found_all && tab_try.length >= EarlyBookingList.EarlyBooking.length){
				possible = false;
			}
		}

		// Classement des 5
		nb_found = tab_found.length;
		tab_found.sort(sortByDest);
		
		// Affichage des 5
		for(i=0; i<nb_found; i++) {
			ItemList= document.createElement("LI");
			Element.extend(ItemList);
			the_devise = EarlyBookingList.EarlyBooking[tab_found[i][0]].devise;
			if (the_devise == "EUR"){
				the_devise = "€";
			}else if (the_devise == "USD"){
				the_devise = "$";
			}else if (the_devise == "GBP"){
				the_devise = "£";
			}
			prix_devise = EarlyBookingList.EarlyBooking[tab_found[i][0]].prix+' '+the_devise;
			if (lg == "gb"){
				prix_devise = the_devise+' '+EarlyBookingList.EarlyBooking[tab_found[i][0]].prix;
			}
			ItemList.innerHTML='<strong>'+EarlyBookingList.EarlyBooking[tab_found[i][0]].destination+'</strong><br /><a href="'+EarlyBookingList.EarlyBooking[tab_found[i][0]].lienFiche+'">'+EarlyBookingList.EarlyBooking[tab_found[i][0]].hotel+'</a> <span class="prix"><a href="'+EarlyBookingList.EarlyBooking[tab_found[i][0]].lienResa+'" target="_top">'+prix_devise+'*</a></span>';
			GlobalList.appendChild(ItemList);
		}
		
		if(document.getElementById("earlyBooking")){
			var myObject = $$("#earlyBooking > .content > ul")[0];
			myObject.innerHTML = GlobalList.innerHTML;
		}
	} else {
		$("earlyBooking").addClassName('hidden');
	}
	
	// NewsList
	if (NewsList&&NewsList.News&&NewsList.News.length>0) {
		var ItemList = "";
		var random = Math.floor(Math.random()*NewsList.News.length);
		var myMoreLink = $$("#relNews .content p.more")[0];
		$$("#relNews .content")[0].innerHTML = "";
		
		if (NewsList.News[random].visuel!==""){
			var fic = NewsList.News[random].visuel.split(".");
			var extension = fic[fic.length-1];
			
			if(extension != "swf") {
				if (NewsList.News[random].lien!=""){
					$$("#relNews .content")[0].innerHTML = '<a href="'+NewsList.News[random].lien+'"><img src="'+NewsList.News[random].visuel+'" width="200" height="140" alt="" /></a>';
				} else {
					$$("#relNews .content")[0].innerHTML = '<img src="'+NewsList.News[random].visuel+'" width="200" height="140" alt="" />';
				}
			}else{
				$$("#relNews .content")[0].innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="200" height="140"><param name="movie" value="'+NewsList.News[random].visuel+'"><param name="quality" value="high"><embed src="'+NewsList.News[random].visuel+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="200" height="140"></embed></object>';
			}
		}
    
		if (NewsList.News[random].lien!=""){
			ItemList = '<ul><li><a href="'+NewsList.News[random].lien+'"><strong>'+NewsList.News[random].titre+'</strong></a><br /><a href="'+NewsList.News[random].lien+'">'+NewsList.News[random].description+'</a></li></ul><p class="more"><a href="'+NewsList.News[random].lien+'">'+NewsList.News[random].libelleLien+'</a></p>';
		} else {
			ItemList = '<ul><li><strong>'+NewsList.News[random].titre+'</strong><br /><span>'+NewsList.News[random].description+'</span></li></ul>';
		}
    
		$$("#relNews .content")[0].innerHTML += ItemList;
	} else {
		$("relNews").addClassName('hidden');
	}
	
	// Focus
	if (FocusList&&FocusList.focus&&FocusList.focus.length>0) {
		ItemList = "";
		var random = Math.floor(Math.random()*FocusList.focus.length);
		$$("#relFocus .content p.more a")[0].setAttribute("href",FocusList.focus[random].lienVille);
		var MoreInfo = $$("#relFocus .content")[0].innerHTML;
		$$("#relFocus .content")[0].innerHTML = "";
		if (FocusList.focus[random].visuel!==""){
			var fic = FocusList.focus[random].visuel.split(".");
			var extension = fic[fic.length-1];
			  
			if(extension != "swf") {
				if (FocusList.focus[random].lienVille!=""){
					$$("#relFocus .content")[0].innerHTML = '<a href="'+FocusList.focus[random].lienVille+'"><img src="'+FocusList.focus[random].visuel+'" width="200" height="140" alt="" /></a>';
				} else {
					$$("#relFocus .content")[0].innerHTML = '<img src="'+FocusList.focus[random].visuel+'" width="200" height="140" alt="" />';
				}
			} else {
				$$("#relFocus .content")[0].innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="200" height="140"><param name="movie" value="'+FocusList.focus[random].visuel+'"><param name="quality" value="high"><embed src="'+FocusList.focus[random].visuel+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="200" height="140"></embed></object>';
			}
		}
    
		if (FocusList.focus[random].lienVille!=""){
			ItemList = '<ul><li class="FirstOffer"><a href="'+FocusList.focus[random].lienVille+'"><strong>'+FocusList.focus[random].titre+'</strong></a><br /><a href="'+FocusList.focus[random].lienVille+'"><span>'+FocusList.focus[random].description+'</span></a></li>';
		}else{
			ItemList = '<ul><li class="FirstOffer"><strong>'+FocusList.focus[random].titre+'</strong><br /><span>'+FocusList.focus[random].description+'</span></li>';
		}
		Hotels = FocusList.focus[random].hotels;
	
		for(i=0;i<Hotels.length;i++) {
			if (Hotels[i].lienFiche!=""){
				ItemList += '<li class="hotelOn"><a href="'+Hotels[i].lienFiche+'"><strong>'+Hotels[i].nom+' &gt;</strong></a></li>';
			}else{
				ItemList += '<li class="hotelOn"><strong>'+Hotels[i].nom+' &gt;</strong></li>';
			}
		}
		
		ItemList += '</ul>';
		if (FocusList.focus[random].lienVille!=""){
			ItemList += MoreInfo;
		}
	
		$$("#relFocus .content")[0].innerHTML += ItemList;
    
		//Affichage de 5 hotels aléatoires s'il y en a plus de 5 dans la liste
		Listhotels = $$("#relFocus .content ul .hotelOn");
		for(e=0;i<Listhotels.length;e++) {
			Listhotels[e].style.display = "none";
		}
		if (Listhotels.length>5){
			for (f=Listhotels.length; f>5;){
				var SSrandom = Math.floor(Math.random()*Listhotels.length);
				ItemLisible = Listhotels[SSrandom];
				if ($(ItemLisible).className == 'hotelOn'){
					$(ItemLisible).className= 'hotelOff';
					ItemLisible.style.display='none';
					f--;
				}
			}
		}
	} else {
		$("relFocus").addClassName('hidden');
	}
};

