
var loadingImage = '../gfx/loading.gif';				

function getPageScroll()
{
	var yScroll;

	if (self.pageYOffset) 
	{
		yScroll = self.pageYOffset;
	} 
	else if (document.documentElement && document.documentElement.scrollTop)
	{	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} 
	else if (document.body) 
	{// all other Explorers
		yScroll = document.body.scrollTop;
	}

	return yScroll;
}

function getPageSize()
{	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) 
	{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
		
	} 
	else 
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) 
	{	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) 
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) 
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	} 
	else 
	{ 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
	{	
		pageWidth = windowWidth;
	} 
	else 
	{
		pageWidth = xScroll;
	}
	/*
	document.writeln("width:" + pageWidth);
	document.writeln("height:" + pageHeight);
	*/
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	
	return arrayPageSize;
}

function pause(numberMillis) 
{
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) 
	{
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

function getKey(e)
{
	if (e == null) 
	{ // ie
		keycode = event.keyCode;
	} 
	else 
	{ // mozilla
		keycode = e.which;
	}
	//key = String.fromCharCode(keycode).toLowerCase();
	if(keycode == 0)
	{ 
		hideLightbox(); 
	}
}

function listenKey () 
{	
	document.onkeypress = getKey; 
}

function showLightbox_html(objLink)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var img = document.getElementById('imgobj');
	objLightbox.style.display = 'none';
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	var imgTop = arrayPageScroll + ((arrayPageSize[3] - 35 - 22 ) / 2);
	var imgLeft = ((arrayPageSize[0] - 20 - 126) / 2);
	/*
	document.writeln("top:" + imgTop);
	document.writeln("left:" + imgLeft);
	*/
	img.style.top = (imgTop < 0) ? "0px" : imgTop + "px";
	img.style.left = (imgLeft < 0) ? "0px" : imgLeft + "px";
	img.style.display = 'block';
	
	// preload image
	var req = mint.Request();
	req.OnSuccess =
		function()
		{
			if (objOverlay.style.display != 'none')
			{
				img.style.display = 'none';
				var lightboxTop = arrayPageScroll + ((arrayPageSize[3] - 35 - 400 ) / 2);
				var lightboxLeft = ((arrayPageSize[0] - 20 - 737) / 2);
				
				objLightbox.style.top 		= (lightboxTop < 0) ? "0px" : lightboxTop + "px";
				objLightbox.style.left 		= (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
				objLightbox.style.height 	= '420px';
				objLightbox.style.width 	= '750px';
				
				if (navigator.appVersion.indexOf("MSIE")!=-1)
				{
					pause(400);
				} 
				
				selects = document.getElementsByTagName("select");
				for (i = 0; i != selects.length; i++) 
				{
					selects[i].style.visibility = "hidden";
				}
				
				el_tmp = document.createElement("div");
				el_tmp.innetHTML =  this.responseText;
				
				fotosy = el_tmp.getElementsByTagName("img");
				objLightbox.innerHTML = this.responseText;

				objLightbox.style.display = 'block';
				//objOverlay.style.width = '100%';
				//objOverlay.style.height = '100%';
				initLightbox();
				listenKey();
			}
			else
			{
				alert('esc');
			}
		}
	var links = objLink.href;
	links += '/light';
	req.Send(links);
}

function hideLightbox()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');
	objimg = document.getElementById('imgobj');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	objimg.style.display = 'none';

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) 
    {
		selects[i].style.visibility = "visible";
	}
	document.onkeypress = '';
}

function initLightbox()
{
	if (!document.getElementsByTagName)
	{ 
		return; 
	}
	var anchors = document.getElementsByTagName("a");

	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox_html"))
		{
			anchor.onclick = function () 
			{
				showLightbox_html(this); 
				return false;
			}
		}
	}
}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
    	window.onload = func;
	} 
	else 
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

function showaddedLightbox_html(linker, width, height)
{
	if (!width)
		width 	= '737';
	if (!height)
		height 	= '400';
	
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var img = document.getElementById('imgobj');
	objLightbox.style.display = 'none';
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	var imgTop = arrayPageScroll + ((arrayPageSize[3] - 35 - 22 ) / 2);
	var imgLeft = ((arrayPageSize[0] - 20 - 126) / 2);
	
	img.style.top = (imgTop < 0) ? "0px" : imgTop + "px";
	img.style.left = (imgLeft < 0) ? "0px" : imgLeft + "px";
	img.style.display = 'block';
	
	// preload image
	var req = mint.Request();
	req.OnSuccess =
		function()
		{
			if (objOverlay.style.display != 'none')
			{
				img.style.display = 'none';
				var lightboxTop = arrayPageScroll + ((arrayPageSize[3] - 35 - height ) / 2);
				var lightboxLeft = ((arrayPageSize[0] - 20 - width) / 2);
				
				objLightbox.style.top 	= (lightboxTop < 0) ? "0px" : lightboxTop + "px";
				objLightbox.style.left 	= (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
				objLightbox.style.height 	= '420px';
				objLightbox.style.width 	= '750px';
				
				if (navigator.appVersion.indexOf("MSIE")!=-1)
				{
					pause(400);
				} 
		
				selects = document.getElementsByTagName("select");
				for (i = 0; i != selects.length; i++) 
				{
					selects[i].style.visibility = "hidden";
				}
				
				objLightbox.innerHTML = this.responseText;
		
				objLightbox.style.display = 'block';
				//objOverlay.style.height = (arrayPageSize[1] + 'px');
				initLightbox();
				listenKey();
			}
			else
			{
				alert('esc');
			}
		};
	req.Send(HOST+linker+'/light');
}

addLoadEvent(initLightbox);	// run initLightbox onLoad
