var interval;
var loader;
var div;

function scroll_top()
{
if (self.pageYOffset) // all except Explorer
{
	return self.pageYOffset;
}
else if (document.documentElement && document.documentElement.scrollTop)
	// Explorer 6 Strict
{
	return document.documentElement.scrollTop;
}
else if (document.body) // all other Explorers
{
	return document.body.scrollTop;
}

}

function page_height()
{
	return (document.body.offsetHeight < document.body.scrollHeight ? document.body.offsetHeight : document.body.scrollHeight);
}

function imgr_init()
{
	page_height();
	
	head = document.getElementsByTagName("head")[0];
	
	link = document.createElement("link");
	
	link.setAttribute("rel", "stylesheet");
	link.setAttribute("type", "text/css");
	link.setAttribute("href", "/css/imgr.css");
	
	head.appendChild(link);
	
	imgs = document.getElementsByTagName("a");
	
	for (var i = 0; i < imgs.length; i++)
	{
		if (imgs[i].getAttribute("rel"))
		{
			imgs[i].onclick 
				= function()
					{
						if (document.getElementById("imgr"))
						{
							return;
						}
						
						body = document.getElementsByTagName("body")[0];
						
						div = document.createElement("div");
						
						div.style.height = page_height() + "px";

						div.setAttribute("id", "imgr");
						
						img = document.createElement("img");
						
						img.style.marginTop = scroll_top() + 50 + "px";
						
						img.src = "/admin/images/loading_coco.gif";
						img.style.width = 'auto';
						img.style.height = 'auto';
						
						//img.oW = img.width;
						//img.oH = img.height;

						//img.style.width = "0px";
						//img.style.height = "0px";
						
						div.appendChild(img);
						
						div.onclick = function () { document.getElementById('imgr').parentNode.removeChild(document.getElementById('imgr')); body.onkeydown = ''; }
						body.onkeydown = function() { if (window.event.keyCode == 88) { document.getElementById('imgr').parentNode.removeChild(div); body.onkeydown = ''; } }
											
						body.appendChild(div);

						// Preload the image in the background
						
						loader = new Image();
						
						loader.onload = function() { 
							document.getElementById('imgr').getElementsByTagName('img')[0].src = loader.src; }

						loader.src = this.getAttribute("href");
						
												
						//interval = setInterval(expand, 25);
						
						this.blur();
						
						return false;
					}
		}
	}
}

function expand()
{
	if (parseInt(img.style.width) < img.oW)
	{
		img.style.width = parseInt(img.style.width) + (img.oW / 15) + "px";
	}
	else
	{
		
	}
	
	if (parseInt(img.style.height) < img.oH)
	{
		img.style.height = parseInt(img.style.height) + (img.oH / 15) + "px";
	}
	else
	{
	}
	
	if (parseInt(img.style.width) >= img.oW && parseInt(img.style.height) >= img.oH)
	{
		clearInterval(interval);
	}
}

if (typeof(window.addEventListener) != "undefined")
{
	// Standards compliant
	window.addEventListener("load", imgr_init, false);
}
else
{
	// Internet Explorer
	window.attachEvent("onload", imgr_init);
}