/**
 * Module d'affichage des hotels favoris
 */
var FavoriteHotels = {

  initialized: false,

  elm_no_hotel: null,
  elm_your_hotels: null,
  elm_template: null,

  getBeans: function() {
      return "ProfileViewBean;OriginViewBean";
  },

  init: function () {
    if (!this.initialized) {
      this.elm_template = $("favorite-hotels-template");
      this.elm_your_hotels = $("your-hotels");
      this.elm_no_hotel = $("no-hotel");
      this.initialized = !!this.elm_template && !!this.elm_your_hotels && !!this.elm_no_hotel;
    }
  },
   
	initJSON: function() {
	},

  rewrite: function() {

    this.init();

    var bean = viewBeans["ProfileViewBean"];
    if (bean && bean.hotels)
      var hotels = $H(bean.hotels);
      
    if (hotels && hotels.size() > 0) {

      // recuperation du template de la ligne d'un hotel
      var formatted = "";
      var tpl_inner = unescape(this.elm_template.innerHTML);

      // creation et replissage du template
      var siteCode = new String(viewBeans["OriginViewBean"].siteCode).toUpperCase();
      var hotel_tpl = new Template(tpl_inner);
      var i = 0;
      hotels.each(function(hotel){
        var h = hotel.value;
        h.i = i++;
        h.url_fh = "/frm_fiche_hotel.svlt?code_hotel=" + h.code;
        h.url_fh = new String(h.brand).toUpperCase() == siteCode ? h.url_fh : "http://www.accorhotels.com" + h.url_fh;
        formatted += hotel_tpl.evaluate(h);
        }
      )

      // maj inner HTML
      this.elm_template.update(formatted);

      Element.show(this.elm_your_hotels);
      Element.hide(this.elm_no_hotel);
    } else {
      Element.show(this.elm_no_hotel);
      Element.hide(this.elm_your_hotels);
    }
  },
  
    // Fermeture de pop up et chargement d'une page dans la fenetre principale
  close_load: function (URL) {
  	window.opener.top.location.href = URL;
  	window.close();
  }
}

core.push(FavoriteHotels);
