/** 
 * Objet javascript permettant de gerer le trie des hotels
 * du de la liste hotels.
 */

var HotelsSort = {
    bookingEngineViewBean: null,
    hotelsViewBean: null,
    
    /* Methodes pour le tri et la reecriture de l'intro */
    nbHotels : null,
	destination : null,
	context : null,
	dates : null,
	widdenSearch : null,
	widdenDestination : null,
	brandCode : null,
	preferentialCode : null,
    
    /**
    * Liste des noms d'objets a utiliser.
    */
    getBeans: function()
    {
      return "BookingEngineViewBean;HotelsViewBean";
    },
      
    /**
    * Initialisation/Mise a jour de la variable JSON
    */
    initJSON: function()
    {
        var num;
        try {
            for( num in beans)
            {
                // A cause de prototype, il ne faut recuperer que nos objet et pas ceux de prototype.
                if(beans.hasOwnProperty(num)) {
                    if("BookingEngineViewBean" == beans[num].nom && null != beans[num].bean)
                        this.bookingEngineViewBean = beans[num].bean;
                        
                    if("HotelsViewBean" == beans[num].nom && null != beans[num].bean) 
                        this.hotelsViewBean = beans[num].bean;
                }
            }
        }
        catch(e)
        {
            //console.log(e);
            //alert(e);
        }
    },
    
    rewrite: function() {
        // Ecrit l'intro et le bloc de tri
        this.triInit();
    },
		
	triInit : function(){
		if(this.hotelsViewBean && this.bookingEngineViewBean){
			this.nbHotels = HotelsSort.hotelsViewBean.hotelsNumber;
			this.destination = HotelsSort.bookingEngineViewBean.destination;
			this.context = HotelsSort.hotelsViewBean.context;
			this.widdenSearch = HotelsSort.hotelsViewBean.widdenSearch;
			this.widdenDestination = HotelsSort.hotelsViewBean.widdenDestination;
			this.preferentialCode = HotelsSort.bookingEngineViewBean.preferentialCode;

			//this.nbHotels = 1;
		}
		this.writeIntro();
		this.marqueRecupURL();
		if(this.nbHotels >= 2){
			this.displayBlocTri();
		}
	},
	
	writeIntro : function(){
		if(this.hotelsViewBean && this.bookingEngineViewBean){
			if(this.nbHotels == 0 && this.hotelsViewBean.context == "meetingBookOnline"){
				if($('cadre_no_result') && $('no_result_pro')){
					$('cadre_no_result').show();
					if($('no_result_bol')){
						$('no_result_bol').show();
						$('no_result_bol').select('a')[0].href=this.bookingEngineViewBean.newSearchUrl;
					}
					$('iframe_liste_hotel').hide();
					$('content').select('.separator_long')[0].hide();
				}
			} 
			else if(this.nbHotels == 0 && this.bookingEngineViewBean.preferentialCode != ""){
				if($('no_result_codePref')) $('no_result_codePref').style.display="block";
			}
			else if(this.nbHotels == 0 && this.hotelsViewBean.context == "chouchou"){
				if($('no_result_codePref')) $('no_result_codePref').style.display="block";
			}
			else if(this.nbHotels == 0 || this.hotelsViewBean.noAvailability == true){
				if($('no_result')) $('no_result').style.display="block";
			}
			else{
				if(this.widdenSearch){
					var spanSingular = $('NoHotelsButWidden').select('.singular')[0];
					var spanPlural = $('NoHotelsButWidden').select('.plural')[0];
					$('NoHotelsButWidden').select('.hotelsNumber')[0].innerHTML = '';
					
					$('NoHotelsButWidden').select('.destination')[0].innerHTML = '';
					$('NoHotelsButWidden').select('.widdenDestination')[0].innerHTML = '';
					$('NoHotelsButWidden').select('.destination')[0].innerHTML = this.destination;
					$('NoHotelsButWidden').select('.widdenDestination')[0].innerHTML = this.widdenDestination;
					if(this.nbHotels > 1){
						$('NoHotelsButWidden').select('.hotelsNumber')[0].innerHTML = this.nbHotels + ' ' + I18N.gettext('hotels.list', 'wHotels');
					}
					else{
						$('NoHotelsButWidden').select('.hotelsNumber')[0].innerHTML = this.nbHotels + ' ' + I18N.gettext('hotels.list', 'wHotel');
					}
					$('NoHotelsButWidden').show();
					
				}
				else{
					if(this.hotelsViewBean.ridSearch == true){
						if($('nbHotels')){
							var span = $('nbHotels').select('.hotelsNumber')[0];
							if(this.nbHotels > 1){
								span.innerHTML = '';
								span.innerHTML = this.nbHotels + ' ' + I18N.gettext('hotels.list', 'wHotels');
							}
							else{
								span.innerHTML = '';
								span.innerHTML = this.nbHotels + ' ' + I18N.gettext('hotels.list', 'wHotel');
							}
							
							$('nbHotels').show();
							}
					}
					else{
						if($('nbHotelsAndDestination')){	
							if(this.hotelsViewBean.context && this.hotelsViewBean.context == "thalassa"
								&& this.hotelsViewBean.thalassa && this.hotelsViewBean.thalassa.spaName){
								$('nbHotelsAndDestination').select('.destination')[0].innerHTML = this.destination + ' - ' + this.hotelsViewBean.thalassa.spaName;
							}
							else{
								$('nbHotelsAndDestination').select('.destination')[0].innerHTML = this.destination;
							}
							
							var span = $('nbHotelsAndDestination').select('.hotelsNumber')[0];
							if(this.nbHotels > 1){
								span.innerHTML = '';
								span.innerHTML = this.nbHotels + ' ' + I18N.gettext('hotels.list', 'wHotels');
							}
							else{
								span.innerHTML = '';
								span.innerHTML = this.nbHotels + ' ' + I18N.gettext('hotels.list', 'wHotel');
							}
							
							$('nbHotelsAndDestination').show();
						}
						
					}
				}
			}
		}
	},
	
	displayBlocTri : function(){
		if($('header-lH') && this.nbHotels >=2){
			
			// tri par prix : tha:oui  lg:non gp:oui rfp:non agv:non  chouchou:non preferedCode:non
			/*if (this.context == null)
				$("div-tri-prix").style.display="block";
			if (this.context == "thalassa")
				$("div-tri-prix").style.display="block";
			
			
			if(this.context != "meetingBookOnline" 
				&& this.context != "rfp" 
				&& this.context != "travelAgency"  
				&& this.context != "leasureGroup"
				&& this.context != "corporate"
				&& this.bookingEngineViewBean.fidelityCardName == null)
			{
				if(this.preferentialCode == ""){
					//if (this.brandCode != 'ETP' && this.brandCode != 'FOR')
						$("div-tri-prix").style.display="block";
				}
			}
			
			if(this.hotelsViewBean.noAvailability == true){
				$("div-tri-prix").style.display="none";
			}*/
			
			if (this.hotelsViewBean.barFound == true)
				$("div-tri-prix").style.display="block";
			
			if (this.brandCode == "ALL")
				$("div-tri-confort").style.display="block";
			
			if($("div-tri-confort").style.display=="none" && $("div-tri-prix").style.display=="none" && $("div-tri-proximite").style.display=="none"){
				$('header-lH').style.display="none";
			}
			else{
				$('header-lH').show();
				this.init_liste_hotels_tri();			
			}
		}
	},
    
  buildHotelsView:function(){
  	if(this.hotelsViewBean && this.hotelsViewBean.city && $("div-tri-proximite"))
  		$("div-tri-proximite").show();
  },
	
	switch_f : function(dir, id){
		var name_elem = dir+id;
		var elem = $(name_elem);
		var name_dest = "tri"+name_elem;
		var dest = $(name_dest);
		dest.style.backgroundImage = "url(/imagerie/reservation/bouton_fleches.gif)";
		switch(dir){
			case "plus":
				dest.style.backgroundPosition = "left top";
				break;
			case "moins":
				dest.style.backgroundPosition = "right top";
				break;
			default:
				break;
		}
		dest.style.backgroundRepeat = "no-repeat";
	},

	end_switch_down : function(id){
		var name_elem = "plus"+id;
		var elem = document.getElementById(name_elem);
		var name_dest = "tri"+name_elem;
		var dest = document.getElementById(name_dest);
		dest.style.background = "none";
	},

	end_switch_up : function(id){
		var name_elem = "moins"+id;
		var elem = document.getElementById(name_elem);
		var name_dest = "tri"+name_elem;
		var dest = document.getElementById(name_dest);
		dest.style.background = "none";
	},
	
	trier : function(typeTri, ordre){
		var iframeDocument = $("iframe_liste_hotel").contentWindow.document;
    var listeHotelContainer = $(iframeDocument.getElementById('liste_hotels'));
    if(iframeDocument.getElementById("s_devise")){
			$('iframe_liste_hotel').src='/geo/liste_hotels.jsp?tri='+ typeTri +'&order=' + ordre +'&currencyCode='+iframeDocument.getElementById("s_devise").value;
		}
		else{
			$('iframe_liste_hotel').src='/geo/liste_hotels.jsp?tri='+ typeTri +'&order=' + ordre;
		}
	},
	
	init_liste_hotels_tri : function(){
		if($("plus1")){
			Event.observe($("plus1"), 'mouseover', function() {HotelsSort.switch_f('plus', 1)});
			Event.observe($("plus1"), 'mouseout', function() {HotelsSort.end_switch_down(1)});
			Event.observe($("plus1"), 'click', function() {HotelsSort.trier('prix', 'desc')});
		}
		if($("moins1")){
			Event.observe($("moins1"), 'mouseover', function() {HotelsSort.switch_f('moins', 1)});
			Event.observe($("moins1"), 'mouseout', function() {HotelsSort.end_switch_up(1)});
			Event.observe($("moins1"), 'click', function() {HotelsSort.trier('prix', 'asc')});
		}
		
		if($("plus2")){
			Event.observe($("plus2"), 'mouseover', function() {HotelsSort.switch_f('plus', 2)});
			Event.observe($("plus2"), 'mouseout', function() {HotelsSort.end_switch_down(2)});
			Event.observe($("plus2"), 'click', function() {HotelsSort.trier('confort', 'asc')});
		}
		if($("moins2")){
			Event.observe($("moins2"), 'mouseover', function() {HotelsSort.switch_f('moins', 2)});
			Event.observe($("moins2"), 'mouseout', function() {HotelsSort.end_switch_up(2)});
			Event.observe($("moins2"), 'click', function() {HotelsSort.trier('confort', 'desc')});
		}
		
		if($("plus3")){
			Event.observe($("plus3"), 'mouseover', function() {HotelsSort.switch_f('plus', 3)});
			Event.observe($("plus3"), 'mouseout', function() {HotelsSort.end_switch_down(3)});
			Event.observe($("plus3"), 'click', function() {HotelsSort.trier('distance', 'asc')});
		}
		if($("moins3")){
			Event.observe($("moins3"), 'mouseover', function() {HotelsSort.switch_f('moins', 3)});
			Event.observe($("moins3"), 'mouseout', function() {HotelsSort.end_switch_up(3)});
			Event.observe($("moins3"), 'click', function() {HotelsSort.trier('distance', 'desc')});
		}
		
		this.buildHotelsView();
	},
	
	marqueRecupURL : function() {
		this.brandCode = "";
		var url = location.host;
		var urldecoup  = url.split (".");
		var brand = urldecoup[1].toLowerCase();
		var siteOrigine = "";
		switch(brand) {
		 case "accorhotels": this.brandCode="ALL"; break;
		 case "etaphotel": this.brandCode="ETP"; break;
		 case "hotelformule1": this.brandCode="FOR"; break;
		 case "ibishotel": this.brandCode="IBI"; break;
		 case "mercure": this.brandCode="MER"; break;
		 case "novotel": this.brandCode="NOV"; break;
		 case "sofitel": this.brandCode="SOF"; break;
		 case "suite-hotel": this.brandCode="SUI"; break;
		 case "thalassa": siteOrigine="THA"; break;
		}
	}
}

core.push(HotelsSort);