var showPreviewWindow = false;
var showThumbnail = false;
var hidingThumbnail = 0;
var lastThumbnail = '';
var maxImageWidth = 400;
var maxImageHeight = 400;
var e, p, y;

function hidePreviewPage() {
	showPreviewWindow = false;
	p = document.getElementById("previewpage_div");
	if (p) {
		p.style.display = "none";
	}
}
function requestHideThumb()
{
	if (!hidingThumbnail)
	{
		hidingThumbnail=1;
		ID=window.setTimeout(hideThumbnail,1000);
	}
	else if (hidingThumbnail == 1)
	{
		hidingThumbnail=2;
	}
	else if (hidingThumbnail == 3)
	{
		hidingThumbnail=2;
	}
}

function hideThumbnail()
{
	if (hidingThumbnail == 1)
	{
		p = document.getElementById("thumbnail_div");
		if (p)
		{
			p.style.display = "none";
			showThumbnail = false;
		}
		lastThumbnail = '';
		hidingThumbnail=0;
	}
	else if (hidingThumbnail == 2)
	{
		hidingThumbnail=0;
		requestHideThumb();
	}
	else if (hidingThumbnail == 3)
	{
		hidingThumbnail=0;
	}
}
function showLargeImage(thumbnail, imagesrc) {

	if (hidingThumbnail)
		hidingThumbnail=3;
	if (lastThumbnail != imagesrc)
	{
		lastThumbnail = imagesrc;
		p = document.getElementById("thumbnail_div");

		if (p)
		{
			showThumbnail=true;
			p.innerHTML = '<img style="border: solid 1px #aaaaaa;" align="left" src="' + imagesrc + '">';
			var x = findPosX(thumbnail);
			var y = findPosY(thumbnail);

			var myWidth = 0, myHeight = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
				myWidth = window.innerWidth;
				myHeight = window.innerHeight;
			}
			else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
			}
			else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				myWidth = document.body.clientWidth;
				myHeight = document.body.clientHeight;
			}

			var scrOfX = 0, scrOfY = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
				scrOfY = window.pageYOffset;
				scrOfX = window.pageXOffset;
			}
			else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				scrOfY = document.body.scrollTop;
				scrOfX = document.body.scrollLeft;
			}
			else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				scrOfY = document.documentElement.scrollTop;
				scrOfX = document.documentElement.scrollLeft;
			}

			if ((y + p.clientHeight) > (myHeight + scrOfY)) {
				y = y - ((y + p.clientHeight) - (myHeight + scrOfY)) ;
			}
			if (p.clientHeight > myHeight ) {
				y = scrOfY ;
			}

			if (y < scrOfY)
				y = scrOfY;

			p.style.left = (x + 120) + 'px';
			p.style.top = (y - 100) +'px';
			p.style.display = "block";
		}
	}
}

function showPreviewPage(aE, link, page) {
	showPreviewWindow = true;
	e = document.getElementById(aE);
	if (e) {
		y = findPosY(e);
		p = document.getElementById("previewpage_div");
		if (p) {
			var myWidth = 0, myHeight = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
				myWidth = window.innerWidth;
				myHeight = window.innerHeight;
			}
			else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
			}
			else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				myWidth = document.body.clientWidth;
				myHeight = document.body.clientHeight;
			}
			var scrOfX = 0, scrOfY = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
				scrOfY = window.pageYOffset;
				scrOfX = window.pageXOffset;
			}
			else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				scrOfY = document.body.scrollTop;
				scrOfX = document.body.scrollLeft;
			}
			else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				scrOfY = document.documentElement.scrollTop;
				scrOfX = document.documentElement.scrollLeft;
			}

			if ((y + p.clientHeight) > (myHeight + scrOfY)) {
				y = y - ((y + p.clientHeight) - (myHeight + scrOfY)) ;
			}
			if (p.clientHeight > myHeight ) {
				y = scrOfY ;
			}

			y = y + 100;
			if (y < scrOfY)
				y = scrOfY + 100;

			p.style.left = (findPosX(document.getElementById(aE)) - 480) + 'px';
			p.style.top = (scrOfY + 25) + 'px';

			p.innerHTML = page;
			p.style.display = "block";
		}
	}
}

function clear_default(e) {
	if ((e.value == 'min') || (e.value == 'max')) {
		e.value = '';
	}
}
function set_default(e, v) {
	if (e.value.length == 0) {
		e.value = v;
	}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)curleft += obj.x;
	return curleft;
}
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)curtop += obj.y;
	return curtop;
}
