var XH_Num="";
var Old_FX=0;
function Order(compare) {
    if(!compare)compare=new Function("a","b","return a-b");
    Array.prototype.sort.call(this,compare);

    //this.pop=undefined;
    //this.shift=undefined;
    //this.join=undefined;
    this.unshift=this.insert;
    this.push=this.insert;

    this.concat=function()
    {
        var r=this;       
        for(var j=0;j<arguments.length;j++)
        {
            var a=arguments[j];
            if(a.length)
            {
                for(var i=0;i<a.length;i++)
                {
                    r.insert(a[i]);
                }
            }
            else r.insert(a);
        }
        return r;
    };    
    this.reverse=function()
    {
        var r=Array.prototype.reverse.call(this);
        var ccompare=compare;
        var rcompare=function(a,b)
        {
            return -ccompare(a,b);
        }
        compare=rcompare;
        return this;
    };
    this.sort=function(sortfunction)
    {
        if(arguments.length==0)sortfunction=compare;
        Array.prototype.sort.call(this,sortfunction)
        compare=sortfunction;
    };
    this.slice=function(start,end){
        var r=Array.prototype.slice.call(this,start,end);
        OrderList.call(r);
        return r;
    };

    this.splice=function(start,deleteCount){
        var r=Array.prototype.splice.call(this,start,deleteCount);
        OrderList.call(r,compare);
        if(arguments.length>2)
        {
            for(var i=2;i<arguments.length;i++)
            {
                this.insert(arguments[i]);
            }
        }
        return r;
    };

    function search(start,end,newNode)
    {        
        if(end==start)
        {
            return end;            
        }
        else
        {
            var m=Math.floor((start+end)/2);
            if(compare(this[m],newNode)==0)return m;
            if(compare(this[m],newNode)<0)return search.call(this,m+1,end,newNode);
            if(compare(this[m],newNode)>0)return search.call(this,start,m,newNode);
        }
    };

    this.insert=function(newNode)
    {
        return Array.prototype.splice.call(this,search.call(this,0,this.length,newNode),0,newNode);
    };
    this.contains=function(newNode)
    {
        if(this[search.call(this,0,this.length,newNode)]==newNode)return true;
        else return false;
    }
}
function OrderArray(compare)
{
    var a=new Array();
    Order.call(a,compare);
    return a;
}
function pathSearch()
{
    this.map;
    var map;
    this.width;
    var me=this;
    this.pos=function (pos)
    {
        if(pos.length)
            return pos[1]*this.width+pos[0];
        else
            return [Math.floor(pos/this.width),pos%this.width+1];
    }
    var defaultFlag=['*','S','E',' '];
    this.loadStrMap=function(strmap,flag)
    {
        flag=flag||defaultFlag;
        this.map=new Array();
        map=this.map;
        this.width=strmap[0].length+2;
        for(var i=0;i<this.width;i++)this.map.push("*");
        
        for(var y=0;y<strmap.length;y++)
        {
            this.map.push("*");     
            for(var x=0;x<this.width;x++)
            {
           
                switch(strmap[y].charAt(x))
                {
                case flag[0]:
                    this.map.push("*");
                    break;
                case flag[1]:
                    this.start=[x+1,y+1];
                    this.map.push(" ");
                    break;
                case flag[2]:
                    this.end=[x+1,y+1];
                    this.map.push(" ");
                    break;
                case flag[3]:
                    this.map.push(" ");
                    break;
                }
            }
            this.map.push("*");

        }
        for(var i=0;i<this.width;i++)this.map.push("*");
    }
    this.loadArrMap=function(strmap,flag)
    {
        flag=flag||[1,2,3,0];
        this.map=new Array();
        map=this.map;
        this.width=strmap[0].length+2;
        for(var i=0;i<this.width;i++)this.map.push("*");
        
        for(var y=0;y<strmap.length;y++)
        {
            this.map.push("*");     
            for(var x=0;x<this.width;x++)
            {
           
                switch(strmap[y][x])
                {
                case flag[0]:
                    this.map.push("*");
                    break;
                case flag[1]:
                    this.start=[x+1,y+1];
                    this.map.push(" ");
                    break;
                case flag[2]:
                    this.end=[x+1,y+1];
                    this.map.push(" ");
                    break;
                case flag[3]:
                    this.map.push(" ");
                    break;
                }
            }
            this.map.push("*");

        }
        for(var i=0;i<this.width;i++)this.map.push("*");

    }

    this.getPath=function(){
        var r=new Array();
        r.push(this.pos(this.end));
        
        if(map[r[0]]==" "||this.map[r[0]]=="*")return null;
        
        while(r[r.length-1]!=-1)
        {
            //document.write(r[r.length-1]+" ");
            //alert(this.map[54]);
            r.push(this.map[r[r.length-1]]);
        }
        r.pop();
      
        r.tran=function(){
            for(var i=0;i<this.length;i++)
            {
                r[i]=me.pos(r[i]);
            }
            return this;
        }
        
        return r.reverse();        
    }

    this.bfs=function(){

        var queue=new Array();
        queue.push(this.pos(this.start));
        this.map[queue[0]]=-1;
        queue.insert=function(pos,curr)
        {

            if(map[pos]==" ")
            {
                map[pos]=curr;
                this.push(pos);
            }
        }
        while(queue.length)
        {            
            var curr=queue.shift();
            //document.write(curr+"\n");
            queue.insert(curr+1,curr);
            queue.insert(curr-1,curr);
            queue.insert(curr+this.width,curr);
            queue.insert(curr-this.width,curr);
            if(this.map[curr+1]!="*"||this.map[curr+this.width]!="*")
                queue.insert(curr+this.width+1,curr);
            if(this.map[curr+1]!="*"||this.map[curr-this.width]!="*")
                queue.insert(curr-this.width+1,curr);
            if(this.map[curr-1]!="*"||this.map[curr+this.width]!="*")
                queue.insert(curr+this.width-1,curr);
            if(this.map[curr-1]!="*"||this.map[curr-this.width]!="*")
                queue.insert(curr-this.width-1,curr);

        }

    }

        this.astar=function(){
        var end=this.end;
        var G=new Array(map.length);
        function distance(pos)
        {
            var base=G[pos];
            pos=me.pos(pos);
            var a=Math.abs(pos[0]-end[1]);
            var b=Math.abs(pos[1]-end[0]-1);
            return base+10*(a+b)-6*((a>b)?a:b);
        }
        
        var queue=new OrderArray(
            function(a,b){            
                return distance(a)-distance(b);
            }
        );
        queue.insert(me.pos(this.start));
        this.map[queue[0]]=-1;
        for(var i=0;i<G.length;i++)G[i]=-1;
        G[queue[0]]=0;
        var width=this.width;
        queue.insert2=function(pos,curr,f)
        {
            var g=G[curr]+10;
            if(f)g+=4;
            if(map[pos]==" "||G[pos]>g)
            {
                map[pos]=curr;
                G[pos]=g;
                this.insert(pos);
            }
        }

        while(queue.length)
        {
            var curr=queue.shift();
            queue.insert2(curr+1,curr);
            queue.insert2(curr-1,curr);
            queue.insert2(curr+this.width,curr);
            queue.insert2(curr-this.width,curr);
            if(this.map[curr+1]!="*"||this.map[curr+this.width]!="*")
                queue.insert2(curr+this.width+1,curr,1);
            if(this.map[curr+1]!="*"||this.map[curr-this.width]!="*")
                queue.insert2(curr-this.width+1,curr,1);
            if(this.map[curr-1]!="*"||this.map[curr+this.width]!="*")
                queue.insert2(curr+this.width-1,curr,1);
            if(this.map[curr-1]!="*"||this.map[curr-this.width]!="*")
                queue.insert2(curr-this.width-1,curr,1);          
            if((typeof map[this.pos(this.end)])== "number")break;
        }
    }

    
    var viewer=[];

    this.show=function(){
        var size=10;
        var x=0,y=0;
        var create=false;
        if(!viewer.length)create=true;
        for(var i=0;i<this.map.length;i++)
        {
            if(!(i%this.width)){y++;x=0;}
            x++;
            if(create)viewer.push(document.createElement("img"));
            viewer[viewer.length-1].style.position="absolute";
            viewer[viewer.length-1].style.top=y*size+"px";
            viewer[viewer.length-1].style.left=x*size+"px";
            if(this.map[i]=="*")viewer[viewer.length-1].style.background="red";
            else viewer[viewer.length-1].style.background="blue";
            viewer[viewer.length-1].style.width=size;
            viewer[viewer.length-1].style.geight=size;
            document.body.appendChild(viewer[viewer.length-1]);
        }
        viewer[this.pos(this.start)].style.background="green";
        viewer[this.pos(this.end)].style.background="black";
        //document.writeln(this.pos([8,7]));
    }
    this.showPath=function(path)
    {
        if(viewer.length)
        {
            for(var i=0;i<path.length;i++)
            {
                viewer[path[i]].style.background="yellow";
            }
        }    
    }
}
var map=""
var mainImg = new Image();
mainImg.src = "1.png";
function MapHtml() {
var Html="";
for(var i=0;i<map.length;i++) {
	for(var j=0;j<map[0].length;j++) {
	Html=Html+"<div class='" + map[i][j] + "'></div>";
	}
}
$("XH_Map").innerHTML=Html;
}
function movingMan(p){
this.oldPoint = p;
this.nowPoint = p;
this.container = $("XH_Player");
this.direction = 0;  // 0--下; 1--左; 2--右; 3--上; 4--左下; 5--右下; 6--左上; 7--右上
this.order = 0;      //行走顺序
this.count = 0;      //行走延时切换图片
this.endPoint;
this.timer;
srcObj = this;
var onfinish=function(){};
this.shiftImage = function(){
XH_Left=$("XH_Player").offsetLeft;
XH_Top=$("XH_Player").offsetTop;
XH_FX=this.direction;
if (Old_FX!==XH_FX) {
	if (XH_WayList=="") {
	XH_WayList=XH_Left+","+XH_Top
	}else{
	XH_WayList=XH_WayList+"_"+XH_Left+","+XH_Top
	}
	Old_FX=XH_FX;
}
if(arguments[0] == true) this.order = 0;
this.container.scrollTop = 131 * this.direction+1;
this.container.scrollLeft = 58 * this.order;
this.order++;
if(this.order == 7) this.order = 1;
	}

this.move = function(){
if(Math.abs(srcObj.nowPoint.x - srcObj.endPoint.x) <=1 && Math.abs(srcObj.nowPoint.y-srcObj.endPoint.y)<=1){
window.clearInterval(srcObj.timer);
onfinish();
//srcObj.shiftImage(true);
}else if(Math.abs(srcObj.nowPoint.x-srcObj.endPoint.x)<=1){
if(srcObj.nowPoint.y < srcObj.endPoint.y) srcObj.direction = 0;
else srcObj.direction = 3;
}else if(Math.abs(srcObj.nowPoint.y-srcObj.endPoint.y)<=1){
if(srcObj.nowPoint.x < srcObj.endPoint.x) srcObj.direction = 2;
else srcObj.direction = 1;
}else{
if(srcObj.nowPoint.x > srcObj.endPoint.x){
	if(srcObj.nowPoint.y < srcObj.endPoint.y) srcObj.direction = 4;
	else srcObj.direction = 6;
	}else{
	if(srcObj.nowPoint.y < srcObj.endPoint.y) srcObj.direction = 5;
	else srcObj.direction = 7;
	}
}
		
switch (srcObj.direction){
case 0:{srcObj.nowPoint.y=srcObj.nowPoint.y+2;break;}
case 1:{srcObj.nowPoint.x=srcObj.nowPoint.x-2;break;}
case 2:{srcObj.nowPoint.x=srcObj.nowPoint.x+2;break;}
case 3:{srcObj.nowPoint.y=srcObj.nowPoint.y-2;break;}
case 4:{srcObj.nowPoint.x=srcObj.nowPoint.x-2;srcObj.nowPoint.y=srcObj.nowPoint.y+2; break;}
case 5:{srcObj.nowPoint.x=srcObj.nowPoint.x+2;srcObj.nowPoint.y=srcObj.nowPoint.y+2; break;}
case 6:{srcObj.nowPoint.x=srcObj.nowPoint.x-2;srcObj.nowPoint.y=srcObj.nowPoint.y-2; break;}
case 7:{srcObj.nowPoint.x=srcObj.nowPoint.x+2;srcObj.nowPoint.y=srcObj.nowPoint.y-2; break;}
}

srcObj.container.style.zIndex=srcObj.nowPoint.y;
srcObj.container.style.left = srcObj.nowPoint.x-33 + "px";
srcObj.container.style.top = srcObj.nowPoint.y-95 + "px";
$('XH_Myname').style.left= srcObj.nowPoint.x - 41 + "px";
$('XH_Myname').style.top = srcObj.nowPoint.y - 105 + "px";
$(XH_MyTalkId).style.left = srcObj.nowPoint.x-130 + "px";
$(XH_MyTalkId).style.top = srcObj.nowPoint.y-130 + "px";

srcObj.count=srcObj.count+2;
if(srcObj.count % 6 == 0){
srcObj.shiftImage();
srcObj.count = 0;
	}
}

this.Go = function(p,onfinished){
	this.endPoint = p;
	onfinish=onfinished||function(){};
	window.clearInterval(this.timer);
	this.timer = window.setInterval(srcObj.move, 10);
	}
this.shiftImage(true);
}

function Point(){
this.x = arguments[0]?arguments[0]:0;
this.y = arguments[1]?arguments[1]:0;
}

function startMove(e){
if ((event.srcElement.className != "0")&&(event.srcElement.className != "click")&&(event.srcElement.className != "newtalk")&&(event.srcElement.className != "MyTalk")) {return}
var end=[Math.floor((event.x-33)/30),Math.floor((event.y-33)/30)];
var start=[Math.floor((actor.nowPoint.x-33)/30),Math.floor((actor.nowPoint.y-33)/30)];
var pathfinder=new pathSearch();
//开始移动事件
//alert(start);
$("XH_MoveImg").style.left=event.x-25;
$("XH_MoveImg").style.top=event.y-5;
$("XH_MoveImg").src="images/click.gif";
map[start[1]+1][start[0]+1]=2
map[end[1]+1][end[0]+1]=3
pathfinder.loadArrMap(map);
map[start[1]+1][start[0]+1]=0
map[end[1]+1][end[0]+1]=0
//pathfinder.astar();
pathfinder.bfs();
//pathfinder.show();
//pathfinder.showPath(pathfinder.getPath());
//alert(end);
if(pathfinder.getPath())
stepqueue=pathfinder.getPath().tran();
stepqueue.shift();
//alert(stepqueue.join(" "));	//获得路径
//RecordWay(stepqueue.join(" "));
if(!moving){
	doMove();
	}
}

var stepqueue=[];
var moving=false;
function doMove() {
if(!stepqueue.length){
//停止移动事件
moving=false;
StopTime=window.setTimeout("$('XH_Player').scrollLeft='0px';EventNow()",10);
if (XH_WayList!=="") {XH_WayList=XH_WayList+"_"+XH_Left+","+XH_Top}
return;
}
var step=stepqueue.shift();
XH_Num=step;
PlaySE(3);
//触发移动事件
//alert(step);
//记入坐标
//alert( [(step[1]-3)*30+33+15,(step[0]-2)*30+33+15] )
actor.Go(new Point((step[1]-3)*30+33+15,(step[0]-2)*30+33+15),doMove);
}
//actor.Go(new Point(700, 400));
$("XH_Myname").innerHTML=XH_Name;
//$("XH_Player").innerHTML="<img src='images/blank.gif' style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/player/"+XH_Head+".png', sizingMethod='scale');width:364px;height:990px;\">";
$("XH_Player").innerHTML="<img src='images/player/"+XH_MyHead+".gif' width='406' height='1179'>";
var actor = "";
document.onclick = startMove;
document.oncontextmenu = function(){return false}
document.onselectstart = function(){return false};

var PreSend="";
document.onkeydown = function(){
//alert(window.event.keyCode);
var KeyCodes=window.event.keyCode;
if (KeyCodes==13) {SendMsgNow()}
else if ((PK_Freak!==0)&&(PK_MyKill!==0)&&(KeyCodes==32)) {PK_KillIng()}
else if (KeyCodes==118) {XH_LoadPK();XH_Nokey()}
else if (KeyCodes==119) {XH_LoadPK_Over();XH_Nokey()}
else if (KeyCodes==115) {alert(XH_Num);XH_Nokey()}
else if (KeyCodes==116) {alert(XH_Left+" "+XH_Top);XH_Nokey()}
else if (KeyCodes==117) {alert(actor.direction + " " + actor.order + " / " + LoadMap.src);XH_Nokey()}
else if (KeyCodes==112) {GameWin("msg","系统帮助","移动到场景边自动切换场景;电视机:单击可放大/缩小,右击换台!",350,80,150,200,2);XH_Nokey()}
else if (KeyCodes==114) {$S(200,300,1,4)}
}
function XH_Nokey() {
window.event.keyCode=0;
window.event.returnValue=false
}
//发送信息
function SendMsgNow() {
if ((XH_MsgContent.value!=="")&&(XH_MsgContent.value!==PreSend)&&(XH_MsgContent.value!=="温馨提示：可在此输入信息后再调色")) {
	PlaySE(2);
	GameAjax("ShowMess.asp?action=send&C="+XH_MsgColor.value+"&Msg="+XH_MsgContent.value+"&","XH_MsgList",1);
	PreSend=XH_MsgContent.value;
	}else{
	PlaySE(8)
	}
XH_MsgContent.value="";
}
function killErrors() {return true;}
window.onerror = killErrors;