var zInterval;
var zStep;
var zAcceleration;
var zSpeed;
var zTopSpeed = 30;
var zStartW;
var zStartH;
var zEndW;
var zEndH;
var zSpeedK;
var zImages = new Array();
/*
function setElementOpacity(sElemId, nOpacity)
{
	var opacityProp = getOpacityProperty();
	var elem = document.getElementById(sElemId);

	if (!elem || !opacityProp) return;
    
	if (opacityProp=="filter")
	{
		nOpacity *= 100;
	
		var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
		if (oAlpha)
			oAlpha.opacity = nOpacity;
		else
			elem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";
	}
	else
		elem.style[opacityProp] = nOpacity;
}

function getOpacityProperty()
{
	if (typeof document.body.style.opacity == 'string') // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9, IE7)
		return 'opacity';
	else
	if (typeof document.body.style.MozOpacity == 'string') // Mozilla 1.6 и младше, Firefox 0.8 
		return 'MozOpacity';
	else
	if (typeof document.body.style.KhtmlOpacity == 'string') // Konqueror 3.1, Safari 1.1
		return 'KhtmlOpacity';
	else
	if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) // Internet Exploder 5.5+
		return 'filter';

	return false;
}
*/
function zHideZoom(sW,sH)
{
	var zoomBlock = document.getElementById('zoomBlockZ');
	zoomBlock.style.display = 'none';
	zoomBlock.style.visibility = 'hidden';
	var fsPhoto = document.getElementById('fullScreenPhotoBlockZ');
	fsPhoto.style.width = sW + 'px';
	fsPhoto.style.height = sH + 'px';
	fsPhoto.style.marginLeft = Math.round((document.documentElement.clientWidth - sW)/2) + 'px';
	fsPhoto.style.marginTop = Math.round((document.documentElement.clientHeight - sH)/2) -(-(document.documentElement.scrollTop||document.body.scrollTop)) + 'px';
	fsPhoto.style.display = 'none';
	fsPhoto.style.visibility = 'hidden';

	var fsImage = document.getElementById('fsImageZ');
	fsImage.style.height = sH + 'px';
	fsImage.style.width = sW + 'px';
	fsImage.style.display = 'none';
	fsImage.style.visibility = 'hidden';
	
	delta = new Array(0,4,6,8,8);
	
	for (i = 1; i <= 5; i++)
	{
		nH = sH + delta[i-1];
		document.getElementById('leftZ_'+i).style.height = nH + 'px';
		document.getElementById('rightZ_'+(6-i)).style.height = nH + 'px';
	}
	
	for (i = 1; i <= 5; i++)
	{
		document.getElementById('rightZ_'+i).style.marginLeft = sW -(-i) -1 + 'px';
	}
	
	document.getElementById('topZ').style.width = sW + 'px';
	document.getElementById('bottomZ').style.width = sW + 'px';

	document.getElementById('bottomZ').style.marginTop = sW + 'px';

	document.getElementById('pointZ_2').style.marginLeft = sW -1 + 'px';
	document.getElementById('pointZ_3').style.marginTop = sW -1 + 'px';
	document.getElementById('pointZ_4').style.marginLeft = sW -1 + 'px';
	document.getElementById('pointZ_4').style.marginTop = sW -1 + 'px';
}

function zZoomImage(photoPath)
{
	var zoomBlock = document.getElementById('zoomBlockZ');
	zoomBlock.style.display = 'block';
	zoomBlock.style.visibility = 'visible';
	zoomBlock.style.height = '1px';
	zoomBlock.style.width = '1px';
	bodyHeight = document.body.offsetHeight;
	if (bodyHeight < document.documentElement.offsetHeight)
	{
		bodyHeight = document.documentElement.offsetHeight;
	}
	if (bodyHeight < document.documentElement.clientHeight)
	{
		bodyHeight = document.documentElement.clientHeight;
	}
	if (bodyHeight < document.body.offsetHeight)
	{
		bodyHeight = document.body.offsetHeight;
	}
	if (bodyHeight < document.body.clientHeight)
	{
		bodyHeight = document.body.clientHeight;
	}
	if (bodyHeight < document.documentElement.scrollHeight)
	{
		bodyHeight = document.documentElement.scrollHeight;
	}
	if (bodyHeight < document.body.scrollHeight)
	{
		bodyHeight = document.body.scrollHeight;
	}
	zoomBlock.style.height = bodyHeight + 'px';
	zoomBlock.style.width = document.documentElement.clientWidth + 'px';
	setElementOpacity('zoomBlockZ',0.8);

	var fsPhoto = document.getElementById('fullScreenPhotoBlockZ');
	fsPhoto.style.display = 'block';
	fsPhoto.style.visibility = 'visible';
	sW = fsPhoto.offsetWidth;
	sH = fsPhoto.offsetHeight
	fsPhoto.style.marginLeft = Math.round((document.documentElement.clientWidth - sW)/2) + 'px';
	fsPhoto.style.marginTop = Math.round((document.documentElement.clientHeight - sH)/2) -(-(document.documentElement.scrollTop||document.body.scrollTop)) + 'px';
	
	var fsImage = new Image();
	
	fsImage.onload = function()
	{
		document.getElementById('fsImageZ').src = fsImage.src;
		document.getElementById('fsImageZ').style.display = 'block';
		document.getElementById('fsImageZ').style.visibility = 'visible';
		zAnimateImage(sW,sH,fsImage.width,fsImage.height);
	}
	
//	var curPhotoNum = eval('photoNumZ');
	fsImage.src = photoPath;//+id+'/fullscreen_'+eval('gallery'+id+'['+curPhotoNum+'][\'filename\']');
//	document.getElementById('fsImage'+id).alt = eval('gallery'+id+'['+curPhotoNum+'][\'caption\']');
//	document.getElementById('fsImage'+id).title = eval('gallery'+id+'['+curPhotoNum+'][\'caption\']');
}

function zAnimateImage(sW,sH,newWidth,newHeight)
{
	zStep = sW;
	zEndW = newWidth;
	zEndH = newHeight;
	zAcceleration = 1;
	zSpeed = 0;
	zSpeedK = (newHeight - sH)/(newWidth-sW);
	
	zInterval = setInterval('zAnimateStep()',50);
}

function zAnimateStep()
{
	var fsImage = document.getElementById('fsImageZ');
	var fsPhoto = document.getElementById('fullScreenPhotoBlockZ');
	var zoomBlock = document.getElementById('zoomBlockZ');
	
	zSpeed += zAcceleration;
	if (zSpeed > zTopSpeed)
	{
		zSpeed > zTopSpeed;
	}

	if (zEndW - zStep <= zSpeed*zSpeed/(6*zAcceleration))
	{
		zAcceleration = -zAcceleration;
	}
	
	zStep += zStep;
	
	if (zAcceleration < 0 && zSpeed == 0)
	{
		zStep = zEndW;
	}
	
	if (zStep >= zEndW)
	{
		zStep = zEndW;
		clearInterval(zInterval);
		zInterval = null;
		zAcceleration = 0;
		zSpeed = 0;
	}

	newMarginLeft = Math.round((document.documentElement.clientWidth - zStep)/2);
	if (newMarginLeft < 0)
	{
		newMarginLeft = 30;
	}
	newMarginTop = Math.round((document.documentElement.clientHeight - zStep*zSpeedK)/2 -(-(document.documentElement.scrollTop||document.body.scrollTop)));
	if (newMarginTop - (document.documentElement.scrollTop||document.body.scrollTop) < 0)
	{
		newMarginTop = 30 + (document.documentElement.scrollTop||document.body.scrollTop);
	}
	
	if (zoomBlock.offsetHeight < (newMarginTop+zStep*zSpeedK+30))
	{
		zoomBlock.style.height = newMarginTop - (-zStep*zSpeedK) -(-30) + 'px';
	}
	
	if (zoomBlock.offsetWidth < (newMarginLeft+zStep-1+30))
	{
		zoomBlock.style.width = newMarginLeft - (-zStep) -(-30) + 'px';
	}
	
	fsPhoto.style.width = zStep + 'px';
	fsPhoto.style.height = Math.round(zStep*zSpeedK) + 'px';

	fsImage.style.width = zStep + 'px';
	fsImage.style.height = Math.round(zStep*zSpeedK) + 'px';
	
	fsPhoto.style.marginLeft = newMarginLeft + 'px';
	fsPhoto.style.marginTop = newMarginTop + 'px';
	
	delta = new Array(0,4,6,8,8);

	for (i = 1; i <= 5; i++)
	{
		nH = zStep*zSpeedK + delta[i-1];
		document.getElementById('leftZ_'+i).style.height = Math.round(nH) + 'px';
		document.getElementById('rightZ_'+(6-i)).style.height = Math.round(nH) + 'px';
	}
	
	for (i = 1; i <= 5; i++)
	{
		document.getElementById('rightZ_'+i).style.marginLeft = zStep -(-i+1) + 'px';
	}
	
	document.getElementById('topZ').style.width =  zStep + 'px';
	document.getElementById('bottomZ').style.width =  zStep + 'px';

	document.getElementById('bottomZ').style.marginTop = Math.round(zStep*zSpeedK) + 'px';

	document.getElementById('pointZ_2').style.marginLeft =  zStep -1 + 'px';
	document.getElementById('pointZ_3').style.marginTop = Math.round(zStep*zSpeedK) -1 + 'px';
	document.getElementById('pointZ_4').style.marginLeft =  zStep -1 + 'px';
	document.getElementById('pointZ_4').style.marginTop = Math.round(zStep*zSpeedK) -1 + 'px';
	
	if (zStep == zEndW)
	{
		fsPhoto.style.width = zEndW + 'px';
		fsPhoto.style.height = zEndH + 'px';
		fsImage.style.width = zEndW + 'px';
		fsImage.style.height = zEndH + 'px';
		document.getElementById('bottomZ').style.marginTop = zEndH + 'px';
		document.getElementById('pointZ_3').style.marginTop = zEndH -1 + 'px';
		document.getElementById('pointZ_4').style.marginTop = zEndH -1 + 'px';

		for (i = 1; i <= 5; i++)
		{
			nH = zEndH + delta[i-1];
			document.getElementById('leftZ_'+i).style.height = Math.round(nH) + 'px';
			document.getElementById('rightZ_'+(6-i)).style.height = Math.round(nH) + 'px';
		}
	}
}

function zInit()
{
	document.write('<div id="zoomBlockZ" class="zoomBlock" onclick="zHideZoom(50,50)"></div><div id="fullScreenPhotoBlockZ" class="fullScreenPhotoBlock" style="width: 50px; height: 50px;"><div class="close" onclick="zHideZoom(50,50)">&times; закрыть</div><b id="topZ" class="horizontal" style="margin-top: -5px; width: 50px;"></b><b id="leftZ_1" class="vertical" style="margin-left: -5px; margin-top: 0px; height: 50px;"></b><b id="leftZ_2" class="vertical" style="margin-left: -4px; margin-top: -2px; height: 54px;"></b><b id="leftZ_3" class="vertical" style="margin-left: -3px; margin-top: -3px; height: 56px;"></b><b id="leftZ_4" class="vertical" style="margin-left: -2px; margin-top: -4px; height: 58px;"></b><b id="leftZ_5" class="vertical" style="margin-left: -1px; margin-top: -4px; height: 58px;"></b><b id="rightZ_1" class="vertical" style="margin-left: 50px; margin-top: -4px; height: 58px;"></b><b id="rightZ_2" class="vertical" style="margin-left: 51px; margin-top: -4px; height: 58px;"></b><b id="rightZ_3" class="vertical" style="margin-left: 52px; margin-top: -3px; height: 56px;"></b><b id="rightZ_4" class="vertical" style="margin-left: 53px; margin-top: -2px; height: 54px;"></b><b id="rightZ_5" class="vertical" style="margin-left: 54px; margin-top: 0px; height: 50px;"></b><b id="pointZ_1" class="point" style="margin-left: 0; margin-top: 0;"></b><b id="pointZ_2" class="point" style="margin-left: 49px; margin-top: 0;"></b><b id="pointZ_3" class="point" style="margin-left: 0; margin-top: 49px;"></b><b id="pointZ_4" class="point" style="margin-left: 49px; margin-top: 49px;"></b><b id="bottomZ" class="horizontal" style="margin-top: 50px; width: 50px;"></b><img id="fsImageZ" src="" alt="" title="" style="width: 50px; height: 50px;" onclick="zHideZoom(50,50)"></div>');
	
	zImages = document.body.getElementsByTagName('img');
	for (var i = 0; i < zImages.length; i++)
	{
		if (zImages[i].id.substr(0,7) != 'fsImage' && zImages[i].onclick == null)
		{
			zImages[i].style.cursor = "pointer";
			zImages[i].onclick = function (e) {
				var image = window.event ? window.event.srcElement : e.target;
				zZoomImage(image.src);
			}
		}
	}
}

