// JScript source code
var map=new Object();
//var declare
//chua mang cac hinh tinh
map.arrImgName = new Array();
//cac container
map.desktop;
map.viewPort;
//Mapping cac ban do
map.view;
map.small;
map.map;
map.header;

//Ban do co the tuong tac
map.ready=true;

//chieu rong va cao cua viewPort
map.viewWidth;
map.viewHeight;
map.halfViewWidth;
map.halfViewHeight;
//Dau cong
map.add;
//
//Chinh sua cac bien sau de xc dinh thuoc tinh cho ung dung
//
//Ten header cua ban do
map.headerText="Ban do TP Can Tho";
//Ti le cua ban do nho so voi ban do lon
map.rat;
map.percent;
//So toi da lan phong va ti le phong
map.maxZoomIn=3;
map.maxZoomOut=3;

//so luong cot va hang chua Img trong ma tran Map
map.xCount;
map.yCount;
map.Count;//se duoc tinh trong app.InitMap, cac gia tri gan cho imgCount o day deu vo nghia
//Chieu Rong va cao cua tung Img (pixel)
map.imgWidth=256;
map.imgHeight=256;
map.imgEndW;
map.imgEndH;
//Ten folder chua hinh
map.folder="DT/";
map.imgType=".jpg";
map.serPage = "Load.aspx";
map.searchPage = "Search.aspx";
//
//Ket thuc chinh sua
//

map.InitMap = function(folder,desktopW,desktopH,smallW,smallH)
{
	map.folder=folder;
	ImgSmall.src=map.folder+"small"+map.imgType; 	
	map.map = document.getElementById("Map");
	
	map.desktop = document.getElementById("Desktop");
	map.viewPort = document.getElementById("ViewPort");
	
	map.view = document.getElementById("View");	
	map.small = document.getElementById("SmallMap");
	
	map.add = document.getElementById("Add");
	
	map.imgWidth=256;
	map.imgHeight=256;
	map.xCount=Math.ceil(desktopW/map.imgWidth);
	map.yCount=Math.ceil(desktopH/map.imgHeight);
	map.percent=1;
	map.rat=smallW/desktopW;
	
	map.desktop.style.width=desktopW;
	map.desktop.style.height=desktopH;
	map.imgEndW=desktopW-map.imgWidth*(map.xCount-1);
	map.imgEndH=desktopH-map.imgHeight*(map.yCount-1);
	
	for(j=0;j<map.yCount;j++)
	{
		for(i=0;i<map.xCount;i++)
		{
			map.arrImgName[i+"_"+j]=i+"_"+j+map.imgType;
		}
	}
	map.BuildDesktop();

	//Init cac gia tri
	map.Count = map.xCount * map.yCount;


}
map.BuildDesktop =function()
{
	map.desktop.innerHTML="<img style='position:absolute;z-index:200;left:100;top:100;visibility:hidden' id='xSign' src='image/x.gif'>";
	var top,left,strImg,img;
	var H=0,W=0;
	
	for(j=0;j<map.yCount;j++)
	{
		top=map.imgHeight*j;
		for(i=0;i<map.xCount;i++)
		{
			left=map.imgWidth*i; 
			strImg = "img" + i + "_" +j;
			
			if(i<map.xCount-1&&j<map.yCount-1)
			{
				map.desktop.innerHTML+="<img class='Cell' id='" + strImg + "' galleryimg='false' "+
					"style='left:"+left+";top:"+top+";width:"+map.imgWidth+";height:"+map.imgHeight+"'>";
			}
			else if(i<map.xCount-1&&j==map.yCount-1)
			{	
				map.desktop.innerHTML+="<img class='Cell' id='" + strImg + "' galleryimg='false' "+
					"style='left:"+left+";top:"+top+";width:"+map.imgWidth+";height:"+map.imgEndH+"'>";
			}
			else if(i==map.xCount-1&&j<map.yCount-1)
			{	
				map.desktop.innerHTML+="<img class='Cell' id='" + strImg + "' galleryimg='false' "+
					"style='left:"+left+";top:"+top+";width:"+map.imgEndW+";height:"+map.imgHeight+"'>";
			}
			else if(i==map.xCount-1&&j==map.yCount-1)
			{
				map.desktop.innerHTML+="<img class='Cell' id='" + strImg + "' galleryimg='false' "+
					"style='left:"+left+";top:"+top+";width:"+map.imgEndW+";height:"+map.imgEndH+"'>";
			}
			
			img = document.getElementById(strImg);
			img.style.visibility="hidden";
		}
	}
	
}
map.GetMidX= function()
{
	return Math.floor( (-parseInt(map.desktop.style.left)+parseInt(map.viewWidth/2))/map.imgWidth );
}
map.GetMidY= function()
{
	return Math.floor( (-parseInt(map.desktop.style.top)+parseInt(map.viewHeight/2))/map.imgHeight );
} 
map.GetLayerVisible = function()
{
	var layer=0;
	for(i=0;i<=parseInt(map.xCount/2);i++)
	{
		if(i*map.imgWidth>map.halfViewWidth) 
		{
			layer=i;
			break;
		}
	}
	for(i=0;i<=parseInt(map.yCount/2);i++)
	{
		if(i*map.imgHeight>map.halfViewHeight) 
		{
			if(layer<i)layer=i;
			break;
		}
	}
	return layer;
}
map.Visible= function(x,y)
{
	var imgP = map.GetImg(x,y);
	if(imgP==null)return;
	var iL=parseInt(imgP.style.left)-(-parseInt(map.desktop.style.left));
	var iT=parseInt(imgP.style.top)-(-parseInt(map.desktop.style.top));;
	var iW=parseInt(imgP.style.width);
	var iH=parseInt(imgP.style.height);
	
	var vW=parseInt(map.viewPort.style.width);
	var vH=parseInt(map.viewPort.style.height);
	if(iL<0&&Math.abs(iL)>=iW) return false;
	if(iT<0&&Math.abs(iT)>=iH) return false;
	if(iL>0&&iL>=vW) return false;
	if(iT>0&&iT>=vH) return false;
	return true;
}
map.GetImg = function(x,y)
{
	return document.getElementById("img"+x+"_"+y);
}
map.NotInit = function(x,y)
{
	var img=map.GetImg(x,y);
	if(img.src=="")return true;
	return false;
}
