﻿function getScrollBarWidth()
{
	var objDiv = document.createElement('DIV');
	if (objDiv)
	{
		objDiv.style.position = 'absolute';
		objDiv.style.left = objDiv.style.top = '-1000px';
		objDiv.style.width = objDiv.style.height = '100px';
		objDiv.style.overflow = 'scroll';
		objDiv.style.visibility = 'hidden';
		if (document.body)
		{
			document.body.appendChild(objDiv);

			if (objDiv && objDiv.offsetWidth && objDiv.clientWidth)
			{
				scrollBarWidth = objDiv.offsetWidth - objDiv.clientWidth;
			}
		}
	}
	if (objDiv)
	{
		objDiv.parentNode.removeChild(objDiv);
	}

	return scrollBarWidth;
}

var scrollBarWidth = getScrollBarWidth();

function getInnerWidth()
{
	return isIE ? ((document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth) : ((document.body.scrollHeight > window.innerHeight) ? window.innerWidth - scrollBarWidth : window.innerWidth);
};

function getFullInnerWidth()
{
	return isIE ? ((document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth) : window.innerWidth;
};

function getInnerHeight()
{
	return isIE ? ((document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight) : ((document.body.scrollWidth > window.innerWidth) ? window.innerHeight - scrollBarWidth : window.innerHeight);
};

function getFullInnerHeight()
{
	return isIE ? ((document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight) : window.innerHeight;
};

function getDocHeight()
{
	return Math.max
  (
    Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
    Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
    Math.max(document.body.clientHeight, document.documentElement.clientHeight)
  );
}

function getDocWidth()
{
	return Math.max
  (
    Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),
    Math.max(document.body.offsetWidth, document.documentElement.offsetWidth),
    Math.max(document.body.clientWidth, document.documentElement.clientWidth)
  );
}

window.currentXScrollPosition = 0;

function showScroll()
{
	if (isFF2) return;
	$T('HTML')[0].style.overflowY = 'scroll';
	document.body.style.marginRight = '0px';
	document.body.scrollTop = document.documentElement.scrollTop = window.currentXScrollPosition;
}

function hideScroll()
{
	if (isFF2) return;
	window.currentXScrollPosition = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
	$T('HTML')[0].style.overflowY = 'hidden';
	document.body.style.marginRight = scrollBarWidth + 'px';
	document.body.scrollTop = document.documentElement.scrollTop = window.currentXScrollPosition;
}

function setOpacity(el, op)
{
	if (isIE)
	{
		el.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + op + ')';
	}
	else
	{
		el.style.MozOpacity = op / 100;
		el.style.KhtmlOpacity = op / 100;
		el.style.opacity = op / 100;
	}
}

window.setTimeoutForSlideOpacity = new Array();
window.currentOpacity = new Array();

function slideOpacity(el, op1, op2, takeCurrentOpacity, msec, step, clear)
{

	if (typeof (el) == "string")
	{
		el = $I(el);
	}

	if (!el.id)
	{
		el.id = "idOpacitySlider" + parseInt(Math.random() * 16);
	}

	if (isNaN(msec))
	{
		msec = 3;
	}

	if (isNaN(step))
	{
		step = 1;
	}

	if (clear !== false)
	{
		clear = true;
	}

	if (takeCurrentOpacity && window.currentOpacity[el.id])
	{
		op1 = window.currentOpacity[el.id];
	}

	diff = op1 < op2 ? parseInt(step) : -1 * parseInt(step);

	var stepOpacity = function ()
	{

		op1 += diff;
		setOpacity(el, op1);
		window.currentOpacity[el.id] = op1;

		if (el.style.visibility == 'hidden')
		{
			return;
		}

		if (window.setTimeoutForSlideOpacity[el.id]) clearTimeout(window.setTimeoutForSlideOpacity[el.id]);

		if (op1 != op2) window.setTimeoutForSlideOpacity[el.id] = setTimeout(stepOpacity, msec);
	}

	if (window.setTimeoutForSlideOpacity[el.id])
	{
		clearTimeout(window.setTimeoutForSlideOpacity[el.id]);
	}

	if (op1 == 0 && clear) toggleElement(el, true);
	stepOpacity();
	if (op2 == 0 && clear) toggleElement(el, false);
}

var shadingDiv = $I('shadingDiv');
var waitingDiv = $I('waitingDiv');
var popupDiv = $I('popupDiv');
var popupTable = $I('popupTable');
var popupImage = $I('popupImage');
var popupHolder = $I('popupHolder');
var popupOuter = $I('popupOuter');
var popupCloseButton = $I('popupCloseButton');
var popupCloseButtonHolder = $I('popupCloseButtonHolder');
var popupLinkPrevImage = $I('popupLinkPrevImage');
var popupLinkNextImage = $I('popupLinkNextImage');
var spaceImage = $I('spaceImage');

var showNavigation = (popupLinkPrevImage != undefined && popupLinkNextImage != undefined);

var borderWidth = 2;
var popupPadding = 30;
var horizontalPadding = showNavigation ? 60 : popupPadding;
var verticalPadding = popupPadding;

var defaultOpacity = 70;
var highlightedOpacity = 100;

var popupHolderWidth;
var popupHolderHeight;
var popupHolderSizeTM;
var popupHolderCounter;

if (showNavigation)
{
	popupLinkPrevImage.onmouseover = function () { setOpacity(this, highlightedOpacity); }
	popupLinkPrevImage.onmouseout = function () { setOpacity(this, defaultOpacity); }

	popupLinkNextImage.onmouseover = function () { setOpacity(this, highlightedOpacity); }
	popupLinkNextImage.onmouseout = function () { setOpacity(this, defaultOpacity); }

	setOpacity(popupLinkPrevImage, defaultOpacity);
	setOpacity(popupLinkNextImage, defaultOpacity);
}

popupCloseButton.onmouseover = function () { setOpacity(this, highlightedOpacity); }
popupCloseButton.onmouseout = function () { setOpacity(this, defaultOpacity); }

setOpacity(popupCloseButton, defaultOpacity);
setOpacity(shadingDiv, defaultOpacity);

function calcPopupSizes()
{
	myInnerWidth = Math.max((isFF2 ? getInnerWidth() : getFullInnerWidth()), horizontalPadding * 2 + borderWidth * 2);
	myInnerHeight = Math.max((isFF2 ? getInnerHeight() : getFullInnerHeight()), verticalPadding * 2 + borderWidth * 2);

	shadingDiv.style.width = myInnerWidth + "px";
	shadingDiv.style.height = myInnerHeight + "px";

	popupDiv.style.width = myInnerWidth + "px";
	popupDiv.style.height = myInnerHeight + "px";

	popupCloseButtonHolderMaxWidth = myInnerWidth - horizontalPadding * 2;
	popupCloseButtonHolderMaxHeight = myInnerHeight - verticalPadding * 2;

	popupHolderMaxWidth = popupCloseButtonHolderMaxWidth - borderWidth * 2 - popupPadding * 2;
	popupHolderMaxHeight = popupCloseButtonHolderMaxHeight - borderWidth * 2 - popupPadding * 2;

	popupCloseButtonHolder.style.maxHeight = popupCloseButtonHolderMaxHeight + "px";
	popupCloseButtonHolder.style.maxWidth = popupCloseButtonHolderMaxWidth + "px";

	popupHolder.style.maxHeight = popupHolderMaxHeight + "px";
	popupHolder.style.maxWidth = popupHolderMaxWidth + "px";
}

function hidePopup()
{
	window.onresize = function () { };
	showScroll();

	toggleElement(waitingDiv, false);
	toggleElement(shadingDiv, false);
	toggleElement(popupDiv, false);
	toggleElement(popupCloseButton, false);
	toggleElement(spaceImage, true);

	popupHolder.style.overflow = 'hidden';
	popupImage.src = '';
}

function showPopup(imageSrc)
{
	hideScroll();
	calcPopupSizes();
	window.onresize = function () { calcPopupSizes(); };
	setOpacity(waitingDiv, 37);
	popupImage.src = imageSrc;

	popupImage.onload = function ()
	{
		toggleElement(spaceImage, false);
		toggleElement(waitingDiv, false);
		popupHolder.style.overflow = 'auto';
	}
    
    document.onkeypress = function(e)
    {
        if (!e && event) e = event;
        if (!e) return;
        if (e.keyCode == 27)
        {
            document.onkeypress = '';
            hidePopup();
        }
    }

	toggleElement(waitingDiv, true);
	toggleElement(popupCloseButton, true);

	if (isIE6)
	{
		size = getImageSize(imageSrc);

		if (size)
		{
			ie6ImageHeight = (Math.min(size[2], popupHolderMaxHeight - scrollBarWidth) + scrollBarWidth + borderWidth * 2);
			ie6ImageWidth = (Math.min(size[1], popupHolderMaxWidth - scrollBarWidth) + scrollBarWidth + borderWidth * 2);
			popupHolder.style.height = ie6ImageHeight + "px";
			popupHolder.style.width = ie6ImageWidth + "px";
			waitingDiv.style.height = (ie6ImageHeight + 2 * popupPadding) + "px";
			waitingDiv.style.width = (ie6ImageWidth + 2 * popupPadding) + "px";
		}

		shadingDiv.style.top = document.documentElement.scrollTop + "px";
		shadingDiv.style.left = document.documentElement.scrollLeft + "px";
		popupDiv.style.top = document.documentElement.scrollTop + "px";
		popupDiv.style.left = document.documentElement.scrollLeft + "px";

		popupHolder.scrollTop = 0;
	}

	toggleElement(shadingDiv, true);
	toggleElement(popupDiv, true);

}

