var $pageObjects = {
	searchTemplate : null,
	dGeoZoneName : null,
	dHotelsNumber : null,
	pagerButtons : null,
	pagerFirstPageBut : null,
	pagerLastPageBut : null,
	pagerNextPageBut : null,
	pagerPreviousPageBut : null,
	pagers : null,
	loader : null,
	
	_init : function(){
		this.searchTemplate = jQuery("#searchTemplate");
		this.dGeoZoneName = jQuery(".dGeoZoneName");
		this.dHotelsNumber = jQuery("#dHotelsNumber");
		this.pagerButtons = jQuery("li.numPage ul");
		this.pagerFirstPageBut = jQuery(".pageNav .firstPage a");
		this.pagerLastPageBut = jQuery(".pageNav .lastPage a");
		this.pagerNextPageBut = jQuery(".pageNav .nextPage a");
		this.pagerPreviousPageBut = jQuery(".pageNav .prevPage a");
		this.pagers = jQuery("div.pageNav");
		this.loader = jQuery("#loading");
	}	
}

var searchMapBookingEngine;

var searchList = {
	
	lang : null,
	hotelsPerPage : 4,
	currentPage : 0,
	totalPages : 0,
	datas : null,
	siteCode : "ALL",
	maporamaDirectory : "accor",
	maporamaSiteOrigin : "ALL",
	
	_init : function(){
		
		this.lang = jQuery("html").attr("lang");
		if(this.lang == "en") this.lang = "gb";
		$pageObjects.searchTemplate.setTemplateURL("/"+this.lang+"/templates-v66/booking/refine-search-list.tpl");
		ajaxRequest._executeByUrl(
			"/bean/getViewBeans.action?beans=GeographicResultMapViewBean|SearchCriteriaViewBean|OriginViewBean|CurrenciesViewBean",
			"errorMessage", 
			"searchList._process(errors, response)"
		);
	},
	
	_process : function(errors, json){
		if(errors && errors.length > 0){
				ajaxRequest.showErrors(errors, "errorMessage");
			}
			else if (json.viewBeans && json.viewBeans.GeographicResultMapViewBean){
				new Calendar('newbookingEngine',json.viewBeans.SearchCriteriaViewBean.bookingEngineRules.maxDateResa,json.viewBeans.SearchCriteriaViewBean.bookingEngineRules.maxPeriodResa);
				searchMapBookingEngine = new BookingEngine("newbookingEngine", "searchMapBookingEngine", true);
				searchMapBookingEngine.success(errors, json);
				utils.manageCallcenterDatas(json.viewBeans.OriginViewBean.accorhotels);
				
				if(json.viewBeans.OriginViewBean.accorhotels && json.viewBeans.SearchCriteriaViewBean.reserverId==null) jQuery(".lnk-advancedsearchContract").css("display","inline")
				
				this.datas = json.viewBeans.GeographicResultMapViewBean;
				$pageObjects.dGeoZoneName.text(this.datas.zone.geoZoneName);
				$pageObjects.dHotelsNumber.text(this.datas.zone.hotelNumber);
				if (this.datas.subZones.length > 0) {
					if(Math.floor(this.datas.subZones.length / this.hotelsPerPage) > 10){
						this.hotelsPerPage = Math.floor(this.datas.subZones.length / 9);
					}
					$pageObjects.searchTemplate.setParam('start', 0);
					$pageObjects.searchTemplate.setParam('step', this.hotelsPerPage);
					$pageObjects.searchTemplate.processTemplate(this.datas);
					if(this.datas.subZones.length > this.hotelsPerPage){
						this._initNavigationPager();
						this._writePager();
					}
					else{
						$pageObjects.pagers.hide();
					}
				} else {
					jQuery("#refineSearchMap .middleContent .listZones").hide();
				}
				
				this.siteCode = json.viewBeans.OriginViewBean.siteCode;
				this.maporamaDirectory = json.viewBeans.OriginViewBean.maporamaDirectory;
				this.maporamaSiteOrigin = json.viewBeans.OriginViewBean.maporamaSiteOrigin;
				
				this.callMap(json.viewBeans.OriginViewBean, json.viewBeans.GeographicResultMapViewBean.complementaryBrand, json.viewBeans.CurrenciesViewBean.defaultCurrency);
				utils.tooltips.init();
				
				if(json.viewBeans.SearchCriteriaViewBean != null && json.viewBeans.SearchCriteriaViewBean.idBox != null && json.viewBeans.SearchCriteriaViewBean.idBox.commission == true){
					utils.initCommissionBox(true, this.lang);
				}
				
				jQuery("#refineSearchMap .middleContent").css("visibility", "visible");
			}
	},
	
	// Initialise la pagination
	_initNavigationPager : function(){
		this.totalPages = Math.floor(this.datas.subZones.length/this.hotelsPerPage);
		if(this.datas.subZones.length%this.hotelsPerPage > 0) this.totalPages += 1;
		
		$pageObjects.pagerFirstPageBut.click(function(){
			if(!$pageObjects.pagerFirstPageBut.hasClass("disabled")){
				searchList.currentPage = 0;
				searchList._doTransition();
			}
			return false;
		});
		
		$pageObjects.pagerLastPageBut.click(function(){
			if(!$pageObjects.pagerLastPageBut.hasClass("disabled")){
				searchList.currentPage = searchList.totalPages-1;
				searchList._doTransition();
			}
			return false;
		});
		
		$pageObjects.pagerPreviousPageBut.click(function(){
			if(!$pageObjects.pagerPreviousPageBut.hasClass("disabled")){
				searchList.currentPage -= 1;
				searchList._doTransition();
			}
			return false;
		});
		
		$pageObjects.pagerNextPageBut.click(function(){
			if(!$pageObjects.pagerNextPageBut.hasClass("disabled")){
				searchList.currentPage += 1;
				searchList._doTransition();
			}
			return false;
		});
	},
	
	_writePager : function(){
		$pageObjects.pagerButtons.empty();
		for(var i=0; i < this.totalPages; i++){
			if(this.currentPage != i) $pageObjects.pagerButtons.append("<li><a href='#'>"+(i+1)+"</a></li>");
			else $pageObjects.pagerButtons.append("<li><span>"+(i+1)+"</span></li>");
		}
		
		if(this.currentPage ==  0){
			$pageObjects.pagerFirstPageBut.addClass("disabled");
			$pageObjects.pagerPreviousPageBut.addClass("disabled");
		}
		else{
				if($pageObjects.pagerFirstPageBut.hasClass("disabled"))$pageObjects.pagerFirstPageBut.removeClass("disabled");
				if($pageObjects.pagerPreviousPageBut.hasClass("disabled"))$pageObjects.pagerPreviousPageBut.removeClass("disabled");
		}
		
		if(this.currentPage ==  searchList.totalPages-1){
			$pageObjects.pagerLastPageBut.addClass("disabled");
			$pageObjects.pagerNextPageBut.addClass("disabled");
		}
		else{
				if($pageObjects.pagerLastPageBut.hasClass("disabled"))$pageObjects.pagerLastPageBut.removeClass("disabled");
				if($pageObjects.pagerNextPageBut.hasClass("disabled"))$pageObjects.pagerNextPageBut.removeClass("disabled");
		}
		
		jQuery("li.numPage a").click(function(){
			searchList.currentPage = jQuery(this).text()-1;
			searchList._doTransition();
			return false;
		});
	},
	
	_doTransition : function(){
		$pageObjects.pagers.slideUp("normal");
		$pageObjects.loader.css("height", $pageObjects.searchTemplate.height()+"px").fadeIn("normal", function(){
			searchList._updateTemplate(searchList.currentPage);
			searchList._writePager();
			$pageObjects.pagers.slideDown("normal");
			jQuery(this).css("height", $pageObjects.searchTemplate.height()+"px").fadeOut("normal");
		});
	},
	
	_updateTemplate : function(start){
		$pageObjects.searchTemplate.setParam('start', start*this.hotelsPerPage);
		$pageObjects.searchTemplate.setParam('step', this.hotelsPerPage);
		$pageObjects.searchTemplate.processTemplate(this.datas);
	},
	
	callMap : function(beanContext, showCompBrand, 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=Too";
		urlMap += "&Lang="+ jQuery("html").attr("lang");
		urlMap += "&siteOrigine="+this.maporamaSiteOrigin;
		urlMap += "&Code_Geo="+this.datas.zone.geoZoneCode;	
		if(beanContext && beanContext.complementaryBrands != null && showCompBrand != null)urlMap += "&Brands=" + beanContext.complementaryBrands;
		if(currency) urlMap += "&Currency=" + currency;
		jQuery("#themap").attr("src", urlMap);
	}
}

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