var marker = null;
var map = null;

$(document).ready(function() {
    init();
});

function init() {
    if (!isAgentHotel) {
        $("#hotelImg").attr("src", dfs + "/picture/" + $("#hotelImg").attr("hotelPassport") + "/thumbnail");
    } else {
        $("#hotelImg").attr("src", agent_hotel_dfs + "/breviaryPicture.do?hotelPassportCode=" + $("#hotelImg").attr("hotelPassport"));
    }
    $("#query").click(function() {
        if (validateQueryCondition()) {
            $("#hotelAvailListFM").attr("action", _contextpath + "/hotel/queryRoomRate.action");
            $("#hotelAvailListFM").submit();
        } else {
            return false;
        }
    });

    $("ul.nav li").each(function() {
        $(this).click(function() {
            if ($(this).attr("class").indexOf("tab_active") < 0) {
                var funName = this.id + "_preFun($(this))";
                try {
                    eval(funName);
                } catch(e) {
                }
                tabChange(this.id);
            }
        })
    });

    $('span[rateInfoId*=rateCal_cash_]').hover(function() {
        drawRoomRateByHidden($(this).attr("index"), false);
        showMoreInfo($(this).parent(), 'rateCal');
    }, function() {
        hideMoreInfo('rateCal');
    })

    $('span[rateInfoId*=rateCal_prepay_]').hover(function() {
        drawRoomRateByHidden($(this).attr("index"), true);
        showMoreInfo($(this).parent(), 'rateCal');
    }, function() {
        hideMoreInfo('rateCal');
    })

    formatHotelDescHtml();
}

function formatHotelDescHtml() {
    var html = $('#hotelDesc').html();
    if (html != null && $.trim(html).length > 0) {
        html = html.replace(/\\n/g, "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
        $('#hotelDesc').html(html);
    }
}

function tab_photoList_preFun(tabEle) {
    if ($("#photoListUl li").length == 0) {
        var url;
        var imageServer;
        if (!isAgentHotel) {
            url = _contextpath + "/hotel/getHotelPicture.action";
            imageServer = pfs;
        } else {
            url = _contextpath + "/hotel/getAgentHotelPicture.action";
            imageServer = agent_hotel_dfs;
        }
        $.ajax({
            type: "POST",
            url: url,
            data:{'hotelPassport':$('#hotelPassport').val()},
            success: function(responseText) {
                if (responseText != "") {
                    if (tabEle.attr("class").indexOf("tab_active") < 0) {
                        var images = responseText.split("##");
                        for (var i = 0; i < images.length; i++) {
                            var image = images[i];
                            image = image.replace('//','/');
                            $("<li><a rel='hotelPic'class='thickbox'  href='" + imageServer + image +
                              "' ><img width='100' height='60' src='" + imageServer + image +
                              "'></a></li>").appendTo($('#photoListUl'));
                        }
                        tb_init('a.thickbox');
                    }
                }
            },
            async: false
        });
    }
}

function tab_mapList_preFun(tabEle) {
    if (tabEle.attr("class").indexOf("tab_active") < 0) {
        google.load('maps', '2', {"callback": loadMapSearch});
    }
}

function loadMapSearch() {
    google.load('search', '1', {"callback" : loadMap});
}

function tabChange(id) {
    $("#contentList").children("div:visible").fadeOut("fast", function() {
        var name = id.substring(id.indexOf("_") + 1);
        $("#" + name).fadeIn("slow");
        $("li.tab").removeClass("tab_active");
        $("#" + id).addClass("tab_active");
    });
    $("#contentList").attr({"style":"*margin-top:-5px"});
}

function getElementName(eleName, index, paymenttype) {
    return paymenttype + "_" + eleName + index;
}

function reserve(hotelPassprotCode, index, but, needGuarantee) {

    var paymenttype = 0;
    var payment = $('input[name=payment' + hotelPassprotCode + index + ']');
    if (!payment.get(0).checked && !payment.get(1).checked) {
        daAlert($(but), txtNoPaymentType);
        return;
    }
    if (payment.get(0).checked) {
        paymenttype = 3;
    }

    $('#roomRatesIndex').val(index);
    $('#roomRatePayment').val(paymenttype);
    $('#hotelJsonText').val($.toJSON(new csHotel(hotelJson["hotel"])));
    
    if (needGuarantee == 'true' && caserver_url != "" && null != caserver_url) {
        changeHttps();
    } else {
        changeHttp();
    }
    $('#reserveHotelFM').submit();
}

function changeHttp() {
    $("#reserveHotelFM").attr("action", _contextpath + "/hotel/newFromHotels.action");
}
function changeHttps() {
    $("#reserveHotelFM").attr("action", caserver_url);
}

function loadMap() {
    if (GBrowserIsCompatible()
            && $.trim(longitude).length > 0 && $.trim(latitude).length > 0) {
        map = new GMap2(document.getElementById('googleMap'));
        map.addControl(new GLargeMapControl());
        map.addControl(new GScaleControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(latitude, longitude), 13);
        marker = new GMarker(map.getCenter());
        map.addOverlay(marker);
        GetLocalSearch();
    }
}
//添加本地搜索、景点
function GetLocalSearch()
{
    $("#contentList .map .btnList span.viewDinning").click(function() {
        var myIcon = new GIcon(G_DEFAULT_ICON);
        myIcon.image = dinningImage;
        myIcon.iconSize = new GSize(16, 20);
        myIcon.iconAnchor = new GPoint(8, 20);
        myIcon.shadow = "";
        var mapSearch = new KMapSearch(map, {container:"dinningContainer",latlng:marker.getLatLng(),icon:myIcon,keyWord:dinningKeyword});
        if ($(this).attr("class").indexOf("hover") > -1)
        {
            mapSearch.clearByType(dinningKeyword);
            $(this).removeClass("hover");
        }
        else
        {
            mapSearch.execute();
            $(this).addClass("hover");
        }
    });
    $("#contentList .map .btnList span.viewScenicSpots").click(function() {
        var myIcon = new GIcon(G_DEFAULT_ICON);
        myIcon.image = spotsImage;
        myIcon.iconSize = new GSize(16, 20);
        myIcon.iconAnchor = new GPoint(8, 20);
        myIcon.shadow = "";
        var mapSearch = new KMapSearch(map, {container:"dinningContainer",latlng:marker.getLatLng(),icon:myIcon,keyWord:spotsKeyWord});
        if ($(this).attr("class").indexOf("hover") > -1)
        {
            mapSearch.clearByType(spotsKeyWord);
            $(this).removeClass("hover");
        }
        else
        {
            mapSearch.execute();
            $(this).addClass("hover");
        }
    });
    $("#contentList .map .btnList span.viewEntertainment").click(function() {
        var myIcon = new GIcon(G_DEFAULT_ICON);
        myIcon.image = yuleImage;
        myIcon.iconSize = new GSize(16, 20);
        myIcon.iconAnchor = new GPoint(8, 20);
        myIcon.shadow = "";
        var mapSearch = new KMapSearch(map, {container:"dinningContainer",latlng:marker.getLatLng(),icon:myIcon,keyWord:yuleKeyWord});
        if ($(this).attr("class").indexOf("hover") > -1)
        {
            mapSearch.clearByType(yuleKeyWord);
            $(this).removeClass("hover");
        }
        else
        {
            mapSearch.execute();
            $(this).addClass("hover");
        }
    });
}