$(document).click(function (s) {
    if ($($(s).attr("target")).hasClass("droplist")) { return true; }
    $(".droplist").css("display", "inline-block");
    $(".lists").hide()
})
$(document).ready(function () {
    $(".tab_content").hide();
    $("#tab0").show();
    $(".tabs .one").addClass("active")
    $("ul.tabs li").click(function () {
        $("ul.tabs li").removeClass("active");
        $(this).addClass("active");
        $(".tab_content").hide();
        var activeTab = $(this).find("a").attr("href");
        $(activeTab).show();
        tabx = activeTab;
        return false;
    });
})
function copy(code) { oncopy(code); }

var tabx = "tab0";
function loadserver(self) {
    $(".lists").hide()
    $(".droplist").css("display", "inline-block");
    var game = $(self).attr("class").replace("droplist server_list_", "");
    var id = ".server_list_for_" + game;
    var b = 0;
   
    tabx = $(self).parents(".tab_content").attr("id");

    $(".card_list_" + game).val("");
    jQuery.ajax({
        dataType: "json",
        url: "/theme/common/gift/gift.ashx?gid=" + game + "&type=server",
        error: function () { alert("error") },
        success: function (server) {
            $("#" + tabx + " .server_list_for_" + game).html("");

            $("#" + tabx + " .server_list_for_" + game).append('<span onclick="loadCard(\'全部服务器\',' + game + ',1)" class="sp2">全部服务器</span>');

            $(server).each(function (i, d) {

                $("#" + tabx + " .server_list_for_" + game).append('<span onclick="loadCard(\'' + d.Name + '\',' + game + ',' + d.ID + ')" class="sp2">' + d.Name + '</span>');
            })
            $(".card_list_" + game).hide();
            $(".sr_" + game).html("领取礼包");
            $("#" + tabx + " .server_list_for_" + game).show();
        }

    })
}


function loadCard(n, gid, sid) {
    $(".droplist").css("display", "inline-block");
    $(".server_list_for_" + gid).hide()
    $(".server_list_" + gid).val(n).attr("datar", sid);
    jQuery.ajax({
        dataType: "json",
        url: "/theme/common/gift/gift.ashx?gid=" + gid + "&type=card&sid=" + sid,
        success: function (cards) {
            var html = "";
            $(cards).each(function (i, d) {
                html += '<span id="card_html_' + gid + '" data="' + d.ID + '" onclick="selectcard(\'' + d.CName + '\',' + d.ID + ',' + gid + ',' + sid + ')" class="sp2">' + d.CName + '</span>';
            })
            $("#" + tabx + " .card_list_for_" + gid).html(html).show();
            $("#" + tabx + " .sr_" + gid).html("领取礼包");
        },
        error: function (k) { alert(k) }
    })
}
function showcards(gid) {
    $(".lists").hide()
    $(".droplist").css("display", "inline-block");
    $(".card_list_for_" + gid).show();
}
function selectcard(n, giftId, gid, sid) {
    $(".card_list_for_" + gid).hide();
    $(".droplist").css("display", "inline-block");
    $("#" + tabx + " .card_list_" + gid).val(n).attr("datar", giftId);

    jQuery.ajax({
        url: "/theme/common/gift/gift.ashx?gid=" + gid + "&sid=" + sid + "&type=html&gfid=" + giftId,
        success: function (saa) {
            $("#" + tabx + " .card_html_for_" + gid).html("<a title='"+saa+"'>" + saa.substring(0, 35) + "..." + "</a>");
            $("#" + tabx + " .sr_" + gid).html("领取礼包"); 
        }
    })
}
function getCard(self) {
    var gid = $(self).attr("class").replace("sr_", "");
    if ($(self).html() == "复制卡号") {
        copy($("#" + tabx + " .kahao_" + gid).html());
        return;
    }
    var sid = $("#" + tabx + " .server_list_" + gid).attr("datar");
    var gifId = $("#" + tabx + " .card_list_" + gid).attr("datar");
    if (typeof gifId == "undefined" || typeof sid == "undefined") {
        alert("请先选择服务器和礼包");
        return;
    }

    jQuery.ajax({
        url: "/theme/common/gift/gift.ashx?gid=" + gid + "&sid=" + sid + "&type=code&gfid=" + gifId,
        success: function (saa) {
            $("#" + tabx + " .kahao_" + gid).html(saa);
            $(self).html("复制卡号");
        }
    })
}
