﻿var $j = jQuery.noConflict();
//校场模拟器入口窗口
function GoToWar() {
    $j('#Operate_Div').hide();
    $j('#geDivList').hide();
    $j('#eqDivList').hide();
    if (Spage.BeInBatte == 0) {
        //$j('#Genbomtext').hide();
        //showDialog("GenPop");
        $j('#GenPop').show();
    }
    else if (Spage.BeInBatte == 1) {
        page.BackToBattField(); //武将模拟器直接返回校场模拟器窗口
    }
}
//返回武将模拟器窗口
function GoBackToGeneral(obj) {
    $j('#GeneralDetail').hide();
    $j('#Generals').show();
    $j('#Genbomtext').show();
    $j('#GenPop').hide();
    $j('#SchoolDiv').hide();
    $j('#WarWord').hide();
    page.ShowDetail(obj);
    $j('#GeneralName').focus();
    $j('#Def_PicAdd').unbind("click");
    $j('#Att_PicAdd').unbind("click");

}
//进入校场模拟器
function GoToSchoolField() {
    $j('#Operate_Div').hide();
    $j('#Generals').hide();
    $j('#Genbomtext').hide();
    $j('#GenPop').hide();
    $j('#SchoolDiv').show();
    $j('#WarWord').show();
    //装备的套装加成   SuitIndex
    var Suits = "";
    for (var j = 0; j < page.NowGeneral.Equips.size(); j++) {
        var thisEquip = page.NowGeneral.Equips.get(j);
        Suits = Suits + thisEquip.SuitIndex + "|";
    }
    if (Suits != "") {
        Suits = Suits.substr(0, Suits.length - 1);

        var Action = "action=getSuitepoint&SuiteIndex=" + Suits;
        AjaxMethod(Action, AddGeneral);
    }
    //确定攻击方
    if ($("warside").value == "0") {//我方为攻击方
        Spage.AttGeneral = page.NowGeneral;
        //Spage.DefenseGeneral = page.OtherGeneral;
        Spage.SelfSide = 0;
    }
    else {
        //Spage.AttGeneral = page.OtherGeneral;
        Spage.DefenseGeneral = page.NowGeneral;
        Spage.SelfSide = 1;
    }
    //确定战场环境
    if ($("warenvironment").value == "0") {//城防战
        Spage.WarType = 0;
    }
    else if ($("warenvironment").value == "1") {
        Spage.WarType = 1;
    } else if ($("warenvironment").value == "2") {
        Spage.WarType = 2;
    }
    Spage.InitSchoolFieldTitle(); //初始化战场
}
function AddGeneral(xmlhttprequest) {
    var result = xmlhttprequest.responseText;
    if (result == "Err") {
        alert("执行出错，请重试！");
    } else {
        var thispage = Spage;
        var json = eval("(" + result.replace(/\r\n/g, '<br\>') + ")");
        var Command = 0, Affairs = 0, Chivalrous = 0, Resourcefulness = 0, power = 0, Energy = 0, attack = 0, Defense = 0, speed = 0, Endure = 0, arange = 0;
        Command = parseInt(json.Points[0].Command);
        Affairs = parseInt(json.Points[0].Affairs);
        Chivalrous = parseInt(json.Points[0].Chivalrous);
        Resourcefulness = parseInt(json.Points[0].Resourcefulness);
        power = parseInt(json.Points[0].power);
        Energy = parseInt(json.Points[0].Energy);
        attack = parseInt(json.Points[0].attack);
        Defense = parseInt(json.Points[0].Defense);
        speed = parseInt(json.Points[0].speed);
        Endure = parseInt(json.Points[0].Endure);
        arange = parseInt(json.Points[0].arange);
        if ($("warside").value == "0") {//我方为攻击方
            Spage.AttGeneral = page.NowGeneral;
            Spage.AttGeneral.NewCommand += Command;
            Spage.AttGeneral.NewAffairs += Affairs;
            Spage.AttGeneral.NewChivalrous += Chivalrous;
            Spage.AttGeneral.NewResourcefulness += Resourcefulness;
            Spage.AttGeneral.NewPower += power;
            Spage.AttGeneral.NewEnergy += Energy;
            Spage.AttGeneral.NewAttack += attack;
            Spage.AttGeneral.NewDefense += Defense;
            Spage.AttGeneral.NewSpeed += speed;

        } else {
            Spage.DefenseGeneral = page.NowGeneral;
            Spage.DefenseGeneral.NewCommand += Command;
            Spage.DefenseGeneral.NewAffairs += Affairs;
            Spage.DefenseGeneral.NewChivalrous += Chivalrous;
            Spage.DefenseGeneral.NewResourcefulness += Resourcefulness;
            Spage.DefenseGeneral.NewPower += power;
            Spage.DefenseGeneral.NewEnergy += Energy;
            Spage.DefenseGeneral.NewAttack += attack;
            Spage.DefenseGeneral.NewDefense += Defense;
            Spage.DefenseGeneral.NewSpeed += speed;
        }
    }


}
var WClass = { Version: '1.0.0.0' }; //Spage
WClass.Page = Class.create({
    initialize: function() {
        this.inital = 1;      //初次加载战场信息 1，其余 0
        this.WarRound = 0;
        this.BeInBatte = 0; //未进入过战场，1：已进入过战场
        this.SelectGeneral = null; //选择的配置武将 0:攻击方 1：防守方
        this.WarType = null; //战场环境，0：城防战  1：野地战
        this.AttGeneral = null; //攻击方武将信息
        this.DefenseGeneral = null; //防守方武将信息
        this.SelfSide = null; //我方位置，0：攻击方 1：防守方

        this.A_ForceConfig = new Array(); //攻击方兵力配置
        this.A_TechnologyConfig = new Array(); //攻击方科技配置

        this.D_ForceConfig = new Array(); //防守方兵力配置
        this.D_TechnologyConfig = new Array(); //防守方科技配置
        this.D_CityDefenseConfig = new Array(); //防守方城防配置

        this.ForcePicList = new Array('images/army_1.gif', 'images/army_2.gif', 'images/army_3.gif', 'images/army_4.gif', 'images/army_5.gif', 'images/army_6.gif',
        'images/army_7.gif', 'images/army_8.gif', 'images/army_9.gif', 'images/army_10.gif', 'images/army_11.gif', 'images/army_12.gif'); //兵种大图片

        this.D_DefensePicList = new Array('images/defence_1.gif', 'images/defence_2.gif', 'images/defence_3.gif', 'images/defence_4.gif', 'images/defence_5.gif', 'images/defence_0.gif'); //防守方城防大图片

        this.AttackDistanse = new Array(10, 20, 20, 50, 30, 1200, 80, 70, 10, 1400, 50, 1600); //各兵种相应攻击距离
        this.CityDefDistanse = new Array(2000, 800, 1250, 500, 250, 0);
        this.WarDistanse = 0; //战场距离
        this.AProps = new Array(0, 0, 0, 0, 0); //攻击方道具
        this.DProps = new Array(0, 0, 0, 0, 0); //防守方道具

        this.ABeginForceNumber = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        this.DBeginForceNumber = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        this.DBeginDefenseNumber = new Array(0, 0, 0, 0, 0);

        this.ACProps = new Array(0, 0, 0, 0, 0);
        this.DCProps = new Array(0, 0, 0, 0, 0);

        this.WarReport = ""; //战报
        this.WinSide = null; //获胜方
        this.IsOver = false; //是否已结束
        this.ASFNum = 0; //攻击方总兵力
        this.DSFNum = 0; //防守方总兵力

    },
    //初始化战场
    InitSchoolFieldTitle: function() {
        // this.GetBattFieldNumber(); //初始化敌我双方信息
        if (this.AttGeneral) {
            //攻击方（左边）信息
            $j('#Att_PicAdd').attr('src', this.AttGeneral.PicAdd);
            $j('#Att_GeneralName').html(this.AttGeneral.Name);
            //显示武将详细信息
            $j('#Att_PicAdd').bind('mouseover', function(event) { ShowGeneralDetail(event, Spage.AttGeneral) });
            $j('#Att_PicAdd').bind('mouseout', function(event) { $j('#GeneralDetail').hide(); }); //隐藏武将详细信息窗口
            $j('#Att_PicAdd').bind('click', function() { Spage.SelectGeneral = 0; Spage.BeInBatte = 1; GoBackToGeneral(Spage.AttGeneral); }); //返回武将模拟器对武将进行编辑
        }
        else {
            $j('#Att_PicAdd').attr('src', 'images/sd-4.jpg');
            $j('#Att_GeneralName').html("无武将");
            $j('#Att_PicAdd').bind('click', function() {
                Spage.SelectGeneral = 0;
                Spage.BeInBatte = 1;
                page.InitOtherGeneral();
                GoBackToGeneral(page.OtherGeneral);
            }); //返回武将模拟器对武将进行编辑
        }

        if (this.DefenseGeneral) {
            //防守方（右边）信息
            $j('#Def_PicAdd').attr('src', this.DefenseGeneral.PicAdd);
            $j('#Def_GeneralName').html(this.DefenseGeneral.Name);

            $j('#Def_PicAdd').bind('mouseover', function(event) { ShowGeneralDetail(event, Spage.DefenseGeneral) });
            $j('#Def_PicAdd').bind('mouseout', function(event) { $j('#GeneralDetail').hide(); }); //隐藏武将详细信息窗口
            $j('#Def_PicAdd').bind('click', function() { Spage.SelectGeneral = 1; Spage.BeInBatte = 1; GoBackToGeneral(Spage.DefenseGeneral); }); //返回武将模拟器对武将进行编辑
        }
        else {
            $j('#Def_PicAdd').attr('src', 'images/sd-4.jpg');
            $j('#Def_GeneralName').html("无武将");
            $j('#Def_PicAdd').bind('click', function() {
                Spage.SelectGeneral = 1;
                Spage.BeInBatte = 1;
                page.InitOtherGeneral();
                GoBackToGeneral(page.OtherGeneral);
            }); //返回武将模拟器对武将进行编辑
        }

        //区分敌我方
        if (this.SelfSide == 0) {
            $j('#Att_WarSide').html("<span>攻击方</span>我方");
            $j('#Def_WarSide').html("<span>防守方</span>敌方");
        }
        else {
            $j('#Att_WarSide').html("<span>攻击方</span>敌方");
            $j('#Def_WarSide').html("<span>防守方</span>我方");
        }
        //战场设置（防守方根据环境不同区别）
        $j('#Battlefieldenvironment').removeClass();
        if (this.WarType == 0) {
            $j('#Def_DefenseConfig').show();
            $j('#CitydefenseTechnology').show();
        }
        else {
            $j('#Def_DefenseConfig').hide();
            $j('#CitydefenseTechnology').hide();
        }

        $j('#spround').html("0");

        $j('#btnWarWord').hide();
        $j('#btnNextRound').hide();
        $j('#btnReBegin').hide();
        $j('#btnBegin').show();
        this.WarRound = 0;
        $j('#Def_ForceConfig').show();
        $j('#Def_TechnologyConfig').show();
        $j('#Att_ForceConfig').show();
        $j('#Att_TechnologyConfig').show();

        $j('#WarWord').show();

        this.InitForceDetail(3);
    },
    CheckForce: function(type) {
        if (type == 2) {
            var AllCount = 0;
            var DAllCount = 0;
            for (var i = 0; i < this.A_ForceConfig.length; i++) {
                if (this.A_ForceConfig[i] != null && this.A_ForceConfig[i] != "") {
                    if (i == 0 || i == 1 || i == 2 || i == 3 || i == 4) {
                        AllCount += parseInt(this.A_ForceConfig[i].ForceNum);
                    } else if (i == 5) {
                        AllCount += parseInt(this.A_ForceConfig[i].ForceNum) * 2;
                    } else if (i == 6) {
                        AllCount += parseInt(this.A_ForceConfig[i].ForceNum) * 3;
                    } else if (i == 7) {
                        AllCount += parseInt(this.A_ForceConfig[i].ForceNum) * 6;
                    } else if (i == 8) {
                        AllCount += parseInt(this.A_ForceConfig[i].ForceNum) * 4;
                    }
                }
            }
            for (var j = 0; j < this.D_ForceConfig.length; j++) {
                if (this.D_ForceConfig[j] != null && this.D_ForceConfig[j] != "") {
                    if (j == 0 || j == 1 || j == 2 || j == 3 || j == 4) {
                        DAllCount += parseInt(this.D_ForceConfig[j].ForceNum);
                    } else if (j == 5) {
                        DAllCount += parseInt(this.D_ForceConfig[j].ForceNum) * 2;
                    } else if (j == 6) {
                        DAllCount += parseInt(this.D_ForceConfig[j].ForceNum) * 3;
                    } else if (j == 7) {
                        DAllCount += parseInt(this.D_ForceConfig[j].ForceNum) * 6;
                    } else if (j == 8) {
                        DAllCount += parseInt(this.D_ForceConfig[j].ForceNum) * 4;
                    }
                }
            }
            var tem = true;
            if (DAllCount > 100000 && AllCount <= 100000) {
                $j('#Force :input').each(function(i) {
                    if (i > 11 && i <= 23) {
                        $j(this).val(0);
                    }
                });
                for (var j = 0; j < this.D_ForceConfig.length; j++) {
                    this.D_ForceConfig[j].ForceNum = 0;
                }
                alert("防守方兵力配置超过人口上限");

                tem = false;
            }
            if (AllCount > 100000 && DAllCount <= 100000) {
                $j('#Force :input').each(function(i) {
                    if (i >= 0 && i <= 11) {
                        $j(this).val(0);
                    }
                });
                for (var j = 0; j < this.A_ForceConfig.length; j++) {
                    this.A_ForceConfig[j].ForceNum = 0;
                }

                alert("攻击方兵力配置超过人口上限");
                tem = false;
            }
            if (AllCount > 100000 && DAllCount > 100000) {
                $j('#Force :input').each(function(i) {

                    $j(this).val(0);

                });
                for (var j = 0; j < this.D_ForceConfig.length; j++) {
                    this.D_ForceConfig[j].ForceNum = 0;
                }
                for (var j = 0; j < this.A_ForceConfig.length; j++) {
                    this.A_ForceConfig[j].ForceNum = 0;
                }
                alert("双方兵力配置超过人口上限");
                tem = false;
            }
            if (tem) {
                this.CloseDiv(1);
            }
            return tem;

        }
    },
    //加载战场兵力信息
    InitForceDetail: function(index) {
        if (Spage.WarType != 2 || index != 1) {
            this.CloseDiv(index);
        }
        switch (index) {
            case 0: this.GetDivNumber(0); this.LoadCityDefenseTable(); break;
            case 1: this.GetDivNumber(1); this.CheckForce(Spage.WarType); this.LoadForceTable(); break;
            case 2: ; this.GetDivNumber(2); break;
            case 3: this.GetBattFieldNumber(); break;
            case 4: this.GetDivNumber(3); this.ChangeGeneralByProps(); break;
            default: this.ShowBattField(); break;
        }
        //加载统帅能力

        if (this.AttGeneral != null) {
            var AForceNum = 0;
            $j.each(this.A_ForceConfig, function(i) {
                AForceNum += parseInt(Spage.A_ForceConfig[i].ForceNum);
            });
            var corrCommand = parseInt(this.AttGeneral.NewCommand);
            var hufu = 0;
            if (this.AProps[4] == 1) {
                corrCommand = parseInt(this.AttGeneral.NewCommand) * 2 / 3;
                hufu = 0.5;
            }
            //var ACommandNumber = parseInt(this.AttGeneral.NewCommand+this.AttGeneral.Command*(parseInt(this.A_TechnologyConfig[6].TechLevel) * 0.1)) * 100 * (1 + parseInt(this.A_TechnologyConfig[6].TechLevel) * 0.1);
            //var ACommandNumber = parseInt(this.AttGeneral.NewCommand + this.AttGeneral.Command * (parseInt(this.A_TechnologyConfig[6].TechLevel) * 0.1)) * 100;
            var ACommandNumber = parseInt(corrCommand * (hufu + parseInt(this.A_TechnologyConfig[6].TechLevel) * 0.1 + 1)) * 100;
            //            if (this.ACProps[4] == 0 && parseInt(this.AProps[4]) == 1) {
            //                ACommandNumber = parseInt(ACommandNumber * 1.5);
            //            }
            var ARank = AForceNum == 0 ? 100 : (((ACommandNumber * 100) / AForceNum) > 100 ? 100 : parseInt((ACommandNumber * 100) / AForceNum));
            document.getElementById('AArmNumState').innerHTML = "满统兵力:" + ACommandNumber + " 当前状态加成:" + ARank + "%";
        }
        else {
            document.getElementById('AArmNumState').innerHTML = "";
        }
        if (this.DefenseGeneral != null) {
            var DForceNum = 0;
            $j.each(this.D_ForceConfig, function(i) {
                DForceNum += parseInt(Spage.D_ForceConfig[i].ForceNum);
            });
            var DcorrCommand = parseInt(this.DefenseGeneral.NewCommand);
            var Dhufu = 0;
            if (this.AProps[4] == 1) {
                DcorrCommand = parseInt(this.DefenseGeneral.NewCommand) * 2 / 3;
                Dhufu = 0.5;
            }
            var DCommandNumber = parseInt(DcorrCommand * (Dhufu + parseInt(this.D_TechnologyConfig[6].TechLevel) * 0.1 + 1)) * 100;

            //var DCommandNumber = parseInt(this.DefenseGeneral.NewCommand) * 100 * (1 + parseInt(this.D_TechnologyConfig[6].TechLevel) * 0.1);
            //var DCommandNumber = parseInt(this.DefenseGeneral.NewCommand + this.DefenseGeneral.Command * (parseInt(this.D_TechnologyConfig[6].TechLevel) * 0.1)) * 100;
            //            if (this.DCProps[4] == 0 && parseInt(this.DProps[4]) == 1) {
            //                DCommandNumber = DCommandNumber * 1.5;
            //            }
            var DRank = DForceNum == 0 ? 100 : (((DCommandNumber * 100) / DForceNum) > 100 ? 100 : parseInt((DCommandNumber * 100) / DForceNum));
            document.getElementById('DArmNumState').innerHTML = "满统兵力:" + DCommandNumber + " 当前状态加成:" + DRank + "%";
        }
        else {
            document.getElementById('DArmNumState').innerHTML = "";
        }
        if (index != 4) {
            //this.GetWarDistanse(); //获得战场距离
            this.LoadBattlefieldenvironment(); //显示战场中间
        }
    },
    //显示兵种，城防等TABLE
    ShowBattField: function() {
        $j('#spround').html("" + this.WarRound + "");
        this.LoadForceTable();

        this.LoadCityDefenseTable();
        //this.GetWarDistanse(); //获得战场距离
        this.LoadBattlefieldenvironment(); //显示战场中间
    },
    //获得全部相关信息 并显示
    GetBattFieldNumber: function() {
        this.GetDivNumber(1);
        this.LoadForceTable();

        this.GetDivNumber(2);

        if (this.WarType == 0) {
            this.GetDivNumber(0);
        }
        this.LoadCityDefenseTable();

        this.GetDivNumber(3);
    },
    //兵种显示
    LoadForceTable: function() {
        var thispage = this;
        $j.each(this.A_ForceConfig, function(i) { thispage.LoadTableDetail(0, i, this); });
        //防守方兵种
        $j.each(this.D_ForceConfig, function(i) { thispage.LoadTableDetail(1, i, this); });


    },
    //防守方城防显示
    LoadCityDefenseTable: function() {
        var thispage = this;
        if (this.WarType == 0 && parseInt(this.D_CityDefenseConfig[5].CiteDefenseNum) > 0 && parseInt(this.D_CityDefenseConfig[5].Endure) > 0) {//防守方城防
            $j.each(this.D_CityDefenseConfig, function(i) { thispage.LoadTableDetail(2, i, this); });
        }
        else {
            for (var dt = 0; dt <= 5; dt++) {
                var thtml = "<tr><td valign=\"top\" class=\"td_2\"></td>";
                thtml += "<td valign=\"top\" class=\"td_3\">";
                thtml += "</td></tr>";
                thtml += "<tr><td colspan=\"2\" align=\"right\" class=\"td_1\"></td></tr>";
                $j('#BattRight table:eq(' + 3 * dt + ')').html(thtml);
            }
        }
        //战场设置（防守方根据环境不同区别）
        $j('#Battlefieldenvironment').removeClass();
        if (this.WarType == 0 && this.D_CityDefenseConfig[5].CiteDefenseNum > 0 && this.D_CityDefenseConfig[5].Endure > 0) {
            $j('#Battlefieldenvironment').addClass("Location Bbj");
        }
        else {
            $j('#Battlefieldenvironment').addClass("Location");
        }
    },
    //显示攻击类型 0：前进，1：防御，2：后退
    ShowAttactTypeDiv: function(a, side, e) {
        $j('#Targetselect').hide();
        var thispage = this;
        page.SetPosition(e, 'OperateSelect');

        $j('#OperateSelect p a:eq(0)').bind('click', function() { thispage.SelectAttack(a, side, 0); });
        $j('#OperateSelect p a:eq(1)').bind('click', function() { thispage.SelectAttack(a, side, 1); });
        $j('#OperateSelect p a:eq(2)').bind('click', function() { thispage.SelectAttack(a, side, 2); });

        $j('#OperateSelectClose').bind('click', function() {
            $j('#OperateSelect').hide();
            $j('#OperateSelect p a:eq(0)').unbind('click');
            $j('#OperateSelect p a:eq(1)').unbind('click');
            $j('#OperateSelect p a:eq(2)').unbind('click');
        });
    },
    //选择攻击类型
    SelectAttack: function(aindex, side, value) {
        if (side == 0) {
            this.A_ForceConfig[aindex].AttType = parseInt(value);
            this.LoadTableDetail(side, aindex, this.A_ForceConfig[aindex]);
            //          this.LoadBattlefieldenvironment();
        }
        else {
            this.D_ForceConfig[aindex].AttType = parseInt(value);
            this.LoadTableDetail(side, aindex, this.D_ForceConfig[aindex]);
            //            this.LoadBattlefieldenvironment();
        }
        $j('#OperateSelect').hide();
        $j('#OperateSelect p a:eq(0)').unbind('click');
        $j('#OperateSelect p a:eq(1)').unbind('click');
        $j('#OperateSelect p a:eq(2)').unbind('click');
    },


    //显示攻击对象
    ShowAttactTargetDiv: function(index, side, e) {
        $j('#OperateSelect').hide();
        var thispage = this;
        $j('#Targetselect p').html("<a id=\"OperateClose\" href=\"javascript:void(0);\">关闭</a>");
        if (side == 0) {
            $j.each(thispage.D_ForceConfig, function(i) {
                if (parseInt(this.ForceNum) > 0) {
                    $j('#OperateClose').before("<a href=\"javascript:void(0);\">" + this.ForceName + "</a><br />");
                }
            });
            if (this.WarType == 0) {
                if (parseInt(thispage.D_CityDefenseConfig[2].CiteDefenseNum) > 0 && (index == 5 || index == 8 || index == 9 || index == 11)) {
                    $j('#OperateClose').before("<a href=\"javascript:void(0);\">" + thispage.D_CityDefenseConfig[2].DefenseName + "</a><br />");
                }
                if (parseInt(thispage.D_CityDefenseConfig[5].CiteDefenseNum) > 0) {
                    $j('#OperateClose').before("<a href=\"javascript:void(0);\">" + thispage.D_CityDefenseConfig[5].DefenseName + "</a><br />");
                }
            }
        }
        else {
            $j.each(thispage.A_ForceConfig, function(i) {
                if (parseInt(this.ForceNum) > 0) {
                    $j('#OperateClose').before("<a href=\"javascript:void(0);\">" + this.ForceName + "</a><br />");
                }
            });
        }
        page.SetPosition(e, 'Targetselect');
        $j('#Targetselect p a').not($j('#OperateClose')[0]).each(function(i) {
            $j(this).bind('click', function() { thispage.SelectAttackTarget(index, side, $j(this).html()); });
        });
        $j('#OperateClose').bind('click', function() {
            $j('#Targetselect').hide();
            $j('#Targetselect p a').not($j('#OperateClose')[0]).each(function(i) {
                $j(this).unbind('click');
            });
        });
    },
    //选择攻击对象
    SelectAttackTarget: function(index, side, name) {
        if (side == 0) {
            this.A_ForceConfig[index].Target = this.GetAttValueByName($j.trim(name));
            this.LoadTableDetail(side, index, this.A_ForceConfig[index]);
        }
        else if (side == 1) {
            this.D_ForceConfig[index].Target = this.GetAttValueByName($j.trim(name));
            this.LoadTableDetail(side, index, this.D_ForceConfig[index]);
        }
        else {
            this.D_CityDefenseConfig[index].Target = this.GetAttValueByName($j.trim(name));
            this.LoadTableDetail(side, index, this.D_CityDefenseConfig[index]);
        }
        $j('#Targetselect').hide();
        $j('#Targetselect p a').not($j('#OperateClose')[0]).each(function(i) {
            $j(this).unbind('click');
        });
    },

    //获得配置窗口值 0:城防，1：兵力，2：科技，3：道具
    GetDivNumber: function(index) {
        switch (index) {
            case 0: this.GetDefenseNum(); break;
            case 1: this.GetForceNum(); break;
            case 2: this.GetTechnologyNum(); break;
            case 3: this.GetPropsNum(); break;
        }
    },
    //开始战斗
    BeginWar: function() {
        if (this.CanBeNext()) {
            var thispage = this;
            $j('#btnNextRound').show();
            $j('#btnNextRound').bind('click', function() { Spage.NextRound(); }); //下一轮
            $j('#btnReBegin').show();
            $j('#btnReBegin').bind('click', function() { Spage.ReLoad(); }); //重新开始
            $j('#btnBegin').hide();
            this.WarRound = 0;
            $j('#WordWarReport').show();
            $j('#Def_DefenseConfig').hide();
            $j('#Def_ForceConfig').hide();
            $j('#Def_TechnologyConfig').hide();
            $j('#Att_ForceConfig').hide();
            $j('#Att_TechnologyConfig').hide();

            $j('#Att_PicAdd').unbind("click");
            $j('#Def_PicAdd').unbind("click");

            $j('#WarReportText').val("");

            $j.each(this.A_ForceConfig, function(i) { thispage.ABeginForceNumber[i] = parseInt(this.ForceNum); });
            $j.each(this.D_ForceConfig, function(i) { thispage.DBeginForceNumber[i] = parseInt(this.ForceNum); });

            if (this.WarType == 0) {
                $j.each(this.D_CityDefenseConfig, function(i) { thispage.DBeginDefenseNumber[i] = this.CiteDefenseNum; });
                if (parseInt(thispage.D_TechnologyConfig[7].TechLevel) > 0) {
                    thispage.D_CityDefenseConfig[5].Endure = parseInt(parseInt(thispage.D_CityDefenseConfig[5].Endure) * (1 + parseInt(thispage.D_TechnologyConfig[7].TechLevel) * 0.1));
                }
            }

            //1-33 add
            this.SetInitPostion();
            GetDistanse();

            this.LoadBattlefieldenvironment();


        }
    },

    //兵力初始位置设定  1-13
    SetInitPostion: function() {

        if (this.inital == 1) {
            var index = 0;
            this.inital = 0;
            for (index = 0; index < 12; index++) {

                if (this.A_ForceConfig[index].AttType == 1) {
                    this.A_ForceConfig[index].Distanse += 50;
                }
                else if (this.A_ForceConfig[index].AttType == 0) {
                    this.A_ForceConfig[index].Distanse += 100;
                }

                if (this.D_ForceConfig[index].AttType == 1) {
                    this.D_ForceConfig[index].Distanse += 50;
                }
                else if (this.D_ForceConfig[index].AttType == 0) {
                    this.D_ForceConfig[index].Distanse += 100;
                }
            }
        }

    },
    //重置
    ReLoad: function() {
        if (!confirm("你确定重置战场吗？")) return;
        $j('#btnReBegin').unbind('click');
        $j('#ReportSubmit').unbind('click');
        this.WarRound = 0;
        this.BeInBatte = 0; //未进入过战场，1：已进入过战场
        this.WarDistanse = 0;
        $j('#warside').val(0);
        $j('#warenvironment').val(2);
        this.A_ForceConfig = new Array(); //攻击方兵力配置
        this.A_TechnologyConfig = new Array(); //攻击方科技配置

        this.D_ForceConfig = new Array(); //防守方兵力配置
        this.D_TechnologyConfig = new Array(); //防守方科技配置
        this.D_CityDefenseConfig = new Array(); //防守方城防配置
        this.WarDistanse = 0;
        this.AProps = new Array(0, 0, 0, 0, 0); //攻击方道具
        this.DProps = new Array(0, 0, 0, 0, 0); //防守方道具
        this.WinSide = null; //获胜方
        this.IsOver = false; //是否已结束

        this.ABeginForceNumber = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        this.DBeginForceNumber = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        this.DBeginDefenseNumber = new Array(0, 0, 0, 0, 0);

        if (this.SelfSide == 0) {
            this.ResetProperties(this.ACProps, this.AttGeneral);
            GoBackToGeneral(this.AttGeneral);
        }
        else {
            this.ResetProperties(this.DCProps, this.DefenseGeneral);
            GoBackToGeneral(this.DefenseGeneral);
        }
        this.ACProps = new Array(0, 0, 0, 0, 0);
        this.DCProps = new Array(0, 0, 0, 0, 0);
        $j('#Att_PicAdd').unbind();
        $j('#Def_PicAdd').unbind();
        $j('#SendReport').hide();

        $j('#battlefieldDistanse').html('战场距离0');
        $j('#Def_DefenseConfig').show();
        $j('#Def_ForceConfig').show();
        $j('#Def_TechnologyConfig').show();
        $j('#Att_ForceConfig').show();
        $j('#Att_TechnologyConfig').show();
        this.AttGeneral = null; //攻击方武将信息
        this.DefenseGeneral = null; //防守方武将信息
        page.InitOtherGeneral();
        $j('#City :input,#Force :input,#Technology :input,#Props :input').val(0);
        this.WarReport = "";
        $j('#WordWarReport').hide();
        $j('#WarReportText').val("");
    },

    //回复被道具改变的武将属性
    ResetProperties: function(array, obj) {
        if (array[2] == 1) {
            obj.AddPropChivalrous(0);
        }
        if (array[3] == 1) {
            obj.AddPropResourcefulness(0);
        }
        if (array[4] == 1) {
            obj.AddProCommand(0);
        }
    },

    //可以开始（继续）战斗
    CanBeNext: function() {
        if (this.AttGeneral == null) {
            alert("当前无法开始战斗，请选择进攻方武将！");
            return false;
        }
        if (this.DefenseGeneral == null) {
            alert("当前无法开始战斗，请选择防守方武将！");
            return false;
        }
        var a = false;
        for (var i = 0; i < this.A_ForceConfig.length; i++) {
            if (parseInt(this.A_ForceConfig[i].ForceNum) > 0) {
                a = true; break;
            }
        }
        var b = false;
        for (var j = 0; j < this.D_ForceConfig.length; j++) {
            if (parseInt(this.D_ForceConfig[j].ForceNum) > 0) {
                b = true; break;
            }
        }
        //        if (this.WarType == 0) {
        //            var c = false;
        //            for (var i = 0; i < this.D_CityDefenseConfig.length - 1; i++) {
        //                if (parseInt(this.D_CityDefenseConfig[i].CiteDefenseNum) > 0) {
        //                    c = true; break;
        //                }
        //            }
        //            if (parseInt(this.D_CityDefenseConfig[5].CiteDefenseNum) > 0 && parseInt(this.D_CityDefenseConfig[5].Endure) > 0) {
        //                c = true;
        //            }
        //            b = b || c;
        //        }
        if (a && b) {
            return true;

        }
        else {
            alert("当前无法开始战斗，请配置参战兵力！");
            return false;
        }
        return true;
    },
    //下一步
    NextRound: function() {
        //if (this.CanBeNext()) {
        ShowTripDiv('TipsDiv', "处理中，请稍候！");
        $j('#CoverDiv').removeClass();
        $j('#CoverDiv').addClass('tisxxdiv');
        $j('#btnNextRound').unbind('click');
        //进攻方武将信息
        var AGeneral = this.AttGeneral.Name + "|" + this.AttGeneral.NewLevel + "|";
        AGeneral += (this.AttGeneral.NewCommand == null ? this.AttGeneral.Command : this.AttGeneral.NewCommand) + "|";
        AGeneral += this.AttGeneral.NewAffairs + "|" + this.AttGeneral.NewChivalrous + "|" + this.AttGeneral.NewResourcefulness + "|";
        AGeneral += (this.AttGeneral.NewAttack == null ? this.AttGeneral.Attack : this.AttGeneral.NewAttack) + "|";
        AGeneral += (this.AttGeneral.NewDefense == null ? this.AttGeneral.Defense : this.AttGeneral.NewDefense) + "|";
        AGeneral += (this.AttGeneral.NewSpeed == null ? this.AttGeneral.Speed : this.AttGeneral.NewSpeed) + "|";
        AGeneral += this.AttGeneral.NewPower + "|" + this.AttGeneral.NewEnergy + "|" + this.AttGeneral.Command;
        //防守方武将信息
        var DGeneral = this.DefenseGeneral.Name + "|" + this.DefenseGeneral.NewLevel + "|";
        DGeneral += (this.DefenseGeneral.NewCommand == null ? this.DefenseGeneral.Command : this.DefenseGeneral.NewCommand) + "|";
        DGeneral += this.DefenseGeneral.NewAffairs + "|" + this.DefenseGeneral.NewChivalrous + "|" + this.DefenseGeneral.NewResourcefulness + "|";
        DGeneral += (this.DefenseGeneral.NewAttack == null ? this.DefenseGeneral.Attack : this.DefenseGeneral.NewAttack) + "|";
        DGeneral += (this.DefenseGeneral.NewDefense == null ? this.DefenseGeneral.Defense : this.DefenseGeneral.NewDefense) + "|";
        DGeneral += (this.DefenseGeneral.NewSpeed == null ? this.DefenseGeneral.Speed : this.DefenseGeneral.NewSpeed) + "|";
        DGeneral += this.DefenseGeneral.NewPower + "|" + this.DefenseGeneral.NewEnergy + "|" + this.DefenseGeneral.Command;
        var Abingli = "";
        var ASForceNum = 0;
        $j.each(this.ABeginForceNumber, function() { ASForceNum += parseInt(this); });
        var DSForceNum = 0;
        $j.each(this.DBeginForceNumber, function() { DSForceNum += parseInt(this); });
        $j.each(this.A_ForceConfig, function(i) {
            if (i >= 0 && i <= 10) {
                Abingli += this.ForceNum + "|" + this.Target + "|" + this.AttType + "|" + this.Distanse + "$";
            }
            else {
                Abingli += this.ForceNum + "|" + this.Target + "|" + this.AttType + "|" + this.Distanse;
            }
        });
        var Dbingli = "";
        $j.each(this.D_ForceConfig, function(i) {
            if (i >= 0 && i <= 10) {
                Dbingli += this.ForceNum + "|" + this.Target + "|" + this.AttType + "|" + this.Distanse + "$";
            }
            else {
                Dbingli += this.ForceNum + "|" + this.Target + "|" + this.AttType + "|" + this.Distanse;
            }
        });
        var ATechnology = "";
        $j.each(this.A_TechnologyConfig, function(i) {
            if (i >= 0 && i <= 6) {
                ATechnology += this.TechLevel + "|";
            }
            else {
                ATechnology += this.TechLevel;
            }
        });
        var DTechnology = "";

        $j.each(this.D_TechnologyConfig, function(i) {
            if (i >= 0 && i <= 6) {
                DTechnology += this.TechLevel + "|";
            }
            else {
                DTechnology += this.TechLevel;
            }
        });
        var CityDefense = "";
        if (this.WarType == 0) {
            for (var x = 0; x <= 4; x++) {
                if (x == 2) {
                    CityDefense += this.D_CityDefenseConfig[2].CiteDefenseNum + "|";
                }
                else {
                    CityDefense += this.DBeginDefenseNumber[x] + "|";
                }
            }

            CityDefense += this.D_CityDefenseConfig[5].CiteDefenseNum + "|" + this.D_CityDefenseConfig[2].Target + "|" + this.D_CityDefenseConfig[5].Endure;

            $j.each(this.D_CityDefenseConfig, function(i) {
                if (i >= 0 && i <= 4) {
                    CityDefense += "|" + this.CiteDefenseNum;
                }
            });
        }
        else {
            CityDefense = "0|0|0|0|0|0|5|0|0|0|0|0|0";
        }
        var APropsList = this.AProps[0] + "|" + this.AProps[1] + "|" + this.AProps[2] + "|" + this.AProps[3] + "|" + this.AProps[4];
        var DPropsList = this.DProps[0] + "|" + this.DProps[1] + "|" + this.DProps[2] + "|" + this.DProps[3] + "|" + this.DProps[4];


        //            alert("攻击方武将" + AGeneral);
        //            alert("防守方武将" + DGeneral);
        //            alert("攻击方兵力" + Abingli);
        //            alert("防守方兵力" + Dbingli);
        //                    alert("攻击方科技" + ATechnology);
        //                    alert("防守方科技" + DTechnology);
        // alert("城防" + CityDefense);
        //            alert("攻击方道具" + APropsList);
        //            alert("防守方道具" + DPropsList);
        //            alert((parseInt(this.WarRound) + 1) + "," + this.WarType + "," + this.WarDistanse);

        var Action = "?action=Battlefield&AGeneral=" + AGeneral + "&DGeneral=" + DGeneral + "&Abingli=" + Abingli + "&Dbingli=" + Dbingli + "&ATechnology=" + ATechnology;
        Action += "&DTechnology=" + DTechnology + "&CityDefense=" + CityDefense + "&Type=" + this.WarType + "&round=" + (parseInt(this.WarRound) + 1);
        Action += "&Distance=" + this.WarDistanse + "&AProps=" + APropsList + "&DProps=" + DPropsList + "&ASNum=" + ASForceNum + "&DSNum=" + DSForceNum;
        AjaxMethod(Action, GetJSONStr);

        //}

    },
    //显示战场中间信息
    LoadBattlefieldenvironment: function() {
        $j('#LeftLine').attr('style', 'padding-left: ' + this.GetLeftLinePosition() + 'px; float: left;');
        $j('#RightLine').attr('style', 'padding-right: ' + this.GetRightLinePosition() + 'px; float: right;');

        for (var j = 0; j < 12; j++) {
            var tr = " <li>";
            var sty = "";
            if (parseInt(this.A_ForceConfig[j].ForceNum) > 0) {
                sty = "padding-left: " + this.GetPositionPx(this.A_ForceConfig[j].Distanse) + "px;";

                tr += "<p class=\"Brightp_1\">" + this.A_ForceConfig[j].ForceName.substring(0, 1) + "</p>";
            }
            if (parseInt(this.D_ForceConfig[j].ForceNum) > 0) {
                sty += "padding-right: " + this.GetPositionPx(this.D_ForceConfig[j].Distanse) + "px;";
                tr += "<p class=\"Brightp_2\">" + this.D_ForceConfig[j].ForceName.substring(0, 1) + "</p>";
            }
            tr += "</li>";
            $j('#WarField li:eq(' + j + ')').replaceWith(tr);
            if (sty != "") {
                $j('#WarField li:eq(' + j + ')').attr('style', sty);
            }

        }
    },

    //

    //加载单个TABLE信息
    LoadTableDetail: function(side, index, obj) {
        //setTimeout('', 10000000);
        var thispage = this;
        if (side == 0) {//攻击方
            $j('#BattLeft table:eq(' + index + ')').empty();
            if (parseInt(obj.ForceNum) > 0) {
                var thtml = "<tr style=\"display:none;\"><td valign=\"top\" class=\"td_2\"><img src=\"" + obj.BigPicture + "\" width=\"36\" height=\"29\" /></td>";
                thtml += "<td valign=\"top\" class=\"td_3\"><img src=\"" + thispage.GetAttPicByType(obj.AttType) + "\" width=\"13\" height=\"13\" style=\"margin-bottom: 2px;\" /><br />";
                thtml += "<p class=\"Brightp\"><a href=\"javascript:void(0);\">" + thispage.GetAttTarget(parseInt(obj.Target)) + "</a></p></td></tr>";
                thtml += "<tr><td colspan=\"2\" align=\"right\" class=\"td_1\">" + obj.ForceNum + "</td></tr>";

                $j('#BattLeft table:eq(' + index + ')').html(thtml);

                $j('#BattLeft table:eq(' + index + ') tr:eq(0) td:eq(1) img').bind('click', function(event) { thispage.ShowAttactTypeDiv(index, 0, event); });
                $j('#BattLeft table:eq(' + index + ') tr:eq(0) td:eq(1)').children('p').children('a').bind('click', function(event) { thispage.ShowAttactTargetDiv(index, 0, event); });
            }
            else {
                var thtml = "<tr style=\"display:none;\"><td valign=\"top\" class=\"td_2\"></td>";
                thtml += "<td valign=\"top\" class=\"td_3\">";
                thtml += "</td></tr>";
                thtml += "<tr><td colspan=\"2\" align=\"right\" class=\"td_1\"></td></tr>";
                $j('#BattLeft table:eq(' + index + ')').html(thtml);

            }
            $j('#BattLeft table:eq(' + index + ') tr').fadeIn(1);

        }
        else if (side == 1) {//防守方
            $j('#BattRight table:eq(' + (index + parseInt(index / 2) + 1) + ')').empty();
            if (parseInt(obj.ForceNum) > 0) {
                var thtml = "<tr style=\"display:none;\"><td valign=\"top\" class=\"td_2\"><img src=\"" + obj.BigPicture + "\" width=\"36\" height=\"29\" /></td>";
                thtml += "<td valign=\"top\" class=\"td_3\"><img src=\"" + thispage.GetAttPicByType(obj.AttType) + "\" width=\"13\" height=\"13\" style=\"margin-bottom: 2px;\" /><br />";
                thtml += "<p class=\"Brightp\"><a href=\"javascript:void(0);\">" + thispage.GetAttTarget(parseInt(obj.Target)) + "</a></p></td></tr>";
                thtml += "<tr><td colspan=\"2\" align=\"right\" class=\"td_1\">" + obj.ForceNum + "</td></tr>";
                $j('#BattRight table:eq(' + (index + parseInt(index / 2) + 1) + ')').html(thtml);


                $j('#BattRight table:eq(' + (index + parseInt(index / 2) + 1) + ') tr:eq(0) td:eq(1) img').bind('click', function(event) { thispage.ShowAttactTypeDiv(index, 1, event); });
                $j('#BattRight table:eq(' + (index + parseInt(index / 2) + 1) + ') tr:eq(0) td:eq(1)').children('p').children('a').bind('click', function(event) { thispage.ShowAttactTargetDiv(index, 1, event); });
            }
            else {
                var thtml = "<tr style=\"display:none;\"><td valign=\"top\" class=\"td_2\"></td>";
                thtml += "<td valign=\"top\" class=\"td_3\">";
                thtml += "</td></tr>";
                thtml += "<tr><td colspan=\"2\" align=\"right\" class=\"td_1\"></td></tr>";
                $j('#BattRight table:eq(' + (index + parseInt(index / 2) + 1) + ')').html(thtml);

            }
            $j('#BattRight table:eq(' + (index + parseInt(index / 2) + 1) + ') tr').fadeIn(1);
        }
        else if (side == 2) {
            $j('#BattRight table:eq(' + 3 * index + ')').empty();
            if (index >= 0 && index <= 4) {
                if (parseInt(obj.CiteDefenseNum) > 0) {
                    var thtml = "";
                    if (parseInt(index) != 2) {
                        thtml = "<tr style=\"display:none;\"><td valign=\"top\" class=\"td_2\"><img src=\"" + obj.BigPicture + "\" width=\"36\" height=\"29\" /></td>";
                        thtml += "<td valign=\"top\" class=\"td_3\">";
                        thtml += "</td></tr>";
                        thtml += "<tr><td colspan=\"2\" align=\"right\" class=\"td_1\">" + obj.CiteDefenseNum + "</td></tr>";
                    }
                    else {
                        thtml = "<tr><td valign=\"top\" class=\"td_2\"><img src=\"" + obj.BigPicture + "\" width=\"36\" height=\"29\" /></td>";
                        thtml += "<td valign=\"top\" class=\"td_3\"><br />";
                        thtml += "<p class=\"Brightp\"><a href=\"javascript:void(0);\">" + thispage.GetAttTarget(parseInt(obj.Target)) + "</a></p></td></tr>";
                        thtml += "<tr><td colspan=\"2\" align=\"right\" class=\"td_1\">" + obj.CiteDefenseNum + "</td></tr>";

                    }
                    $j('#BattRight table:eq(' + 3 * index + ')').html(thtml);

                    $j('#BattRight table:eq(' + 3 * index + ') tr:eq(0) td:eq(1)').children('p').children('a').bind('click', function(event) { thispage.ShowAttactTargetDiv(index, 2, event); });
                }
                else {
                    var thtml = "<tr style=\"display:none;\"><td valign=\"top\" class=\"td_2\"></td>";
                    thtml += "<td valign=\"top\" class=\"td_3\">";
                    thtml += "</td></tr>";
                    thtml += "<tr><td colspan=\"2\" align=\"right\" class=\"td_1\"></td></tr>";
                    $j('#BattRight table:eq(' + 3 * index + ')').html(thtml);
                }
            }
            else if (index == 5) {
                var thtml = "";
                if (obj.Endure > 0) {
                    thtml = "<tr style=\"display:none;\"><td valign=\"top\" class=\"td_2\"><img src=\"" + obj.BigPicture + "\" width=\"36\" height=\"29\" /></td>";
                    thtml += "<td valign=\"top\" class=\"td_3\">";
                    thtml += "</td></tr>";
                    thtml += "<tr><td colspan=\"2\" align=\"right\" class=\"td_1\">" + obj.CiteDefenseNum + "</td></tr>";
                }
                else {
                    thtml = "<tr style=\"display:none;\"><td valign=\"top\" class=\"td_2\"></td>";
                    thtml += "<td valign=\"top\" class=\"td_3\">";
                    thtml += "</td></tr>";
                    thtml += "<tr><td colspan=\"2\" align=\"right\" class=\"td_1\"></td></tr>";
                }
                $j('#BattRight table:eq(15)').html(thtml);
            }
            $j('#BattRight table:eq(' + 3 * index + ') tr').fadeIn(1);
        }
    },
    //获得战场距离
    GetWarDistanse: function() {
        var thispage = this;
        var A_Distanse = 0;
        $j.each(this.A_ForceConfig, function(i) {
            if (parseInt(this.ForceNum) > 0) {
                var distanse = parseInt(thispage.AttackDistanse[i]); //1-8修改战距离  4-14修改

                if (i == 5 || i == 9 || i == 11) {
                    if (parseInt(thispage.A_TechnologyConfig[5].TechLevel) > 0) {
                        distanse = parseInt(distanse * (1 + (parseInt(thispage.A_TechnologyConfig[5].TechLevel) * 0.05)));
                    }
                }
                if (parseInt(distanse) > parseInt(A_Distanse)) {
                    A_Distanse = parseInt(distanse);
                }
            }
        });
        var D_Distanse = 0;
        $j.each(this.D_ForceConfig, function(i) {
            if (parseInt(this.ForceNum) > 0) {
                var distanse = parseInt(thispage.AttackDistanse[i]); //1-8修改战距离 4-14修改

                if (i == 5 || i == 9 || i == 11) {
                    if (parseInt(thispage.D_TechnologyConfig[5].TechLevel) > 0) {
                        distanse = parseInt(distanse * (1 + parseInt(thispage.D_TechnologyConfig[5].TechLevel) * 0.05));
                        //4-14修改
                        if (parseInt(this.D_CityDefenseConfig[5].CiteDefenseNum) > 0) {
                            distanse = parseInt(distanse * (1 + (0.03 * parseInt(this.D_CityDefenseConfig[5].CiteDefenseNum))));
                        }
                    }
                }
                if (parseInt(distanse) > parseInt(D_Distanse)) {
                    D_Distanse = parseInt(distanse);
                }
            }
        });
        if (this.WarType == 0) {
            var CityD_Distanse = 0;
            $j.each(this.D_CityDefenseConfig, function(i) {
                if (i >= 0 && i <= 4) {
                    if (parseInt(this.CiteDefenseNum) > 0) {
                        var distanse = parseInt(thispage.CityDefDistanse[i]); //1-8修改战距离  4-14修改

                        if (i == 2) {
                            if (parseInt(thispage.D_TechnologyConfig[5].TechLevel) > 0) {
                                distanse = parseInt(distanse * (1 + (parseInt(thispage.D_TechnologyConfig[5].TechLevel) * 0.05)));
                            }
                            //4-14修改
                            if (parseInt(this.D_CityDefenseConfig[5].CiteDefenseNum) > 0) {
                                distanse = parseInt(distanse * (1 + (0.03 * parseInt(this.D_CityDefenseConfig[5].CiteDefenseNum))));
                            }
                        }

                        if (parseInt(distanse) > parseInt(CityD_Distanse)) {
                            CityD_Distanse = parseInt(distanse);
                        }
                        CityD_Distanse += 100;
                    }
                }
            });
            if (D_Distanse > CityD_Distanse) {
                D_Distanse = D_Distanse;
            }
            else {
                D_Distanse = CityD_Distanse;
            }
            //4-14修改
            //            if (parseInt(this.D_CityDefenseConfig[5].CiteDefenseNum) > 0) {
            //                D_Distanse = parseInt(D_Distanse * (1 + (0.03 * parseInt(this.D_CityDefenseConfig[5].CiteDefenseNum))));
            //            }
        }
        if (A_Distanse > D_Distanse)
            thispage.WarDistanse = A_Distanse + 399;
        else
            thispage.WarDistanse = D_Distanse + 399;


        $j('#battlefieldDistanse').html('战场距离' + thispage.WarDistanse + '');
    },
    //活动科技配置信息
    GetTechnologyNum: function() {
        var thispage = this;
        $j('#Technology :input').each(function(i) {
            if (i % 2 == 0) {
                thispage.A_TechnologyConfig[i / 2] = new TechnologyConfig($j(this).val() == "" ? 0 : $j(this).val());
            }
            else {
                thispage.D_TechnologyConfig[(i - 1) / 2] = new TechnologyConfig($j(this).val() == "" ? 0 : $j(this).val());
            }
        });
    },
    //获得兵力配置信息(数量)
    GetForceNum: function() {
        var thispage = this;
        $j('#Force :input').each(function(i) {
            if (i >= 0 && i <= 11) {
                if (thispage.A_ForceConfig[i]) {
                    thispage.A_ForceConfig[i].ForceNum = $j(this).val() == "" ? 0 : $j(this).val();
                }
                else {
                    thispage.A_ForceConfig[i] = new ForceConfig($j(this).val() == "" ? 0 : $j(this).val(), $j(this).parent().prev('li').html(), i, thispage.ForcePicList[i], 0, 0);
                }
            }
            else {
                if (thispage.D_ForceConfig[i - 12]) {
                    thispage.D_ForceConfig[i - 12].ForceNum = $j(this).val() == "" ? 0 : $j(this).val();
                }
                else {
                    thispage.D_ForceConfig[i - 12] = new ForceConfig($j(this).val() == "" ? 0 : $j(this).val(), $j(this).parent().prev('li').html(), (i - 12), thispage.ForcePicList[i - 12], 0, 0);
                }
            }
        });
    },
    //获得城防配置信息
    GetDefenseNum: function() {
        var thispage = this;
        $j('#City :input').each(function(i) {
            if (thispage.D_CityDefenseConfig[i]) {
                thispage.D_CityDefenseConfig[i].CiteDefenseNum = $j(this).val() == "" ? 0 : $j(this).val();
                thispage.D_CityDefenseConfig[i].Endure = parseInt($j(this).val() == "" ? 0 : $j(this).val()) * 1000000;
            }
            else {
                thispage.D_CityDefenseConfig[i] = new CityDefenseConfig($j(this).val() == "" ? 0 : $j(this).val(), $j(this).parent().prev('li').html(), 5, thispage.D_DefensePicList[i], parseInt($j(this).val() == "" ? 0 : $j(this).val()) * 1000000);
            }
        });
    },
    //获得道具数据
    GetPropsNum: function() {
        var thispage = this;
        $j('#Props :input').each(function(i) {
            if (i % 2 == 0) {
                thispage.AProps[i / 2] = $j(this).val();
            }
            else {
                thispage.DProps[(i - 1) / 2] = $j(this).val();
            }
        });
    },
    //更新武将勇武,智谋,统帅根据道具
    ChangeGeneralByProps: function() {
        var thispage = this;
        $j('#Props :input').each(function(i) {
            if (i == 4) {
                if (thispage.AttGeneral && (parseInt(thispage.ACProps[2]) != parseInt($j(this).val()))) {
                    thispage.AttGeneral.AddPropChivalrous(parseInt($j(this).val()));
                }
            }
            else if (i == 5) {
                if (thispage.DefenseGeneral && (parseInt(thispage.DCProps[2]) != parseInt($j(this).val()))) {
                    thispage.DefenseGeneral.AddPropChivalrous(parseInt($j(this).val()));
                }
            }
            else if (i == 6) {
                if (thispage.AttGeneral && (parseInt(thispage.ACProps[3]) != parseInt($j(this).val()))) {
                    thispage.AttGeneral.AddPropResourcefulness(parseInt($j(this).val()));
                }
            }
            else if (i == 7) {
                if (thispage.DefenseGeneral && (parseInt(thispage.DCProps[3]) != parseInt($j(this).val()))) {
                    thispage.DefenseGeneral.AddPropResourcefulness(parseInt($j(this).val()));
                }
            }
            else if (i == 8) {
                if (thispage.AttGeneral && (parseInt(thispage.ACProps[4]) != parseInt($j(this).val()))) {
                    thispage.AttGeneral.AddProCommand(parseInt($j(this).val()));
                }
            }
            else if (i == 9) {
                if (thispage.DefenseGeneral && (parseInt(thispage.DCProps[4]) != parseInt($j(this).val()))) {
                    thispage.DefenseGeneral.AddProCommand(parseInt($j(this).val()));
                }
            }
        });
        $j.each(this.ACProps, function(i) { thispage.ACProps[i] = parseInt(thispage.AProps[i]); });
        $j.each(this.DCProps, function(i) { thispage.DCProps[i] = parseInt(thispage.DProps[i]); });
    },
    //获得进攻方最前位置
    GetLeftLinePosition: function() {
        var thispage = this;
        var LeftPostion = 0;
        $j.each(this.A_ForceConfig, function(i) {
            if (parseInt(thispage.A_ForceConfig[i].ForceNum) > 0) {
                if (thispage.GetPositionPx(this.Distanse) > LeftPostion) {
                    LeftPostion = thispage.GetPositionPx(this.Distanse);
                }
            }
        });
        return LeftPostion + 14;
    },

    //获得防守方最前位置
    GetRightLinePosition: function() {
        var thispage = this;
        var RightPosition = 0;
        $j.each(this.D_ForceConfig, function(i) {
            if (parseInt(thispage.D_ForceConfig[i].ForceNum) > 0) {
                if (thispage.GetPositionPx(this.Distanse) > RightPosition) {
                    RightPosition = thispage.GetPositionPx(this.Distanse);
                }
            }
        });
        return RightPosition + 14;
    },
    GetPositionPx: function(value) {
        if (this.WarDistanse == 0) {
            return 0;
        }
        else if (parseInt(value) > this.WarDistanse) {
            return parseInt(this.GetPositionPx(this.WarDistanse - 10)) - 15;
        }
        else {
            return parseInt((332 * value) / (this.WarDistanse));
        }
    },
    //获得攻击类型图片
    GetAttPicByType: function(value) {
        switch (value) {
            case 0: return "images/001.gif"; break;
            case 1: return "images/003.gif"; break;
            case 2: return "images/002.gif"; break;
            default: return "images/001.gif"; break;
        }
    },
    //获得攻击对象首字
    GetAttTarget: function(value) {
        switch (value) {
            case 0: return "民"; break;
            case 1: return "义"; break;
            case 2: return "斥"; break;
            case 3: return "长"; break;
            case 4: return "刀"; break;
            case 5: return "弓"; break;
            case 6: return "轻"; break;
            case 7: return "铁"; break;
            case 8: return "辎"; break;
            case 9: return "床"; break;
            case 10: return "冲"; break;
            case 11: return "投"; break;
            case 12: return "箭"; break;
            case 13: return "城"; break;
            default: return ""; break;
        }
    },
    //根据攻击对象名称获得攻击对象值
    GetAttValueByName: function(Aname) {
        switch (Aname) {
            case "民夫": return 0; break;
            case "义兵": return 1; break;
            case "斥候": return 2; break;
            case "长枪兵": return 3; break;
            case "刀盾兵": return 4; break;
            case "弓箭兵": return 5; break;
            case "轻骑兵": return 6; break;
            case "铁骑兵": return 7; break;
            case "辎重车": return 8; break;
            case "床弩": return 9; break;
            case "冲车": return 10; break;
            case "投石车": return 11; break;
            case "箭塔": return 12; break;
            case "城墙": return 13; break;
            default: return 14; break;
        }
    },
    //打开城防,兵种，科技配置层 side 0：攻击方 1：防守方
    OpenDiv: function(index) {
        this.CloseDiv(5);
        $j('#Targetselect').hide();
        $j('#OperateSelect').hide();
        $j('#Att_PicAdd').mouseout();
        $j('#Def_PicAdd').mouseout();
        $j('#Att_PicAdd').unbind();
        $j('#Def_PicAdd').unbind();
        switch (index) {
            case 0: this.LoadNumber(0); showDialog("City"); break; //城防
            case 1: this.LoadNumber(1); showDialog("Force"); this.showOrHide(Spage.WarType); break; //兵种
            case 2: this.LoadNumber(2); showDialog("Technology"); break; //科技
            case 3: this.LoadNumber(3); showDialog("Props"); break; //道具
        }
    },
    showOrHide: function(type) {
        if (type == 2) {
            $j('#ChuanNu').hide();
            $j('#ChongChe').hide();
            $j('#TouShiChe').hide();
            $j('#DChuanNu').hide();
            $j('#DChongChe').hide();
            $j('#DTouShiChe').hide();
            $j('#Ul2').show();
            $j('#Ul1').show();
        } else {
            $j('#ChuanNu').show();
            $j('#ChongChe').show();
            $j('#TouShiChe').show();
            $j('#DChuanNu').show();
            $j('#DChongChe').show();
            $j('#DTouShiChe').show();
            $j('#Ul2').hide();
            $j('#Ul1').hide();
        }

    },


    //加载配置窗口值0：城防，1：兵种，2：科技，3：道具
    LoadNumber: function(index) {
        switch (index) {
            case 0: this.LoadCityDefense(); break;
            case 1: this.LoadForce(); break;
            case 2: this.LoadTechnology(); break;
            case 3: this.LoadProps(); break;
            default:
                if (parseInt(this.WarType) == 0) {
                    this.LoadCityDefense();
                } this.LoadForce(); this.LoadTechnology(); this.LoadProps(); break;
        }
    },
    //加载城防窗口信息
    LoadCityDefense: function() {
        var thispage = this;
        $j('#City :input').each(function(i) {
            $j(this).val(thispage.D_CityDefenseConfig[i].CiteDefenseNum);
        });
    },
    //加载兵种窗口信息
    LoadForce: function() {
        var thispage = this;
        $j('#Force :input').each(function(i) {
            if (i >= 0 && i < 12) {
                $j(this).val(thispage.A_ForceConfig[i].ForceNum);
            }
            else {
                $j(this).val(thispage.D_ForceConfig[i - 12].ForceNum);
            }
        });
    },
    //加载科技窗口信息
    LoadTechnology: function() {
        var thispage = this;
        $j('#Technology :input').each(function(i) {
            if (i % 2 == 0) {
                $j(this).val(thispage.A_TechnologyConfig[i / 2].TechLevel);
            }
            else {
                $j(this).val(thispage.D_TechnologyConfig[(i - 1) / 2].TechLevel);
            }
        });
    },
    //加载道具值
    LoadProps: function() {
        var thispage = this;
        $j.each(this.AProps, function(i) {
            $j('#Props :input:eq(' + (i * 2) + ')').val(this);
        });
        $j.each(this.DProps, function(i) {
            $j('#Props :input:eq(' + ((i * 2) + 1) + ')').val(this);
        });
    },
    //关闭城防,兵种，科技配置层
    CloseDiv: function(index) {
        switch (index) {
            case 0: $j('#City').hide(); break; //城防
            case 1: $j('#Force').hide(); break; //兵种
            case 2: $j('#Technology').hide(); ; break; //科技
            case 4: $j('#Props').hide(); $j('#Props :input').unbind("change"); break;
            default: $j('#City').hide(); $j('#Force').hide(); $j('#Technology').hide(); $j('#Props').hide(); $j('#Targetselect').hide(); $j('#OperateSelect').hide(); break;
        }
        if (this.AttGeneral) {
            $j('#Att_PicAdd').bind('mouseover', function(event) { ShowGeneralDetail(event, Spage.AttGeneral) });
            $j('#Att_PicAdd').bind('mouseout', function(event) { $j('#GeneralDetail').hide(); }); //隐藏武将详细信息窗口
            $j('#Att_PicAdd').bind('click', function() { Spage.SelectGeneral = 0; Spage.BeInBatte = 1; GoBackToGeneral(Spage.AttGeneral); }); //返回武将模拟器对武将进行编辑
        }
        else {
            $j('#Att_PicAdd').attr('src', 'images/sd-4.jpg');
            $j('#Att_GeneralName').html("无武将");
            $j('#Att_PicAdd').bind('click', function() {
                Spage.SelectGeneral = 0;
                Spage.BeInBatte = 1;
                page.InitOtherGeneral();
                GoBackToGeneral(page.OtherGeneral);
            }); //返回武将模拟器对武将进行编辑
        }

        if (this.DefenseGeneral) {
            $j('#Def_PicAdd').bind('mouseover', function(event) { ShowGeneralDetail(event, Spage.DefenseGeneral) });
            $j('#Def_PicAdd').bind('mouseout', function(event) { $j('#GeneralDetail').hide(); }); //隐藏武将详细信息窗口
            $j('#Def_PicAdd').bind('click', function() { Spage.SelectGeneral = 1; Spage.BeInBatte = 1; GoBackToGeneral(Spage.DefenseGeneral); }); //返回武将模拟器对武将进行编辑
        }
        else {
            $j('#Def_PicAdd').attr('src', 'images/sd-4.jpg');
            $j('#Def_GeneralName').html("无武将");
            $j('#Def_PicAdd').bind('click', function() {
                Spage.SelectGeneral = 1;
                Spage.BeInBatte = 1;
                page.InitOtherGeneral();
                GoBackToGeneral(page.OtherGeneral);
            }); //返回武将模拟器对武将进行编辑
        }
    }
})


//AJAX成功函数
function GetJSONStr(xmlhttprequest) {
    var result = xmlhttprequest.responseText;
    if (result == "Err") {
        alert("执行出错，请重试！");
    }
    else {
        var thispage = Spage;
        Spage.WarRound++;
        var json = eval("(" + result.replace(/\r\n/g, '<br\>') + ")");

        GetAllNumber(json);

        Spage.LoadNumber(4);
        Spage.ShowBattField();
        $j('#WarReportText').val(Spage.WarReport);
        document.getElementById('WarReportText').scrollTop = document.getElementById('WarReportText').scrollHeight;

        //结束后显示战报
        WarEnd();

        $j('#CoverDiv').removeClass();
        $j('#TipsDiv').hide();
        $j('#btnNextRound').bind('click', function() { Spage.NextRound(); }); //下一轮
    }
}

//获取各个参数
function GetAllNumber(json) {
    var result = json;
    //进攻方兵力
    $j.each(result.War.AttackWarparty, function() {
        Spage.A_ForceConfig[parseInt(this.ArmType)].ForceNum = this.WarNum;
        Spage.A_ForceConfig[parseInt(this.ArmType)].Target = this.Target;
        Spage.A_ForceConfig[parseInt(this.ArmType)].Distanse = this.WalkLength;
    });
    //防守方兵力
    $j.each(result.War.Defenseparty, function() {
        Spage.D_ForceConfig[parseInt(this.ArmType)].ForceNum = this.WarNum;
        Spage.D_ForceConfig[parseInt(this.ArmType)].Target = this.Target;
        Spage.D_ForceConfig[parseInt(this.ArmType)].Distanse = this.WalkLength;
    });
    //防守方城防信息
    if (Spage.WarType == 0) {
        $j.each(result.War.DefenceCity, function() {
            if (parseInt(this.TType) >= 0 && parseInt(this.TType) <= 4) {
                if (parseInt(this.TType) == 2) {
                    Spage.D_CityDefenseConfig[parseInt(this.TType)].CiteDefenseNum = parseInt(this.Num);
                }
                else {
                    Spage.D_CityDefenseConfig[parseInt(this.TType)].CiteDefenseNum = parseInt(this.CurrNum);
                }
            }
            else if (parseInt(this.TType) == 5) {
                Spage.D_CityDefenseConfig[parseInt(this.TType)].Endure = parseInt(this.Endure);
            }
        });
    }
    Spage.WarReport += result.War.Information.replace(/<br\>/g, '\r\n');   //战报
    Spage.WarDistanse = result.War.Distance;
    Spage.WarRound = result.War.Round;

    Spage.IsOver = result.War.IsOver == "False" ? false : true;  //是否已结束
    if (Spage.IsOver) {
        Spage.WinSide = result.War.WinPart;
    }
    else {
        Spage.WinSide = "-1";
    }
   

    $j('#battlefieldDistanse').html('战场距离' + Spage.WarDistanse + '');
}

//结束后加载战报信息
function WarEnd() {
    if (Spage.IsOver && Spage.WinSide != "-1") {
        $j('#btnNextRound').hide();
        $j('#btnNextRound').unbind('click');
        $j('#btnWarWord').show();
        $j('#btnWarWord').bind('click', function() { showDialog('WarReportDetail'); }); //显示最终战报
        var AResult = "";
        var DResult = "";
        var EndRound = "";
        if (Spage.WinSide == "2") {//攻击方胜利
            AResult = "胜利";
            DResult = "失败";
            if (Spage.SelfSide == 0) {
                EndRound = "战斗共持续" + Spage.WarRound + "回合 我方(" + Spage.AttGeneral.Name + ")战斗胜利";
            }
            else {
                EndRound = "战斗共持续" + Spage.WarRound + "回合 敌方(" + Spage.AttGeneral.Name + ")战斗胜利";
            }
        }
        else if (Spage.WinSide == "1") {//防守方胜利
            AResult = "失败";
            DResult = "胜利";
            if (Spage.SelfSide == 0) {
                EndRound = "战斗共持续" + Spage.WarRound + "回合 敌方(" + Spage.DefenseGeneral.Name + ")战斗胜利";
            }
            else {
                EndRound = "战斗共持续" + Spage.WarRound + "回合 我方(" + Spage.DefenseGeneral.Name + ")战斗胜利";
            }
        }
        else if (Spage.WinSide == "0") {//平局
            AResult = "平局";
            DResult = "平局";
            EndRound = "战斗共持续" + Spage.WarRound + "回合 平局";
        }

        var ASG = "<ul class=\"Warulli\"><li class=\"Wargk\">攻击方：" + (Spage.SelfSide == 0 ? "我方" : "敌方") + "(" + Spage.AttGeneral.Name + ")</li>";
        ASG += "<li class=\"Wargk1 S22FF2B\">" + AResult + "</li></ul>";
        ASG += " <ul class=\"Warulli_1\"><li class=\"Wargk2\"><img src=\"" + Spage.AttGeneral.PicAdd + "\" width=\"31\" heigth=\"39\" /></li>";
        ASG += "<li class=\"Wargk3 S01D9F4\">等级" + Spage.AttGeneral.NewLevel + "</li></ul>";

        var DSG = "<ul class=\"Warulli\"><li class=\"Wargk\">防守方：" + (Spage.SelfSide == 1 ? "我方" : "敌方") + "(" + Spage.DefenseGeneral.Name + ")</li>";
        DSG += "<li class=\"Wargk1 S22FF2B\">" + DResult + "</li></ul>";
        DSG += " <ul class=\"Warulli_1\"><li class=\"Wargk2\"><img src=\"" + Spage.DefenseGeneral.PicAdd + "\" width=\"31\" heigth=\"39\" /></li>";
        DSG += "<li class=\"Wargk3 S01D9F4\">等级" + Spage.DefenseGeneral.NewLevel + "</li></ul>";

        $j('#ResultRound').html(EndRound);
        $j('#ArrackSideGeneral').html(ASG);
        $j('#DefenseSideGeneral').html(DSG);

        var ASA = "";
        $j.each(Spage.ABeginForceNumber, function(i) {
            if (parseInt(this) > 0) {
                ASA += " <ul class=\"Warulli_2\">";
                ASA += "  <li class=\"Wargk5\">" + Spage.A_ForceConfig[i].ForceName + "</li>";
                ASA += "  <li class=\"Wargk6\">" + this + "</li>";
                ASA += " <li class=\"Wargk5\">" + (parseInt(this) - Spage.A_ForceConfig[i].ForceNum) + "</li>";
                ASA += " </ul>";
            }
        });
        var DSA = "";
        $j.each(Spage.DBeginForceNumber, function(i) {
            if (parseInt(this) > 0) {
                DSA += " <ul class=\"Warulli_2\">";
                DSA += "  <li class=\"Wargk5\">" + Spage.D_ForceConfig[i].ForceName + "</li>";
                DSA += "  <li class=\"Wargk6\">" + this + "</li>";
                DSA += " <li class=\"Wargk5\">" + (parseInt(this) - Spage.D_ForceConfig[i].ForceNum) + "</li>";
                DSA += " </ul>";
            }
        });
        //防守方城防信息
        if (Spage.WarType == 0) {
            for (var w = 0; w <= 4; w++) {
                if (parseInt(Spage.DBeginDefenseNumber[w]) > 0) {
                    DSA += " <ul class=\"Warulli_2\">";
                    DSA += "  <li class=\"Wargk5\">" + Spage.D_CityDefenseConfig[w].DefenseName + "</li>";
                    DSA += "  <li class=\"Wargk6\">" + parseInt(Spage.DBeginDefenseNumber[w]) + "</li>";
                    DSA += " <li class=\"Wargk5\">" + (parseInt(Spage.DBeginDefenseNumber[w]) - Spage.D_CityDefenseConfig[w].CiteDefenseNum) + "</li>";
                    DSA += " </ul>";
                }
            }
        }
        $j('#AttackSideArms').html(ASA);
        $j('#DefenseSideArms').html(DSA);

        alert("战斗结束！");

        $j('#SendReport').show();
        $j('#ReportSubmit').bind('click', function() { SendWarReport(); });
        $j('#ReportChanel').bind('click', function() { CancelSend(); });
    }
}

//发送战报
function SendWarReport() {
    var title = $j.trim($j('#ReportTitle').val());
    if (title == "" || title == "请输入战报标题！") {
        alert("请输入战报名称！"); return;
    }
    if ($j('#Reportdiscuss').val() == "") {
        alert("请输入战报评论！"); return;
    }
    if (!confirm("确定发送此次战斗战报？")) return;
    var content = $j.trim($j('#WarReportText').val() + "\n" + "评论：" + $j('#Reportdiscuss').val());
    content += "\r\n本战报由热血网（http://www.rexue.com）武将模拟器系统生成，如您有意见和建议或想了解更多功能请登录网站并联系我们。";
    var Data = RXSG.GameData.Simulator.SendWarReport(title, content.replace(/\r\n|\n/g, '<br\>')).value;
    if (Data == null) {
        alert("系统忙,请重试！");
        return;
    }
    alert(Data.ExecMessage);
    if (Data.IsSuccess) {
        $j('#ReportSubmit').unbind('click');
        $j('#ReportChanel').unbind('click');
        $j('#SendReport').hide();
    }
    return;
}
//取消发送战报
function CancelSend() {
    $j('#ReportTitle').val("");
    $j('#Reportdiscuss').val("");
}


function GetDistanse()
{
    Spage.GetWarDistanse();
}
