var setX, offsetLeft, iHorizCount, iVertCount, iWidth, iHeight, leftX, zoomValue;
var _mousedown=0;
var _constValue = 252;
var _sumOffset = 0;
var _imgArray;
var globalLink, divMenuZoomId, hdnZoomTypeId;

function BindDiv(sName, _link, _zoomValue, _divMenuZoomId, _hdnZoomTypeId)
{
	var objDiv = document.getElementById(sName);
	if(objDiv == null){setTimeout("BindDiv('"+sName+"', '"+_link+"',"+_zoomValue+",'"+_divMenuZoomId+"','"+_hdnZoomTypeId+"')", 300);}
	else
	{
		globalLink = _link;
		_imgArray = [];
		iWidth = objDiv.clientWidth;
		iHeight = objDiv.clientHeight;
		if(iWidth==0 && objDiv.parentNode.clientWidth>0)
			iWidth = objDiv.parentNode.clientWidth;
		iHorizCount = Math.floor(iWidth/_constValue) + 3;
		iVertCount = Math.floor(iHeight/_constValue) + 1;
		leftX = 0;
		var objIMGDIV = document.createElement("div");
		objIMGDIV.id = "divCont"
		objIMGDIV.style.backgroundColor="#e5e3df";
		objIMGDIV.style.position = "absolute";
		objIMGDIV.style.top = "0px";
		objIMGDIV.style.zIndex = 0;
		objIMGDIV.style.left = (-_constValue).toString()+"px";
		offsetLeft = (-_constValue);
		for(var i=0; i<iHorizCount; i++)
		{
			_imgArray[i] = [];
			for(var j=0;j<iVertCount;j++)
			{
				var iLeft = i*_constValue;
				var iTop = j*_constValue;
				var objIMG = document.createElement("img");	
				objIMG.style.position="absolute";
				objIMG.style.left=iLeft.toString()+"px";
				objIMG.style.top=iTop.toString()+"px";
				objIMG.style.width = _constValue.toString()+"px";
				objIMG.style.height = _constValue.toString()+"px";
				objIMG.style.border="0";
				objIMG.style.padding="0";
				objIMG.style.margin="0";
				objIMG.style.zIndex = 0;
				objIMG.width = _constValue.toString();
				objIMG.height = _constValue.toString();
				objIMG.setAttribute("coordX",i);
				objIMG.setAttribute("coordY",j);
				objIMG.setAttribute("unselectable","on");
				objIMG.setAttribute("galleryImg","no");
				objIMG.setAttribute("ganttChart","yes");
				var d = new Date();
				var sPath = "&x="+i+"&y="+j+"&idt=" + d.getHours() + d.getMinutes() + d.getSeconds();
				objIMG.src= globalLink + sPath;
				objIMGDIV.appendChild(objIMG);
				_imgArray[i][j] = objIMG;
			}
		}
		objDiv.appendChild(objIMGDIV);
		if(_zoomValue!='undefined' && _zoomValue>=0)
		{
			zoomValue = _zoomValue;
			var objIMGDIV2 = document.createElement("div");
			objIMGDIV2.id = "divZoom"
			objIMGDIV2.style.position = "absolute";
			objIMGDIV2.style.top = (iHeight - 20).toString() + "px";
			objIMGDIV2.style.left = (iWidth - 20).toString() + "px";
			objIMGDIV2.style.border = "0";
			
			var objIMGZoom = document.createElement("img");
			objIMGZoom.id = "divZoomImg"
			objIMGZoom.style.position = "absolute";
			objIMGZoom.style.top = "1px";
			objIMGZoom.style.left = "1px";
			objIMGZoom.style.border = "0";
			var s = "../Layouts/Images/zoom-mini.png";
			if(browseris.ie)
				objIMGZoom.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+s+"',sizingMethod=scale)";
			objIMGZoom.setAttribute("src",s);
			objIMGZoom.setAttribute("galleryImg","no");
			objIMGZoom.setAttribute("unselectable","on");
			objIMGZoom.setAttribute("cleared","0");
			objIMGZoom.style.height = "18px";
			objIMGZoom.style.width = "18px";
			objIMGZoom.style.padding="0";
			objIMGZoom.style.margin="0";
			if(browseris.ie)
				objIMGZoom.style.cursor = 'hand';
			else
				objIMGZoom.style.cursor = 'pointer';
			objIMGZoom.style.zIndex = 1;
			objIMGZoom.style.MozUserSelect="none";
			objIMGZoom.onclick = function()
			{
				var objdivMenuZoom = document.getElementById(divMenuZoomId);
				if(objdivMenuZoom)
					objdivMenuZoom.style.display='';
				return false
			};
			objIMGDIV2.appendChild(objIMGZoom);
			
			objDiv.appendChild(objIMGDIV2);
			
			var objdivMenuZoom = document.getElementById(_divMenuZoomId);
			if(objdivMenuZoom)
			{
				off = GetTotalOffset(objIMGZoom);
				objdivMenuZoom.style.display = "";
				objdivMenuZoom.style.left = off.Left+18-objdivMenuZoom.clientWidth;
				objdivMenuZoom.style.top = off.Top+15;
				objdivMenuZoom.style.display = "none";
				divMenuZoomId = _divMenuZoomId;
			}
			if(_hdnZoomTypeId!='undefined')
				hdnZoomTypeId = _hdnZoomTypeId;
		}
	}
}

var iDivTimerSchedAdd = -1;
function PrepareClosing()
{
	iDivTimerSchedAdd = window.setInterval(closeZoomMenu, 1000);
}

function CancelClosing()
{
	if(iDivTimerSchedAdd>=0)
	{
		window.clearInterval(iDivTimerSchedAdd);
		iDivTimerSchedAdd = -1;
	}
}
	
function closeZoomMenu()
{
	document.getElementById(divMenuZoomId).style.display = "none";
}

function GetTotalOffset(eSrc)
{
	this.Top = 0;
	this.Left = 0;
	while (eSrc)
	{
		this.Top += eSrc.offsetTop;
		this.Left += eSrc.offsetLeft;
		eSrc = eSrc.offsetParent;
	}
	return this;
}

function _Zoom(type, koeff)
{
	if(zoomValue==type){
		closeZoomMenu();
		return;
	}
	var hdnZoomType = document.getElementById(hdnZoomTypeId);
	if(hdnZoomType)
		hdnZoomType.value = type.toString();
	var _leftX = Math.round(leftX*koeff);
	for(i=0;i<_imgArray.length;i++)
		for(j=0;j<_imgArray[i].length;j++)
		{
			var objImg = _imgArray[i][j];
			var oldPath = objImg.src;
			var _s = oldPath.substring(oldPath.indexOf("&x="),oldPath.indexOf("&y="));
			var _i = parseInt(_s.substr(3));
			objImg.coordX = _leftX + _i - leftX;
			objImg.attributes["coordX"].value = _leftX + _i - leftX;
			oldPath = oldPath.replace(_s, "&x="+(_leftX + _i - leftX).toString());
			oldPath = oldPath.replace("?Type="+zoomValue.toString(),"?Type="+type.toString());
			globalLink = globalLink.replace("?Type="+zoomValue.toString(),"?Type="+type.toString());
			oldPath = oldPath.substr(0, oldPath.indexOf("&idt="));
			var d = new Date();
			var sUniquePart = "&idt=" + d.getHours() + d.getMinutes() + d.getSeconds();
			objImg.src= oldPath + sUniquePart;
		}
	leftX = _leftX;
	zoomValue = type;
	closeZoomMenu();
	return false;
}

function _resizing(sName)
{
	var objDiv = document.getElementById(sName);
	iWidth = objDiv.clientWidth;
	if(objDiv.parentNode.clientWidth>0){iWidth = objDiv.parentNode.clientWidth;}
	var iOldHorizCount = iHorizCount;
	iHorizCount = Math.floor(iWidth/_constValue) + 3;
	var remArray = new Array();
	if(iOldHorizCount-iHorizCount>0){iHorizCount = iOldHorizCount;}
	else if(iOldHorizCount-iHorizCount<0)
	{
		var objDivCont = document.getElementById('divCont');
		for(var i=leftX + iOldHorizCount; i<leftX + iHorizCount; i++)
			for(var j=0;j<iVertCount;j++)
			{
				var iLeft = i*_constValue + _sumOffset;
				var iTop = j*_constValue;
				var objIMG = document.createElement("img");	
				objIMG.style.position="absolute";
				objIMG.style.left=iLeft.toString()+"px";
				objIMG.style.top=iTop.toString()+"px";
				objIMG.style.width = _constValue.toString()+"px";
				objIMG.style.height = _constValue.toString()+"px";
				objIMG.style.border="0";
				objIMG.style.padding="0";
				objIMG.style.margin="0";
				objIMG.style.zIndex = 0;
				objIMG.width = _constValue.toString();
				objIMG.height = _constValue.toString();
				objIMG.setAttribute("coordX",i);
				objIMG.setAttribute("coordY",j);
				objIMG.setAttribute("unselectable","on");
				objIMG.setAttribute("galleryImg","no");
				objIMG.setAttribute("ganttChart","yes");
				var d = new Date();
				var sPath = "&x="+i+"&y="+j+"&lx="+leftX.toString()+"&idt=" + d.getHours() + d.getMinutes() + d.getSeconds();
				objIMG.src= globalLink + sPath;
				objDivCont.appendChild(objIMG);
			}
	}
	var objdivImgZoom = document.getElementById('divZoom');
	if(objdivImgZoom)
	{
		objdivImgZoom.style.left = (iWidth - 20).toString() + "px";
		var objdivMenuZoom = document.getElementById(divMenuZoomId);
		if(objdivMenuZoom)
		{
			objIMGZoom = document.getElementById('divZoomImg');
			if(objIMGZoom)
			{
				off = GetTotalOffset(objIMGZoom);
				objdivMenuZoom.style.display = "";
				objdivMenuZoom.style.left = off.Left+18-objdivMenuZoom.clientWidth;
				objdivMenuZoom.style.top = off.Top+15;
				objdivMenuZoom.style.display = "none";
			}
		}
	}
}

function setFlag(e, sName)
{
	e = (e)? e : ((window.event)?window.event : null);
	_mousedown=1;
	var objDiv = document.getElementById(sName);
	objDiv.style.cursor='w-resize';
	setX=e.clientX;
}

function unSetFlag(sName)
{
	_mousedown=0;
	var objDiv = document.getElementById(sName);
	if(browseris.ie)
		objDiv.style.cursor='hand';
	else
		objDiv.style.cursor='pointer';
}

function _moving(e, sName)
{
	e = (e)? e : ((window.event)?window.event : null);
	var objDiv = document.getElementById(sName);
	if(_mousedown==1)
	{
		var _curOffset = e.clientX - setX;
		_sumOffset += _curOffset;
		setX = e.clientX;
		var _left, _right, min_value, max_value;
		min_value = 0;
		max_value = 0;
		var fl = false;
		for(i=0;i<_imgArray.length;i++)
			for(j=0;j<_imgArray[i].length;j++)
			{
				var objImg = _imgArray[i][j];
				_left=_curOffset + parseInt(objImg.style.left.substring(0,objImg.style.left.length-2));
				_right=_left+_constValue;
				objImg.style.left = _left + "px";
				if(_curOffset<0) //to left
				{
					if(_left<=-Math.floor(_constValue/2))
					{
						objImg.src= "../layouts/images/BLANK.GIF";
						if(!fl)
						{
							leftX = leftX + 1;
							fl = true;
						}
						
						objImg.coordX = leftX + iHorizCount - 1;
						objImg.coordY = objImg.attributes["coordY"].value;
						
						objImg.attributes["coordX"].value = leftX + iHorizCount - 1;
						
						var d = new Date();
						var sPath = "&x="+objImg.coordX+"&y="+objImg.coordY+"&lx="+leftX.toString()+"&idt=" + d.getHours() + d.getMinutes() + d.getSeconds();
						objImg.src= globalLink + sPath;
						
						objImg.style.left = (_left+_constValue*iHorizCount).toString() + "px";
					}
				}
				else if(_curOffset>0) //to right
				{
					if(_right>=_constValue*(iHorizCount+1)-Math.floor(_constValue/2))
					{
						objImg.src= "../layouts/images/BLANK.GIF";
						if(!fl)
						{
							leftX = leftX - 1;
							fl = true;
						}
						objImg.coordX = leftX;
						objImg.coordY = objImg.attributes["coordY"].value;
						
						objImg.attributes["coordX"].value = leftX;
						
						objImg.style.left = (_left-_constValue*iHorizCount).toString() + "px";
						var d = new Date();
						var sPath = "&x="+objImg.coordX+"&y="+objImg.coordY+"&lx="+leftX.toString()+"&idt=" + d.getHours() + d.getMinutes() + d.getSeconds();
						objImg.src= globalLink + sPath;
					}
				}
			}
	}
}
