﻿$(function () {
    var searchError = $("#SearchError");
    var current_Date = new Date();
    var currentTime = current_Date.getFullYear() + "-" + (current_Date.getMonth() + 1) + "-" + current_Date.getDate();
    var next_Date = new Date(current_Date.getFullYear(), current_Date.getMonth(), current_Date.getDate() + 2);
    var nextTime = next_Date.getFullYear() + "-" + (next_Date.getMonth() + 1) + "-" + (next_Date.getDate());

    /*------------------酒店---------------*/
    $("input[name='HotelType']").click(function () {
        var value = $(this).val();
        var input_hot_name = $("#input_hot_name");
        var input_hot_name2 = $("#input_hot_name2");
        var input_city = $("#input_city");
        var input_city2 = $("#input_city2");
        var input_query = $("#input_query");
        var input_query2 = $("#input_query2");
        if (value == "S") {
            input_city.show();
            input_city2.hide();
            input_hot_name.val('').show();
            input_hot_name2.val('').hide();
            input_query.show();
            input_query2.hide();
        } else if (value == "G") {
            input_city2.show();
            input_city.hide();
            input_hot_name2.val('').show();
            input_hot_name.val('').hide();
            input_query2.show();
            input_query.hide();
        }
    });
    /*-------------------------------------*/

    //显示快速搜索层
    $(".tagView").unbind('click').bind('click', function (event) {
        var hotelContent = $(".hotelContent");
        var ticketContent = $(".ticketContent");
        var vacationContent = $(".vacationContent");
        hotelContent.hide();
        ticketContent.hide();
        vacationContent.hide();
        $(".flight").removeClass("flight_active");
        $(".vacation").removeClass("vacation_active");
        $(".hotel").removeClass("hotel_active");
        if ($(this).hasClass('hotel')) {
            $(this).addClass("hotel_active");
            hotelContent.show();
        } else if ($(this).hasClass('flight')) {
            $(this).addClass("flight_active");
            ticketContent.show();
        } else if ($(this).hasClass('vacation')) {
            $(this).addClass("vacation_active");
            vacationContent.show();
        }
        searchError.hide();
    });
    /*------------------------ 机票 ----------------------------*/

    //机票航程选择
    $(".TripType").each(function (index) {
        $(this).unbind('click').bind('click', function (event) {
            event.stopPropagation();
            var tripType = $(this).val();
            var checked = $(this).attr("checked");
            if (checked != undefined && !checked) return;
            var tripTypeD = $(".TripTypeD");
            switch (tripType) {
                case "1":
                    tripTypeD.hide();
                    searchError.hide();
                    $("#FlightDepartureDateEnd").val('');
                    $("#FlightDepartureDateEnd").hide();
                    break;
                case "2":
                    tripTypeD.show();
                    searchError.hide();
                    $("#FlightDepartureDateEnd").show();
                    break;
                default:
                    break;
            }
            $(this).blur();
        });
        if ($(this).attr("checked")) {
            $(this).click();
        }
    });

    //机票按钮验证
    $(".BtnFlightSearch").click(function (event) {
        searchError.hide();
        //出发城市
        var FlightOriginalCity = $(".FlightOriginalCity");
        var OriginalCity = $.trim(FlightOriginalCity.val());
        var Original = $.trim(FlightOriginalCity.attr("targetid"));
        //目的城市
        var FlightDestinationCity = $(".FlightDestinationCity");
        var DestinationCity = $.trim(FlightDestinationCity.val());
        var Destination = $.trim(FlightDestinationCity.attr("targetid"));
        //出发时间
        var FlightDepartureDate = $(".FlightDepartureDate");
        var DepartureDate = $.trim(FlightDepartureDate.val());
        var startDate = new Date(DepartureDate);
        //当前时间
        var currentDate = new Date(new Date() - 24 * 60 * 60 * 1000);
        if (OriginalCity == '' || Original == '') {
            searchError.text('请选择出发城市');
            searchError.css({
                left: FlightOriginalCity.position().left + FlightOriginalCity.outerWidth() + 5,
                top: FlightOriginalCity.position().top - 4
            }).show();
            return false;
        }
        if (DestinationCity == '' || Destination == '') {
            searchError.text('请选择目的城市');
            searchError.css({
                left: FlightDestinationCity.position().left + FlightDestinationCity.outerWidth() + 5,
                top: FlightDestinationCity.position().top - 4
            }).show();
            return false;
        }

        if (DepartureDate == '') {
            searchError.text('请选择出发日期');
            searchError.css({
                left: FlightDepartureDate.position().left + FlightDepartureDate.outerWidth() + 5,
                top: FlightDepartureDate.position().top - 4
            }).show();
            return false;
        }

        if (currentDate > startDate) {
            searchError.text('日期错误，航程日期不能早于当天');
            searchError.css({
                left: FlightDepartureDate.position().left + FlightDepartureDate.outerWidth() + 5,
                top: FlightDepartureDate.position().top - 4
            }).show();
            return false;
        }
        //验证出发城市和到达城市是否相同
        if (OriginalCity == DestinationCity || Original == Destination) {
            searchError.text('目的城市不能与出发城市相同');
            searchError.css({
                left: FlightOriginalCity.position().left + FlightOriginalCity.outerWidth() + 5,
                top: FlightOriginalCity.position().top - 4
            }).show();
            return false;
        }

        //往返
        if ($(".TripTypeD:visible").length > 0 && $(".FlightDepartureDateEnd:visible").length > 0) {
            var DepartureDateEnd = $(".FlightDepartureDateEnd");
            var FlightDepartureDateEnd = $.trim(DepartureDateEnd.val());

            if (FlightDepartureDateEnd == '') {
                searchError.text('请选择返回日期');
                searchError.css({
                    left: DepartureDateEnd.position().left + DepartureDateEnd.outerWidth() + 5,
                    top: DepartureDateEnd.position().top - 4
                }).show();
                return false;
            }
            var EndDate = new Date(DepartureDateEnd);

            if (currentDate > EndDate) {
                searchError.text('日期错误，航程日期不能早于当天');
                searchError.css({
                    left: DepartureDateEnd.position().left + DepartureDateEnd.outerWidth() + 5,
                    top: DepartureDateEnd.position().top - 4
                }).show();
                return false;
            }

            if (startDate > EndDate) {
                searchError.text('请重新选择返回日期');
                searchError.css({
                    left: DepartureDateEnd.position().left + DepartureDateEnd.outerWidth() + 5,
                    top: DepartureDateEnd.position().top - 4
                }).show();
                return false;
            }
        }
        //机票类型
        var ticketType = $(".TripType:checked").val();
        //出发城市
        var src = Original;
        //到达城市
        var dst = Destination;
        //出发日期
        var date = DepartureDate;
        //返程日期
        var sdate = $.trim($(".FlightDepartureDateEnd").val());
        //提交订单链接
        //{11}?flightType={0}&ticketType={1}&src={2}&dst={3}&sdst={4}&cabin={5}&date={6}&sdate={7}&airline={8}&ticketnum={9}&ticketcity={10}
        var subStr = "http://flights.holsun.com/Search.aspx?flightType=1";
        subStr += "&ticketType=" + ticketType;
        subStr += "&src=" + src;
        subStr += "&dst=" + dst;
        subStr += "&date=" + date;
        subStr += "&sdate=" + sdate;
        window.location.href = subStr;
        return true;
    });

    //-------------------------------------------------- 度假 -------------------------------------------

    $(".BtnVacationSearch").click(function () {
        var Vacation_StartCity = $(".Vacation_StartCity");
        var Vacation_DestCity = $(".Vacation_DestCity");
        var startCity = $.trim(Vacation_StartCity.val());
        var destCity = $.trim(Vacation_DestCity.val());
        var startCityCode = $.trim($(".Vacation_StartCityCode").val());
        var destCityCode = $.trim($(".Vacation_DestCityCode").val());
        if (startCity == '' || startCityCode == '') {
            searchError.text('请选择出发地');
            searchError.css({
                left: Vacation_StartCity.position().left + Vacation_StartCity.outerWidth() + 5,
                top: Vacation_StartCity.position().top - 4
            }).show();
            return false;
        }

        if (destCityCode == '' || destCity == '') {
            searchError.text('请选择目的地');
            searchError.css({
                left: Vacation_DestCity.position().left + Vacation_DestCity.outerWidth() + 5,
                top: Vacation_DestCity.position().top - 4
            }).show();
            return false;
        }

        return true;
    });

    $(".Vacation_StartCity").blur(function () {
        searchError.hide();
    });

    $(".Vacation_DestCity").blur(function () {
        searchError.hide();
    });

    //自动 出发城市
    $(".Vacation_StartCity").autocomplete({
        defaultText: "输入出发城市或↑↓选择",
        queryText: "<span>{0}</span> ",
        defaultClientSort: false,
        clientSort: false,
        clientCache: false,
        remoteData: function (queryText, inst) {
            var freeType = 'gnl';
            $('.vacation-type:checked').each(function (index) {
                if (this.checked) {
                    freeType = this.value;
                }
            });
            var uri = '/Resources/StaticResources.aspx?Type=Free100&SubType=StartCity';
            return uri + '&Text=' + queryText + '&FreeType=' + freeType;
        },
        onSelect: function (selected, inst) {
            $(".Vacation_StartCityCode").val(selected[3]);
        },
        onClose: function (selected, inst) {
            $(".Vacation_DestCity").focus();
        }
    });
    //自动 目的城市
    $(".Vacation_DestCity").autocomplete({
        defaultText: "输入到达城市或↑↓选择",
        queryText: "<span>{0}</span> ",
        defaultClientSort: false,
        clientSort: false,
        clientCache: false,
        onBeforeShow: function (queryText, inst) {
            var startCity = $(".Vacation_StartCityCode").val();
            if (startCity == '') {
                $(".Vacation_StartCity").focus();
                return false;
            }
            return true;
        },
        remoteData: function (queryText, inst) {
            var startCity = $(".Vacation_StartCityCode").val();
            var freeType = 'gnl';

            $('.vacation-type:checked').each(function (index) {
                if (this.checked) {
                    freeType = this.value;
                }
            });
            var uri = '/Resources/StaticResources.aspx?Type=Free100&SubType=DestCity';
            return uri + '&Text=' + queryText + '&FreeType=' + freeType + '&Start=' + startCity;
        },
        onSelect: function (selected, inst) {
            $(".Vacation_DestCityCode").val(selected[3]);
        }
    });
    $('.vacation-type').unbind('click').bind('click', function (event) {
        $(".Vacation_StartCity").val('');
        $(".Vacation_StartCityCode").val('');
        $(".Vacation_DestCity").val('');
        $(".Vacation_DestCityCode").val('');
    });

    //-------------------------------------------------- 酒店展示 ------------------------------------------
    HeadHotelShow();
    //--------------------------------------------------- 机票信息 -------------------------------------------
    $('.tab-flight-info').each(function (index) {
        var tab = $(this);
        $('.tab-flight-info:first').addClass('current');
        tab.unbind('click').bind('click', function () {
            $('.tab-flight-info').removeClass('current');
            $(this).addClass('current');
            var cityCode = $(this).attr("city");
            $('.flight-info').hide();
            $('.info-startcity-' + cityCode).show();
            $(this).blur();
        });
    });

    $('.flight-ticket').each(function (index) {
        var flight = $(this);
        flight.unbind('click').bind('click', function () {
            var startCity = flight.attr('startcity');
            var startCode = flight.attr('startcode');
            var endCity = flight.attr('endCity');
            var endcode = flight.attr('endcode');
            var date = flight.attr('date');
            var subStr = "http://flights.holsun.com/Search.aspx?flightType=1";
            subStr += "&ticketType=1";
            subStr += "&src=" + startCode;
            subStr += "&dst=" + endcode;
            subStr += "&date=" + date;
			//alert(subStr);
            window.location.href = subStr;
			return true;
        });
    });

    //---------------------------------------------------  积分兑换  --------------------------------------------------
    scrollPoint();



    updateLoginStatus();
});


function scrollPoint() {
    var timeout = null;
    var interval = null;

    timeout = setTimeout(function () {
        $('.point-scroll').css("margin-top", "0px");
        interval = setInterval(autoPlay, 1000);
    }, 0);
}

function autoPlay() {
    var marginTop = $('.point-scroll').css("margin-top");
    var pointTop = $('.point').height();
    var scrollTop1 = $('.point-table1').height();
    var scrollTop2 = $('.point-table2').height();
    if ((parseInt(marginTop) - parseInt(pointTop) + parseInt(scrollTop1) + parseInt(scrollTop2)) <= 0)
        $('.point-scroll').css("margin-top", "0px");
    else
        $('.point-scroll').css("margin-top", (parseInt(marginTop) - 20) + "px");

}



//{{START: 检查登录状态
function updateLoginStatus() {
    $.post("/Hotels/StaticResources.aspx",
		{ Type: 'LoginStatus' },
		function (data) {
		    if (data.indexOf('true') == 0)
		        $('.online-status').html('<span style="display:block; float:left; width: 144px; overflow: hidden;">您好！' + data.slice(4) + '</span>&nbsp;┆&nbsp;<a href="/Member/Logout.aspx" target="_blank">退&nbsp;出</a>');
		    else
		        $('.online-status').html('<a href="../Member/Login.aspx" target="_blank" class="islogin">登&nbsp;录</a>&nbsp;┆&nbsp;<a href="../Member/Join.aspx" target="_blank">注&nbsp;册</a>');
		});
}
//}}END: 检查登录状态

//首页酒店显示
function HeadHotelShow() {
    var tabList = $('.hotel-view-list-tabpage-tui');

    //初始化
    $('.hotel-view-tab').each(function (index) {
        var tab = $(this);
        //城市列表
        $('.hotel-view-tab > li > a:first').addClass('current');
        tab.unbind('click').bind('click', function () {
            //清空城市列表
            $('.hotel-view-tabpage-tui').empty();
            var type = $(this).attr('type');
            $('.hotel-view-tab').removeClass('current');
            $(this).addClass('current');
            //模板
            var __Template = $('.tag-city-list').clone();
            //显示城市
            $.ajax({
                type: "get", //使用post方法访问后台
                dataType: "text", //返回text格式的数据
                url: "/Resources/StaticResources.aspx?Type=HeadHotelCity&Text=" + type, //要访问的后台地址
                success: function (msg) {//msg为返回的数据，在这里做数据绑定
                    var arys = new Array();
                    arys = eval(msg);
                    var options = '';
                    $.each(arys, function (i, n) {
                        var row = __Template.clone();
                        row.html('<a class="' + n[2] + '" code="' + n[1] + '" href="javascript:;" title="' + n[0] + '">' + n[0] + '</a>');
                        row.show().appendTo('.hotel-view-tabpage-tui');
                    });

                    //清空现有酒店
                    tabList.empty();
                    //显示城市酒店
                    ShowTagHotel(type, arys[0][1]);
                    //绑定事件
                    ShowHotelCity();
                }
            });
            $(this).blur();
        });

        ShowHotelCity();
    });
}

//给城市绑定事件
function ShowHotelCity() {
    var tabList = $('.hotel-view-list-tabpage-tui');
    //酒店列表
    $('.hotel-view-tabpage-tui> li > a').unbind('click').bind('click', function (event, args) {
        //清空现有酒店
        tabList.empty();
        var link = $(this);
        var city = link.attr('code');
        var type = $('a[class="tag_hotel hotel-view-tab current"]').attr('type');
        //显示城市酒店
        ShowTagHotel(type, city);
        $('.hotel-view-tabpage-tui > li > a').removeClass('current');
        link.addClass('current');
        link.blur();
    });
}

//获取城市相关酒店
function ShowTagHotel(type, city) {
    //显示样式模板
    var __PriceRowTemplate = $('.hotel-list-info').clone();
    //显示城市酒店
    $.ajax({
        type: "get", //使用post方法访问后台
        dataType: "text", //返回text格式的数据
        url: "/Resources/StaticResources.aspx?Type=HeadHotel&Text=" + type + "&city=" + city, //要访问的后台地址
        success: function (msg) {//msg为返回的数据，在这里做数据绑定
            var arys = new Array();
            arys = eval(msg);
            var options = '';
            $.each(arys, function (i, n) {
                var row = __PriceRowTemplate.clone();
                row.find('.name').html('<a href=' + n[5] + ' title=' + n[1] + ' class="darkBlue" style="cursor: pointer;" target="_blank">' + n[1] + '</a>');
                row.find('.price').html('<img src="Resources/Static/Hotel/Images/Star/star' + n[8] + '.gif" visible=' + n[2] + ' />');
                row.find('.price').html(n[7] + '元');
                row.find('.pic').html('<a href=' + n[5] + ' style="cursor: pointer;" target="_blank"><img src=' + n[6] + ' height="60" width="80" /></a>');
                row.show().appendTo('.hotel-view-list-tabpage-tui');
            });
        }
    });
}


