/**
 * 网站机票功能js
 */
/**
 * 初始化航程类型的选择按钮
 */
function init_radio_flightway(){
	var flightway = $("input[name='flightway']:checked").val();
	init_flightway_input(flightway);
	$("input[name='flightway']").click(function(){		
		flightway = $("input[name='flightway']:checked").val();
		init_flightway_input(flightway);
	});
}
/**
 * 根据用户默认的航程类型初始化输入项
 * @param flightway
 */
function init_flightway_input(flightway){
	switch(flightway){
	case '0':
		$("#returnDate1").hide();
		$("#secondFlight").hide();
		$("#departureDate2").hide();
	break;
	case '1':
		$("#returnDate1").show();
		$("#secondFlight").hide();
		$("#departureDate2").hide();
		break;
	case '2':
		$("#returnDate1").hide();
		$("#secondFlight").show();
		$("#departureDate2").show();
	}
}
/**
 * 初始化航空公司选择连接
 */
function init_airline_link(){
	$("#preferredAirline").click(function(){
		$("#airlineList").toggle();
	});
}
/**
 * 初始化机票日期组件
 */
function init_flight_date(){
	
	$("#depart_date1").datepicker({
		showOn: "both",
		buttonImage: webContext + "/images/calendar.gif",
		buttonImageOnly: true,
		gotoCurrent: true,
		numberOfMonths: 2,
		dateFormat: 'dd/mm/yy',
		minDate: 1,
		onSelect: function(dateText, inst){
			var flightway = $("input[name='flightway']:checked").val();
			date = $.datepicker.parseDate(inst.settings.dateFormat,dateText, inst.settings );
			if(flightway == '1'){
				$("#return_date1").datepicker('option','minDate',date);
			}else if(flightway == '2'){
				$("#depart_date2").datepicker('option','minDate',date);
			}
		}
	});
	$("#return_date1").datepicker({
		showOn: "both",
		buttonImage: webContext + "/images/calendar.gif",
		buttonImageOnly: true,
		gotoCurrent: true,
		numberOfMonths: 2,
		dateFormat: 'dd/mm/yy',
		minDate: 1		
	});
	$("#depart_date2").datepicker({
		showOn: "both",
		buttonImage: webContext + "/images/calendar.gif",
		buttonImageOnly: true,
		gotoCurrent: true,
		numberOfMonths: 2,
		dateFormat: 'dd/mm/yy',
		minDate: 1		
	});
}
/**
 * 初始化机场自动填充组件
 */
function init_depart_city1_airport(){
	var options = {
		source:function(request,response){
			$.getJSON(webContext + "/airPortCode/findAirPortListOfSpell",{citySpell: request.term},function(data){
				response($.map(data,function(item){
					return{
						label: item.cityName + "("+(item.shortSpell)+")",
						value: item.airportCode
					}
				}));
			});
		},
		minLength: 0,
		delay: 0,
		focus: function( event, ui ) {
			$( "#depart_city1_airport" ).val(ui.item.label);
			$("#depart_city1").empty();
			return false;
		},
		select: function(event,ui){	
			$("#depart_city1_airport").val(ui.item.label);
			$("#depart_city1").val(ui.item.value);			
			return false;			
		}
	};
	
	$("#depart_city1_airport").autocomplete(options);	
	//$("#dest_city1").autocomplete(options);
	//$("#depart_city2").autocomplete(options);
	//$("#dest_city2").autocomplete(options);	
}
function init_dest_city1_airport(){
	var options = {
			source:function(request,response){
				$.getJSON(webContext + "/airPortCode/findAirPortListOfSpell",{citySpell: request.term},function(data){
					response($.map(data,function(item){
						return{
							label: item.cityName + "("+(item.shortSpell)+")",
							value: item.airportCode
						}
					}));
				});
			},
			minLength: 0,
			delay: 0,
			focus: function( event, ui ) {
				$( "#dest_city1_airport" ).val(ui.item.label);
				$("#dest_city1").empty();
				return false;
			},
			select: function(event,ui){	
				$("#dest_city1_airport").val(ui.item.label);
				$("#dest_city1").val(ui.item.value);			
				return false;			
			}
		};
	$("#dest_city1_airport").autocomplete(options);
}
function init_depart_city2_airport(){
	var options = {
			source:function(request,response){
				$.getJSON(webContext + "/airPortCode/findAirPortListOfSpell",{citySpell: request.term},function(data){
					response($.map(data,function(item){
						return{
							label: item.cityName + "("+(item.shortSpell)+")",
							value: item.airportCode
						}
					}));
				});
			},
			minLength: 0,
			delay: 0,
			focus: function( event, ui ) {
				$( "#depart_city2_airport" ).val(ui.item.label);
				$("#depart_city2").empty();
				return false;
			},
			select: function(event,ui){	
				$("#depart_city2_airport").val(ui.item.label);
				$("#depart_city2").val(ui.item.value);			
				return false;			
			}
		};
	$("#depart_city2_airport").autocomplete(options);
}
function init_dest_city2_airport(){
	var options = {
			source:function(request,response){
				$.getJSON(webContext + "/airPortCode/findAirPortListOfSpell",{citySpell: request.term},function(data){
					response($.map(data,function(item){
						return{
							label: item.cityName + "("+(item.shortSpell)+")",
							value: item.airportCode
						}
					}));
				});
			},
			minLength: 0,
			delay: 0,
			focus: function( event, ui ) {
				$( "#dest_city2_airport" ).val(ui.item.label);
				$("#dest_city2").empty();
				return false;
			},
			select: function(event,ui){	
				$("#dest_city2_airport").val(ui.item.label);
				$("#dest_city2").val(ui.item.value);			
				return false;			
			}
		};
	$("#dest_city2_airport").autocomplete(options);
}
/**
 * 机票查询表单提交前需要检查
 * @returns
 */
function sumbit_before_check(){
	return $('#flightsearchtabform').validationEngine('validate');
}
/**
 * 打开查询航班等待模式窗口
 */
var searchWaitModal;
function init_flight_search_loading(){
	var loadingUrl = webContext + "/flightSearch/flightLoading.gsp";	
	searchWaitModal = showWaitModal('',loadingUrl,"flightSearchLoading",400,500);
	
}
//关闭等待窗口
function close_flight_search_loading(){	
	searchWaitModal.hide();
}
//查询成功处理
function search_flight_success(){
	document.location.href = webContext + "/flightSearch/showFlightList";
}
//处理航班排序的问题
function init_flight_sort_radio(){
	$("input[name='flightSort']").click(function(){
		$("#flightSortForm").submit();
	});
}
/**
 * 舱位显示切换:最低票价舱位和全部舱位的现实，隐藏功能
 */
function display_cabin(flightIndex){
	$("#cabinList"+flightIndex).toggle();
	$("#cheapest_"+flightIndex).toggle();
	var display = $("#cabinList"+flightIndex).attr("style");
	if(display == "display: none;"){
		$("#displayCabinImg"+flightIndex).attr("src",webContext + "/images/flight/jiao_x.gif");		
	}else{
		$("#displayCabinImg"+flightIndex).attr("src",webContext + "/images/flight/jiao_s.gif");		
	}
}
/**
 * 用户选择的航班和舱位
 * @param flightCode：航班号
 * @param cabinCode：舱位号
 */
function select_flight(flightCode,cabinCode){
	$("#selectFlightNo").val(flightCode);
	$("#selectCabinCode").val(cabinCode);
	$("#selectFlightForm").submit();
}
function init_flightSearch_tabs(){
	$("#flightSearchTabs").tabs();
}


