// based on lightbox
// http://www.huddletogether.com/projects/lightbox/


// based on http://www.quirksmode.org/
function getYScroll(){

	var scroll;

	if (self.pageYOffset) // all except Explorer
		return self.pageYOffset; 
	else if (document.documentElement && document.documentElement.scrollTop) // IE6Strict
		return document.documentElement.scrollTop;
	else if (document.body) // all other Explorer
		return document.body.scrollTop;

	return 0;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
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;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);

	return arrayPageSize;
}

function hideImage()
{
	var objOverlay = document.getElementById('overlay');
	var objLoading = document.getElementById('loading');
	var objImage = document.getElementById('image');
	
	objOverlay.style.display = "none";
	objImage.innerHTML = "";
	objImage.style.display = "none";
	objImage.style.top = "0px";
	objImage.style.left = "0px";
	objLoading.style.display = "none";
}

function showImage(url, src, srcurl)
{
	var objOverlay = document.getElementById('overlay');
	var objImage = document.getElementById('image');
	var objLoading = document.getElementById('loading');

	var arrayPageSize = getPageSize();
	var YScroll = getYScroll();	

	var img = document.createElement("IMG");
	img.src = url;

	var text = document.createElement("DIV");
	text.innerHTML = "original by <a href='"+srcurl+"'>"+src+"</a>";

	objImage.appendChild(img);
	objImage.appendChild(text);
	objImage.hash = url.substring(url.lastIndexOf('/')+1,url.length-4);

	img.onload = function()
	{
		var devnull = document.getElementById("devnull");
		devnull.src = url;

		if (img.width==0)
		{
			objImage.onclick = hideImage;
			objLoading.style.display = 'none';
			objImage.style.display = "block";
		}

		objImage.style.minWidth = img.width + 4 + "px";

		objImage.style.top = (YScroll + (arrayPageSize[3] - img.height-24)/3) + "px";
		objImage.style.left = ((arrayPageSize[0] - img.width-4)/2) + "px";

		objImage.onclick = hideImage;
		objLoading.style.display = 'none';
		objImage.style.display = "block";

		pprzImage();
	};

	// set height of Overlay to take up whole page and show
	objLoading.style.display = 'block';
	objLoading.style.top = (YScroll + (arrayPageSize[3]/2) -16) + "px";
	objOverlay.style.height = (arrayPageSize[1] - 2 + 'px');
	objOverlay.style.display = 'block';
	objOverlay.onclick = hideImage;

	return false;
}

function resize()
{
	var arrayPageSize = getPageSize();

	var objOverlay = document.getElementById('overlay');
	var objContent = document.getElementById('content');

	objOverlay.style.height = (arrayPageSize[1] - 2 + 'px');
	
	maxright = 0;
	objContent.style.width = "100%";

	for (c in objContent.childNodes)
	{
		o = objContent.childNodes[c];

		right = o.offsetLeft + o.offsetWidth;
		if (right>maxright)
			maxright = right;
	}
	
	objContent.style.width = (maxright) + "px";
}

window.onresize = resize;

pprzImage = function () {};
pprzInit = function () {};

function frameInit()
{
	hideImage(); 
	resize();
	pprzInit();
}
