﻿var lastnavdivid = "nav_1";
var gmapready = 0;
$(function () {
    gmapready = 1;
    //酒店查询页按钮切换事件
    $("#nav_1,#nav_2,#nav_3").mousedown(function () {
        $("#" + lastnavdivid).attr("className", "select_out");
        $("#" + lastnavdivid.substr(0, 3) + "content" + lastnavdivid.substr(3, 2)).hide();
        $(this).attr("className", "select_over");
        $("#" + $(this).attr("id").substr(0, 3) + "content" + $(this).attr("id").substr(3, 2)).show();
        lastnavdivid = $(this).attr("id");
        if ($(this).attr("id") == "nav_3" && gmapready == 1) {
            $(".gmapflag").css("display", "block");
            setTimeout(delayshowmappoint, 100);

        }
        else {
            $(".gmapflag").css("display", "none");
        }

    });

    $(".hotel_open").each(
        function () {
            var index = $(this).attr("id").substr(11);
            if ($("#hoteldetail_" + index).children('tbody').children("tr").length <= 4) {
                $("#HshowClick_" + index).hide();
            }
        }
    );

    $(".hotel_title").wTooltip({ offsetY: -25 });

});

function delayshowmappoint() {
    if (firstloadmap != 1) {
        OnLoad();
        DrawPointList();
        firstloadmap = 1;
    }
    if (ajaxloadmap == 1) {
        DrawPointList();
        ajaxloadmap = 0;
    }
}

//酒店房价展开
function expandroomprice(obj) {
    var index = $(obj).attr("id").substr(11);
    if ($(obj).html().substr(0, 4) == "全部房型") {
        $(obj).html("隐藏房型<img src='/0-Static/images/arrow_hide.gif' align='absmiddle' />");
        $("#panel_hotelroom_" + index).css("height", "auto");
            }
            else {
                $(obj).html("全部房型<img src='/0-Static/images/arrow_display.gif' align='absmiddle' />");
                if ($.browser.msie)
                    $("#panel_hotelroom_" + index).css("height", "133px");
                else
                    $("#panel_hotelroom_" + index).css("height", "131px");

            }
}
    
//打开预订页面
function CreateResv(obj, roomtype, hotelid, present,isNotLogin) {

    var startdate = $("#datepickerin").val();
    var enddate = $("#datepickerout").val();
    var url = "/Resv/create.aspx?hotelid=" + hotelid + "&roomtype=" + roomtype + "&startdate=" + startdate + "&enddate=" + enddate + "&present=" + present;
    if (isNotLogin == "True") {
        showIframe(url, 480, 285);
    }
    else {
        window.location.href = url;
    }
} 
//打开登录页面
function ToLogin(url) {
    showIframe(url, 480, 285);
}

//////////////////////////////////////////////////////////////////////////////
/////////////// googlemap  ///////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
var gMap;
var gMap2;
var points = [];
var markers = [];
var bounds;
var firstloadmap = 0;
var ajaxloadmap = 0;
var ajaxroominforesult = "";

// Set up the map and the local searcher.
function OnLoad() {
    if (typeof (GMap2) != "undefined") {
        // Initialize the map
        gMap = new GMap2(document.getElementById("map"));
        gMap.addControl(new GLargeMapControl());
        bounds = new GLatLngBounds();
        SetPoints();
        //测试点集的中间点
        var smallpoints = [];
        for (var i = 0; i < points.length; i++) {
            if (points[i].lat != undefined && points[i].lng != undefined) {
                var smallpoint = {};
                smallpoint.lat = points[i].lat;
                smallpoint.lng = points[i].lng;
                smallpoints.push(smallpoint);
            }
        }
        if (smallpoints.length == 1) {
            gMap.setCenter(new GLatLng(smallpoints[0].lat, smallpoints[0].lng), 14);
        }
        if (smallpoints.length > 1) {
            var center = GetCenterLatLng(smallpoints);
            gMap.setCenter(new GLatLng(center.lat, center.lng), 0);
        } 
    }
}

function DrawPointList() {
    for (var i = 0; i < points.length; i++) {
        if (points[i].lat != undefined && points[i].lng != undefined) {
            bounds.extend(new GLatLng(points[i].lat, points[i].lng));
            ShowInfo(points[i]);
        }
        else {
            markers.push(null);
        }
    }
    if (points.length > 1) {
        zoomfix();
    }
}

function ShowInfo(point) {

    var gSmallIcon = new GIcon();
    gSmallIcon.image = HTSEO_Img+"/images/gmarker_" + point.index + ".png";
    gSmallIcon.iconSize = new GSize(21, 21);
    gSmallIcon.iconAnchor = new GPoint(6, 20);
    gSmallIcon.infoWindowAnchor = new GPoint(8, 5);

    var marker = new GMarker(new GLatLng(point.lat, point.lng), { icon: gSmallIcon });
    GEvent.addListener(marker, "infowindowopen", function() {
        showroominfo();
    });
    gMap.addOverlay(marker);


    GEvent.bind(marker, 'click', this, function() {
        var infoDetail = GetInfoHtml(point);
        marker.openInfoWindow(infoDetail);
    });
    markers.push(marker);

}

function SetPoints() {
    $("input[name='gmapinfo']").each(function(i) {
        var point = {};
        if ($(this).val().split('||')[0].split('|').length == 2) {
            point.lat = $(this).val().split('||')[0].split('|')[1];
            point.lng = $(this).val().split('||')[0].split('|')[0];
        }
        point.index = i+1;
        point.title = $(this).val().split('||')[1];
        point.addr = $(this).val().split('||')[2];
        point.phone = $(this).val().split('||')[3];
        point.hotelid = $(this).val().split('||')[4];
        point.hotelstyle = $(this).val().split('||')[5];
        points.push(point);
    });
}

//拼接infowindow显示内容
function GetInfoHtml(point) {
    var color;
    switch (point.hotelstyle) {
        case "HZ":
            color = "#51C7FF";
            break;
        case "HK":
            color = "#004EA2";
            break;
        case "HT":
            color = "#006F4F";
            break;
    }

    var html = "<ul style='text-align:left;'>"
             + "<li ><span style='color:"+color+"; font-size:14px; font-weight:bold;'>" + point.title + "</span>&nbsp;&nbsp;&nbsp;"
             + "<a target='_blank'  href='hotel_detail.aspx?hotelid=" + point.hotelid + "&startdate=" + $("#hddatepickerin").val() + "&enddate=" + $("#hddatepickerout").val() + "'>查看详情</a>&raquo;</li>"
             + "<li>地址：" + point.addr + "</li>"
              + "<li>电话：" + point.phone + "</li>"
             + "</ul><div id='roomprice" + point.hotelid + "' style='width:420px;  overflow:auto;'><img src='/0-Static/images/loading.gif' /></div>";
    if (checkdate())
        getPrice({ hotelid: point.hotelid, startdate: $("#hddatepickerin").val(), enddate: $("#hddatepickerout").val() });
    return html;
}

//确定最佳缩放比例
function zoomfix() {
    gMap.setZoom(gMap.getBoundsZoomLevel(bounds));
    gMap.setCenter(bounds.getCenter());
    //bounds = new GLatLngBounds();
}

function GetCenterLatLng(apoints) {
    var lat_max = apoints[0].lat;
    var lat_min = apoints[0].lat;
    var lng_max = apoints[0].lng;
    var lng_min = apoints[0].lng;
    for (var i = 0; i < apoints.length; i++) {
        var point = apoints[i];
        lat_max = Math.max(lat_max, point.lat);
        lat_min = Math.min(lat_min, point.lat);
        lng_max = Math.max(lng_max, point.lng);
        lng_min = Math.min(lng_min, point.lng);
    }
    return { lat: lat_min + (lat_max - lat_min) / 2, lng: lng_min + (lng_max - lng_min) / 2 };
}

function ajaxmapreload() {
    if(typeof GLatLngBounds =="undefined")
        return ;
    if (gMap != undefined)
        gMap.clearOverlays();
    bounds = new GLatLngBounds();
    points = [];
    markers = [];
    SetPoints();
    //测试点集的中间点
    var smallpoints = [];
    for (var i = 0; i < points.length; i++) {
        if (points[i].lat != undefined && points[i].lng != undefined) {
            var smallpoint = {};
            smallpoint.lat = points[i].lat;
            smallpoint.lng = points[i].lng;
            smallpoints.push(smallpoint);
        }
    }
    if (smallpoints == 1) {
        if (gMap != undefined)
            gMap.setCenter(new GLatLng(smallpoints[0].lat, smallpoints[0].lng), 14);
    }
    if (smallpoints > 1) {
        var center = GetCenterLatLng(smallpoints);
        if (gMap != undefined)
            gMap.setCenter(new GLatLng(center.lat, center.lng), 0);
    }

    if ($("#navcontent_3").css("display") != "none") {

        DrawPointList();
        firstloadmap = 1;
    }
    else
        ajaxloadmap = 1;
}

function showsinglehotelgmap(info) {
    var thotelname;
    var tlat;
    var tlng;
    if (info.split("||").length != 2) {
        alert("暂时无法查看！");
        return false;
    }
    if (info.split("||")[0].split('|').length != 2) {
        alert("暂时无法查看！");
        return false;
    }
    else {
        thotelname = info.split("||")[1];
        tlat = info.split("||")[0].split('|')[1];
        tlng = info.split("||")[0].split('|')[0];

        //$.blockUI({ message: $("#panel_map2") });
        showBlockUI("panel_map2", 590, 440);
        if (typeof (GMap2) != "undefined") {
            gMap2 = new GMap2(document.getElementById("map2"));
            gMap2.addControl(new GLargeMapControl());
            gMap2.setCenter(new GLatLng(tlat, tlng), 16);

            var marker = new GMarker(new GLatLng(tlat, tlng));
            gMap2.addOverlay(marker);
            marker.openInfoWindow(thotelname);
            GEvent.bind(marker, 'click', this, function() {
                marker.openInfoWindow(thotelname);
            });
        }
    }
}

function map2close() {
    $.unblockUI();
}

function checkdate() {
    var checkresult = CheckDate("datepickerin", "datepickerout");
    if (checkresult == "") {
        $("#datepickerin").removeHtip();
        return true;
    }
    else {
        $("#datepickerin").showHtip("bottom", null, checkresult, { left: -2, top: 2 });
        return false;
    }
}

function getPrice(params) {
    last_hotel_selected = params.hotelid;
    $.get("../hotelinfo/ajax_hotel_room.aspx?r=" + Math.random(), params, getPrice_callback);
}
var last_hotel_selected = 0;
function getPrice_callback(data) {
    ajaxroominforesult = data;
    var hotelid = data.split('|')[0];

    if (last_hotel_selected == hotelid) {
        $("#roomprice" + hotelid).html(data.split('|')[1]);
        gMap.updateInfoWindow();
    }
}

function ShowDailyRate(ojb, id)
{
    $("#"+id).css("display", "block");
}

function HideDailyRate(ojb, id)
{
    $("#"+id).css("display", "none");
}

function ShowInfoWindow(obj, index) {
    var point = points[index];
    if (point.lat != undefined && point.lng != undefined) {
        obj.href = "#amap";
        var infoDetail = GetInfoHtml(point);
        markers[index].openInfoWindow(infoDetail);
    }
    else {
        alert("暂时无法查看");
    }
}

function showroominfo() {
    if (ajaxroominforesult != "") {
        getPrice_callback(ajaxroominforesult);
        ajaxroominforesult = "";
    }
}

$(window).unload(function() {
    try {
        GUnload();
    } catch (e) { }
});

