
function alert(){}

//function xitinit(){}

/**
 * Description : empty the fields specified in agruments when the user clicks upon
 * Arguments : list of input ids
 */
function SUI_emptyField(){
	if($A(arguments).size()){
		$A(arguments).each(function(item){
			Event.observe($(item), 'click',function(){
				this.value = '';
			});
		});
	}
}

/**
 * Description :
 * Arguments :
 */
function SUI_cancelEmptyLinks(){
	if(typeof(arguments[0]) == 'string'){
		arguments[0] = arguments[0].gsub('action="#"', 'action="javascript:;"');
		return arguments[0].gsub('href="#"', 'href="javascript:;"');
	}else{
		var arg = !arguments[0] ? $$('body')[0] : arguments[0];
		arg.getElementsBySelector('a[href=#], form[action=#]').each(function(item){
			if(item.getAttribute('href'))
				item.setAttribute('href', 'javascript:;');
			if(item.getAttribute('action'))
				item.setAttribute('action', 'javascript:;');
		});
	}
}

/**
 *  ClassName : GarbageCollector
 * Arguments :
 */
var GarbageCollector = {
	handlers : {},
	fctBinded : function(){},
	set : function(event, fct){
		var id = Event.element(event).getAttribute('id') ? Event.element(event).getAttribute('id') : 'anonymous';
		if(!this.handlers[id])
			this.handlers[id] = {};
		else this.free(Event.element(event), event.type);
		this.handlers[id][event.type] = fct;
		this.fctBinded = fct.bind(Event.element(event), event);
		this.fctBinded();
	},
	free : function(item){
		var id = item.getAttribute('id') ? item.getAttribute('id') : 'anonymous';
		var eventName = arguments[1];
		
		if(!eventName){
			for(var index in this.handlers[id]){
				Event.stopObserving(item, index, this.handlers[id][index]);
			}
		}else{
			if(this.handlers[id][eventName]){
				Event.stopObserving(item, eventName, this.handlers[id][eventName]);
			}
		}
	}
}

/**
 *  ClassName :  SUI_BookingBox
 * Arguments :
 */
var SUI_BookingBox = {
	isOpen: false,
	load: function(){
		this.isOpen = false;
		if($('bookingbox')){
			if($('hotel_ou_ville')){
				Event.observe($('hotel_ou_ville'), 'change',function(){
					if($('a04-content'))
						$('a04-content').setStyle({display:'none'});
					if($F('hotel_ou_ville') != '')
						this.toggle();
					if(this.isOpen && $F('hotel_ou_ville') == '')
						this.toggle('close');
				}.bind(this));
			}
		}
	},
	toggle: function(){
		if(!this.isOpen || arguments[0] == 'close' ){
			this.isOpen = true;
			if(arguments[0] == 'close') this.isOpen = false;
			new Effect.toggle('bookingbox', 'appear', {duration:0});
		}
	},
	close: function(){
		if($('closeexpress'))
			Event.observe($('closeexpress'), 'click', GarbageCollector.set.bindAsEventListener(GarbageCollector, 
				function(){this.toggle('close');}.bind(this))
			);
	}
}

/**
* ClassName : SUI_OverBox
* Description : Class which performs the speach balloons into 'shortlinks' element. they appear when the user is on the links.
 */
var SUI_OverBox = Class.create();
SUI_OverBox.prototype = {
	shortLinks: $('shortlinks'),
	overBox: $('overbox'),
	lastItem: null,
	isBlocked: false,
	initialize: function() {
		var thisObj = this;
		if(thisObj.shortLinks){
			thisObj.shortLinks.getElementsBySelector('li').each(function(item){
				/*Event listener*/
				Event.observe(item, 'mouseover',function(){
					if(thisObj.overBox){
						/*Performs Close button*/
						if(item.down('a').getAttribute('rel')){
							if(closeButton = thisObj.overBox.down('.' + item.down('a').getAttribute('rel') + ' .close')){
								Event.observe(closeButton, 'click',function(){
									thisObj.fadeItem(this.up('li'), null);
									thisObj.lastItem = null;
								});
							}
						}
						/*Performs Appear/Hide Behaviours*/
						callbackFunction = function(){
							if(!thisObj.isBlocked){
								thisObj.isBlocked = true;
								if(item.down('a').getAttribute('rel')){
									Effect.Appear(thisObj.overBox.down('.' + item.down('a').getAttribute('rel')), {duration: .1, afterFinish:function(){thisObj.isBlocked = false;}});
									thisObj.lastItem = item;
								}
								else thisObj.isBlocked = false;
							}
						}
						/*One balloon case*/
						if(thisObj.lastItem == null){
							callbackFunction();
						/*Hide the last box to appear the new box*/
						}else if(thisObj.lastItem.down('a').getAttribute('rel') != item.down('a').getAttribute('rel')){
							if(item.down('a').getAttribute('rel'))
								thisObj.fadeItem(thisObj.overBox.down('.'+thisObj.lastItem.down('a').getAttribute('rel')), callbackFunction);
						}
					}
				});
			});
		}
	},
	/*Method which hides box */
	fadeItem:function(item, callBackOnFinish){
		if(item != null) Effect.Fade(item, {duration: .1, afterFinish: callBackOnFinish});
	}
}

/**
* ClassName : SUI_Tabs
* Description : 
 */

var SUI_Tabs = Class.create();
SUI_Tabs.prototype = {
	afterFinish : function(){if($('accordion')) Accordian.load('accordion',1);},
	beforeStart : function(){
			var selectedItem = 
				this.tabsArea.getElementsByClassName(this.selectedItem.firstChild.getAttribute('rel'))[0];
			var lastItem = 
				this.tabsArea.getElementsByClassName(this.lastItem.firstChild.getAttribute('rel'))[0];
			if(this.lastAElement){
				lastItem.innerHTML = '';
				new Insertion.Bottom(lastItem, this.lastAElement);
			}
			this.lastAElement = selectedItem.down('a');
			var url = this.lastAElement.getAttribute('href');
			new Ajax.Request(url,{
				method: 'get',
				onComplete: function(response){
					selectedItem.innerHTML = SUI_cancelEmptyLinks(response.responseText);
					bookingEngine.init();
					SUI_BookingBox.load();
					SUI_Engine.load();
					SUI.ajax.init();
				}
			});
	},
	tabsArea : $('tabs'),
	lastItem: $('searchboxcontenttabs') ? $('searchboxcontenttabs').getElementsBySelector('li')[0] : null,
	selectedItem : null,
	isBlocked: false,
	initialize: function(){
		this.selectedItem = this.lastItem;
		this.beforeStart();
		var thisObj = this;
		$('searchboxcontenttabs').getElementsBySelector('li').each(function(item){
			Event.observe(item.firstChild, 'click',function(){
				thisObj.selectedItem = item;
				thisObj.beforeStart();
				thisObj.moveOnClass(item);
				if(thisObj.tabsArea){
					if(!thisObj.isBlocked){
						thisObj.isBlocked = true;
						if(item.firstChild.getAttribute('rel')){
							callbackFunction = function(){
								if(item != null){
									Effect.Appear(thisObj.tabsArea.down('.'+item.firstChild.getAttribute('rel')), {duration: .1, afterFinish: function(){thisObj.isBlocked = false;thisObj.lastItem = item; thisObj.afterFinish();}});
								}
							}
							Effect.Fade(thisObj.tabsArea.down('.'+thisObj.lastItem.firstChild.getAttribute('rel')), {duration: .1, afterFinish: callbackFunction});
						}
						else thisObj.isBlocked = false;
					}
				}
				return false;
			});
		});
	},
	moveOnClass:function(item){
		item.className = item.className+' on';
		if(this.lastItem.className.startsWith('last')) this.lastItem.className = 'last';
		else this.lastItem.className = '';
	}
}

/**
* ClassName : SUI_Popup
* Description : 
 */
var SUI_Popup = {
	items : [
		{id:'phonebooking', height:400, width:400},
		{id:'lostpassword', height:400, width:400},
		{id:'favoritelink', height:550, width:550}
	],
	load : function(){
		this.items.each(function(item){
			if($(item.id)){
				var href = $(item.id).getAttribute('href');
				$(item.id).setAttribute('href','#');
				Event.observe($(item.id), 'click', function() {
					var popup = window.open (href, this.getAttribute('title'), config='height='+item.height+', width='+item.width+', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
					if(item.callback)
						item.callback(popup);
					return false;
				});
			}
		});
	}
}

var SUI_ClassOn = {
	load : function(){
		var itemHref = new String();
		var itemSubHref = new String();
		var regex = new RegExp('\/.*$');
		var href = (regex.exec(document.URL.gsub('http://', ''))[0]).substring(1);
		
		$$('#header #menu li').each(function(item){
			item.className = item.className.gsub('on', '')
			itemHref = (regex.exec(item.down('a').href.gsub('http://', ''))[0]).substring(1);
			
			if($('submenu')){
				$$('#submenu li a').each(function(item2){
					itemSubHref = (regex.exec(item2.href.gsub('http://', ''))[0]).substring(1);
					if(itemSubHref == itemHref){
						href = itemHref
					}
				});
			}
			
			if(href == itemHref) item.className = item.className + " on";
		});
	}
}

/**
* ClassName :
* Description : 
 */
if(!SUI) {var SUI = {};}
SUI.ajax = {
    init : function(){
		if($('hotel_ou_ville') && $('code_langue')){
			Event.observe('hotel_ou_ville','change', GarbageCollector.set.bindAsEventListener(GarbageCollector, 
				function(e){SUI.ajax.load($F('hotel_ou_ville'), $F('code_langue'))})
			);
			this.load($F('hotel_ou_ville'), $F('code_langue'));
		}
    },
    load : function (rid, lang){
		if(rid != ''){
	        new Ajax.Request('/fichehotel/scripts-v60b/'+rid+'_'+lang+'.js',{
				method : 'get',
				onSuccess: function(transport){
					SUI.ajax.display(transport.responseText.evalJSON());
				}
			});
		}
    },
    display : function(h) { 
		var p = h.medias.find(function (m){return (m.cat == "HO");});
		var img = (p)?'<img src="'+p.file+'"/>':'';

		var t = new Template('<a class="close" id="closeexpress" title="'+I18N.express['close']+'" href="javascript:;">'+I18N.express['close']+'</a><div class="textblock"><div class="image">'+img+'</div><div class="text"><h2>#{meta.name}</h2><p>#{info.desc}</p><a class="more" href="/frm_fiche_hotel.svlt?code_hotel=#{meta.code}&code_langue=#{meta.lang}" title="'+I18N.express['more']+'" ><img alt="'+I18N.express['more']+'" src="/#{meta.lang}/images/commun/more.gif" /></a></div></div><div class="evalbox"><div><h2><img src="/imagerie/commun/title-suite-advisor.gif" alt="Suite Advisor" /></h2><p>'+I18N.express['Show hotel average']+'</p></div><iframe width="24" height="33" scrolling="no" frameborder="0" src="http://www.mysuiteblog.com/club/index.php/' + $F('code_langue') + '/advisor/widgetRating?code=#{meta.code}" id="range" class="range"></iframe><ul><li><a target="_blank" href="http://www.mysuiteblog.com/club/index.php/'+$F('code_langue')+'/advisor/index/" >'+I18N.express['Show web average']+'</a></li><li><a target="_blank" href="http://www.mysuiteblog.com/club/index.php/'+$F('code_langue')+'/advisor/index/" >'+I18N.express['Vote']+'</a></li></ul></div>');
		if($('bookingbox'))
			$('bookingbox').innerHTML = t.evaluate(h);
		SUI_BookingBox.close();
    }
}

/**
* ClassName : 
* Description : 
 */
var SUI_Engine = {
	pe : null,
	error : {
		date:{state:false, message: I18N.express['date'], element:'#dateaccordion'},
		destination:{state:false, message: I18N.express['destination'], element:'.destination'}
	},
	load : function(){
		
		if($('a04-header')){
			Event.observe($('a04-header'), 'click',  GarbageCollector.set.bindAsEventListener(GarbageCollector, 
				function(event){
					if($F('hotel_ou_ville')=='') this.error.destination.state = true;
					if($F('jour_arrivee')=='') this.error.date.state = true;				
					if(this.checkErrors()){
						Bar.doBarAction();
						Accordian.blocker['a04-header'] = false;
					}else{
						Accordian.blocker['a04-header'] = true;
					}
				}.bind(this))
			);
		}
		
		
		if($('priceaccordionmentions')){
			Event.observe($('priceaccordionquestion'), 'mouseover',  GarbageCollector.set.bindAsEventListener(GarbageCollector, 
				function(){
					Effect.Appear($('priceaccordionmentions'), {duration: 0});
					Event.observe($('priceaccordionquestion'), 'mouseout',  GarbageCollector.set.bindAsEventListener(GarbageCollector, 
						function(){Effect.Fade($('priceaccordionmentions'), {duration: 0});})
					);
				})
			);
		}
	
		if($('checkboxavantage') && $('fidelityaccordionlabel')){
			Event.observe($('checkboxavantage'), 'click', GarbageCollector.set.bindAsEventListener(GarbageCollector, 
				function(){
					if($('fidelityaccordionlabel')){
						$$('#fidelityaccordionlabel span')[0].innerHTML = 
							$F('checkboxavantage') ? I18N.express['With card'] : I18N.express['No cards'];
						$('action_SUI_BOOKING_ENGINE_EXPRESS').setValue($F('checkboxavantage') ? '': 'availability');
					}
					return false;
				})
			);
		}
		
		if($('nb_nuit')){
			Event.observe($('nb_nuit'), 'change', GarbageCollector.set.bindAsEventListener(GarbageCollector, 
				function(){
					if($('nightcontent')){
						$('nightcontent').innerHTML = $F('nb_nuit');
					}
					return false;
				})
			);
		}
		
		if($('nb_personne')){
			Event.observe($('nb_personne'), 'change', GarbageCollector.set.bindAsEventListener(GarbageCollector, 
				function(){
					if($('perscontent')){
						$('perscontent').innerHTML = $F('nb_personne');
					}
					return false;
				})
			);
		}
		
		Event.observe($('bookingEngine'), 'submit', GarbageCollector.set.bindAsEventListener(GarbageCollector, 
			function(event){
				if($F('hotel_ou_ville')=='') this.error.destination.state = true;
				if($F('jour_arrivee')=='') this.error.date.state = true;
				this.checkErrors()
				submitFormBean('label_ok', 'label_ko' , Event.element(event).getAttribute('action'), Event.element(event));
				Event.stop(event);
			}.bind(this))
		);
		
		if($('dateaccordioncontent')){
			if(!this.pe){
				this.pe = new PeriodicalExecuter(function(){
					if($('dateaccordioncontent'))
						$('dateaccordioncontent').innerHTML = $F('arrivee');
				}, 1);
			}
		}
	},
	checkErrors: function(){
		var error = false;
		var errorItem = null;
		if($('errorMessage_SUI_BOOKING_ENGINE_EXPRESS'))
			$('errorMessage_SUI_BOOKING_ENGINE_EXPRESS').setStyle({display:'none'});
		
		for (var index in this.error){
			if($$(this.error[index].element).size() != 0)
				$$(this.error[index].element)[0].className = $$(this.error[index].element)[0].className.gsub('label_ko', '');
			error = error || this.error[index].state;
			if(this.error[index].state){
				errorItem = this.error[index];
			}
			this.error[index].state = false;
		}
		
		if(error){
			alert(errorItem.message);
			if($$(errorItem.element).size() != 0) $$(errorItem.element)[0].className = $$(errorItem.element)[0].className+' label_ko';
		}
		
		return !error;
	}
}

function SUI_radio(){
	if($('destRadioList')){
		$$('#destRadioList ul li').each(function(item){
			Event.observe(item.down('input'), 'click', function(event){
				$('hotel_ou_ville').value = item.down('input').value;
			});
		});
	}
}

var SUI_deploy = {
	load: function(){
		var parent = arguments[0];
		var itemToClick = arguments[1];
		var itemToDeploy = arguments[2];
		
		if(parent){
			$$(parent+' '+itemToClick).each(function(item){
				item.className="deployplus";
				if(item.next(itemToDeploy)){
					item.next(itemToDeploy).setStyle({display: 'none'});
					Event.observe(item, 'click', function(){
						var dItem = item.next(itemToDeploy);
						dItem.update('<div>'+dItem.innerHTML+'</div>');
						
						new Effect.toggle(dItem, 'slide', {duration:.2, afterFinish: function(){
							if(item.className=="deployplus")
								item.className = "deployminus";
							else item.className = "deployplus";
							}
						});
					}.bind(this));
				}
			}.bind(this));
		}
	}
}

/* IPIX */
function IpixObject(ipixPath, ipixFile, id, dim, element, tabOption)
{
 var content = '';
 content = content +'<APPLET CODE="hm35.class" ARCHIVE="hotmedia.jar" codebase="'+ipixPath+'" ';
 content = content +'NAME="'+id+'" '+dim+' align="middle" MAYSCRIPT>';
 content = content +'<PARAM NAME="mvrfile" value="'+ipixFile+'">';
 
 maxParam = tabOption.length;
 for(i=0;i<maxParam;i++)
  {
  paramList = tabOption[i].split("|");
  content = content +'<PARAM NAME="'+paramList[0]+'" VALUE="'+paramList[1]+'(\''+paramList[2]+'\','+paramList[3]+','+paramList[4]+',\''+paramList[5]+'\')">';
  }
 
 content = content +'</APPLET>';
 document.getElementById(element).innerHTML=content; 
}

Event.observe(window, 'load', function(){
	SUI_cancelEmptyLinks();
	
	if($('formiframe')){
		SUI_radio();
		bookingEngine.init();
		Event.observe($('bookingEngine'), 'submit', function(event){
			submitFormBean('label_ok', 'label_ko' , Event.element(event).getAttribute('action'), Event.element(event));
			Event.stop(event);
		});
	}
	
	if($('form') || $$('body.static')[0]){
		SUI_ClassOn.load();
		bookingEngine.init();
		SUI_Engine.load();
		if($('flash-container')){
			var so = new SWFObject("/flash/rechgeo/rechlabel_sui.swf", "cartoLabel", "686", "308", "6", "#6E1940");
			var lang =  $$('html')[0].getAttribute('xml:lang') == 'en' ? 'gb' : $$('html')[0].getAttribute('xml:lang');
			so.addVariable("marque_var", "SUI");
			so.addVariable("langue", lang);
			so.addVariable("label", "ALL");
			so.addVariable("webApp", "suitehotel");
			so.addParam("allowScriptAccess", "always");
			so.addParam("quality", "high");
			so.addParam("scale", "noscale");
			so.addParam("wmode", "transparent");
			so.write("flash-container");
		}
	}
	
	if($$('body.ipix #video a')[0]){
		/*var paramArray = new Array();
		paramArray[0] = "boutique|doo|http://www.suitehotel.com/suitehotel/fr/decouvrez/visitez/popup/boutique_gourmande.html|600|380|menubar=no,scrollbars=no,statusbar=no";*/
		
		var link = $$('body.ipix #video a')[0];
		var dir = new RegExp('^.*\/');
		var file = new RegExp('\/[^\/]*$');
		
		IpixObject(dir.exec(link.getAttribute('href'))[0], (file.exec(link.getAttribute('href'))[0]).substring(1), "HotMedia", "width=400 height=300", "video", new Array());
	}
	
	if($$('.best-price')[0])
		SUI_deploy.load('#staticontent', 'h3', 'div');
	if($$('.help')[0])
		SUI_deploy.load('#staticontent', 'dl dt', 'dl dd');	
	
});