// JScript source code
//Cac Ham xu ly su kien ve chuot: ket hop su kien nhan,keo,tha de co the keo MAP di theo chuot khi ta nhan va keo
var face = new Object();
face.bDown=false;
face.dx=0;
face.dy=0;
face.viewPortInfo = new Array();
face.viewPortInfo["width"]="543px";
face.viewPortInfo["height"]="412px";
face.viewPortInfo["left"]="23px";
face.viewPortInfo["top"]="17px";

face.maxViewPortInfo = new Array();
face.maxViewPortInfo["width"]="";
face.maxViewPortInfo["height"]="";
face.maxViewPortInfo["left"]="";
face.maxViewPortInfo["top"]="";

face.smallMapInfo = new Array();
face.smallMapInfo["width"]=null;
face.smallMapInfo["height"]=null;
face.smallMapInfo["left"]="585px";
face.smallMapInfo["top"]="185px";
face.viewMode=null;


face.ChangeView = function()
{
	if(face.viewMode==null) face.viewMode="normal";
	if(face.viewMode=="normal")
	{
		face.viewMode="large";
		//face.btnZoomClick(null,true);		
		face.SetViewPort(face.maxViewPortInfo["width"],face.maxViewPortInfo["height"],face.maxViewPortInfo["left"],face.maxViewPortInfo["top"]);
		face.SetSmallMap(null,null,parseInt(map.map.style.width)-parseInt(map.small.style.width),parseInt(map.map.style.height)-parseInt(map.small.style.height));
		Choice.style.visibility="hidden";
		map.small.style.visibility="hidden";
		Search.style.zIndex="9";
		map.map.style.borderWidth="0";
		SetPlaceNameCenter();
		face.FixMap();
		face.FillView();
		return;
	}
	if(face.viewMode=="large")
	{
		face.viewMode="normal";
		face.SetViewPort(face.viewPortInfo["width"],face.viewPortInfo["height"],face.viewPortInfo["left"],face.viewPortInfo["top"]);
		face.SetSmallMap(null,null,face.smallMapInfo["left"],face.smallMapInfo["top"]);
		Choice.style.visibility="visible";
		map.small.style.visibility="visible";
		Search.style.zIndex="150";
		map.map.style.borderWidth="1";
		SetPlaceNameCenter();
		face.FixMap();
		face.FillView();
		return;
	}
	
}
face.ResetMap = function(folder,desktopW,desktopH,smallW,smallH)
{
	load.Stop();
	map.InitMap(folder,desktopW,desktopH,smallW,smallH);
	face.zoom=0;
	face.SetViewPort(face.viewPortInfo["width"],face.viewPortInfo["height"],face.viewPortInfo["left"],face.viewPortInfo["top"]);
	face.SetSmallMap(smallW,smallH,face.smallMapInfo["left"],face.smallMapInfo["top"]);
	
	var midLeft=parseInt( (parseInt(map.desktop.style.width)-map.viewWidth)/2 );

	var midTop=parseInt( (parseInt(map.desktop.style.height)-map.viewHeight)/2 );
	map.desktop.style.left=-midLeft;
	map.desktop.style.top=-midTop;
	face.FillView();
	load.Start();
}
face.HiddenSearch = function()
{
	Search.style.visibility="hidden";
	SPan.className="SPanOff";
}
face.VisibleSearch = function()
{
	Search.style.visibility="visible";
	document.getElementById("sResult").innerHTML="";
}
face.SetSmallMap = function(W,H,L,T)
{
	if(W!=null) map.small.style.width = parseInt(W)+1;
	if(H!=null)	map.small.style.height = parseInt(H)+1;
	if(L!=null) map.small.style.left = parseInt(L);
	if(T!=null) map.small.style.top = parseInt(T);	
}
face.SetViewPort = function(W,H,L,T)
{
	if(W!=null)
	{
		map.viewPort.style.width = parseInt(W);
		map.viewWidth = parseInt(map.viewPort.style.width);
		map.halfViewWidth = (map.viewWidth/2);
		map.add.style.left = map.halfViewWidth-8;
	}
	if(H!=null)
	{
		map.viewPort.style.height = parseInt(H);
		map.viewHeight = parseInt(map.viewPort.style.height);
		map.halfViewHeight = (map.viewHeight/2);
		map.add.style.top = map.halfViewHeight-8;
	}
	if(L!=null)
	{
		map.viewPort.style.left = parseInt(L);
	}
	if(T!=null)
	{
		map.viewPort.style.top = parseInt(T);
	}
}
var left=-1;
var top=-1;
face.MapMove = function()
{	
    var newLeft=parseInt(map.desktop.style.left);
    var newTop=parseInt(map.desktop.style.top);
    if(newLeft!=left||newTop!=top)
    {
        left=newLeft;
        top=newTop;
        load.NewLoad();		
    }    
}
face.MouseDown = function(e)
{   
	if(window.event)	
	{
		e=window.event;
		dx=e.clientX-map.desktop.getBoundingClientRect().left+map.viewPort.getBoundingClientRect().left;	
		dy=e.clientY-map.desktop.getBoundingClientRect().top+map.viewPort.getBoundingClientRect().top;	
	}
	else
	{		
		dx=e.clientX-document.getBoxObjectFor(map.desktop).screenX + document.getBoxObjectFor(map.viewPort).screenX;
		dy=e.clientY-document.getBoxObjectFor(map.desktop).screenY + document.getBoxObjectFor(map.viewPort).screenY;
	}
	face.bDown=true;
	map.desktop.onmousemove=face.MouseMove;				
}
face.MouseUp = function()
{
	face.bDown=false;
	//if(!map.ready) return;
	map.desktop.onmousemove=null;
}
face.oldL=0;
face.oldT=0;
face.MouseMove = function(e)
{
	//if(!map.ready) return;
	if(face.bDown)
	{
		if(window.event) e=window.event;		
		face.oldL=parseInt(map.desktop.style.left);
		face.oldT=parseInt(map.desktop.style.top);
		map.desktop.style.left=(e.clientX-dx)+"px";
		map.desktop.style.top=(e.clientY-dy)+"px";	
		if(!face.AllowMove())return;
		face.FixMap();
		face.FillView();
	}
}
//Ham chinh sua ban do khong cho vuot qua ViewPort
face.FixMap= function()
{
	var dLeft = parseInt(map.desktop.style.left);
	var dTop = parseInt(map.desktop.style.top);
	
	if(dLeft>0)
	{
		map.desktop.style.left=0;
	}
	if(dTop>0)
	{
		map.desktop.style.top=0;
	}
	if(dLeft+parseInt(map.desktop.style.width)-map.viewWidth<0)
	{
		map.desktop.style.left=-parseInt(map.desktop.style.width)+map.viewWidth;
	}
	if(dTop+parseInt(map.desktop.style.height)-map.viewHeight<0)
	{
		map.desktop.style.top=-parseInt(map.desktop.style.height)+map.viewHeight;
	}

	if(parseInt(map.desktop.style.width)<=parseInt(map.viewPort.style.width))
	{
		map.desktop.style.left = (parseInt(map.viewPort.style.width)-parseInt(map.desktop.style.width))/2;	
	}
	if(parseInt(map.desktop.style.height)<=parseInt(map.viewPort.style.height))
	{
		map.desktop.style.top = (parseInt(map.viewPort.style.height)-parseInt(map.desktop.style.height))/2;	
	}
}
face.AllowMove= function()
{ 
	var img= document.getElementById("img"+map.GetMidX()+"_"+map.GetMidY());	
	if(img==null)
	{
		map.desktop.style.left=face.oldL+"px";
		map.desktop.style.top=face.oldT+"px";
		return false;
	}
	if((img.readyState && img.readyState!="complete") || img.complete ==false) 
	{
		map.desktop.style.left=face.oldL+"px";
		map.desktop.style.top=face.oldT+"px";
		return false;
	}
	return true;
}
// Khi nhan vao ban do nho
//Dich chuyen den vung moi
face.GoLoc = function(e)
{   	
	var offset = 139 // hang so chech lech bien do giua hai trinh duyet firefox va ie. chua hieu lam cho nay....
	if(!map.ready) return;
	if(window.event)
	{
		e=window.event;		
		var xx=e.clientX-parseInt(map.small.getBoundingClientRect().left)-parseInt(parseInt(map.view.style.width)/2);
		var yy=e.clientY-parseInt(map.small.getBoundingClientRect().top)-parseInt(parseInt(map.view.style.height)/2);
		var v = document.getElementById("View");
		map.desktop.style.left=parseInt(-xx/map.rat);
		map.desktop.style.top=parseInt(-yy/map.rat);		
	}
	else
	{			
		var xx=e.clientX-parseInt(document.getBoxObjectFor(map.small).screenX)-parseInt(parseInt(map.view.style.width)/2);
		var yy=e.clientY-(parseInt(document.getBoxObjectFor(map.small).screenY)-offset)-parseInt(parseInt(map.view.style.height)/2);
		var v = document.getElementById("View");
		map.desktop.style.left=parseInt(-xx/map.rat);
		map.desktop.style.top=parseInt(-yy/map.rat);
	}
	face.FixMap();
	face.FillView();
}
face.GoTo = function(x,y)
{
	if(!map.ready) return;
	face.btnZoomClick(1,true);
	var xx=x-parseInt(map.viewWidth/2);
	var yy=y-parseInt(map.viewHeight/2);
	map.desktop.style.left=parseInt(-xx);
	map.desktop.style.top=parseInt(-yy);
	xSign.style.left=x-8;
	xSign.style.top=y-8;
	xSign.style.visibility="visible";
	face.FixMap();
	face.FillView();

}
//Xu ly su kien nhan nut R,L,U,D dich chuyen MAP
//MAP dich chuyen sang dir mot khoang la dis
face.stepCount=0;
face.ival;
face.step;
face.dir;
face.disStep;
face.BackMove = function(oldL,oldT)
{ 
	var img= document.getElementById("img"+map.GetMidX()+"_"+map.GetMidY());
	if(img==null)
	{
		map.desktop.style.left=oldL;
		map.desktop.style.top=oldT;
		return true;
	}
	if(img.readyState!="complete") 
	{
		map.desktop.style.left=oldL;
		map.desktop.style.top=oldT;
		return true;
	}
	return false;
}
face.MoveStep=function()
{
	var dir=face.dir;
	var oldX=map.GetMidX();
	var oldY=map.GetMidY();
	var oldL=parseInt(map.desktop.style.left);
	var oldT=parseInt(map.desktop.style.top);
	if(face.stepCount>=face.step)
	{
		clearInterval(face.ival);
		map.ready=true;
		return;
	}
	face.stepCount++;	
	if(dir=="L") map.desktop.style.left=parseInt(map.desktop.style.left)+face.disStep;
	if(dir=="R") map.desktop.style.left=parseInt(map.desktop.style.left)-face.disStep;
	if(dir=="D") map.desktop.style.top=parseInt(map.desktop.style.top)-face.disStep;
	if(dir=="U") map.desktop.style.top=parseInt(map.desktop.style.top)+face.disStep;
	
	face.FixMap();
	face.FillView();
}
face.btnMoveClick=function(dir,dis,disStep)
{
	if(!map.ready) return;
	map.ready=false;
	if(disStep==null)disStep=10;
	face.stepCount=0;
	face.disStep=disStep;
	face.step = parseInt(dis/disStep);
	face.dir=dir;
	face.ival = setInterval("face.MoveStep()",5);
}
//Xu ly su kien ZoomIn,ZoomOut
face.zoom=0;
face.FillView = function()
{	
	
	var W = parseInt(map.viewPort.style.width)*map.rat;
	var H = parseInt(map.viewPort.style.height)*map.rat;
	
	//window.status = map.rat+" "+W+" "+H;
	//window.status=map.rat+" "+parseInt(map.small.style.width)/parseInt(map.desktop.style.width)
	//				+" " +parseInt(map.small.style.height)/parseInt(map.desktop.style.height);
	
	map.view.style.left=((-parseInt(map.desktop.style.left)+map.halfViewWidth)*map.rat - W/2);
	map.view.style.top=((-parseInt(map.desktop.style.top)+map.halfViewHeight)*map.rat - H/2);
	//map.view.style.width=W;
	//map.view.style.height=H;
	map.view.style.width= parseInt(parseInt(map.viewPort.style.width)/parseInt(map.desktop.style.width)*(parseInt(map.small.style.width)-3))+2;
	map.view.style.height= parseInt(parseInt(map.viewPort.style.height)/parseInt(map.desktop.style.height)*(parseInt(map.small.style.height)-3))+2;
}
face.btnZoomClick=function(percent,res)
{	
	if(!map.ready) return;
	if(res==true)
	{
		percent=1/map.percent;
		face.zoom=0;
		map.percent=1;
	}
	else
	{
		if(percent>1)face.zoom++; 
		if(percent<1)face.zoom--;
		
		if(face.zoom > map.maxZoomIn)
		{
			face.zoom=map.maxZoomIn; 
			return;
		}
		if(face.zoom < -map.maxZoomOut)
		{
			face.zoom=-map.maxZoomOut; 
			return;
		}
		map.percent=map.percent*percent;
	}
	var newDesktopWidth=parseInt(map.desktop.style.width)*percent;
	var newDesktopHeight=parseInt(map.desktop.style.height)*percent;
	
	if(newDesktopHeight<map.viewHeight)return;
	if(newDesktopWidth<map.viewWidth)return;
	
	map.imgWidth=map.imgWidth*percent;
	map.imgHeight=map.imgHeight*percent;
	map.imgEndW=map.imgEndW*percent;
	map.imgEndH=map.imgEndH*percent;
	map.desktop.style.width=newDesktopWidth;
	map.desktop.style.height=newDesktopHeight;
	
	map.desktop.style.left=	parseInt(map.desktop.style.left)-map.halfViewWidth;
	map.desktop.style.top= parseInt(map.desktop.style.top)-map.halfViewHeight;
	
	map.desktop.style.left=	parseInt(map.desktop.style.left)*percent;
	map.desktop.style.top=parseInt(map.desktop.style.top)*percent;
	
	map.desktop.style.left=	parseInt(map.desktop.style.left)+map.halfViewWidth;
	map.desktop.style.top= parseInt(map.desktop.style.top)+map.halfViewHeight;
	
	for(j=0;j<map.yCount;j++)
	{
		var top=map.imgHeight*j;
		for(i=0;i<map.xCount;i++)
		{
			var left=map.imgWidth*i; 
			var img = map.GetImg(i,j);
			if(i<map.xCount-1&&j<map.yCount-1)
			{
				img.style.width=map.imgWidth;
				img.style.height=map.imgHeight;
				img.style.top=top;
				img.style.left=left;
			}
			else if(i<map.xCount-1&&j==map.yCount-1)
			{
				img.style.width=map.imgWidth;
				img.style.height=map.imgEndH;
				img.style.top=top;
				img.style.left=left;
			}
			else if(i==map.xCount-1&&j<map.yCount-1)
			{
				img.style.width=map.imgEndW;
				img.style.height=map.imgHeight;
				img.style.top=top;
				img.style.left=left;
			}
			else if(i==map.xCount-1&&j==map.yCount-1)
			{
				img.style.width=map.imgEndW;
				img.style.height=map.imgEndH;
				img.style.top=top;
				img.style.left=left;
			}
		}
	}
	map.rat=parseInt(map.small.style.width)/parseInt(map.desktop.style.width);
	face.FixMap();
	face.FillView();
}
