﻿var lastshowcontid = "right_cont_4Map_cont";
var lastshowhotelsimgid = "hotelsimg1";

$(function () {
    //地图 点评 图片 tab效果
    $("div[id='right_cont_4menutab']>div").mousedown(
        function () {
            $("#" + lastshowcontid.substr(0, lastshowcontid.length - 5)).attr("className", "inactive");
            $(this).attr("className", "active");
            $("#" + lastshowcontid).hide();
            $("#" + $(this).attr("id") + "_cont").show();
            lastshowcontid = $(this).attr("id") + "_cont";
        }
    );

    //酒店缩略图点击脚本
    $("#panel_" + lastshowhotelsimgid).css("border", "solid 2px #636363");

    $("div[id='hotelimg']>div>img").click(function () {
        $("#panel_" + lastshowhotelsimgid).css("border", "solid 2px White");
        $("#panel_" + $(this).attr("id")).css("border", "solid 2px #636363");
        lastshowhotelsimgid = $(this).attr("id");
        $("#hotelbimg").attr("src", $(this).attr("src"));
    });
    //可接受信用卡信息
    $("#cardtype").mouseover(function () {
        $("#panel_cardtype").css("display", "block");
        $(this).css("backgroundColor", "#696969");
    });
    $("#cardtype").mouseout(function () {
        $("#panel_cardtype").css("display", "none");
        $(this).css("backgroundColor", "#9D9D9D");
    });

    //优惠提示
    $("#youhui100").wTooltip({ offsetY: -25 });
    $("#hotel_icons div").wTooltip({ offsetY: -25 });
    $(".hotel_title").wTooltip({ offsetY: 5 });

    if (window.initTab) {
        setTimeout('$("#" + window.initTab).mousedown();', 200);
    }
});


function AddMemberUsualHotelList(hotelid, vno) {
    if (confirm("确定要收藏该酒店吗？")) {
        $("#divaddmember").html("<img src='/0-Static/images/loading.gif' align='absmiddle'/> ... ");
        pointlistajax.AddMemberUsualHotelList(hotelid, vno, Cancel_callback)
    }
}

function Cancel_callback(result) {
    if (!result.error) {
        if (result != "") {
            $("#divaddmember").html("已收藏");
        }
    } else {
        alert("网络通讯错误！请稍候重试~");
    }
}

//打开预订页面
function CreateResv(obj, roomtype, hotelid, present, isNotLogin) {
    if (document.getElementById("message_datechanged") != null) {
        if (document.getElementById("message_datechanged").style.display == "block") {
            FlashDateChanged();
            return false;
        }
    }
    var startdate = $("#startdate").val();
    var enddate = $("#enddate").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);
}

var dateflashtimes = 3;
function FlashDateChanged() {
    if (dateflashtimes > -1) {
        dateflashtimes--;
        document.getElementById("message_datechanged").style.display = (document.getElementById("message_datechanged").style.display != "block") ? "block" : "none";
        setTimeout("FlashDateChanged();", 100);
    }
    else {
        dateflashtimes = 3;
        document.getElementById("message_datechanged").style.display = "block";
    }
}

var fujinmendian = 10;
function FlashDateChangeds() {
    if (fujinmendian > -1) {
        fujinmendian--;
        var newCss = ($("#editor_8").attr("class") != "zbhotels") ? "zbhotels" : "zbhotels_flash";
        $("#editor_8").attr("class", newCss);
        setTimeout("FlashDateChangeds();", 200);
    }
    else {
        fujinmendian = 10;
        $("#editor_8").attr("class", "zbhotels");
    }
}
//******************************************************************
//**************************   google map  *************************
//******************************************************************

var gMap;
var bounds;
var gSmallIcon = null;


// Set up the map and the local searcher.
function OnLoad() {
    // Initialize the map
    if (typeof (GMap2) != "undefined") {
        gMap = new GMap2(document.getElementById("map"));
        gMap.addControl(new GLargeMapControl());
        //gMap.addControl(new google.maps.MapTypeControl());

        bounds = new GLatLngBounds();
        gMap.setCenter(new GLatLng(lat, lng), 0);    // 将视图移到当前酒店

        ClientGetPoints(hotelid, "交通位置");
    }
}

function ShowInfo(point) {
    var gSmallIcon = new google.maps.Icon();
    gSmallIcon.image = HTSEO_Img + "/images/gmarker_" + point.index + ".png";
    gSmallIcon.iconSize = new google.maps.Size(21, 21);
    gSmallIcon.iconAnchor = new google.maps.Point(6, 20);
    gSmallIcon.infoWindowAnchor = new google.maps.Point(8, 5);

    var marker = new GMarker(new GLatLng(point.lat, point.lng), { icon: gSmallIcon });
    //marker.result = info;
    gMap.addOverlay(marker);
    var infoDetail = GetInfoHtml(point);

    GEvent.bind(marker, 'click', this, function () {
        //marker.openInfoWindow(marker.result);

        marker.openInfoWindow(infoDetail);
        //关联右边div的样式
        $("#" + lastshowpositionid).removeClass("mouseon").addClass("mouseout");
        lastshowpositionid = currenttypedivid + point.index;
        $("#" + lastshowpositionid).removeClass("mouseout").addClass("mouseon");

    });

    googleMarkers[currenttypedivid].push(marker);

}


var lastshowpositionid = "editor_1_cont1";
//var lastshowpositionid = "cont1";
var currenttypedivid = "editor_1_cont";
var ajaxcallresults = [];
ajaxcallresults["editor_1_cont"] = new Array();
ajaxcallresults["editor_2_cont"] = new Array();
ajaxcallresults["editor_3_cont"] = new Array();
ajaxcallresults["editor_4_cont"] = new Array();
ajaxcallresults["editor_5_cont"] = new Array();
ajaxcallresults["editor_6_cont"] = new Array();
ajaxcallresults["editor_7_cont"] = new Array();
ajaxcallresults["editor_8_cont"] = new Array();
var googleMarkers = [];
googleMarkers["editor_1_cont"] = new Array();
googleMarkers["editor_2_cont"] = new Array();
googleMarkers["editor_3_cont"] = new Array();
googleMarkers["editor_4_cont"] = new Array();
googleMarkers["editor_5_cont"] = new Array();
googleMarkers["editor_6_cont"] = new Array();
googleMarkers["editor_7_cont"] = new Array();
googleMarkers["editor_8_cont"] = new Array();
$(function () {
    $("#nav_bar").tabs({
        select: function (event, ui) {

            $("#" + currenttypedivid).css("display", "none");
            $("#" + lastshowpositionid).removeClass("mouseon").addClass("mouseout");
            $("#" + currenttypedivid).css("display", "none");
            lastshowpositionid = currenttypedivid + "1";
            currenttypedivid = ui.panel.id + "_cont";
            $("#" + currenttypedivid + "actionpanel").css("display", "none");

            switch (currenttypedivid) {
                case "editor_1_cont":
                    ClientGetPoints(hotelid, "交通位置");
                    break;
                case "editor_2_cont":
                    ClientGetPoints(hotelid, "旅游");
                    break;
                case "editor_3_cont":
                    ClientGetPoints(hotelid, "餐饮");
                    break;
                case "editor_4_cont":
                    ClientGetPoints(hotelid, "购物");
                    break;
                case "editor_5_cont":
                    ClientGetPoints(hotelid, "娱乐");
                    break;
                case "editor_6_cont":
                    ClientGetPoints(hotelid, "医疗");
                    break;
                case "editor_7_cont":
                    ClientGetPoints(hotelid, "银行");
                    break;
                case "editor_8_cont":
                    ClientGetPoints(hotelid, "周边汉庭");

                    break;
            }
        }
    });

    $("#azhoubian").click(function () {
        $("#nav_bar ul li").attr("class", "ui-state-default ui-corner-top");
        $("#nav_bar ul li").eq(1).attr("class", "ui-corner-top ui-tabs-selected ui-state-active ui-state-focus");
        $("#editor_8_cont").css("display", "block");
        $("#" + currenttypedivid).css("display", "none");
        $("#" + lastshowpositionid).removeClass("mouseon").addClass("mouseout");
        $("#" + currenttypedivid).css("display", "none");
        $("#editor_8").attr("class", "ui-tabs-panel ui-widget-content ui-corner-bottom");
        lastshowpositionid = currenttypedivid + "1";
        currenttypedivid = "editor_8_cont";
        $("#" + currenttypedivid + "actionpanel").css("display", "none");
        ClientGetPoints(hotelid, "周边汉庭");
        FlashDateChangeds();
    });
    HT$("right_cont_4Map_cont").style.display = "block";
    OnLoad();
});

function ClientGetPoints(hotelid, type) {
    if (ajaxcallresults[currenttypedivid].length == 0) {
        pointlistajax.GetPoints(hotelid, type, ClientGetPoints_callback);
    }
    else {
        DrawPointList(false);
    }
}

function ClientGetPoints_callback(result) {
    if (result.value) {

        for (var i = 1; i < result.value.Rows.length + 1; i++) {
            var point = {};

            if (result.value.Rows[i - 1]["category"] == undefined) {

                point.hotelId = result.value.Rows[i - 1]["hotelId"];
                var latlng = result.value.Rows[i - 1]["geoinfo"].split("|");
                point.lat = latlng[1];
                point.lng = latlng[0];
                point.index = i;
                point.title = result.value.Rows[i - 1]["hotelName"];
                point.dist = result.value.Rows[i - 1]["dis"];
                point.dist2 = result.value.Rows[i - 1]["dis"];
                point.category = "周边汉庭";
                point.addr = result.value.Rows[i - 1]["addr"];
                point.phone = result.value.Rows[i - 1]["tel"];

                if (i > 10) {
                    break;
                }
            }
            else {

                point.lat = result.value.Rows[i - 1]["lat"];
                point.lng = result.value.Rows[i - 1]["lng"];
                point.index = i;
                point.title = result.value.Rows[i - 1]["Title"];
                point.dist = result.value.Rows[i - 1]["Distance"];
                point.dist2 = result.value.Rows[i - 1]["Distance2"];
                point.category = result.value.Rows[i - 1]["category"];
                point.addr = result.value.Rows[i - 1]["streetAddress"];
                point.phone = result.value.Rows[i - 1]["Phone"];
                //把ajax返回坐标集合放入变量
            }
            ajaxcallresults[currenttypedivid].push(point);
            //  
        }
    }
    DrawPointList(true);
}
function DrawPointList(drawLeftListP) {
    googleMarkers[currenttypedivid] = [];
    if (typeof (GMap2) != "undefined") {
        gMap.clearOverlays();
        //
        var leftListHTML = "";
        for (var i = 1; i < ajaxcallresults[currenttypedivid].length + 1; i++) {
            var point = ajaxcallresults[currenttypedivid][i - 1];
            var amarker = new GLatLng(point.lat, point.lng);
            //处理缩放比例
            bounds.extend(amarker);
            ShowInfo(point);
            if (drawLeftListP)
                leftListHTML += getLeftPointBlocks(point, i);
        }
        //
        if (drawLeftListP) {
            leftListHTML += getActionLink();
            $("#" + currenttypedivid).html(leftListHTML);
        }
        $("#" + currenttypedivid).css("display", "block");
        // 
        //绘制当前酒店
        var hotelinfohtml = "<p style='font-size:14px;font-weight:bold;margin-bottom:10px;'>" + hotelname + "</p>" + hoteladdr;
        showhotel(new GLatLng(lat, lng), hotelinfohtml);
        //缩放比例适配
        bounds.extend(new GLatLng(lat, lng));
        zoomfix();
    }
}

function onListItemClick(lineDom, aitemIndex) {
    var amarker = googleMarkers[currenttypedivid][aitemIndex - 1];
    var apoint = ajaxcallresults[currenttypedivid][aitemIndex - 1];
    amarker.openInfoWindow(GetInfoHtml(apoint));
    //左侧point block点击样式改变
    $("#" + lastshowpositionid).removeClass("mouseon").addClass("mouseout");
    $(lineDom).removeClass("mouseout").addClass("mouseon");
    lastshowpositionid = $(lineDom).attr("id");

}

function getActionLink() {
    var html = "<div style='text-align:center;margin:10px 0 0 0;'>";
    if (currenttypedivid == "editor_1_cont") {
        html += "<p style='color:#999999;'>*选择以上交通枢纽，查看行车路线</p>"
        html += "<a class='btn_100' href='##' onclick='return moreaction(this)' style='margin-top:10px;margin-left:50px;*margin-left:0px;'>更多查询路线</a>"
    }
    else {
        html += "<p style='color:#999999;'>*选中选定项，在地图上查看位置</p>"
        html += "<a class='btn_100' href='##' onclick='return moreaction(this);' style='margin-top:10px;margin-left:50px;*margin-left:0px;'>查询更多</a>"
    }
    html += "</div>";
    return html;
}

function getLeftPointBlocks(point, aindex) {

    var html = "<div id='" + currenttypedivid + point.index + "' class='cont mouseout' onclick='onListItemClick(this, " + aindex + ")'>";
    html += "<div class='left'>" + point.index + ".</div>";
    html += "<div class='middle' title='" + point.title + "'>" + point.title + "</div>";
    html += "<div class='right' title='直线距离'>" + getDistanceSub(point.dist) + "</div>";
    html += "<div class='clear'></div>";
    html += "</div>";
    return html;
}

function getDistance(dist1, dist2) {
    if (parseInt(dist2) == 0) {
        return getDistanceSub(dist1);
    }
    else {
        return getDistanceSub(dist2);
    }
}

function getDistanceSub(dist) {
    if (parseInt(dist) < 1000) {
        return dist + "米";
    }
    else {
        return (dist / 1000).toFixed(1) + "公里";
    }
}

//绘制当前酒店位置
function showhotel(point, info) {
    var gSmallIcon = new google.maps.Icon();
    gSmallIcon.image = "/gmap/images/ht_mapicon.png";
    gSmallIcon.shadow = "/gmap/images/ht_mapicon_shadow.png";
    gSmallIcon.iconSize = new google.maps.Size(20, 30);
    gSmallIcon.shadowSize = new google.maps.Size(39, 30);
    gSmallIcon.iconAnchor = new google.maps.Point(6, 20);
    gSmallIcon.infoWindowAnchor = new google.maps.Point(5, 1);

    var marker = new GMarker(point, { icon: gSmallIcon });
    gMap.addOverlay(marker);
    GEvent.bind(marker, 'click', this, function () {
        marker.openInfoWindow(info)
    });
}

//确定最佳缩放比例
function zoomfix() {
    gMap.setZoom(gMap.getBoundsZoomLevel(bounds));
    gMap.setCenter(bounds.getCenter());
    bounds = new GLatLngBounds();
}
function getArgs() {
    var args = new Object();
    var query = location.search.substring(1); //获取查询串   
    var pairs = query.split("&"); //在逗号处断开   
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('='); //查找name=value   
        if (pos == -1) continue; //如果没有找到就跳过   
        var argname = pairs[i].substring(0, pos); //提取name   
        var value = pairs[i].substring(pos + 1); //提取value   
        args[argname] = unescape(value); //存为属性   
    }
    return args; //返回对象   
}
//拼接infowindow显示内容
function GetInfoHtml(point) {
    var html = "";

    html += "<div id='infowindow' style='width:235px;'>"

    if (point.category == "周边汉庭") {
        var startdate = getArgs()["startdate"];
        var enddate = getArgs()["enddate"];
        html += "<p class='title'><a target='_blank' href=/hotelinfo/hotel_detail.aspx?hotelid=" + point.hotelId + "&startdate=" + startdate + "&enddate=" + enddate + ">" + point.title + "</a></p>"
    }
    else {
        html += "<p class='title'>" + point.title + "</p>"
    }
    html += "<p style='margin-bottom:10px;'>" + point.addr + "</p>"
    if (point.category == "交通位置") {
        html += "<div id='infobox'>"
        html += "<p>从酒店到这里</p>"
        html += "<a href='" + GetFromHotelUrl(point.addr, '公交') + "' style='margin-left:30px;' target='_blank'>公交怎么乘？</a>"
        html += "<a href='" + GetFromHotelUrl(point.addr, '驾车') + "' style='margin-left:10px;' target='_blank'>开车怎么走？</a>"
        html += "<p>从这里去酒店</p>"
        html += "<a href='" + GetToHotelUrl(point.addr, '公交') + "' style='margin-left:30px;' target='_blank'>公交怎么乘？</a>"
        html += "<a href='" + GetToHotelUrl(point.addr, '驾车') + "' style='margin-left:10px;' target='_blank'>开车怎么走？</a>"
        html += "</div>"
    }
    else {
        html += "<a href='" + GetFromHotelUrl(point.addr, '公交') + "' style='margin-left:10px; color:#0000CC' target='_blank'>公交</a>"
        html += "<a href='" + GetFromHotelUrl(point.addr, '驾车') + "' style='margin-left:10px; color:#0000CC' target='_blank'>驾车</a>"
    }
    html += "</div>"

    return html;
}

function moreaction(alinkObj) {
    if (currenttypedivid == "editor_1_cont") {
        $("#" + currenttypedivid).css("display", "none");
        $("#" + currenttypedivid + "actionpanel").css("display", "block");
        return false;
    }
    else {
        switch (currenttypedivid) {
            case "editor_2_cont":
                alinkObj.href = GetNearUrl("旅游");
                break;
            case "editor_3_cont":
                alinkObj.href = GetNearUrl("餐饮");
                break;
            case "editor_4_cont":
                alinkObj.href = GetNearUrl("购物");
                break;
            case "editor_5_cont":
                alinkObj.href = GetNearUrl("娱乐");
                break;
            case "editor_6_cont":
                alinkObj.href = GetNearUrl("医疗");
                break;
            case "editor_7_cont":
                alinkObj.href = GetNearUrl("银行");
                break;
            case "editor_8_cont":
                alinkObj.href = GetNearUrl("周边汉庭");
                break;
        }
        alinkObj.target = "_blank";
        return true;
    }

}

function moreactionreturn() {
    $("#" + currenttypedivid).css("display", "block");
    $("#" + currenttypedivid + "actionpanel").css("display", "none");
    return false;

}

//查询自定义地址
function SearchMyAddr() {
    var myaddr = $("#touraddr").val();
    var geocoder = new GClientGeocoder();
    var gSmallIcon = new google.maps.Icon();
    gSmallIcon.image = "/gmap/images/red.png";
    gSmallIcon.shadow = "/gmap/images/shadow.png";
    gSmallIcon.iconSize = new google.maps.Size(12, 20);
    gSmallIcon.shadowSize = new google.maps.Size(22, 20);
    gSmallIcon.iconAnchor = new google.maps.Point(6, 20);
    gSmallIcon.infoWindowAnchor = new google.maps.Point(5, 1);

    geocoder.getLatLng(myaddr, function (point) {
        //如果该地址得不到正确的坐标,则设置为中国坐标
        if (!point) {
            alert("无法在google地图上找到该地址，请重新输入！");
            $("#touraddr").val("");
        }
        else {
            var marker = new GMarker(new GLatLng(point.y, point.x), { icon: gSmallIcon });
            //marker.result = info;
            gMap.addOverlay(marker);

            var pointinfo = {};
            pointinfo.title = "我的查询地址";
            pointinfo.addr = myaddr;
            pointinfo.phone = "";
            pointinfo.category = "交通位置";

            var infoDetail = GetInfoHtml(pointinfo);

            GEvent.bind(marker, 'click', this, function () {
                //marker.openInfoWindow(marker.result);

                marker.openInfoWindow(infoDetail)
            });

            marker.openInfoWindow(infoDetail);
        }
    });
}

function GetToHotelUrl(startaddr, type) {
    var url = "";
    url += "http://ditu.google.cn/maps?";
    url += "f=d&";
    url += "source=s_d&";
    url += "saddr=" + encodeURI(startaddr) + "&";
    url += "daddr=" + encodeURI(hoteladdr) + "&";
    url += "hl=zh-CN&";
    url += "geocode=&";
    url += "mra=cc&";
    url += "sll=" + lat + "," + lng + "&";
    url += "sspn=0.105738,0.153809&";
    url += "ie=UTF8&";
    url += "z=13&";
    url += "brcurrent=3,0x35b264c8bb78a883:0x6e625b27913261fe%3B5,0&";
    if (type == "驾车") {
        url += "noexp=0&";
        url += "noal=0&";
        url += "sort=time";
    }
    if (type == "公交") {
        url += "dirflg=r&";
        url += "start=0";
    }

    return url;
}

function GetFromHotelUrl(endaddr, type) {
    var url = "";
    url += "http://ditu.google.cn/maps?";
    url += "f=d&";
    url += "saddr=" + encodeURI(hoteladdr) + "&";
    url += "daddr=" + encodeURI(endaddr) + "&";
    url += "geocode=&";
    url += "ie=UTF8&";
    if (type == "驾车") {
        url += "sll=" + lng + "," + lat + "&";
        url += "sspn=0.006295,0.007856&";
        url += "cid=3298147914670025106&";
        url += "dirflg=&";
    }
    if (type == "公交") {
        url += "sll=" + lat + "," + lng + "&";
        url += "sspn=0.105749,0.153809&";
        url += "mra=cc&";
        url += "dirflg=r&";
        url += "z=13&";
        url += "source=s_d&";
        url += "hl=zh-CN&";
        url += "brcurrent=3,0x35b264c8bb78a883:0x6e625b27913261fe%3B5,0&";
        url += "start=0";
    }

    return url;
}

//周边信息
function GetNearUrl(type) {
    var url = "";
    url += "http://ditu.google.cn/maps?";
    url += "near=" + encodeURI(hoteladdr) + "&";
    url += "geocode=&";
    url += "q=" + encodeURI(type) + "&";
    url += "f=l&";
    url += "sll=" + lat + "," + lng + "&";
    url += "sspn=0.006967,0.009656&";
    url += "ie=UTF8&";
    url += "z=16&";
    url += "brcurrent=3,0x35b2652a4d452d2d:0x3dd2300555733cf4%3B5,0.3";

    return url;
}

$(window).unload(function () {
    if (typeof (GUnload) != "undefined")
        GUnload();
});
    
