/**
 * 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";
  },

  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 = this.getSiteCode();
			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();
	},
	
  getSiteCode: function(url) {
    if (this.siteCode==null) {
      var h = url ? url : document.location.host; 
      if (h.indexOf("accorhotels")>-1)   {this.siteCode="ALL";}
      else if (h.indexOf("sofitel")>-1)  {this.siteCode="SOF";}
      else if (h.indexOf("novotel")>-1)  {this.siteCode="NOV";}
      else if (h.indexOf("mercure")>-1)  {this.siteCode="MER";}
      else if (h.indexOf("mgallery")>-1) {this.siteCode="MGA";}
      else if (h.indexOf("ibis")>-1)     {this.siteCode="IBI";}
      else if (h.indexOf("etap")>-1)     {this.siteCode="ETP";}
      else if (h.indexOf("formule1")>-1) {this.siteCode="FOR";}
      else if (h.indexOf("suite")>-1)    {this.siteCode="SUI";}
      else if (h.indexOf("thalassa")>-1) {this.siteCode="THA";}
      else if (h.indexOf("seasons")>-1)  {this.siteCode="ASE";}
      else if (h.indexOf("pullman")>-1)  {this.siteCode="PUL";}
      else {this.siteCode="accorhotels";}
    } 
    return this.siteCode;
  }
}

core.push(FavoriteHotels);
