﻿$(function(){
    if(page_type!=undefined)
    {
        $("#page_menu>li:eq(" + page_type + ")").addClass("menu_select");
    }
    RefreshAttribute();
    $("#attribute_more").click(ShowAttributeMoreBox);
});


$.ajaxSetup({error:function(XMLHttpRequest, textStatus, errorThrown){
    //$("a.ui-dialog-titlebar-close").show();
    //alert("与服务器通信错误，建议重新连接！");
    //document.write(XMLHttpRequest.responseText);
    //alert(XMLHttpRequest.responseText);
    }
 });
 
var RefreshAttributeTimer;
 
 function RefreshAttribute()
 {
    if(RefreshAttributeTimer!=undefined) clearInterval(RefreshAttributeTimer);
    $.post("response.aspx?action=2","",RefreshedAttribute,"json");
 }
 
 function LocalRefreshAttribute()
 {
    var flag=false;
    if(Person.Datas.HP<Person.Datas.MaxHP)
    {
        Person.Datas.RenewHPDateTime--;
        if(Person.Datas.RenewHPDateTime<=0)
        {
            Person.Datas.HP++;
            Person.Datas.RenewHPDateTime=Person.Datas.RenewHP;
            $("#attribute_life").html("生命：" + Person.Datas.HP + " / " + Person.Datas.MaxHP);
        }
        flag=true;
    }    
    if(Person.Datas.SP<Person.Datas.MaxSP)
    {
        Person.Datas.RenewSPDateTime--;
        if(Person.Datas.RenewSPDateTime<=0)
        {
            Person.Datas.SP++;
            Person.Datas.RenewSPDateTime=Person.Datas.RenewSP;
            $("#attribute_power").html("体力：" + Person.Datas.SP + " / " + Person.Datas.MaxSP);
        }
        flag=true;
    }
    if(flag)
    { if($("#attribute_more_box").is(":visible")) SetAttributeMoreBox();}
    else
    {clearInterval(RefreshAttributeTimer);}
 }
 
function RefreshedAttribute(obj) {
    if(obj.Status==1)
    {
        Person.Datas=obj.Datas;
        $("#attribute_money").html("金钱：" + obj.Datas.Money);
        $("#attribute_life").html("生命：" + obj.Datas.HP + " / " + obj.Datas.MaxHP);
        $("#attribute_power").html("体力：" + obj.Datas.SP + " / " + obj.Datas.MaxSP);
        $("#attribute_experience").html("经验：" + obj.Datas.Experience + " / " + obj.Datas.MaxExperience);
        $("#attribute_level").html("等级：" + obj.Datas.Level);
        if(Person.Datas.HP<Person.Datas.MaxHP || Person.Datas.SP<Person.Datas.MaxSP)
        {
            RefreshAttributeTimer=setInterval(LocalRefreshAttribute,1000);ShowAttributeMoreBox();
        }
    }
}
  
function ShowAttributeMoreBox() {
    if(Person.Datas==undefined)
    {alert("数据尚未完全载入，请稍候再试！"); return;}
    if($("#attribute_more_box").val()==undefined)
    {$("body").append("<div id=\"attribute_more_box\"></div>");$("#attribute_more_box").click(ShowAttributeMoreBox);}
    else
    {
        if($("#attribute_more_box").is(":visible")){$("#attribute_more_box").hide();return;}
        else{$("#attribute_more_box").show();}
    }
    SetAttributeMoreBox();
  }
  
  function SetAttributeMoreBox() {
    var height=40;
    if(Person.Datas.HP<Person.Datas.MaxHP) height+=20;
    if(Person.Datas.SP<Person.Datas.MaxSP) height+=20;
    $("#attribute_more_box").height(height);
    $("#attribute_more_box").css("left",$("#attribute_more").position().left-160);
    $("#attribute_more_box").css("top",$("#attribute_more").position().top+25);
    var text="攻击力：" + Person.Datas.AP + "<br>防御力：" + Person.Datas.DP;    
    if(Person.Datas.HP<Person.Datas.MaxHP) text+="<br>" + Person.Datas.RenewHPDateTime + "秒后恢复1点生命！";
    if(Person.Datas.SP<Person.Datas.MaxSP) text+="<br>" + Person.Datas.RenewSPDateTime + "秒后恢复1点体力！";
    $("#attribute_more_box").html(text);
  }


 function StartService() {
    Dialog(300,100,"系统提示","Loading...",false);
    $.post("response.aspx?action=1","",WebResponse,"json");
 }

 function Mission(missionID){
     Dialog(300,100,"系统提示","Loading...",false);
     $.post("response.aspx?action=3","MissionID=" + missionID,WebResponse,"json");
 }

 function FactionJoin(factionID){
     Dialog(300,100,"系统提示","Loading...",false);
     $.post("response.aspx?action=50","FactionID=" + factionID,WebResponse,"json");
 }

function FactionAllowJoin(flag,userID,userName) {
    if(!confirm("确定要" + (flag?"接受":"拒绝") + "<" + userName + ">的加入？")) return;
     Dialog(300,100,"系统提示","Loading...",false);
     $.post("response.aspx?action=55","UserID=" + userID + "&Flag=" + (flag?1:0) ,WebResponse,"json");
}

 function FactionCM(type,name,salary) {
    if(type==undefined){type=0;name="";salary=100;}
    Dialog(300,120,"帮派信息","帮派名称：<input name=\"msg_faction_name\" id=\"msg_faction_name\" type=\"text\" value=\"" + name + "\" /><br />帮派薪水：<input name=\"msg_faction_salary\" id=\"msg_faction_salary\" type=\"text\" value=\"" + salary + "\" /><br /><a href=\"#\" onclick=\"FactionCM2(" + type + ")\" class=\"red\">" + (type==0?"创建":"修改") + "</a>",true);
 }
 function FactionCM2(type) {
    var factionName=$("#msg_faction_name").val();
    var factionSalary=parseInt($("#msg_faction_salary").val(),10);
    if(factionName==""){alert("请输入帮派名称！");return;}
    if(isNaN(factionSalary)|| factionSalary<0){alert("无效的薪水！请输入一个大于0的整数！");return;}
    Dialog(300,100,"系统提示","Loading...",false);
    $.post("response.aspx?action=52","Type=" + type + "&FactionName=" + factionName + "&FactionSalary=" + factionSalary,WebResponse,"json");
 }

 function FactionDissolve() {
     Dialog(300,100,"系统提示","Loading...",false);
     $.post("response.aspx?action=54","",WebResponse,"json");
 }

 function FactionFire(userID,userName) {
     if(!confirm("确定要开除<" + userName + ">吗？")) return;
     Dialog(300,100,"系统提示","Loading...",false);
     $.post("response.aspx?action=53","UserID=" + userID,WebResponse,"json");
 }

 function FactionQuit() {
     Dialog(300,100,"系统提示","Loading...",false);
     $.post("response.aspx?action=51","",WebResponse,"json");
 }
 

function Bank(type) {
    var value=parseInt($("#bank_input_" + type).val(),10);
    if(isNaN(value)){alert("无效的数字，请输入一个整数！");return;}
    if(value<bank_low_money){alert("最低限额为" + bank_low_money + "!");return;};
    Dialog(300,100,"系统提示","Loading...",false);
    $.post("response.aspx?action=5","Type=" + type + "&Value=" +value,WebResponse,"json");
}


function Police() {
    var name=$("#police_name").val();
    if(name=="") {alert("请输入被举报者的用户名！");return;}
    var type=$("#police_type").val();
    Dialog(300,100,"系统提示","Loading...",false);
    $.post("response.aspx?action=8","Type=" + type + "&Name=" + name,WebResponse,"json");
}


function Hospital() {
    Dialog(300,100,"系统提示","Loading...",false);
    $.post("response.aspx?action=6","",WebResponse,"json");
}


function Fight(userID) {
    Dialog(300,100,"系统提示","Loading...",false);
    $.post("response.aspx?action=7","UserID=" + userID ,WebResponse,"json");
}


function Reward(userName) {
    Dialog(300,120,"悬赏令","悬赏人头：<input name=\"reward_add_name\" id=\"reward_add_name\" type=\"text\" value=\"" + userName + "\" /><br />悬赏金额：<input name=\"reward_add_money\" id=\"reward_add_money\" type=\"text\" /><br /><a href=\"#\" onclick=\"RewardAdd2()\" class=\"red\">发布悬赏</a>",true);
}
function RewardAdd() {
    Dialog(300,120,"悬赏令","悬赏人头：<input name=\"reward_add_name\" id=\"reward_add_name\" type=\"text\" /><br />悬赏金额：<input name=\"reward_add_money\" id=\"reward_add_money\" type=\"text\" /><br /><a href=\"#\" onclick=\"RewardAdd2()\" class=\"red\">发布悬赏</a>",true);
}
function RewardAdd2() {
    var money=parseInt($("#reward_add_money").val(),10);
    if(isNaN(money)){alert("请输入有效的金额！");return;}
    var name=$("#reward_add_name").val();
    if(name==""){alert("请输入被悬赏者的用户名！");return;}
    Dialog(300,100,"系统提示","Loading...",false);
    $.post("response.aspx?action=60","TargetName=" + name + "&Money=" + money,WebResponse,"json");
}

function RewardFight(rewardID) {
    Dialog(300,100,"系统提示","Loading...",false);
    $.post("response.aspx?action=61","RewardID=" + rewardID ,WebResponse,"json");
}


function Search() {
    var name=$("#fight_name").val();
    if(name==""){alert("请输入用户名！");return;}
    Dialog(300,100,"系统提示","Loading...",false);
    $.post("response.aspx?action=80","UserName=" + name ,WebResponse,"json");
}

function God(type,count) {
    if(count<=0) return;
    Dialog(300,100,"系统提示","Loading...",false);
    $.post("response.aspx?action=90","Type=" + type + "&Count=" + count ,WebResponse,"json");
}
 
function WebResponse(obj) {
 	//if(obj.Status!=1){alert(obj.Error);$("#Dialog").dialog("close");return;}
 	if(obj.Status!=1)
 	{
 	    switch(obj.Action)
 	    {
            default:
 	            Dialog(300,100,"发生错误",obj.Error,true);
 	            break;
        }
        return;
 	}
 	
 	switch(obj.Action)
 	{
 	    case 1:
 	    case 6:
 	    case 51:
 	    case 52:
 	    case 53:
 	    case 54:
 	    case 55:
 	    case 60:
 	    case 90:
 	        window.location.reload();
 	        break;
        case 5:
            Dialog(300,100,"银行","交易成功！",true);
            $("#attribute_money").html("金钱：" + obj.Money);
            $("#bank_count>span").html(obj.BankMoney);
 	        break;
        case 3:
            Dialog(300,100,"任务完成",obj.Tip,true);
            RefreshAttribute();
 	        break;
        case 7:
            Dialog(300,100,"火拼",obj.Tip,true);
            RefreshAttribute();
 	        break;
        case 8:
            Dialog(300,100,"报案",obj.Tip,true);
 	        break;
        case 50:
            Dialog(300,100,"系统提示","已经成功提交申请！等待帮派老大批准！",true);
            break;
        case 61:
            Dialog(300,100,"悬赏令",obj.Tip,true);
            RefreshAttribute();
            break;
        case 80:
            window.location.href="user.aspx?uid=" + obj.UserID;
            break;
 	}       
 }
 
 function Dialog(width,height,title,body,bCloseButton) {
    if($("#Dialog").val()==undefined)
    {
        $("body").append("<div id=\"Dialog\" title=\"\"></div>")
         $("#Dialog").dialog({closeOnEscape:false,modal:true,draggable:false,resizable:false,autoOpen: false});
    }
    if(bCloseButton)
        $("a.ui-dialog-titlebar-close").show();
    else
    {
        //$("a.ui-dialog-titlebar-close").unbind("click");
        $("a.ui-dialog-titlebar-close").hide();
        // $(".ui-dialog-titlebar").hide();
    }    
    $("#Dialog").html(body);
    $("#Dialog").dialog("open");    
    $('#Dialog').dialog("option", "title", title).dialog("option", "height", height).dialog("option", "width", width);
 }
 
 function PersonClass() {
 }
 
var Person=new PersonClass();