var pet={
id:0,
name:"",
exp:0,
happy:0,
inuse:0,
lv:0,
ahp:0,
amp:0,
atk:0,
matk:0,
dex:0,
def:0,
ahpadd:0,
ampadd:0,
atkadd:0,
matkadd:0,
dexadd:0,
defadd:0,
skills:"",
pic:"",
status:"",
query:function()
{
var queryid = arguments.length === 0 ? pet.id : arguments[0]; 
jQuery.getJSON("pet.asp?act=query&id="+queryid,function(json)
{
if(json.length==0)
{
pet.cls();//location.reload();
return;
}
json=json[0];
pet.id=json.id;
pet.name=json.name;
pet.exp=json.exp;
pet.happy=json.happy;
pet.inuse=json.inuse;
pet.lv=json.lv;
pet.ahp=json.ahp;
pet.amp=json.amp;
pet.atk=json.atk;
pet.matk=json.matk;
pet.dex=json.dex;
pet.def=json.def;
pet.ahpadd=json.ahpadd;
pet.ampadd=json.ampadd;
pet.matkadd=json.matkadd;
pet.atkadd=json.atkadd;
pet.defadd=json.defadd;
pet.dexadd=json.dexadd;
pet.skills=json.skills;
pet.status=json.status;
pet.pic=json.pic;
pet.init();
}
);
},
feed:function(str)
{
if(pet.id==0){return;}
jQuery.get("pet.asp?act=feed&id="+pet.id+"&food="+escape(str),function(str){
var msg="";
switch(parseInt(str)) {
case -1:
    msg="该宠物不需要喂养!";
break;
case -2:
    msg="您没有改饲料!";
break;
default: 
msg="喂养成功,增加经验"+str+"点!";
}
parent.AddSysMsg('系统', msg);
pet.query();
});
},
change:function()
{
if(pet.id==0){return;}
jQuery.get("pet.asp?act=change&id=" + pet.id, function(str){
//parent.mapifr
pet.query(pet.id);
});
},
rename:function()
{
if(pet.id==0){return;}
prompt("改名:",function(){
jQuery("#checkdiv").hide();
jQuery.get("pet.asp?act=rename&id="+pet.id+"&name="+escape(jQuery("#text0").val()),function(str){pet.query(pet.id);pet.getlist();});
},function(){jQuery("#checkdiv").hide();return false;});
},
drop:function()
{
if(pet.id==0){return;}
confirm("你真的要丢弃"+pet.name+"么?",function()
{
jQuery("#checkdiv").hide();
jQuery.get("pet.asp?act=drop&id=" + pet.id, function(str)
{
if (str == "1") 
{
parent.AddSysMsg('系统', '遣散成功.');
pet.query(0);
pet.getlist();
}
});
},function(){jQuery("#checkdiv").hide();return false;});

},
init:function()
{
jQuery("#petdiv").html("<img src='."+imgServer+"pet/"+pet.pic+"-1-1.gif' width='48' height='48' />");
jQuery("span").each(
function()
{
if (!isNaN(pet[this.id])) 
{
jQuery(this).html(parseInt(pet[this.id]));
}else{
jQuery(this).html(pet[this.id]);
}

);
jQuery("#inuse").html(pet.inuse=="0"?"出战":"闲置");

},
cls:function()
{
pet.id=0;
jQuery("#petdiv").html("");
jQuery("span").html("");

},
feedshow:function()
{
jQuery("#checkstr").html("低级<input value='低级' type='radio' />&nbsp;中级<input value='中级' type='radio'/>&nbsp;高级<input value='高级' type='radio'/>&nbsp;顶级<input value='顶级' type='radio'/>");
jQuery("#checkstr input").click(function(){ jQuery(this).attr("checked",true).siblings().attr("checked",false);});
$("ckyes").onclick=function(){jQuery("#checkdiv").hide();pet.feed(jQuery("#checkstr input[checked]").val());};
$("ckno").onclick=function(){jQuery("#checkdiv").hide();};
jQuery("#checkdiv").show();
},
getlist:function()
{
jQuery.getJSON("pet.asp?act=getlist",
function(json)
{
if(json.length==0)
{
jQuery("#petlist").html("你没有宠物.");
return;
}
var petlist=new arrstr();
for(var i=0;i<json.length;i++)
{
petlist.add("<img src='."+imgServer+"pet/"+json[i].pic+"-1-1.gif' id='"+json[i].id+"' alt='"+json[i].petname+"' />");
}
jQuery("#petlist").html(petlist.toString());
jQuery("#petlist img").click(function(){
pet.query(this.id);
});
}
);
}
};
function arrstr(){ 
    this._strings = new Array(); 
}
  
arrstr.prototype.add = function(str){ 
    this._strings.push(str); 
};
arrstr.prototype.toString = function(){ 
    var str = arguments.length === 0 ? '' : arguments[0]; 
    return this._strings.join(str); 
};
window.confirm=function(str,ctrue,cfalse){
jQuery("#checkstr").html(str);
$("ckyes").onclick=ctrue;
$("ckno").onclick=cfalse;
jQuery("#checkdiv").show();
};window.prompt=function(str,ctrue,cfalse)
{
jQuery("#checkstr").html(str+"<input id='text0' value='' type='text'/>");
$("ckyes").onclick=ctrue;
$("ckno").onclick=cfalse;
jQuery("#checkdiv").show();
};
pet.query();
pet.getlist();
jQuery("#drop").click(function(){pet.drop();});
jQuery("#inuse").click(function(){pet.change();});
jQuery("#rename").click(function(){pet.rename();});
jQuery("#feed").click(function(){pet.feedshow();});