function CClientDC()
{
	this.m_hDC=0;
	this.left=0;
	this.top=0;
	this.wdith=1;
	this.height=1;
	this.BW=0;
	this.BH=0;
	this.borderwidth=1;
	this.color=0;
	this.SRC="";

	this.GetDC=GetDC;
	this.CreateDC=CreateDC;
};

function GetDC()
{
	return this.m_hDC;
};

function CreateDC(m_ileft,m_itop,m_iwidth,m_iheight)
{
	this.left=m_ileft;
	this.top=m_itop;
	this.wdith=m_iwidth;
	this.height=m_iheight;
	var i=parseInt(Math.random()*10000);
	var j=parseInt(Math.random()*10000);
	var nn=eval("'name'+i*40+j");
	var zz=eval('"MainCDC"+i*40+j');
	var mm=eval("'image'+i*40+j");
	document.body.innerHTML+='<div id=MainCDC style=position:absolute;left:'+this.left+';top:'+this.top+';width:'+this.wdith+';height:'+this.height+';border-width:'+this.borderwidth+';border-style:inset;border-color:this.color></div>';
	this.m_hDC=MainCDC;
};
function CDC()
{
	this.FillRect=FillRect;
	this.BitBlt=BitBlt;
	this.DeleteObject=DeleteObject;
	this.ClearDC=ClearDC;
	this.CreateBitmap=CreateBitmap;
}

CDC.prototype=new CClientDC;

function FillRect(m_dcolor)
{
	this.m_hDC.style.background=m_dcolor;
}

function BitBlt(lsp,m_iIndex,m_jIndex,m_imageIndex)
{
	lsp.wnd.AllIMG[m_iIndex*6+m_jIndex].src=lsp.wnd.imageArray[m_imageIndex].src;
	Show(lsp.wnd.AllDIV[m_iIndex*6+m_jIndex],'visible');
}

function DeleteObject()
{
}

function ClearDC()
{
}

function CreateBitmap(m_ileft,m_itop,m_iwidth,m_iheight)
{
	this.left=m_ileft;
	this.top=m_itop;
	this.wdith=m_iwidth;
	this.height=m_iheight;

	var i=parseInt(Math.random()*10000);
	var j=parseInt(Math.random()*10000);
	var nn=eval("'name'+i*40+j");
	var zz=eval('"SubMainCDC"+i*40+j');
	var mm=eval("'image'+i*40+j");
	document.body.innerHTML+='<div id=SubMainCDC style=position:absolute;left:'+this.left+';top:'+this.top+';width:'+this.wdith+';height:'+this.height+';border-width:'+this.borderwidth+';border-style:inset;border-color:this.color><IMG SRC="" id=mm></div>';
	this.m_hDC=SubMainCDC;
};
var AND_01=new Array(0x8000,0x4000,0x2000,0x1000,
					 0x0800,0x0400,0x0200,0x0100,
					 0x0080,0x0040,0x0020,0x0010,
					 0x0008,0x0004,0x0002,0x0001);
var PI_01=new Array(
					0,0x6006,0x700e,0x781e,0x6c36,0x6666,0x63c6,0x6186,0x6006,0x6006,0x6006,0x0,					
					0xffff,0x8e57,0x8a54,0x8a54,0x8a54,0x8a54,0x8a56,0x8a54,0x8a54,0x8a54,0xEE27,0xffff,
					0x93C9,0x4422,0x2A54,0x9189,0x2814,0x4422,0x8A51,0x518A,0x2184,0x9249,0x8C31,0xF99F,
					0xffff,0x0000,0x7FFE,0x0000,0x7FFE,0x0000,0x7FFE,0x0000,0x7FFE,0x0000,0xffff,0xffff,
					0xE3C7,0xD00B,0xCFF3,0xCC33,0xCA53,0xC993,0xC993,0xCA53,0xCC33,0xCFF3,0xD00B,0xE3C7,
					0x0810,0xF3CF,0x0420,0xF99F,0x0240,0xFC3F,0xFC3F,0x0240,0xF99F,0x0420,0xF3CF,0x0810,
					0xedb7,0xedb7,0,0xedb7,0xedb7,0,0,0xedb7,0xedb7,0,0xedb7,0xedb7,
					0x6db6,0x9249,0x9249,0x9249,0x6db6,0,0,0x6db6,0x9249,0x9249,0x9249,0x6db6,
					0xFFFF,0x71A8,0x75AB,0x75AB,0x75AB,0x75AB,0x75A9,0x75AB,0x75AB,0x75AB,0x11D8,0xFFFF,
					0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
					0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
					0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
					0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
					0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,
					0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff
					);
var FRONTWIDTH=30;
var FRONTHEIGHT=30;
var BKWIDTH=30;
var BKHEIGHT=30;
var ROWCOUNT=12;
var COLCOUNT=16;

function BROAD()
{
	this.i1=0;
	this.j1=0;
	this.i2=0;
	this.j2=0;
};

function COMMAND()
{
	this.buffer=new Array[256+16];
	this.body=new Array[256];
	this.head=new Array[16];
	this.kind=0;
	this.place=0;
	this.number=0;
	this.table=0;
	this.length=0;
	this.num1=0;
	this.num2=0;
	this.m_rsnum=0;
	this.m_fsnum=0;
	this.code=0;
	this.state=0;
	this.broad=new BROAD();
};

function PUBLIC()
{
	this.stime=0;
	this.erase=0;
	this.going=0;
	this.isDownTime=0;
	this.isMap=0;				
	this.isMove=0;			
	this.isWin=0;				
	this.TestInit=0;
	this.level=0;
	this.maxlevel=9;
	this.isPlace=0;			
	this.number=0;				
	this.table=0;				
	this.m_iParkNumber=100;		
};

function WINDOWS()
{
	this.mdc=0;				
	this.m_selemdc=0;
	this.imageArray=new Array(43);
	this.LineArray=new Array(6);
	this.AllIMG=0;
	this.AllDIV=0;
};

function NUMBER()
{
	this.state=0;
	this.iCount=0;
};

function TABLE()
{
	this.state=0;
	this.iCount=0;
	this.number=new Array[6];
};

function LINKSPACE()
{
	this.man=new PUBLIC();
	this.wnd=new WINDOWS();
	this.m_stime=300;
};

function BUTTON()
{
	this.table=0;
	this.number=0;
};

lspace=new LINKSPACE();



function dimension3(row,col,num)
{
	var i = row * num + col;
	return (this[i]);
}


function dimension2(row,col,num,num1)
{
	var i = row * num + col;
	this[i]=num1;
}

Array.prototype.setd = dimension2;
Array.prototype.getd = dimension3;

var m_Player=new Array(
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
			-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
			);

function DrawIf()
{
}

function CBroad()
{
	this.GetBK=GetBK;

	this.setBroad=setBroad;
	this.getBroad=getBroad;
	this.setPlace=setPlace;
	this.countPlace=countPlace;
	this.Init=Init;
};

CBroad.prototype=new DrawIf;

function Init()
{	
	var i,j;
	i=-1;
	do
	{
		j=-1;
		do
		{	
			setBroad(i,j,-1);
			j++;
		}while(j<ROWCOUNT+1);
		i++;
	}while(i<COLCOUNT+1);
}

function countPlace()
{
	var sum=0;

	for(var i1=0;i1<COLCOUNT;i1++)
	{
		for(var j1=0;j1<ROWCOUNT;j1++)
		{
			if(getBroad(i1,j1)!=-1)
			{
				sum++;
			}			
		};		
	};

	return sum;
}

function setPlace(lsp)
{
	var temp=lsp.man.level;
	if(lsp.man.level>lsp.man.maxlevel)
	{
		temp=lsp.man.maxlevel;
	}

	for(var j=0;j<ROWCOUNT;j++)
	{
		for(var i=0;i<COLCOUNT;i++)
		{
			if((PI_01[temp*12+j]&AND_01[i])!=0)
			{
				setBroad(i,j,1);
			}
		}
	}
}

function GetBK(lsp)
{

	setPlace(lsp);

	var sum=countPlace();
	if(sum%4!=0)
	{
		alert("不是4的倍数");
	}
	var arPlayer=new Array(sum);
	for(var i=0;i<sum/4;i++)
	{
		var pp=Math.floor(((Math.random()*100)%71*(Math.random()*1000)%57)%41);
		for(var j=0;j<4;j++)
		{
			arPlayer[i*4+j]=pp;
		}
	}

	for(var ii=0;ii<10;ii++)
	{
		for(var jj=0;jj<100;jj++)
		{
			var aa=Math.floor(((Math.random()*100)%37*(Math.random()*1000)%87)%(sum));
			var bb=Math.floor(((Math.random()*1000)%53*(Math.random()*1000)%91)%(sum));
			var temp=arPlayer[aa];
			arPlayer[aa]=arPlayer[bb];
			arPlayer[bb]=temp;
		}
	}

	var sumloop=sum;
	for(var i1=0;i1<COLCOUNT;i1++)
	{
		for(var j1=0;j1<ROWCOUNT;j1++)
		{
			if(getBroad(i1,j1)==1)
			{
				var ar=Math.floor(((Math.random()*100)%37*(Math.random()*1000)%87)%sumloop);
				setBroad(i1,j1,arPlayer[ar]);
				sumloop--;
				arPlayer[ar]=arPlayer[sumloop];
			}
		};		
	};
}

function getBroad(col,row)
{
	var i=(col+1)*(ROWCOUNT+2)+row+1;
	return this.m_Player[i];
}

function setBroad(col,row,num)
{
	var i=(col+1)*(ROWCOUNT+2)+row+1;
	this.m_Player[i]=num;
}

function CThink()
{
	this.Pass=Pass;

	this.m_iChanB;
	this.m_iChanA;
	this.nSaveDC;
	this.SetRowLine=SetRowLine;
	this.CmpSwapEx=CmpSwapEx;
	this.Swap=Swap;
	this.FindRowRoad=FindRowRoad;
	this.FindRowChannel=FindRowChannel;
	this.FindRowBAssociation=FindRowBAssociation;
	this.FindRowAAssociation=FindRowAAssociation;
	this.FindColRoad=FindColRoad;
	this.FindColBAssociation=FindColBAssociation;
	this.FindColAAssociation=FindColAAssociation;
	this.FindColChannel=FindColChannel;
	this.CmpSwap=CmpSwap;
	this.CancleLine=CancleLine;

	this.m_inum;
	this.SetColLine=SetColLine;	
};

CThink.prototype=new CBroad;

function CancleLine()
{
	for(var i2=0;i2<COLCOUNT+2;i2++)
	{
		for(var j2=0;j2<ROWCOUNT+2;j2++)
		{
			if(getBroad(i2-1,j2-1)==-1)
			{
				lspace.wnd.AllIMG[i2*(ROWCOUNT+2)+j2].src=lspace.wnd.imageArray[0].src;
				Show(lspace.wnd.AllDIV[i2*(ROWCOUNT+2)+j2],'hidden');
			}
		}
	}
}

function Pass(lsp,x1,y1,x2,y2,flag)
{
	m_inum=-100;
	var i1=x1,i2=x2,j1=y1,j2=y2;
	m_iChanA=getBroad(x1,y1);
	m_iChanB=getBroad(x2,y2);
	setBroad(x1,y1,-1);
	setBroad(x2,y2,-1);
	broad=new BROAD;
	broad.i1=i1;
	broad.i2=i2;
	broad.j1=j1;
	broad.j2=j2;
	CmpSwapEx(broad);
	i1=broad.i1;
	i2=broad.i2;
	j1=broad.j1;
	j2=broad.j2;
	if(FindColRoad(i1,j1,i2,j2))
	{
		if(flag)
		{
			if(j2<j1)
			{
				var nn=i1;
				i1=i2;
				i2=nn;

				var mm=j1;
				j1=j2;
				j2=mm;
			}
			SetColLine(lsp,i1,j1,i2,j2,true);
			setTimeout("CancleLine()",30);
		}
		return true;
	}
		
	i1=x1,i2=x2,j1=y1,j2=y2;		
	broad.i1=i1;
	broad.i2=i2;
	broad.j1=j1;
	broad.j2=j2;
	CmpSwapEx(broad);
	i1=broad.i1;
	i2=broad.i2;
	j1=broad.j1;
	j2=broad.j2;
	if(FindRowRoad(i1,j1,i2,j2))
	{
		if(flag)
		{
			if(i2<i1)
			{
				var nn=i1;
				i1=i2;
				i2=nn;

				var mm=j1;
				j1=j2;
				j2=mm;
			}
			SetRowLine(lsp,i1,j1,i2,j2,true);
			setTimeout("CancleLine()",30);
		}
		return true;
	}
	setBroad(x1,y1,m_iChanA);
	setBroad(x2,y2,m_iChanB);
	return false;
}

function CmpSwap(broad)
{
	var nn=broad.i1;
	if(broad.i1>broad.i2)
	{
		broad.i1=broad.i2;
		broad.i2=nn;
	}
}

function FindColChannel(i,j1,j2)
{
	if(j1==j2)
		return false;
	var J1=j1,J2=j2;
	broad=new BROAD;
	broad.i1=J1;
	broad.i2=J2;
	CmpSwap(broad);
	J1=broad.i1;
	J2=broad.i2;
	var j=J1;
	do
	{
		if(getBroad(i,j)!=-1)
			return false;
		j++;
	}while(j<J2);

	return true;
}

function FindColAAssociation(i,i1,j1)
{
	if(i==i1)
		return true;
	var s=i,s1=i1;
	broad=new BROAD;
	broad.i1=s;
	broad.i2=s1;
	CmpSwap(broad);
	s=broad.i1;
	s1=broad.i2;
	do
	{
		if(getBroad(s,j1)>-1)
			return false;
		s+=1;
	}while(s<=s1);
	return true;
}

function FindColBAssociation(i,i2,j2)
{
	if(i==i2)
		return true;
	var s=i,s2=i2;
	broad=new BROAD;
	broad.i1=s;
	broad.i2=s2;
	CmpSwap(broad);
	s=broad.i1;
	s2=broad.i2;
	do
	{
		if(getBroad(s,j2)!=-1)
			return false;
		s+=1;
	}while(s<=s2);
	return true;
}

function FindColRoad(i1,j1,i2,j2)
{
	var temp=COLCOUNT+2;

	var i=-1;
	do
	{
		m_inum=i;
		if(FindColChannel(i,j1,j2))
		{
			if(FindColAAssociation(i,i1,j1))
			{
				if(FindColBAssociation(i,i2,j2))
				{
					if(temp==COLCOUNT+2)
					{
						temp=m_inum;
					}
					else
					{
						if(Math.abs(Math.abs(m_inum-i1)+Math.abs(m_inum-i2))<Math.abs(Math.abs(temp-i1)+Math.abs(temp-i2)))
						{
							temp=m_inum;
						}
					}
				}
			}
		}
		i++;
	}while(i<=COLCOUNT+1);

	if(temp!=COLCOUNT+2)
	{
		m_inum=temp;
		return true;
	}

	return false;
}

function FindRowAAssociation(j,j1,i1)
{
	if(j==j1)
		return true;
	var t=j,t1=j1;
	broad=new BROAD;
	broad.i1=t;
	broad.i2=t1;
	CmpSwap(broad);
	t=broad.i1;
	t1=broad.i2;
	do
	{
		if(getBroad(i1,t)!=-1)
			return false;
		t++;
	}while(t<=t1);
	return true;
}

function FindRowBAssociation(j,j2,i2)
{
	if(j==j2)
		return true;
	var t=j,t2=j2;
	broad=new BROAD;
	broad.i1=t;
	broad.i2=t2;
	CmpSwap(broad);
	t=broad.i1;
	t2=broad.i2;
	do
	{
		if(getBroad(i2,t)!=-1)
			return false;
		t++;
	}while(t<=t2);
	return true;
}

function FindRowChannel(j,i1,i2)
{
	if(i1==i2)
		return false;
	var I1=i1,I2=i2;
	broad=new BROAD;
	broad.i1=I1;
	broad.i2=I2;
	CmpSwap(broad);
	I1=broad.i1;
	I2=broad.i2;
	var i=I1;
	do
	{
		if(getBroad(i,j)!=-1)
			return false;
		i++;
	}while(i<I2);
	return true;
}

function FindRowRoad(i1,j1,i2,j2)
{
	var temp=ROWCOUNT+2;

	var j=-1;
	do
	{
		m_inum=j;
		if(FindRowChannel(j,i1,i2))
		{
			if(FindRowAAssociation(j,j1,i1))
			{
				if(FindRowBAssociation(j,j2,i2))
				{
					if(temp==ROWCOUNT+2)
					{
						temp=m_inum;
					}
					else
					{
						if(Math.abs(Math.abs(m_inum-j1)+Math.abs(m_inum-j2))<Math.abs(Math.abs(temp-j1)+Math.abs(temp-j2)))
						{
							temp=m_inum;
						}
					}
				}
			}
		}
		j++;
	}while(j<=ROWCOUNT+1);

	if(temp!=ROWCOUNT+2)
	{
		m_inum=temp;
		return true;
	}

	return false;
}


function SetColLine(lsp,i1,j1,i2,j2,flag)
{
	var n=0,x,y;
	var sty='visible';
	var s1=m_inum,s2;
	s2=i1;
	var sty1='hidden';
	broad=new BROAD;
	broad.i1=s1;
	broad.i2=s2;
	CmpSwap(broad);
	s1=broad.i1;
	s2=broad.i2;
	if(s1!=s2)
	{
		n=s1;
		do
		{
			if(n==m_inum)
			{
				if(n==s1)
				{
					lsp.wnd.AllIMG[(n+1)*(ROWCOUNT+2)+j1+1].src=lsp.wnd.LineArray[3].src;
				}
				else
				{
					lsp.wnd.AllIMG[(n+1)*(ROWCOUNT+2)+j1+1].src=lsp.wnd.LineArray[5].src;
				}
			}
			else
			{
				lsp.wnd.AllIMG[(n+1)*(ROWCOUNT+2)+j1+1].src=lsp.wnd.LineArray[0].src;
			}
			lsp.wnd.AllDIV[(n+1)*(ROWCOUNT+2)+j1+1].style.background="";
			Show(lsp.wnd.AllDIV[(n+1)*(ROWCOUNT+2)+j1+1],sty);
			n++;			
		}while(n<s2+1);		
	}	
	
	var t1=j1,t2=j2;
	broad.i1=t1;
	broad.i2=t2;
	CmpSwap(broad);
	t1=broad.i1;
	t2=broad.i2;
	if(t1!=t2)
	{
		n=t1+1;
		do
		{
			lsp.wnd.AllIMG[(m_inum+1)*(ROWCOUNT+2)+n+1].src=lsp.wnd.LineArray[1].src;
			lsp.wnd.AllDIV[(m_inum+1)*(ROWCOUNT+2)+n+1].style.background="";
			Show(lsp.wnd.AllDIV[(m_inum+1)*(ROWCOUNT+2)+n+1],sty);
			n++;			
		}while(n<t2);		
	}

	s1=m_inum;
	s2=i2;
	broad.i1=s1;
	broad.i2=s2;
	CmpSwap(broad);
	s1=broad.i1;
	s2=broad.i2;
	if(s1!=s2)
	{
		n=s1;
		do
		{
			if(n==m_inum)
			{
				if(n==s1)
				{
					lsp.wnd.AllIMG[(n+1)*(ROWCOUNT+2)+j2+1].src=lsp.wnd.LineArray[2].src;
				}
				else
				{
					lsp.wnd.AllIMG[(n+1)*(ROWCOUNT+2)+j2+1].src=lsp.wnd.LineArray[4].src;
				}
			}
			else
			{
				lsp.wnd.AllIMG[(n+1)*(ROWCOUNT+2)+j2+1].src=lsp.wnd.LineArray[0].src;
			}
			lsp.wnd.AllDIV[(n+1)*(ROWCOUNT+2)+j2+1].style.background="";
			Show(lsp.wnd.AllDIV[(n+1)*(ROWCOUNT+2)+j2+1],sty);
			n++;			
		}while(n<s2+1);		
	}
}

function Swap(broad)
{
	var nn=broad.i1;
	broad.i1=broad.i2;
	broad.i2=nn;
}

function CmpSwapEx(broad)
{
	if(broad.i1>broad.i2 && broad.j1>broad.j2)
	{
		var nn=broad.i1;
		broad.i1=broad.i2;
		broad.i2=nn;

		nn=broad.j1;
		broad.j1=broad.j2;
		broad.j2=nn;
	}
}

function SetRowLine(lsp,i1,j1,i2,j2,flag)
{
	var n=0,x,y;
	sty='visible';
	var t1=m_inum,t2=j1;
	sty1='hidden';
	broad=new BROAD;
	broad.i1=t1;
	broad.i2=t2;
	CmpSwap(broad);
	t1=broad.i1;
	t2=broad.i2;
	if(t1!=t2)
	{
		n=t1;
		do
		{
			if(n==m_inum)
			{
				if(n==t1)
				{
					lsp.wnd.AllIMG[(i1+1)*(ROWCOUNT+2)+n+1].src=lsp.wnd.LineArray[3].src;
				}
				else
				{
					lsp.wnd.AllIMG[(i1+1)*(ROWCOUNT+2)+n+1].src=lsp.wnd.LineArray[2].src;
				}
			}
			else
			{
				lsp.wnd.AllIMG[(i1+1)*(ROWCOUNT+2)+n+1].src=lsp.wnd.LineArray[1].src;
			}
			lsp.wnd.AllDIV[(i1+1)*(ROWCOUNT+2)+n+1].style.background="";
			Show(lsp.wnd.AllDIV[(i1+1)*(ROWCOUNT+2)+n+1],sty);
			
			n++;			
		}while(n<t2+1);		
	}

	var s1=i1,s2=i2;
	broad.i1=s1;
	broad.i2=s2;
	CmpSwap(broad);
	s1=broad.i1;
	s2=broad.i2;
	if(s1!=s2)
	{
		n=s1+1;
		do
		{
			lsp.wnd.AllIMG[(n+1)*(ROWCOUNT+2)+m_inum+1].src=lsp.wnd.LineArray[0].src;
			lsp.wnd.AllDIV[(n+1)*(ROWCOUNT+2)+m_inum+1].style.background="";
			Show(lsp.wnd.AllDIV[(n+1)*(ROWCOUNT+2)+m_inum+1],sty);
			n++;			
		}while(n<s2);		
	}	

	t1=m_inum;t2=j2;
	broad.i1=t1;
	broad.i2=t2;
	CmpSwap(broad);
	t1=broad.i1;
	t2=broad.i2;
	if(t1!=t2)
	{
		n=t1;
		do
		{
			if(n==m_inum)
			{
				if(n==t1)
				{
					lsp.wnd.AllIMG[(i2+1)*(ROWCOUNT+2)+n+1].src=lsp.wnd.LineArray[5].src;
				}
				else
				{
					lsp.wnd.AllIMG[(i2+1)*(ROWCOUNT+2)+n+1].src=lsp.wnd.LineArray[4].src;
				}
			}
			else
			{			
				lsp.wnd.AllIMG[(i2+1)*(ROWCOUNT+2)+n+1].src=lsp.wnd.LineArray[1].src;
			}
			lsp.wnd.AllDIV[(i2+1)*(ROWCOUNT+2)+n+1].style.background="";
			Show(lsp.wnd.AllDIV[(i2+1)*(ROWCOUNT+2)+n+1],sty);
			n++;			
		}while(n<t2+1);		
	}
}

function Draw()
{
	this.m_iCount=0;
	this.SetCount=SetCount;
	this.SetWin=SetWin;
	this.m_iWin=-1;
};

Draw.prototype=new CThink;

function SetWin(lsp,self)
{
	var i=0,num=-1;
	m_iWin=0;
	do
	{
		if(lsp.man.m_iParkNumber2[i]<lsp.man.m_iParkNumber1[self]			
			&& lsp.man.m_iParkNumber2[i]!=num)
		{
			m_iWin++;
		}
		num=lsp.man.m_iParkNumber2[i];
		i++;
	}while(i<6);
	SetCount(lsp,self);

	return false;
}


function SetWinNumber(lsp)
{
	
}

function SetCount(lsp,self)
{
	m_iCount=(lsp.man.m_iParkNumber/2-lsp.man.m_iParkNumber1[self])*1;
	if(m_iWin==0 && m_iCount<20)
	{
		m_iCount=20;
	}
}

function SubDraw()
{
	this.Click=Click;
	this.RunClick=RunClick;
	this.DrawMan=DrawMan;
	this.DrawWin=DrawWin;
	this.DrawSelect=DrawSelect;
	this.FillMemDC=FillMemDC;
	this.RepaintSame=RepaintSame;
	this.ClearSysEcho=ClearSysEcho;
	this.ShowSysEcho=ShowSysEcho;
	this.NextLevel=NextLevel;
	this.i1=-1;
	this.j1=-1;
	this.i2;
	this.j2;
	this.m_iTimer=0;	
};

SubDraw.prototype=new Draw;

function NextLevel()
{
	Show(idfalse,'hidden');
	lspace.man.isWin=false;
	lspace.man.level++;

	if(Math.floor(lspace.man.level/10)==0)
	{
		idlevel.innerText='0';
	}
	else
	{
		idlevel.innerText='';
	}

	idlevel.innerText+=lspace.man.level;

	draw.Init();
	draw.GetBK(lspace);
	lspace.m_stime=300-lspace.man.level*20;
	if(lspace.m_stime<120)
	{
		lspace.m_stime=120;
	}

	setTimeout("TimeEvent()",1000);


	ShowSysEcho(lspace);
}

function ShowSysEcho(lsp)
{
	for(var i1=0;i1<COLCOUNT+2;i1++)
	{
		for(var j1=0;j1<ROWCOUNT+2;j1++)
		{
			if(getBroad(i1-1,j1-1)<0)
			{
				lsp.wnd.AllIMG[i1*(ROWCOUNT+2)+j1].src=lsp.wnd.imageArray[0].src;
				Show(lsp.wnd.AllDIV[i1*(ROWCOUNT+2)+j1],'hidden');
			}
			else
			{
				lsp.wnd.AllIMG[i1*(ROWCOUNT+2)+j1].src=lsp.wnd.imageArray[getBroad(i1-1,j1-1)].src;
				Show(lsp.wnd.AllDIV[i1*(ROWCOUNT+2)+j1],'visible');
			}
		}
	}

	sele.src=lsp.wnd.imageArray[42].src;
	Show(MainMap2,'hidden');
	MainMap2.style.background="";
}

function ClearSysEcho(lsp)
{
	for(var i1=0;i1<COLCOUNT+2;i1++)
	{
		for(var j1=0;j1<ROWCOUNT+2;j1++)
		{
			setBroad(i1,j1,-1);
			lsp.wnd.AllIMG[i1*(ROWCOUNT+2)+j1].src=lsp.wnd.imageArray[0].src;
			Show(lsp.wnd.AllDIV[i1*(ROWCOUNT+2)+j1],'hidden');
		}
	}

	//sele.src=lsp.wnd.imageArray[42].src;
	Show(MainMap2,'hidden');
	MainMap2.style.background="";
}

function FillMemDC(lsp)
{
	var i=0,j;
	do
	{
		j=0;
		do
		{			
			if(getBroad(i,j)!=-1)
			{
			}
			j++;
		}while(j<ROWCOUNT);
		i++;
	}while(i<COLCOUNT);	
}

function Click(X,Y)
{
	var x=X-1;
	var y=Y-1;

	if( x<0 || x>=COLCOUNT || y<0 || y>=ROWCOUNT || getBroad(x,y)<0)
	{
		this.i1=-1;
		this.j1=-1;
		return false;
	}
	
	this.i2=x;
	this.j2=y;

	MainMap2.style.left=(x+1)*BKWIDTH;
	MainMap2.style.top=(y+1)*BKWIDTH;
	Show(MainMap2,'visible');

	if(this.i1<0)
	{
		this.i1=this.i2;
		this.j1=this.j2;
		return false;
	}
	
	if(this.i1==this.i2 && this.j1==this.j2)
	{
		RepaintSame(lsp);
		this.i1=this.i2;
		this.j1=this.j2;	
		return false;
	}

	if(getBroad(this.i1,this.j1)!=getBroad(this.i2,this.j2))
	{
		RepaintSame(lspace);
		this.i1=this.i2;
		this.j1=this.j2;	
		return false;
	}


	if(Pass(lspace,this.i1,this.j1,this.i2,this.j2,true))
	{
		Show(MainMap2,'hidden');

		RepaintSame(lspace);
		this.i1=-1;
		this.j1=-1;
		if(countPlace()<=0)
		{
			lspace.man.isWin=true;
			idfalse.innerText="正在进入下一关";
			Show(idfalse,'visible');
			if(lspace.m_stime<=0)
			{
				lspace.m_stime=0;
			}						
			setTimeout("NextLevel()",1000);				
		}
		return true;
	}

	RepaintSame(lspace);
	this.i1=this.i2;
	this.j1=this.j2;
	
	return false;	
}

function RunClick(lsp)
{
	return false;
}

function RepaintSame(lsp)
{

}

function SetWinNumber(lsp)
{
	SetWin(lsp,lsp.recom.number);
	if(m_iWin==0)
	{
		lsp.man.S56=true;
	}
}

function DrawSelect(lsp)
{
	var X=StartX+j2*FRONTWIDTH;
	var Y=StartY+i2*FRONTHEIGHT;
}

function DrawWin(lsp)
{
	var W=new Array(204,247,241,232,244,251);
	var H=new Array(147,153,159,147,153,153);
}

function DrawMan(lsp)
{	
}

function Show(m_id,index)
{
	m_id.style.visibility=index;
}

function CPicture()
{
	this.BorderWidth=5;
	this.StartX=13;
	this.StartY=71;
	this.dc=new CDC();
	this.m_selemdc=new CDC();

	this.SetMainHandle=SetMainHandle;
	this.SetCacheImage=SetCacheImage;
	this.SetHandHandle=SetHandHandle;
	this.SetImageHandle=SetImageHandle;
	this.Create=Create;
};

CPicture.prototype=new SubDraw;

function SetMainHandle(lsp)
{
	this.dc.CreateDC(this.StartX,this.StartY,(COLCOUNT+2)*BKWIDTH,(ROWCOUNT+2)*BKWIDTH);
	//this.dc.FillRect(0x0000ff);
	lsp.wnd.mdc=this.dc.GetDC();
	lsp.wnd.mdc.style.background="";
};

function SetCacheImage(lsp)
{
	for(var i=0;i<41;i++)
	{
		lsp.wnd.imageArray[i]=new Image(BKWIDTH,BKWIDTH);
		var temp="img/lk_00"+Math.floor(i/10);
		temp+=Math.floor(i%10);
		temp+=".gif";
		lsp.wnd.imageArray[i].src=temp;
	}
	lsp.wnd.imageArray[42]=new Image(BKWIDTH,BKWIDTH);
	lsp.wnd.imageArray[42].src="000000.gif";
	lsp.wnd.LineArray[0]=new Image(BKWIDTH,BKWIDTH);
	lsp.wnd.LineArray[0].src="line_1.gif";
	lsp.wnd.LineArray[1]=new Image(BKWIDTH,BKWIDTH);
	lsp.wnd.LineArray[1].src="line_2.gif";
	lsp.wnd.LineArray[2]=new Image(BKWIDTH,BKWIDTH);
	lsp.wnd.LineArray[2].src="left_bottom.gif";
	lsp.wnd.LineArray[3]=new Image(BKWIDTH,BKWIDTH);
	lsp.wnd.LineArray[3].src="left_top.gif";
	lsp.wnd.LineArray[4]=new Image(BKWIDTH,BKWIDTH);
	lsp.wnd.LineArray[4].src="right_bottoom.gif";
	lsp.wnd.LineArray[5]=new Image(BKWIDTH,BKWIDTH);
	lsp.wnd.LineArray[5].src="right_top.gif";
};

function SetHandHandle(lsp)
{
	lsp.wnd.AllIMG=lspace.wnd.mdc.all.tags('IMG')
	lsp.wnd.AllDIV=lspace.wnd.mdc.all.tags('DIV')
};

function Create(lsp)
{
	this.SetMainHandle(lsp);
	this.SetCacheImage(lsp);
	this.SetHandHandle(lsp);
	this.SetImageHandle(lsp);
	this.i1=-1;
	this.j1=-1;
}

function SetImageHandle(lsp)
{
	for(var i=0;i<(COLCOUNT+2)/2;i++)
	{
		lsp.wnd.mdc.innerHTML+=
		 '<DIV onClick="Click('+2*i+',0);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+0*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',1);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+1*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',2);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+2*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',3);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+3*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',4);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+4*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',5);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+5*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',6);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+6*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',7);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+7*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',8);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+8*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',9);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+9*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',10);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+10*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',11);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+11*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',12);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+12*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+2*i+',13);" style=position:absolute;visibility:hidden;left='+2*i*BKWIDTH+';top='+13*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',0);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+0*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',1);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+1*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',2);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+2*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',3);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+3*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',4);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+4*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',5);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+5*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',6);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+6*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',7);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+7*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',8);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+8*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',9);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+9*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',10);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+10*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',11);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+11*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',12);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+12*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>'
		+'<DIV onClick="Click('+(2*i+1)+',13);" style=position:absolute;visibility:hidden;left='+(2*i+1)*BKWIDTH+';top='+13*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id="zz"><IMG SRC="" id="mm"></DIV>';
	}

	lsp.wnd.mdc.innerHTML+='<DIV style=position:absolute;visibility:hidden;left='+(COLCOUNT+2)*BKWIDTH+';top='+(ROWCOUNT+2)*BKWIDTH+';width:BKWIDTH;height:BKWIDTH;overflow:hidden class=Snake id=MainMap2><IMG SRC="" id="sele"></DIV>';
	lsp.wnd.m_selemdc=MainMap2;

	lsp.wnd.mdc.innerHTML+='<div id=idfalse style="left:100;top:100;visibility:hidden">你输了!</div>';

	ShowSysEcho(lsp);
}

draw=new CPicture();

function TimeEvent()
{
	if(lspace.man.isWin)
	{
		return;
	}

	lspace.m_stime--;
	if(Math.floor(lspace.m_stime/60/10)==0)
	{
		idtimem.innerText='0';
	}
	else
	{
		idtimem.innerText='';
	}

	idtimem.innerText+=Math.floor(lspace.m_stime/60%10);
	if(Math.floor(lspace.m_stime%60/10)==0)
	{
		idtimes.innerText='0';
	}
	else
	{
		idtimes.innerText='';
	}

	idtimes.innerText+=Math.floor(lspace.m_stime%60);
	if(lspace.m_stime<=0)
	{
		lspace.m_stime=300;
		draw.ClearSysEcho(lspace);
		idfalse.innerText="你输了!"
		Show(idfalse,'visible');
	}
	else
	{
		setTimeout("TimeEvent()",1000);
	}
}

function Main()
{
	draw.Init();
	draw.GetBK(lspace);
	draw.Create(lspace);
	lspace.m_stime=300;
	setTimeout("TimeEvent()",1000);
}

onload=Main;