addLoadListener(function()
{
	transLink = document.getElementById('trans_link');
	othersLink = document.getElementById('others_link');
	tocLink = document.getElementById('toc_link');
	extrasLink = document.getElementById('extras_link');
	
	if (transLink) {
		transLink.onclick = new Function("openPopup(this,'trans');return false;");
	}
	
	if (othersLink) {
		othersLink.onclick = new Function("openPopup(this,'others');return false;");
	}
	
	if (tocLink) {
		tocLink.onclick = new Function("openPopup(this,'toc');return false;");
	}
	
	if (extrasLink) {
		extrasLink.onclick = new Function("openPopup(this,'extras');return false;");
	}
	
	bObj = document.getElementsByTagName('body').item(0);
	
	transDiv = document.createElement('div');
	transDiv.setAttribute('id','transDiv');
	bObj.insertBefore(transDiv, bObj.nextSibling);
	document.getElementById('transDiv').innerHTML = '<div id="transDiv_top"></div><div id="transDiv_middle_wrap"><div id="transDiv_middle_inner"></div></div><div id="transDiv_bottom"></div><div id="popup_header_gold"><h2>Translations</h2></div><a class="close" onclick="closePopup()" href="javascript:;">close</a>';

	popupDiv = document.createElement('div');
	popupDiv.setAttribute('id','popupDiv');
	bObj.insertBefore(popupDiv, bObj.nextSibling);
	document.getElementById('popupDiv').innerHTML = '<div id="popupDiv_top"></div><div id="popupDiv_middle_wrap"><div id="popupDiv_middle_inner"></div></div><div id="popupDiv_bottom"></div><div id="popup_header_blue"><h2></h2></div><div id="bottom_bg"></div><a class="close" onclick="closePopup()" href="javascript:;">close</a>';
	
	isIE67 = /msie|MSIE 6|MSIE 7/.test(navigator.userAgent);
	ieOffset = (isIE67) ? -45:0; // Need this for IE's to negotiate the 45px padding on #main_content_middle_wrap.

	preloading(
	"headers/translations_bg.gif",
	"buttons/close_slammed.png",
	"books/flags/dutch.gif",
	"books/flags/spanish.gif",
	"books/flags/russian.gif",
	"popup_gold/gold_bottom_bg_slammed.png",
	"popup_gold/gold_top_bg_slammed.png",
	"popup_gold/gold_middle_bg_slammed.png",
	"popup_gold/gold_middle_inner_bg.gif",
	"popup_blue/blue_top_bg2_slammed.png",
	"popup_blue/blue_middle_bg_slammed.png",
	"popup_blue/blue_middle_inner_bg.gif",
	"popup_blue/blue_bottom_bg_slammed.png"
	);

});

var myimages = [];
//var loadCount = 0;
function preloading() {
for (i = 0; i < preloading.arguments.length; i++) {
	myimages[i] = new Image();
	myimages[i].src = "images/"+preloading.arguments[i];
//	myimages[i].onload = new function() {
//		loadCount++;
//		if (loadCount == preloading.arguments.length) {
//			alert(preloading.arguments.length);
//		}
//	};
}
}

var ieOffset;
var popupType;

function openPopup(obj,type) {
bid = obj.href.match(/bid=(\d\d?)/); // bid[1] holds the book id number

req = createXMLHttpRequest();
if (!req) {
	location.href='waynes-books.php?a='+type+'&bid='+bid[1];
} else {
//	document.getElementById('loading').style.display = 'block';
	url = 'waynes-books-process.php?a='+type+'&bid='+bid[1];
	req.open('GET', url, true);
	req.onreadystatechange = handleResponseTrans;
	req.send(null);
	return false;
}
}

function handleResponseTrans() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var response = req.responseText;
			popupSplit = response.split('|');
			
			if (popupSplit[0] == 'trans') {
				transDiv = document.getElementById('transDiv');
				document.getElementById('transDiv_middle_inner').innerHTML = popupSplit[1];
				coor = findPos(document.getElementById('book_img_nav').getElementsByTagName('ul')[0]);
	//			vOffset = document.getElementById('book_img_nav').parentNode.offsetHeight - 4;
				vOffset = 0;
				transDiv.style.left = coor[0] + ieOffset + 'px';
				transDiv.style.top = coor[1] + vOffset + 'px';
				transDiv.style.display = 'block';
				if (popupType == 'popupDiv') {
					closePopup();
				}
				popupType = 'transDiv';
			} else {
				vPadding = 10 * 2;
				popupDiv = document.getElementById('popupDiv');
				outerDiv = document.getElementById('main_content_middle'); // The div that the popup should be inside of.
				
				outerHeight = outerDiv.offsetHeight;
				brHeight = f_clientHeight();
				coor = findPos(outerDiv);
				outerBottomPos = coor[1] + outerHeight;
				topScrollPos = f_scrollTop();
				
				vOffset = (topScrollPos <= coor[1] - vPadding) ? 0:topScrollPos - coor[1] + vPadding;
				popupDiv.style.left = coor[0] + 0 + 'px';
				popupDiv.style.top = coor[1] + vOffset + 'px';

				calcHeightFinal = outerHeight - vOffset - vPadding;

				bottomOffset = (outerBottomPos - (brHeight + topScrollPos) >= 0) ? outerBottomPos - (brHeight + topScrollPos):0;

				popupDiv.style.height = calcHeightFinal - bottomOffset + 'px';

				popupDivMI = document.getElementById('popupDiv_middle_inner');
				popupDivHeader = document.getElementById('popup_header_blue');
				
				if (popupSplit[0] == 'toc') {
					//popupDiv.innerHTML = popupSplit[1];
					//document.getElementById('popupDiv_middle_inner').innerHTML = popupSplit[1];
					popupDivHeader.getElementsByTagName('h2')[0].className = 'h2_toc';
				} else if (popupSplit[0] == 'others') {
					//popupDiv.innerHTML = popupSplit[1];
					//document.getElementById('popupDiv_middle_inner').innerHTML = popupSplit[1];
					popupDivHeader.getElementsByTagName('h2')[0].className = 'h2_others';
				} else { // popupSplit[0] == 'extras'
					//popupDiv.innerHTML = popupSplit[1];
					//document.getElementById('popupDiv_middle_inner').innerHTML = popupSplit[1];
					popupDivHeader.getElementsByTagName('h2')[0].className = 'h2_extras';
				}
				
				popupDiv.style.display = 'block';

				popupDivHeight = popupDiv.offsetHeight;
				popupDivMI.innerHTML = popupSplit[1];

				if (popupDivMI.offsetHeight < popupDivHeight - 30) { // Center div vertically, because content is smaller than the containing div.
					popupDiv.style.top = popupDiv.offsetTop + parseInt((popupDivHeight - popupDivMI.offsetHeight - 30 - 30) / 2) + 'px';
					popupDiv.style.height = popupDivMI.offsetHeight + 30 + 30 + 'px';
				} else {
					popupDivMI.style.height = popupDivHeight - 30 - 45 + 'px';
				}
				
				popupType = 'popupDiv';
			}
		}
	}
}

function closePopup() {
document.getElementById(popupType).style.display = 'none';
document.getElementById('popupDiv_middle_inner').innerHTML = ''; // Need this for IE6 so height calculations next time around will be corrent.
document.getElementById('popupDiv_middle_inner').style.height = 'auto'; // Just in case the the big popup is open.
}

function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
	curleft = obj.offsetLeft
	curtop = obj.offsetTop
	while (obj = obj.offsetParent) {
		curleft += obj.offsetLeft
		curtop += obj.offsetTop
	}
}
return [curleft,curtop];
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
