if(!ACH) {var ACH = {};}
if(!ACH.fh) {ACH.fh = {};};


ACH.fh.dyn = (	function(){
	
	function _success(json){
			if (!json.viewBeans){
				_failure(json);
				return;
			}
			FH.log("succes");
			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");
				  if (estListeHotel){
            jQuery("#lnk_country").attr('href','/'+FH.imgpath+'/booking/advanced-search.shtml#search.destination='+encodeURIComponent(jQuery("#lnk_country").attr('title')));
				    jQuery("#lnk_city").attr('href','/'+FH.imgpath+'/booking/advanced-search.shtml#search.destination='+encodeURIComponent(jQuery("#lnk_city").attr('title')+', '+jQuery("#lnk_country").attr('title')));			    
				  }
			  }
			} 
			
			_favorites(json.viewBeans.ProfileViewBean);
			
			// test conversion
			//_convert(json.viewBeans.CurrenciesViewBean);
			
			// Criteo : périmètre accorhotels grand public uniquement + restriction sur certains marchés géré dans le script criteo.js
			if (json.viewBeans.OriginViewBean.accorhotels) {
				var hotelCode = FH.rid;
				var dateIn = null;
				var dateOut = null;
				if (json.viewBeans.BookingEngineViewBean && json.viewBeans.BookingEngineViewBean.yearIn && json.viewBeans.BookingEngineViewBean.yearOut) {
					dateIn = new Date(json.viewBeans.BookingEngineViewBean.yearIn, json.viewBeans.BookingEngineViewBean.monthIn-1, json.viewBeans.BookingEngineViewBean.dayIn);
					dateOut = new Date(json.viewBeans.BookingEngineViewBean.yearOut, json.viewBeans.BookingEngineViewBean.monthOut-1, json.viewBeans.BookingEngineViewBean.dayOut);
				}
				Criteo.triggerProductPageHit(hotelCode, dateIn, dateOut);
			}
			// Fin Criteo
	};
		
	function _failure(json){
		FH.log("failure");FH.log("pb while getting json.ViewBeans");FH.log(json);
		_nav(false);_favorites (null);
		
	};
	
  function _nav(active,link_all,link_prev,link_next){
    if (active){
      jQuery('#lnk_prev').attr('href',link_prev);jQuery('#lnk_next').attr('href',link_next);
      jQuery('#lnk_list1').attr('href',link_all);jQuery('#lnk_list2').attr('href',link_all);
      try{
        jQuery('#precedente').css({'visibility':'visible'});
        jQuery('#suivante').css({'visibility':'visible'});
        jQuery('#bloc_list1').show();
        jQuery('#bloc_list2').show();
      }catch(e){
      }
    } else {
      try{
        jQuery('#precedente').css({'visibility':'hidden'});
        jQuery('#suivante').css({'visibility':'hidden'});
        jQuery('#bloc_list1').remove();
        jQuery('#bloc_list2').remove();
      }catch(e){
      }
    }
  };

	function _convert(currenciesViewBean){
			FH.currencies.convert(currenciesViewBean.currencies,currenciesViewBean.profileCurrency);
	};
	
	function _favorites (profileViewBean){
			var link ='';
			if (profileViewBean){
				link = '/managePreferredHotels.svlt?action=addHotel&code_hotel='+FH.rid+'&code_chaine='+FH.brand;
				//jQuery("#login-account-lnk").attr('href','https://secure.accorhotels.com/'+FH.imgpath+'/profil/modify.shtml').html(I18N._('fh','Change your user ID'));
				//jQuery("#create-account-lnk").attr('href','https://secure.accorhotels.com/authentication/logout.jshtml').html(I18N._('fh','Disconnect'));
				
				/*test ecriture des favoris : var inn ='<div id="bloc-fav"><div class="bloc"><h2>Favorites</h2><ul>';for(hotel in profileViewBean.hotels){inn+= '<li><a href="/frm_fiche_hotel.svlt?code_langue='+FH.imgpath+'&code_hotel='+profileViewBean.hotels[hotel]['code']+'">'+profileViewBean.hotels[hotel]['name']+'</a></li>';}inn+='</ul></div></div>';jQuery("#bloc-historique").after(inn).show();*/
				
				jQuery("#myaccorhotels").show();
			} else {
				link = '/'+FH.imgpath+'/fh/favorites.html';
				jQuery("#myaccorhotels").remove();
			}
			jQuery(".login-form *").show();
			
			jQuery('#lnk_favorites1').attr('href',link);
			jQuery('#lnk_favorites2').attr('href',link);
		} ;
	
	function _groupeLoisir(flag){
		if (flag){
			jQuery("#prix-dispo").remove();
			jQuery("#renvoi-1").remove();
		}
	}
	
	return {
			
			init:function(){
				
				var r = FH.dyn.callJson('OriginViewBean|ProfileViewBean|FicheHotelViewBean|HotelsViewBean|CurrenciesViewBean|BookingEngineViewBean',_success,_failure);
			
			}
	}
	
	
})();

FH.dyn.init(ACH);

