Event.observe(window,"load",function() {		
		$$("#listeAvantages img").findAll(function(node){
			return node.getAttribute('alt');
		}).each(function(node){
			   //new Tooltip(node,node.alt);
				
                //element, tool_tip, widthww,deltax, deltay
                var tooltip =  new Tooltip(node,node.alt,0,0,0);
				//tooltip.options.default_css = true;
               $(tooltip.tool_tip).setStyle({display:'block'});
              var width = $(tooltip.tool_tip).firstChild.getDimensions().width + 45;
              $(tooltip.tool_tip).setStyle({display:'none'});
              $(tooltip.tool_tip).setStyle({
  width:  width+'px'});

                node.removeAttribute("alt");
	 });
     
        $$("input[type=text]").each(function(elmt,i){
           elmt.observe("focus", function(event){
                resetInput(elmt);
           });           
       });
     
			initGallery();
			initLastHotels();
            doChangeTooltipDOM();
            initExternal();
});     

function doChangeTooltipDOM(){
    $$(".tooltip").each(function(node){
       var element = node.firstChild;
        var parentNode =   node.parentNode;     
         // parentNode.className = "tooltip_box";
         var title = element.innerHTML;
        var div_bg = $(document.createElement("div")); 
        div_bg.addClassName("bg");
            node.appendChild(div_bg);
        
        var div_bloc = $(document.createElement("div")); 
        div_bloc.addClassName("bloc");
        node.appendChild(div_bloc);
        div_bloc.innerHTML = title;
  
      element.style.display = "none"; /* IE */
    
   });
   
 
}

     

     function initExternal(){
     	var regex = new RegExp("hotelformule1.com");
		if(!regex.test(window.document.URL))
			$$('body')[0].className = $$('body')[0].className + ' external';
     }


    
	function initGallery(){
		var thumbs = $$("#thumb li");
		thumbs.each(function(elm,index){
			elm.observe('click', function(event){
				var images = $$("#photoHotel img");
				images.each(function(image,indexImg){
					image.style.display = "none";
					thumbs[indexImg].removeClassName("active");
				});
				images[index].style.display = "block";
				this.addClassName('active');		
			});
		});
	}
	
	
	function EcrireCookie(nom, valeur) {
		var argv=EcrireCookie.arguments;
		var argc=EcrireCookie.arguments.length;
		var expires=(argc > 2) ? argv[2] : null;
		var path= "/";
       // var path=(argc > 3) ? argv[3] : null;
                
		var domain=(argc > 4) ? argv[4] : null;
		var secure=(argc > 5) ? argv[5] : false;
		document.cookie=nom+"="+escape(valeur)+ ((expires==null) ? "" : ("; expires="+expires.toGMTString()))+ ((path==null) ? "" : ("; path="+path))+ ((domain==null) ? "" : ("; domain="+domain))+ ((secure==true) ? "; secure" : "");
	}
		
        
	function getCookieVal(offset) {
		var endstr=document.cookie.indexOf (";", offset);
		if (endstr==-1) endstr=document.cookie.length;
		return unescape(document.cookie.substring(offset, endstr));
	}
	
    
	function LireCookie(nom) {
		var arg=nom+"=";
		var alen=arg.length;
		var clen=document.cookie.length;
		var i=0;
		while (i<clen) {
			var j=i+alen;
			if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
			i=document.cookie.indexOf(" ",i)+1;
			if (i==0) break;
		}
		return false;
	}


function resetInput(obj) {
	if(obj.value != "" && obj.id != "dateArrival" && obj.id != "tripDateArrival" ) {
		var myValue = obj.value;
		obj.value = "";
	}
	if(myValue) {
	/*	obj.observe("blur", function(event){
			obj.value = myValue;
		});
        */
	}
}

function initLastHotels(){

	var hotelName = $$(".titre h1")[0].innerHTML;
	var hotelUrl = document.URL;
	var listPlaceHolder = $("lastSearchHotels_list");
	var isAlreadyInList = false;

	var last_hostels = LireCookie("lastHostels");
	if (last_hostels) {
		try {
			var lastHostelsObject = last_hostels.evalJSON();
		}
		catch (e) {
			var lastHostelsObject = new Array();
		}
	}
	else {
		var lastHostelsObject = new Array();
	}
	
	// recherche hôtel dans la liste si existante
	var reg_nb = new RegExp("[0-9]{4}", "g");
	var currentHotelRid = hotelUrl.match(reg_nb);
	lastHostelsObject.each(function(item,i) {
		var hotelRidFromCookie = item.href.match(reg_nb)
		if((item.href == hotelUrl) || (parseInt(currentHotelRid) == parseInt(hotelRidFromCookie))) {
			isAlreadyInList = true;
		}
	});
	
	// si hotel pas déja présent dans le cookie, ajout du nouvel hotel dans le tableau
	if(isAlreadyInList == false){
		var currentHotelObject = {href: hotelUrl, title: hotelName};
		lastHostelsObject.push(currentHotelObject);
	}
	
	// limitation à 5 hôtels max
	if(lastHostelsObject.length > 5) {
		lastHostelsObject.shift();
	}
	
	// ecriture du cookie
	EcrireCookie("lastHostels", lastHostelsObject.toJSON());
	
	// affichage du bloc
	lastHostelsObject.each(function(item,i) {
		var hotelNode =  $(document.createElement("li")); 
		hotelNode.insert("<a href="+item.href+">"+item.title+"</a>");
		listPlaceHolder.appendChild(hotelNode);
	});
	$("lastSearchHotels").style.display = "block";
}

