
// init namespace (a utiliser pour eviter toute collision)
if(!SOF) {var SOF = {};}

SOF.fhdyn = (function (){
	
	
	function _success(json){
		if (!json.viewBeans){
				_failure(json);
				return;
		}
		if (json.viewBeans.FicheHotelViewBean){
				with(json.viewBeans.FicheHotelViewBean){
					_nav(estListeHotel,urlLH,'/frm_fiche_hotel.svlt?liste=1&indice='+(positionDansListeHotel-1),'/frm_fiche_hotel.svlt?liste=1&indice='+(positionDansListeHotel+1));
					//_groupeLoisir(codeEncart=="LEI");
				}
		}
		
		_resaEngine(json.viewBeans.FicheHotelViewBean);	
		_favorites(json.viewBeans.ProfileViewBean)	;
		_chooselanguagehref();
		_histo();
		FH.tip.init();

	};
	
	
	function _failure(json){
		FH.log(json);
	};
	
  function _histo(){
		//recuperation/stockage
		
		var c = new Cookies().get("histo");
		c= (!c)?[]:c=c.split("|");
		var h = FH.rid+":"+FH.name;
		for (var i=0;i<c.length;i++){
			if (c[i].split(":")[0]==FH.rid){h='';}
		}
		if (h!=''){c.unshift(h);}	
		c = c.slice(0,5);//ongarde les 5 derniers
		var cc = new Cookies('/', 'sofitel.com');
		cc.set("histo",c.join("|"), 7);
		
		//ecriture		
		var inn ='';
		for (var i=0;i<c.length;i++){
			var cs = c[i].split(":");
			inn+= '<li> > <a href="/frm_fiche_hotel.svlt?code_langue='+FH.imgpath+'&code_hotel='+cs[0]+'">'+cs[1]+'</a></li>';
		}
		$$("#bloc-historique ul")[0].innerHTML =inn; 
	};
	
	function _nav(active,link_all,link_prev,link_next){
			if (active){
				var nav = Box.nodes.get('#ul-nav')[0];
				nav.innerHTML='<li class="list"><a href="'+link_all+'">'+I18N._('fh','List of hotels')+'</a></li>'
          +'<li class="prev"><a href="'+link_prev+'">'+I18N._('fh','Previous hotel')+'</a></li>'
          +'<li class="next"><a href="'+link_next+'">'+I18N._('fh','Next hotel')+'</a></li>';
			} else {
				//
			}
	};
	
	
	function _resaEngine(ficheHotelViewBean){
		
	  if (ficheHotelViewBean.rfpWeb) {
	  	src= '/'+FH.imgpath+'/fh/bloc_moteur_mice_fh-v66.htm#rid='+FH.rid+'&brand=sof&lang='+FH.en2gb(FH.lang);
		} else if (ficheHotelViewBean.estReservationAutorisee ) {
			src= '/'+FH.imgpath+'/fh/bloc_moteur_fh-v66.htm#rid='+FH.rid;
	  } else  {
			src= '/'+FH.imgpath+'/fh/bloc_moteur_empty_fh-v66.htm#rid='+FH.rid;
	  }
    var ifres = $('iframe_resa');
		ifres.replace('<iframe id="iframe_resa" frameborder="0" scrolling="no" src="'+src+'"></iframe>');
    
	};

	function _favorites(profileViewBean){
	
			var fav = Box.nodes.get('#lnk_favorites')[0];

			if (profileViewBean){
  		link = '/managePreferredHotels.svlt?action=addHotel&code_hotel='+FH.rid+'&code_chaine=sof';
			} else {
  		link = '/'+FH.imgpath+'/fh/pop_favoris.html';
			}
  	Box.events.add(fav, 'click', function(e) {e.preventDefault();window.open(link,'pop','scrollbars=yes,resizable=yes,width=453,height=360');});

	};
	
	 function  _chooselanguagehref (){ 	 
			var cl = Box.nodes.get('#choose-language');
			if (!cl.length) {return;}
			var sub = Box.nodes.get('a',cl); 	 
      for(var i = 0; i<sub.length; i++) { 	 
      	sub[i].search += '&ref='+escape(window.location); 	 
      sub[i].search = sub[i].search.replace(/langue/,'lang');
      } 	 
  	};
  	
	
	return {
	
		init : function(){
			FH.fhdyn.callJson('OriginViewBean|ProfileViewBean|FicheHotelViewBean|HotelsViewBean|CurrenciesViewBean',_success,_failure);	
		}
	}
}
)();	

// chargement des fonctions
Box.events.load(SOF.fhdyn.init);

// d‚chargement (nettoyage variables)
Box.events.add(window, 'unload', function() {
	SOF.fhdyn = null;
});


