var $pageObjects = {
	searchTemplate : null,
	dGeoZoneName : null,
	
	_init : function(){
		this.searchTemplate = jQuery("#searchTemplate");
		this.dGeoZoneName = jQuery("#dGeoZoneName");
	}	
}

var alternativeDestinationBookingEngine;

var alternativeDestination = {
	siteCode : "ALL",
	maporamaDirectory : "accor",
	maporamaSiteOrigin : "ALL",
	lang : null,
	
	_init : function(){
		
		ajaxRequest._executeByUrl(
			"/bean/getViewBeans.action?beans=GeographicResultEmptyViewBean|SearchCriteriaViewBean|OriginViewBean|CurrenciesViewBean",
			"tAjaxErrorMessage", 
			"alternativeDestination._process(errors, response)"
		);
	},
	
	_process : function(errors, json){
		if(errors && errors.length > 0){
				ajaxRequest.showErrors(errors, "tAjaxErrorMessage");
		}
		else if (json.viewBeans && json.viewBeans.GeographicResultEmptyViewBean){
			this.lang = jQuery("html").attr("lang");
			if(this.lang == "en") this.lang = "gb";
			
			alternativeDestinationBookingEngine = new BookingEngine("newbookingEngine", "alternativeDestinationBookingEngine",true);
			alternativeDestinationBookingEngine.success(errors, json);
			
			utils.manageCallcenterDatas(json.viewBeans.OriginViewBean.accorhotels);
			$pageObjects.dGeoZoneName.text(json.viewBeans.GeographicResultEmptyViewBean.search.destination);
			this._submitForm();
			
			this.siteCode = json.viewBeans.OriginViewBean.siteCode;
			this.maporamaDirectory = json.viewBeans.OriginViewBean.maporamaDirectory;
			this.maporamaSiteOrigin = json.viewBeans.OriginViewBean.maporamaSiteOrigin;
			
			if(json.viewBeans.OriginViewBean.accorhotels && json.viewBeans.SearchCriteriaViewBean.reserverId==null) jQuery(".lnk-advancedsearchContract").css("display","inline")
			
			this.callMap(json.viewBeans.GeographicResultEmptyViewBean, json.viewBeans.OriginViewBean, json.viewBeans.CurrenciesViewBean.defaultCurrency);
			
			if(json.viewBeans.SearchCriteriaViewBean != null && json.viewBeans.SearchCriteriaViewBean.idBox != null && json.viewBeans.SearchCriteriaViewBean.idBox.commission == true){
				utils.initCommissionBox(true, this.lang);
			}
			
			jQuery("#alternativeDestination .middleContent").css("visibility", "visible");
		}
	},
	
	_submitForm : function(){
		jQuery("#searchLink").click(function(){
				ajaxRequest._executeByForm(
					"newSearch",
					"tAjaxFormErrorMessage",
					"",
					"alternativeDestination._processForm(errors, response)"
				);
				return false;
		});
	},
	
	_processForm : function(errors, json){
		if(errors && errors.length > 0){
				ajaxRequest.showErrors(errors, "tAjaxFormErrorMessage");
		}
	},
	
	callMap : function(beanGeo, beanContext, currency){
		var urlMap = "http://accor.maporama.com/idl/"+this.maporamaDirectory+"/process.aspx";
		var mapoXml = "world";
		if(this.maporamaSiteOrigin=="BUS" || this.maporamaSiteOrigin == "TRV")	mapoXml = "resa2010pro";
		urlMap += "?xsl=cm&xml="+mapoXml+"&Search=None";
		urlMap += "&Lang="+ jQuery("html").attr("lang");
		urlMap += "&siteOrigine="+this.maporamaSiteOrigin;
		if(beanGeo.zone != null && beanGeo.zone.location != null) urlMap += "&WGS_coords="+beanGeo.zone.location.latitude+"|"+beanGeo.zone.location.longitude;		
		if(beanContext && beanContext.complementaryBrands != null)urlMap += "&Brands=" + beanContext.complementaryBrands;
		if(currency)urlMap += "&Currency=" + currency;
		jQuery(".iframeMap").attr("src", urlMap);
	}
}

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