var $pageObjects = {
	searchTemplate : null,
	pagerButtons : null,
	pagerFirstPageBut : null,
	pagerLastPageBut : null,
	pagerNextPageBut : null,
	pagerPreviousPageBut : null,
	pagers : null,
	loader : null,
	
	_init : function(){
		this.searchTemplate = jQuery("#searchTemplate");
		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 searchDestinationBookingEngine;

var searchDestination = {
	
	lang : null,
	hotelsPerPage : 8,
	currentPage : 0,
	totalPages : 0,
	datas : null,
	
	_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-destination.tpl");
		ajaxRequest._executeByUrl(
			"/bean/getViewBeans.action?beans=GeographicResultMultipleZonesViewBean|SearchCriteriaViewBean|OriginViewBean",
			"errorMessage", 
			"searchDestination._process(errors, response)"
		);
	},
	
	_process : function(errors, json){
		if(errors && errors.length > 0){
				ajaxRequest.showErrors(errors, "errorMessage");
				jQuery("#refineSearchDestination .middleContent").css("visibility", "visible");
			}
			else if (json.viewBeans && json.viewBeans.GeographicResultMultipleZonesViewBean){
				searchDestinationBookingEngine = new BookingEngine("newbookingEngine", "searchDestinationBookingEngine", true);
				searchDestinationBookingEngine.success(errors, json);
				utils.manageCallcenterDatas(json.viewBeans.OriginViewBean.accorhotels);
				searchDestination.datas = json.viewBeans.GeographicResultMultipleZonesViewBean;
				
				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', searchDestination.hotelsPerPage);
					$pageObjects.searchTemplate.processTemplate(searchDestination.datas);
					if(searchDestination.datas.subZones.length > searchDestination.hotelsPerPage){
						searchDestination._initNavigationPager();
						searchDestination._writePager();
					}
					else{
						$pageObjects.pagers.hide();
					}
				} else {
					jQuery("#refineSearchMap .middleContent .listZones").hide();
				}
				
				utils.tooltips.init();
				
				if(json.viewBeans.SearchCriteriaViewBean != null && json.viewBeans.SearchCriteriaViewBean.idBox != null && json.viewBeans.SearchCriteriaViewBean.idBox.commission == true){
					utils.initCommissionBox(true, this.lang);
				}
				if(json.viewBeans.OriginViewBean != null && json.viewBeans.OriginViewBean.accorhotels && json.viewBeans.SearchCriteriaViewBean.reserverId==null) jQuery(".lnk-advancedsearchContract").css("display","inline")		
				jQuery("#refineSearchDestination .middleContent").css("visibility", "visible");
			}
	},
	
	// Initialise la pagination
	_initNavigationPager : function(){
		searchDestination.totalPages = Math.floor(searchDestination.datas.subZones.length/searchDestination.hotelsPerPage);
		if(searchDestination.datas.subZones.length%searchDestination.hotelsPerPage > 0) searchDestination.totalPages += 1;
		
		$pageObjects.pagerFirstPageBut.click(function(){
			if(!$pageObjects.pagerFirstPageBut.hasClass("disabled")){
				searchDestination.currentPage = 0;
				searchDestination._doTransition();
			}
			return false;
		});
		
		$pageObjects.pagerLastPageBut.click(function(){
			if(!$pageObjects.pagerLastPageBut.hasClass("disabled")){
				searchDestination.currentPage = searchDestination.totalPages-1;
				searchDestination._doTransition();
			}
			return false;
		});
		
		$pageObjects.pagerPreviousPageBut.click(function(){
			if(!$pageObjects.pagerPreviousPageBut.hasClass("disabled")){
				searchDestination.currentPage -= 1;
				searchDestination._doTransition();
			}
			return false;
		});
		
		$pageObjects.pagerNextPageBut.click(function(){
			if(!$pageObjects.pagerNextPageBut.hasClass("disabled")){
				searchDestination.currentPage += 1;
				searchDestination._doTransition();
			}
			return false;
		});
	},
	
	_writePager : function(){
		$pageObjects.pagerButtons.empty();
		for(var i=0; i < searchDestination.totalPages; i++){
			if(searchDestination.currentPage != i) $pageObjects.pagerButtons.append("<li><a href='#'>"+(i+1)+"</a></li>");
			else $pageObjects.pagerButtons.append("<li><span>"+(i+1)+"</span></li>");
		}
		
		if(searchDestination.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(searchDestination.currentPage ==  searchDestination.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(){
			searchDestination.currentPage = jQuery(this).text()-1;
			searchDestination._doTransition();
			return false;
		});
	},
	
	_doTransition : function(){
		$pageObjects.pagers.slideUp("normal");
		$pageObjects.loader.css("height", $pageObjects.searchTemplate.height()+"px").fadeIn("normal", function(){
			searchDestination._updateTemplate(searchDestination.currentPage);
			searchDestination._writePager();
			$pageObjects.pagers.slideDown("normal");
			jQuery(this).css("height", $pageObjects.searchTemplate.height()+"px").fadeOut("normal");
		});
	},
	
	_updateTemplate : function(start){
		$pageObjects.searchTemplate.setParam('start', start*searchDestination.hotelsPerPage);
		$pageObjects.searchTemplate.setParam('step', searchDestination.hotelsPerPage);
		$pageObjects.searchTemplate.processTemplate(searchDestination.datas);
	}
}

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