var dtdom=new ActiveXObject("Microsoft.XMLDOM")
var gwdom=new ActiveXObject("Microsoft.XMLDOM")
var jqdom=new ActiveXObject("Microsoft.XMLDOM")
var wpdom=new ActiveXObject("Microsoft.XMLDOM")
dtdom.async=false;
gwdom.async=false;gwdom.load("server/gw.xml");
jqdom.async=false;jqdom.load("server/jq.xml");
wpdom.async=false;wpdom.load("server/wp.xml");
var xmlerr=",加载错误"
if(gwdom.parseError.errorCode!=0)alert("异常:怪物GW-XML"+xmlerr)
if(jqdom.parseError.errorCode!=0)alert("异常:剧情JQ-XML"+xmlerr)
if(wpdom.parseError.errorCode!=0)alert("异常:物品WP-XML"+xmlerr)

var xml={
getwp:function(ndname,wpname){  //获取物品与装备属性
var obj=ndname=="zb"?zbdom.getElementsByTagName(ndname):wpdom.getElementsByTagName(ndname);
for(var i=0;i<obj.length;i++){
if(obj[i].getAttribute("n")!=wpname.replace(/\$\$\$\d+/g,""))continue;
return obj[i]
}
}
}


var round=Math.round,random=Math.random
var bodywid=document.body.offsetWidth,bodyhei=document.body.offsetHeight
var fmwid=fm1.offsetWidth,fmhei=fm1.offsetHeight,fmleft=fm1.offsetLeft,fmtop=fm1.offsetTop
var evx=bodywid/2,evy=bodyhei/2,xdtwid=xdt.offsetWidth,xdthei=xdt.offsetHeight
var selobj=null,selckin=false;  //选定的对象
var fmmovetime=null,ckin=false,ckx=0,cky=0;  //地图卷动
var inx=0,iny=0  //可视地图部分的left/top坐标
var dtwenjian="",dtname="未知",guishu="",yinyue="",bgimg="img/background/xin1.jpg",jinfei=false  //存储地图tagname/name
var gws="",gwmax=0,kuang="",migong=false
var huichengx="",huichengy="",hjy=""

var mousesuoding=false  //某些特殊地方用于判断鼠标是否锁定
var untext=new Array(200),unyoubiao=0  //存储撤销内容
var sd_mark=false  //弹出一些设置类界面时,锁定地图禁止事件
var wujianlen=0  //记录地图中的总物件个数

function window.onbeforeunload(){return "确认要退出编辑器?请注意保存!";}  //内存释放
function window.onunload(){dtdom=null;gwdom=null;jqdom=null;wpdom=null;fsoobj=null;fsoxml=null;}
function window.onresize(){bodywid=document.body.offsetWidth,bodyhei=document.body.offsetHeight}
function closemenu(){tab1.style.display='none';tab2.style.display='none';}
function getcolor(){
var colors=Math.floor(random()*16777215).toString(16)
while(colors.length<6)colors=Math.floor(random()*16777215).toString(16)
return colors
}
function window.onload(){
xdtkuang.style.width=bodywid/(fmwid/xdtwid);xdtkuang.style.height=bodyhei/(fmhei/xdthei)
fmmovetime=setInterval(function(){  //负责更新坐标消息,与地图卷动控制
if(ckin==true){
if(evx>ckx&&fmleft>bodywid-fmwid)fm1.style.left=fmleft-=Math.ceil((evx-ckx)/10)
if(evx<ckx&&fmleft<0)fm1.style.left=fmleft+=Math.ceil((ckx-evx)/10)
if(evy>cky&&fmtop>-(fmhei-bodyhei))fm1.style.top=fmtop-=Math.ceil((evy-cky)/10)
if(evy<cky&&fmtop<20)fm1.style.top=fmtop+=Math.ceil((cky-evy)/10)
}else if(selckin==true){
if(evx>bodywid-40&&fmleft>bodywid-fmwid)fm1.style.left=fmleft-=20
if(evx<40&&fmleft<0)fm1.style.left=fmleft+=20
if(evy>bodyhei-40&&fmtop>-(fmhei-bodyhei))fm1.style.top=fmtop-=20
if(evy<40&&fmtop<0)fm1.style.top=fmtop+=20
}
xdtkuang.style.left=-(fmleft/(fmwid/xdtwid));xdtkuang.style.top=-(fmtop/(fmhei/xdthei))
inx=fmleft;iny=fmtop;
shuzhix.innerText=round((-inx+evx)/10)+"m";shuzhiy.innerText=round((-iny+evy)/10)+"m";
},40)
setInterval("sm3.hjy()",1000)
sm3.loadxdt();
}
function fm1.oncontextmenu(){
if(sd_mark)return false;ckin=false;fm1.releaseCapture();
tab1.style.left=evx;tab1.style.top=evy;var obj=event.srcElement
if(obj.id=="fm1"){addx=event.offsetX;addy=event.offsetY;selobj=null;xdk.style.display="none"}else{  //定位插入坐标
addx=event.offsetX+obj.offsetLeft;addy=event.offsetY+obj.offsetTop;
if(obj.id=="me")return false;
selobj=obj
xdk.style.left=obj.offsetLeft+inx;xdk.style.top=obj.offsetTop+iny
xdk.style.width=obj.offsetWidth;xdk.style.height=obj.offsetHeight;xdk.style.display="";}
for(var i=2;i<9;i++){
if(obj.id=="fmkuang"||obj.id=="me"){td1[i].disabled=true;continue;}
td1[i].disabled=selobj==null?true:false;
}
td1[9].innerHTML="撤销 - (闪存="+unyoubiao+") <font color='gray'>Ctrl+Z</font>"
td1[7].disabled=copyobj==""?true:false;if(obj.id=="fmkuang")td1[7].disabled=false
tab2.style.display="none";tab1.style.display="";
var dayux=bodywid-parseInt(tab1.offsetWidth)-parseInt(tab1.offsetWidth)
if(evx>dayux)tab1.style.left=dayux
var dayuy=bodyhei-parseInt(tab1.offsetHeight)
if(evy>dayuy)tab1.style.top=dayuy
return false
}
function fm1.onmousedown(){
if(sd_mark)return false;if(event.button==2)return;var obj=event.srcElement;ckx=evx;cky=evy;
if(obj.id=="me")return sm3.medingwei()
if(obj.id=="fm1"||obj.id=="aqq"||obj.id=="shui"){ckin=true;selobj=null;xdk.style.display="none";return fm1.setCapture();}
selckin=true;xdk.style.left=obj.offsetLeft+inx;xdk.style.top=obj.offsetTop+iny
xdk.style.width=obj.offsetWidth;xdk.style.height=obj.offsetHeight;xdk.style.display="";
obj.oldx=event.offsetX;obj.oldy=event.offsetY;
obj.sizex=obj.offsetLeft;obj.sizey=obj.offsetTop;
selobj=obj;fm1.setCapture();mousesuoding=true
}
function fm1.onmouseup(){
if(chajiantab.style.display!="none")chajiantab.style.display="none";if(yunxingtab.style.display!="none")yunxingtab.style.display="none";
if(sd_mark)return false;if(selobj!=null){if(mousesuoding==true)bianji.saveundo();
selobj.releaseCapture();xdk.style.left=selobj.offsetLeft+inx;xdk.style.top=selobj.offsetTop+iny;
if(selobj.tagName=="DIV"){xdk.style.display="none";};
}
ckin=false;selckin=false;fm1.releaseCapture();if(event.button!=1)return false;
closemenu();mousesuoding=false
}
function document.onmousemove(){
if(sd_mark)return;
evx=event.x;evy=event.y;if(mousesuoding!=true)return;
if(event.ctrlKey==true){var tempx=evx+-fmleft,tempy=evy+-fmtop;
if(tempx>selobj.sizex){selobj.style.width=tempx-selobj.sizex}else{selobj.style.left=tempx;selobj.style.width=selobj.sizex-tempx}
if(tempy>selobj.sizey){selobj.style.height=tempy-selobj.sizey}else{selobj.style.top=tempy;selobj.style.height=selobj.sizey-tempy}
xdk.style.left=selobj.offsetLeft+inx;xdk.style.top=selobj.offsetTop+iny;
xdk.style.width=selobj.offsetWidth;xdk.style.height=selobj.offsetHeight
}
else{
try{selobj.style.left=-fmleft+evx-selobj.oldx;selobj.style.top=-fmtop+evy-selobj.oldy
}catch(e){return "异常捷径"}
}}
function fm1.onmousewheel(){
if(sd_mark)return false;
var obj=event.srcElement;mst=35;closemenu();
if(obj.id=="me")return false;
if(event.wheelDelta>0){
if(obj!=selobj||obj.id=="fm1"||obj.id=="aqq"||obj.id=="shui"){selobj=null;xdk.style.display='none';
if(fmtop<0)fm1.style.top=fmtop+=mst;}
else{obj.style.width=obj.offsetWidth+Math.ceil(obj.offsetWidth/mst)
obj.style.height=obj.offsetHeight+Math.ceil(obj.offsetHeight/mst)}
}else{
if(obj!=selobj||obj.id=="fm1"||obj.id=="aqq"||obj.id=="shui"){selobj=null;xdk.style.display='none';
if(fmtop>-(fmhei-bodyhei))fm1.style.top=fmtop-=mst;}
else{if(obj.offsetWidth>mst)obj.style.width=obj.offsetWidth-Math.ceil(obj.offsetWidth/mst)
if(obj.offsetHeight>mst)obj.style.height=obj.offsetHeight-Math.ceil(obj.offsetHeight/mst)}
}
if(selobj!=null){xdk.style.width=obj.offsetWidth;xdk.style.height=obj.offsetHeight}
if(event.ctrlKey==true)return false}
function showtmk(neirong1,tit1,color1,color2){  //透明框控制
if(event.button!=0)return tmk1.style.visibility="hidden"
tmk1.style.left=event.x+10;tmk1.style.top=event.y+10;
if(tmk1.offsetLeft+tmk1.offsetWidth>bodywid)tmk1.style.left=event.x-tmk1.offsetWidth-10
if(tmk1.offsetTop+tmk1.offsetHeight>bodyhei)tmk1.style.top=bodyhei-tmk1.offsetHeight
if(neirong1.indexOf("guaiwu or boos%#&@!-$^=")==0){  //怪物
var xmlgw=xml.getgw(neirong1.split("=")[1]),evobj=event.srcElement
var col1="white",col2="white";
var booshelp="",tempboos=xmlgw.getAttribute("boos")
if(tempboos!=null){
booshelp="<br>&nbsp;<br>Boos头目 "+evobj.sboos
if(tempboos=="神魔级"){col1="orange";col2="#ED6A44"}
if(tempboos=="传说级"){col1="lime";col2="#20D714"}
if(tempboos=="帝王级"){col1="yellow";col2="#D2D619"}
if(tempboos=="经典级"){col1="#FD7AF2";col2="#E8CFF5"}
}
var tempjn=xmlgw.getAttribute("jn"),tempwp=xmlgw.getAttribute("wp")
if(tempjn==null)tempjn="无";else {tempjn=tempjn.split(",").length+"种"}
if(tempwp==null)tempwp="无";else {tempwp=tempwp.split(",").length+"种"}
var zttext="<br>&nbsp;";if(evobj.szt==null)zttext+="<br>状态 无";else{
for(var i=0;i<evobj.szt.length;i++){
if(evobj.szt[i]==null)continue;var temparr=evobj.szt[i].split("=")
zttext+="<br>状态 "+temparr[0]+"(威力="+temparr[1]+",持续="+temparr[2]+"回合)"
}
if(zttext=="<br>&nbsp;")zttext="<br>&nbsp;<br>状态 无"
}
tmk1.innerHTML="<font style='color:"+col1+";font-weight:bold;'>"+xmlgw.tagName+"</font><br>&nbsp;<br><font style='color:"+col2+";'>级别 "+evobj.slv+"<br>生命 "+evobj.shp1+"/"+evobj.shp2+"<br>魔法 "+evobj.smp1+"/"+evobj.smp2+"<br>速度 "+evobj.ssu+"<br>经验 "+evobj.sexp+"<br>价值 "+evobj.scold+"<br>&nbsp;<br>魔法技能 "+tempjn+"<br>掉落物品 "+tempwp+booshelp+zttext+"</font>"
}else{  //其他常规数据
if(tit1==null){tmk1.innerHTML=neirong1;}else{
var color11=color1==null?"white":color1;
var color22=color2==null?"white":color2;
tmk1.innerHTML="<font style='color:"+color11+";font-weight:bold;'>"+tit1+"</font><br>&nbsp;<br><font style='color:"+color22+";'>"+neirong1+"</font>"
}}
if(tmk1.style.visibility!="hidden")return;tmk1.style.visibility="visible";
event.srcElement.onmouseout=function(){tmk1.style.visibility='hidden'}
}