var $myObjects = {
	
	hotelsResultTemplate : null,
	loader : null,
	titleResultNbHotelSpan : null,
	titleResultSingleHotelSpan : null,
	titleResultSeveralHotelsSpan : null,
	titleResultDestinationSpans : null,
	listeMinorZones : null,
	
	_init : function(){
		this.hotelsResultTemplate = jQuery("#templateResult");
		this.loader = jQuery("#loading");
		this.titleResultNbHotelSpan = jQuery(".tNbHotelsAvailable");
		this.titleResultSingleHotelSpan = jQuery("span.tSingleHotel");
		this.titleResultSeveralHotelsSpan = jQuery("span.tSeveralHotels");
		this.titleResultDestinationSpans = jQuery(".tDestination");
	}
}

var hotelCityBookingEngine;

var hotelCity = {
   datas : null,
   context : null,
   lang : null,
   
   init : function(){
   	this.lang = jQuery("html").attr("lang");
   	if(this.lang == "en") this.lang = "gb";
   	ajaxRequest._executeByUrl(
			"/bean/getViewBeans.action?beans=GeographicResultBigCityViewBean|CurrenciesViewBean|SearchCriteriaViewBean|OriginViewBean",
			"tAjaxError", 
			"hotelCity._success(errors, response)"
		);
   },
   
   _success : function(errors, json){
   	if(errors && errors.length > 0){
			ajaxRequest.showErrors(errors, "tAjaxError");
			$myObjects.loader.hide();
			jQuery(".middleContent").css("visibility", "visible");
		}
		else if (json.viewBeans && json.viewBeans.GeographicResultBigCityViewBean && json.viewBeans.CurrenciesViewBean){
			utils.manageCallcenterDatas(json.viewBeans.OriginViewBean.accorhotels);
			hotelCityBookingEngine = new BookingEngine("newbookingEngine", "hotelCityBookingEngine", true);
			hotelCityBookingEngine.success(errors, json);
			hotelCity.datas = json;
			hotelCity.context = $myObjects.hotelsResultTemplate.attr("class");
			hotelCity._writeMap(hotelCity.context);
			if(json.viewBeans.OriginViewBean.accorhotels && json.viewBeans.SearchCriteriaViewBean.reserverId==null) jQuery(".lnk-advancedsearchContract").css("display","inline")
			
			if(json.viewBeans.GeographicResultBigCityViewBean.zone)
			$myObjects.titleResultDestinationSpans.text(json.viewBeans.GeographicResultBigCityViewBean.zone.geoZoneName);
			$myObjects.titleResultNbHotelSpan.text(json.viewBeans.GeographicResultBigCityViewBean.zone.hotelNumber);
			$myObjects.titleResultSingleHotelSpan.hide();
			$myObjects.titleResultSeveralHotelsSpan.hide();
			
			if(hotelCity.datas.viewBeans.GeographicResultBigCityViewBean.minorZones.length > 0){
				$myObjects.listeMinorZones = jQuery("#minorZones");
				$myObjects.listeMinorZones.find(".tSingleHotelMinor").hide();
				$myObjects.listeMinorZones.find(".tSeveralHotelsMinor").hide();
				this._createAndShowMinorZonesForm();
				if(json.viewBeans.GeographicResultBigCityViewBean.zone.hotelNumber == 1) $myObjects.listeMinorZones.find(".tSingleHotelMinor").show();
				else if(json.viewBeans.GeographicResultBigCityViewBean.zone.hotelNumber > 1) $myObjects.listeMinorZones.find(".tSeveralHotelsMinor").show();
			}
			else{
				if(json.viewBeans.GeographicResultBigCityViewBean.zone.hotelNumber == 1) $myObjects.titleResultSingleHotelSpan.show();
				else if(json.viewBeans.GeographicResultBigCityViewBean.zone.hotelNumber > 1) $myObjects.titleResultSeveralHotelsSpan.show();
			}
			
			//affichage
			$myObjects.loader.hide();
			jQuery(".middleContent").css("visibility", "visible");
		}
		else{
			jQuery(".middleContent").text("Plus de session").css("visibility", "visible").css("text-align", "center");
		}
   },
   
   _createAndShowMinorZonesForm : function(){
		jQuery(hotelCity.datas.viewBeans.GeographicResultBigCityViewBean.minorZones).each(function(){
			var oOption = document.createElement("option");
			oOption.value=this.geoZoneType+"-"+this.geoZoneCode;
			oOption.innerHTML = this.geoZoneName;
			$myObjects.listeMinorZones.find("select").append(oOption);
		});
		
		$myObjects.listeMinorZones.find("select").change(function(){
			if(this.value != "0"){
				var geoType = this.value.split("-")[0];
				var geoCode = this.value.split("-")[1];
				
				document.location.href = $myObjects.listeMinorZones.attr("action")+"?search.geoZone.geoZoneCode="+geoCode+"&search.geoZone.geoZoneType="+geoType;
			}
		});
		
		$myObjects.listeMinorZones.show();
	},
   
  _getAvailableAreas:function(context) {
	
  	var datas = hotelCity.datas.viewBeans.GeographicResultBigCityViewBean.subZones;
  	var availableAreas = new Array();
  	jQuery(datas).each(function(){
  		if(this.geoZoneCode && this.geoZoneCode != "" &&  this.geoZoneType && this.geoZoneType != "")
  				availableAreas.push(this.geoZoneType+"-"+this.geoZoneCode);
  	});
  	var sAvailableAreas = availableAreas.join(",");
  	
  	switch(context){
  		case "paris":
  			sAvailableAreas += ",VI-V7496,CI-CI150,CI-CI226,CI-CI082,CI-CI229,CI-CI402,CI-CI317,SI-SI7";
  			return document.getElementById("paris").sendAviables(sAvailableAreas);
  			break;
  			
  		case "lyon":
  			return document.getElementById("lyon").sendAviables(sAvailableAreas);
  			break;
  	}
		
	},
	
	_callHotelsList : function(param){
	
		var params = param.split("-");
		if(params[0] && params[0].length == 2){
			var typeGeo = params[0];
			var codeGeo = params[1];
		}
		else{
			var typeGeo = "CI";
			var codeGeo = params[0];
		}
		
		switch(hotelCity.context){
  		case "paris":
  			document.getElementById("paris").jsCallBack();
			break;
  		case "lyon":
  			document.getElementById("lyon").jsCallBack();
			break;
		}
		
		document.location.href = "/geo/searchZone.action?search.geoZone.geoZoneCode="+codeGeo+"&search.geoZone.geoZoneType="+typeGeo;
	},
	
	_callClickTag : function(param){
		var numLangCode = getCodeLangue(this.lang);
		return xt_med('C', numLangCode,param,'N')
	},
	
	_writeMap : function(context) {
  	
		switch(context){
			case "paris" :
				fo = new SWFObject("/flash/booking/paris.swf?lang="+hotelCity.lang, "paris", "499", "421", "9.0.0.0", "#ffffff", true);
				fo.addVariable("xmlPath","/flash/booking/xml/paris-"+hotelCity.lang+".xml");
			break;
			
			case "lyon" :
				fo = new SWFObject("/flash/booking/lyon.swf?lang="+hotelCity.lang, "lyon", "499", "421", "9.0.0.0", "#ffffff", true);
				fo.addVariable("xmlPath","/flash/booking/xml/lyon-"+hotelCity.lang+".xml");
			break;
		}

		fo.addParam("scale", "noScale");
		fo.addParam("wmode", "opaque");
		fo.addParam("allowScriptAccess", "always");
		fo.write("flashMap");
	}
  
};

jQuery(document).ready(function(){
	utils.rewriteTitle();
	jQuery.noConflict();
	utils.tooltips.init();
	popins.init();
	$myObjects._init();
	hotelCity.init();
});
