var loading = false;
var theTimeout = null;

function GetXMLHttpObject() {
	var xmlHttp=null;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}	
	
	return xmlHttp;
}

function processCloseClick() {
	var obj = document.getElementById('divPicturePreview');
	new Effect.Squish(obj);
}

function processThumbMouseOver(objThumb, objOrigPath) {
	var objPicPreview = document.getElementById('divPicturePreview');

	if (!loading && objPicPreview.style.display != '') {
		loading = true;
	
		var picturePath = objOrigPath;
		var picture = new Image();
		picture.src = picturePath;
		var thumbPos = getPos(objThumb);
		var centerCoords = getObjCenteringCoords(picture); 
	
		objPicPreview.innerHTML = '';
		objPicPreview.style.display = '';
		objPicPreview.style.border = '1px solid #555555';
		objPicPreview.style.left = thumbPos[0] + 'px';
		objPicPreview.style.top = thumbPos[1] + 'px';
		objPicPreview.style.width = objThumb.scrollWidth;
		objPicPreview.style.height = objThumb.scrollHeight;
	
		new Effect.Morph(objPicPreview.id, {style:{width: picture.width + 'px', height: picture.height + 'px'}});
		new Effect.Morph(objPicPreview.id, {style:{left: (centerCoords[0] - (picture.width / 2)) + 'px', top: (centerCoords[1] - (picture.height / 2)) + 'px'}, afterFinish: function() {objPicPreview.innerHTML = "<a href=\"" + objThumb.parentNode.href + "\"><img src=\"" + picturePath + "\" border=\"0\" /></a>"; loading = false;}});
	}
}

function processThumbMouseOut(e, objThumb) {
	var objPP = document.getElementById('divPicturePreview');
	var arrImgs = objPP.getElementsByTagName('img');

	try {
			if (typeof objPP.contains == "undefined") {objPP.contains = function (arg) {return !!(this.compareDocumentPosition(arg) & 16);}}
			if (!e.srcElement) e.srcElement = e.target;
			if (!e.toElement) e.toElement = e.relatedTarget;
			if (!e.toElement || (e.toElement == objPP) || (e.toElement == objThumb) || (e.toElement && objPP.contains(e.toElement)) || 
				(typeof e.toElement != "undefined" && e.toElement.tagName == 'IMG' && e.toElement.src.replace('_thumb_100_100', '') == arrImgs[0].src)) return;
	} catch (e) {}

	if (typeof theTimeout != "undefined" && theTimeout) clearTimeout(theTimeout);

	theTimeout = setTimeout(function() {hidePreviewDiv(e, objThumb);}, 500);
}

function hidePreviewDiv(e, objThumb) {
	if (!loading) {
		loading = true;
		var obj = document.getElementById('divPicturePreview');
		obj.style.border = 'none';
		var arrImgs = obj.getElementsByTagName('img');
		new Effect.Puff(arrImgs[0], {afterFinish: function() {loading = false; obj.style.display = 'none';}});
	} else {
		processThumbMouseOut(e, objThumb);
	}
}