var windowHeight;
var windowWidth;
var documentHeight;
var documentWidth;

window.onresize=windowResized;
window.onload=pageLoaded;

function pageLoaded(){
	document.getElementById("popupimg").onload=resizeDiv;
	document.getElementById("overlay").onclick=displayPopup;
}
function windowResized(){
	setSizes();
	window.location.href=window.location.href;
}
function displayPopup(url,title,desc,views,fullurl) {
	// If already displayed hide, else display
	if (document.getElementById("popup").style.display == "block"){
		// Hide
		document.getElementById("overlay").style.display="none";
		document.getElementById("popup").style.display="none";
		document.getElementById("popup").style.top="10000px";
		document.getElementById("popup").style.position="fixed";
		document.getElementById("popupimg").src="";
	}
	else {
		// Display the block
		document.getElementById("popup").style.display="block";
		document.getElementById("overlay").style.display="block";
		// Set the img src to the passed url
		document.getElementById("popupimg").src=url;
		// Set the title
		document.getElementById("popuptitle").innerHTML='<a href="'+unescape(fullurl)+'">'+unescape(title)+'</a>'
		document.getElementById("popupdesc").innerHTML=unescape(desc);
		document.getElementById("popupviews").innerHTML=views+1;
		// Position the "loading" div
		setSizes();
	}
}
function setSizes(){
	//Get the width/height of the screen	
	if (navigator.appName.indexOf("Microsoft")!=-1) {
		winW = document.body.clientWidth;
		winH = document.body.clientHeight;
	}
	else {
		winW = window.innerWidth;
		winH = window.innerHeight;
	}
	windowHeight = winH;
	windowWidth = winW;
	var docHeight;
	if (typeof document.height != 'undefined') {
		docHeight = document.height;
	}
	else if (document.body && typeof document.body.scrollHeight !='undefined') {
		docHeight = document.body.scrollHeight;
	}
	if (typeof document.height != 'undefined') {
		docHeight = document.height;
	}
	else if (document.compatMode && document.compatMode != 'BackCompat') {
		docHeight = document.documentElement.scrollHeight;
	}
	else if (document.body && typeof document.body.scrollHeight !='undefined') {
		docHeight = document.body.scrollHeight;
	}
	documentHeight = docHeight;	
	documentWidth = document.width;
	
	// Change the cookie so that the correct number of images are displayed.
	// NB last number is number of rows
	var displayitems = (Math.floor(documentWidth/164))*3
	createCookie("itemstodisplay",displayitems);
}
function resizeDiv(){
	// Get the width/height of the Image
	divHeight = document.getElementById("popup").offsetHeight;
	divWidth = document.getElementById("popupimg").offsetWidth;
	document.getElementById("popup").style.width=divWidth+"px";
	
	// Get the sizes
	setSizes();
	
	// display the image
	document.getElementById("popup").style.position="absolute";
	// Set the style to place the image in the centre
	height = ((windowHeight-divHeight)/2)-5;
	if (height<0){
		height=10;
	}
	document.getElementById("popup").style.top=height+"px";
	width = ((windowWidth-divWidth)/2)-5;
	document.getElementById("popup").style.left=width+"px";
	
	// Set the height of the overlay, so if the user scrolls, it's still there.
	if (winH>documentHeight){
		document.getElementById("overlay").style.height=winH+"px";
	}
	else {
		document.getElementById("overlay").style.height=documentHeight+"px";
	}
}
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}