var TIMER = 5;
var SPEEDMASK = 4;
var SPEEDPHOTO = 50;
var address;
var newimg = new Image;

window.onload = function() { childs = document.body.childNodes }

document.onkeydown = function getEsc (e)
{
	if (window.event)
		value = window.event.keyCode;
	else if (e)
		value = e.which;
	if (value == 27)
		hidePhoto ();
}

function pageWidth ()
{
	return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

function pageHeight ()
{
	return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

function topPosition ()
{
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function leftPosition ()
{
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function photoViewer (item)
{
	var photost;
	var photostmask;
	var photostclose;
	address = item;

	if (!document.getElementById ('photost'))
	{
		photost = document.createElement ('div');
		photost.id = 'photost';
		photostclose = document.createElement ('div');
		photostclose.id = 'photostclose'
		photostclose.setAttribute ('onclick','hidePhoto ()');
		photostclose.onclick = hidePhoto;
		photostimg = document.createElement ('img');
		photostimg.id = 'photostimg'
		photostimg.setAttribute ('src', '/img/loading.gif');
		photostmask = document.createElement ('div');
		photostmask.id = 'photostmask';
		photostmask.onclick = hidePhoto;
		photostmask.setAttribute ('onclick','hidePhoto ()');
		document.body.appendChild (photostmask);
		document.body.appendChild (photost);
		photost.appendChild (photostclose);
		photost.appendChild (photostimg);
	}
	else
	{
		photost = document.getElementById ('photost');
		photostclose = document.getElementById ('photostclose');
		photostimg = document.getElementById ('photostimg');
		newimg.src = "/img/loading.gif";
		photostimg.src = "/img/loading.gif";
		photostimg.style.width = newimg.width;
		photostimg.style.height = newimg.height;
		photostimg.style.margin = "21px auto 0 auto";
		photostmask = document.getElementById ('photostmask');
		photostmask.style.visibility = "visible";
	}
	photost.style.width = "100px";
	photost.style.height = "100px";

	photostmask.style.opacity = .00;
	photostmask.style.filter = 'alpha (opacity=0)';
	photostmask.alpha = 0;

	photost.style.top = topPosition () + (pageHeight () / 4) - (photost.offsetHeight / 2) + "px";
	photost.style.left = leftPosition () + (pageWidth () / 2) - (photost.offsetWidth / 2) + "px";

	photostmask.style.height = 0;
	var tempheight = 0;
	for (i = 0; i < childs.length; i ++)
		if (childs[i].offsetHeight)
		{
			childs2 = childs[i].childNodes;
			break;
		}

	for (i = 0; i < childs2.length; i ++)
		if (childs2[i].offsetHeight)
			tempheight += childs2[i].offsetHeight;

	photostmask.style.height = tempheight + "px";
	timer = setInterval ("fadeMask (1)", TIMER);

	return false;
}

function fadeMask (flag)
{
	var photostmask = document.getElementById ('photostmask');
	var value;

	if (flag == 1)
	{
		value = photostmask.alpha + SPEEDMASK;
	}
	else
		value = photostmask.alpha - SPEEDMASK;
	photostmask.alpha = value;
	photostmask.style.opacity = (value / 100);
	photostmask.style.filter = 'alpha (opacity=' + value + ')';
	if (value >= 75)
	{
		clearInterval (timer);
		timer = null;
		loadPhoto ();
	}
	else if (value <= 1)
	{
		photostmask.style.visibility = "hidden";
		document.getElementById ('photostmask').style.visibility = "hidden";
		clearInterval (timer);
	}
}

function loadPhoto ()
{
	var now = new Date();
	var photost = document.getElementById ('photost');

	photost.style.visibility = "visible";
	newimg.onload = function () { timer = setInterval ("heightPhoto (newimg.height)", TIMER) };
	newimg.src = address;
}

function heightPhoto (imgHeight)
{
	var photost = document.getElementById ('photost');
	var photostimg = document.getElementById ('photostimg');
	var value;

	value = parseInt (photost.style.height.substring (0, photost.style.height.length - 2)) + Math.round (imgHeight / SPEEDPHOTO);
	photost.style.height = value + "px";

	if (value >= imgHeight + 26)
	{
		photost.style.height = imgHeight + 26 + "px";
		clearInterval (timer);
		timer = null;
		setTimeout ("timer = setInterval ('widthPhoto (newimg.width)', TIMER)", 300);
	}
}

function widthPhoto (imgWidth)
{
	var photost = document.getElementById ('photost');
	var photostimg = document.getElementById ('photostimg');
	var value;

	photost.style.visibility = "visible";
	value = parseInt (photost.style.width.substring (0, photost.style.width.length - 2)) + Math.round (imgWidth / SPEEDPHOTO);
	photost.style.width = value + "px";
	photost.style.left = parseInt (photost.style.left.substring (0, photost.style.left.length - 2)) - Math.round (imgWidth / SPEEDPHOTO) / 2 + "px";

	if (value >= imgWidth)
	{
		photost.style.width = imgWidth + 20 + "px";
		clearInterval (timer);
		timer = null;
		photostimg.style.opacity = .00;
		photostimg.style.filter = 'alpha (opacity=0)';
		photostimg.alpha = 0;
		photostimg.src = address;
		photostimg.style.width = newimg.width;
		photostimg.style.height = newimg.height;
		photostimg.style.margin = "0 auto 0 auto";
		timer = setInterval ("fadePhoto ()", TIMER)
	}
}

function fadePhoto ()
{
	var photostimg = document.getElementById ('photostimg');
	var value;

	value = photostimg.alpha + SPEEDMASK;
	photostimg.alpha = value;
	photostimg.style.opacity = (value / 100);
	photostimg.style.filter = 'alpha (opacity=' + value + ')';
	if (value >= 99)
	{
		clearInterval (timer);
		timer = null;
	}
}

function hidePhoto ()
{
	var photost = document.getElementById ('photost');
	var photostimg = document.getElementById ('photostimg');

	newimg.onload = "";
	if (typeof timer != "undefined")
	{
		clearInterval (timer);
		photost.style.visibility = "hidden";
		timer = setInterval ("fadeMask (0)", TIMER);
	}
}

