﻿$(document).ready(loadAnnouncement);
function loadAnnouncement() {

    var gaJsHost = (("https:" == document.location.protocol) ? "https://" : "http://");
    gaJsHost = gaJsHost + location.hostname;

    //修改BaseUrl,使其与当前请求的LocalUrl一至(注意修改后执行请求完毕,必须将BaseUrl改回以前)
    var thebase = document.getElementsByTagName("base");
    var ohref = thebase[0].href;
    thebase[0].href = gaJsHost;

    //判断用户是否登录
    $.ajax({
        type: "POST",
        url: gaJsHost + "/travel-agent-centre/checklogin",
        success: function(msg) {
            if (msg == 'true') {
                $('#toplogout').css('display', '');
                $('#tacfix').css('display', 'none');
                $('#tacfixb').css('display', 'none');
            }
            else {
                $('#toplogout').css('display', 'none');
                $('#tacfix').css('display', '');
                $('#tacfixb').css('display', '');
            }
        }
    });


    var urlpath = window.location.pathname;
    var urlpath = urlpath.substring(1);

    $.ajax({
        url: gaJsHost + "/Announcements/GetAnnouncementPage?url=" + encodeURI(urlpath),
        type: 'POST',
        error: function() {
            alert('Error loading XML document');
        },
        success: function(json) {
            var announcements = eval(json);
            var str = "";
            var leftcount = 0;
            for (var i = 0; i < announcements.length; i++) {
                if ($.cookie(announcements[i].ID) == null) {
                    var html = "";
                    if (announcements[i].Priority == 0) //standard
                    {
                        leftcount++;
                        if (announcements[i].Link != "")
                            html = $.validator.format('<li id="lileft"><a href="{0}">{1}</a></li>', announcements[i].Link, announcements[i].Text);
                        else
                            html = $.validator.format('<li id="lileft">{0}</li>', announcements[i].Text);
                        $("#uileft").append(html);
                    }
                    else {
                        var divId = "ann-red";
                        if (announcements[i].Priority == 1)
                            divId = "ann-amber";

                        //html = $.validator.format('<div id="{2}" name="{3}"><a href="javascript:hideDiv(\'{3}\');" id="close">Don\'t show me this announcement again</a><h1>{0}</h1><p>{1}</p></div>', announcements[i].Header, announcements[i].Text, divId, announcements[i].ID);

                        if (announcements[i].Link != "")
                            html = $.validator.format('<div id="{2}" name="{3}"><a href="javascript:hideDiv(\'{3}\');" id="close">Don\'t show me this announcement again</a><span><a href="{4}">{0}</a></span><p><a href="{4}">{1}</a></p></div>', announcements[i].Header, announcements[i].Text, divId, announcements[i].ID, announcements[i].Link);
                        else
                            html = $.validator.format('<div id="{2}" name="{3}"><a href="javascript:hideDiv(\'{3}\');" id="close">Don\'t show me this announcement again</a><span>{0}</span><p>{1}</p></div>', announcements[i].Header, announcements[i].Text, divId, announcements[i].ID);
                        $("#topAnnouncement").append(html);
                    }
                }
            }

            if (leftcount == 0) {
                if (urlpath == '') {
                    $("#news").show();
                }
                $("#anleft").remove();
            }
        }
    });



    if ($("#find select>option").text() == "Select Destination") {
        $.ajax({
            type: "POST",
            url: gaJsHost + "/Region/RegionList",
            success: function(json) {
                var region = eval(json);
                //var html = "<option>Select Destination</option>";
                var html = "";
                for (var i = 0; i < region.length; i++) {
                    html += $.validator.format('<option value="/plan-cruise/by-destination/{0}">{1}</option>', region[i].UrlName, region[i].Name);
                }
                //alert(html);
                $("#find select").append(html);
            }
        });
    }
    thebase[0].href = ohref;
}

function hideDiv(divName) {
    var express = $.validator.format("div[name={0}]", divName);
    //$.cookie(divName, "hide");

    $.cookie(divName, "hide", { expires: 7000, path: '/' });
    $(express).hide();
}
