var imgArchive = new Array();
var curUrl = null;
var lastTip;

function hoverPopUp(e){
	e = e || window.event;

	var cursor = {x:0, y:0};
	if (e.pageX || e.pageY) {
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	}else {
		var de = document.documentElement;
		var b = document.body;
		cursor.x = e.clientX +
		(de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
		cursor.y = e.clientY +
		(de.scrollTop || b.scrollTop) - (de.clientTop || 0);
	}

	document.getElementById('tooltip').style.top = (cursor.y+10)+'px';
	document.getElementById('tooltip').style.left = (cursor.x+15)+'px';
	document.getElementById('tooltip').style.display = 'block';
}

function hoverHide(){
	document.getElementById('tooltip').style.display = 'none';
}

function showImage(url,origwidth,origheight){
	hoverHide();
	if(curUrl != null) closeImage();

	var height = 0;
	var width = 0;
	var WW = document.body.clientWidth;
	var WH = document.body.clientHeight;

	if(window.innerHeight)
		WH = window.innerHeight;
	else if(document.documentElement && document.documentElement.clientHeight)
		WH = document.documentElement.clientHeight;

	delta = 100;

	if(origwidth+delta > WW && origheight+delta > WH){
		if(origwidth-WW < origheight-WH){
			height = (origheight+delta > WH) ? WH-delta : origheight;
			width = (height/origheight)*origwidth;
		}else{
			width = (origwidth+delta > WW) ? WW-delta : origwidth;
			height = (width/origwidth)*origheight;
		}
	}else if(origwidth+delta > WW){
		width = (origwidth+delta > WW) ? WW-delta : origwidth;
		height = (width/origwidth)*origheight;
	}else if(origheight+delta > WH){
		height = (origheight+delta > WH) ? WH-delta : origheight;
		width = (height/origheight)*origwidth;
	}else{
		height = origheight;
		width = origwidth;
	}

	width = parseInt(width);
	height = parseInt(height);

	curUrl = url;

	document.getElementById('loading').style.display = 'block';
	document.getElementById('closebtn').style.display = 'block';
	document.getElementById('tr1').style.visibility = 'hidden';
	document.getElementById('tr2').style.visibility = 'hidden';
	document.getElementById('imgd').style.width = (width)+'px';
	document.getElementById('imgd').style.height = (height)+'px';
	document.getElementById('imgd').style.marginLeft = '-'+(width/2+18)+'px';

	document.getElementById('imgd').onclick=closeImage;

	if(!imgArchive[url]){
		imgArchive[url] = document.createElement('img');
		imgArchive[url].src = url;
		imgArchive[url].className = 'imgdimg';

		imgArchive[url].style.width = width+'px';
		imgArchive[url].style.height = height+'px';

		if(imgArchive[url].complete){
			finishShowing(url);
		}else{
			imgArchive[url].onload=function(){
				finishShowing(url);
			}
		}
	}else{
		finishShowing(url);
	}
}

function finishShowing(url){
	document.getElementById('imgd').appendChild(imgArchive[url]);
	document.getElementById('loading').style.display = 'none';
	document.getElementById('imgd').style.display = 'block';

	document.getElementById('imgd').scrollTop = 0;
	document.getElementById('imgd').scrollLeft = 0;
	document.body.scrollTop = 0;
}

function closeImage(){
	if(document.getElementById('loading').style.display == 'block'){
		// Cancel loading of an image
		document.getElementById('loading').style.display = 'none';
		imgArchive[curUrl].onload=function(){}
		delete imgArchive[curUrl];
	}else{
		document.getElementById('imgd').removeChild(document.getElementById('imgd').lastChild);
	}

	document.getElementById('tr1').style.visibility = 'visible';
	document.getElementById('tr2').style.visibility = 'visible';
	document.getElementById('imgd').style.display = 'none';
	document.getElementById('closebtn').style.display = 'none';
	curUrl = null;
}