﻿function changeList(index) {
    $(".ph_box>li:not(:eq(" + index + "))").removeClass("ph_1").addClass("ph_2");
    $(".ph_box>li:eq(" + index + ")").removeClass("ph_2").addClass("ph_1");
    $("#ph>.box1_body:eq(" + index + ")").show();
    $("#ph>.box1_body:not(:eq(" + index + "))").hide();
}

function Search() {
    if ($.trim($("#search_wd").val()) == "") {
        alert("没有填写游戏名称！");
        return false;
    }
    return true;
}

function Big() {
    if ($("#page_right").is(":visible")) {
        $("#page_left").css("width", "100%");
        $("#page_right").hide();
        $("#show_arrow").removeClass("show_arrow_1").addClass("show_arrow_2");
    }
    else {
        $("#page_left").css("width", "665px");
        $("#page_right").show();
        $("#show_arrow").removeClass("show_arrow_2").addClass("show_arrow_1");
    }
}

//获取随机游戏
function RandomGame() {
    Dialog(300,80, "随机游戏", "数据通信中，请稍候...", true);
    $.post("response.aspx?action=100", null, WebResponse, "json");
}

//启动服务
function Start() {
    Dialog(300,80, "开启服务", "数据通信中，请稍候...", false);
    $.post("response.aspx?action=200", null, WebResponse, "json");
}
function Reg(path) {
    Dialog(300,80, " 用户注册", "数据通信中，请稍候...", false);
    $.post(path == undefined ? "" : path + "response.aspx?action=1", null, WebResponse, "json");
}

function Login(path) {
    Dialog(300,80, " 用户登录", "数据通信中，请稍候...", false);
    $.post(path == undefined ? "" : path + "response.aspx?action=2", null, WebResponse, "json");
}

function Logout(path) {
    Dialog(300,80, " 退出系统", "数据通信中，请稍候...", false);
    $.post(path == undefined ? "" : path + "response.aspx?action=3", null, WebResponse, "json");
}
function ShowInit() {
    if (enableGame == 0 && costmsg != "") {
        Dialog(500, 200, " 支付金币", costmsg, false);
        return;
    }

    $("#pk_status_box").html(pkstatus == 1 ? "攻(守)擂" : "自由游戏");
    if (pkstatus != 1 && pkmsg != '') {
        Dialog(500, 200, " 擂台赛", pkmsg, false);
    }
    $("#show_flash").show();
}

function PayGame(bPay) {
    if (bPay) {
        Dialog(300,80, "支付金币", "数据通信中，请稍候...", false);
        $.post("response.aspx?action=212", "id=" + GameID, WebResponse, "json");
    }
    else {
        window.location.href = "index.aspx";
    }
}

function CreatePK() {
	var money=parseInt($("#pk_money").val(),10);
	if(isNaN(money))
	{alert("无效的金币数目！");return;}
	if(money<=0)
	{alert("请输入一个大于0的整数！");return;}
	Dialog(300, 80, "摆擂台", "数据通信中，请稍候...", false);
	$.post("response.aspx?action=210", "id=" + GameID + "&m=" + money, WebResponse, "json");
	$("#show_flash").hide();
}

function ChangePKStatus2(status) {
    ShowPkStatusMenu();
    if (pkstatus == status)
        return;
    if (!confirm("确定要改变状态吗？" + (status==1?"设置为攻（守）擂，将会扣除您的金币！":"设置为自由游戏，不会退还您参加擂台赛支付的金币。"))) return;
    Dialog(300, 80, "擂台赛", "数据通信中，请稍候...", false);
    $.post("response.aspx?action=211", "id=" + GameID + "&s=" + status, WebResponse, "json");
}

function ChangePKStatus(status) {
    Dialog(300, 80, "擂台赛", "数据通信中，请稍候...", false);
    $.post("response.aspx?action=211", "id=" + GameID + "&s=" + status, WebResponse, "json");
}

function ShowPkStatusMenu() {
    if ($("#pk_status_menu").is(":hidden")) {
        $("#pk_status_menu").css("left", $("#pk_status_box").position().left).show();
    }
    else
        $("#pk_status_menu").hide();
}


function WebResponse(obj) {
    if (obj.Status != 1) {
    switch(obj.Action) {
        case 210:
            {
                Dialog(300, 80, "发生错误", obj.Error, true);
                $("#Dialog").dialog("option", "close", function(event, ui) { $("#show_flash").show(); });
                $("#show_flash").hide();
                return;
            }
        case 212:
            {
                Dialog(300, 80, "发生错误", obj.Error + "<br /><a href=\"index.aspx\">点击返回首页</a>", false);
                return;
            }
        default:
            {
                Dialog(300, 80, "发生错误", obj.Error, true);
                return;
            }
        }
    }
    DialogClose();
    switch (obj.Action) {
        case 200:
            {
                window.location.href = "index.aspx";
            }
            break;
        case 1:
        case 2:
        case 3:
            {
                window.location.href = obj.Url;
            }
            break;
        case 100:
            {
                window.location.href = "show-" + obj.GameID + ".aspx";
            }
            break;
        case 210:
            {
                window.location.href = window.location.href;
            }
            break;
        case 211:
            {
                DialogClose();
                pkstatus =parseInt(obj.PKStatus);
                $("#pk_status_box").html(pkstatus == 1 ? "攻(守)擂" : "自由游戏");
            }
            break;
        case 212:
            {
                enableGame = 1;
                ShowInit();
            }
            break;
    }
}
