// copyright 2007 Julius Volz & Stefan Schnegg, COPROZESSOR, info@coprozessor.de
// version 1.1 19:36:15 Wednesday, May 09, 2007

/* Config section */
var hideDelay = 500;		// onmouseout popup hiding delay
var pathReplacement = new Array('/images', '/images/gross');
var imgFilenameReplacement = new Array('.jpg', '_gr.jpg');
var spinnerImg = "images/spinner.gif";
var popupBorderStyle = "2px solid #666666";
var bigImageCaption = "erneut klicken um zu schließen";

/* Code section*/
var offsets = new Object();	// pixels DeltaX, DeltaY from left/top of calling img
var popup;
var spinner;
var smallImage = new Image();
var bigImage;
var hideTimer;
var loading = 0;

function initImgPopup() {
	popup = document.createElement("div");
	popup.onmouseout = function() {
		                   if (!loading)
			                   hideTimer = setTimeout("hidePopup()", hideDelay);
	                   };
	popup.onclick = function() {
		                hidePopup();
	                };
	popup.onmouseover = function() {
		                    clearTimeout(hideTimer);
	                    };
	popup.className = "imgpopup";
	popup.id = "popup";
	popup.style.position = "absolute";
	popup.style.zIndex = "999999";	
	spinner = new Image();
	spinner.src = spinnerImg;
	bigImage = new Image();
}

function hidePopup() {
	loading = 0;
	// document.body.removeChild(popup);
	$("popup").hide(); //*
}

function showPopup(img) {
	if (loading)
		hidePopup();//*
	if (smallImage.src == img.src) {$("popup").show(); return;} 
		else {if ($("popup")) document.body.removeChild(popup);}   //*
	loading = 1;
	smallImage = img;
	popup.style.border = "none";
	while (popup.firstChild)
		popup.removeChild(popup.firstChild);
	popup.appendChild(spinner);
	spinner.src = spinner.src;
	document.body.appendChild(popup);
	t = img.name.split('#'); // extrahieren der Offsets aus name Attr des Quellbildes
	if (t != '' && (t[0] != '' && t[1] != '')) {
		offsets.x = parseInt(t[0]);
		offsets.y = parseInt(t[1]);
	}
	else {
		offsets.x = 0;
		offsets.y = 0;
	}
	$("popup").show(); //*
	bigImage.alt = "";
	bigImage.title = bigImageCaption;
	bigImage.onload = onImgLoaded;
	bigImage.src = img.src.sub(pathReplacement[0], pathReplacement[1], 1).sub(imgFilenameReplacement[0], imgFilenameReplacement[1], 1);
	Position.clone(img, popup, {
                   setWidth: false,
                   setHeight: false,
                   offsetLeft: img.width / 2 - spinner.width / 2,
                   offsetTop: img.height / 2 - spinner.height / 2
	               });
	
}

function onImgLoaded() {
	while (popup.firstChild)
		popup.removeChild(popup.firstChild);
	$("popup").hide();	
	Position.clone(smallImage, popup, {
                   setWidth: false,
                   setHeight: false,
                   offsetLeft: offsets.x,
                   offsetTop: offsets.y
	               }); 
	popup.appendChild(bigImage);	
	loading = 0;
	popup.style.border = popupBorderStyle;
	$("popup").show();
}

