var openRllover = null; 
var globalimage;
var MINDISTANCE=8; //minimaler abstand zum Rand in px
var mouseX;
var mouseY;

if(!document.all)
	MINDISTANCE=20;


if (document.captureEvents)
	document.captureEvents (Event.MOUSEMOVE);
	
document.onmousemove = saveMouseCoords;
document.onclick = saveMouseCoords;


function saveMouseCoords(e)
{
	if(document.all){
		mouseX = window.event.clientX + document.body.scrollLeft;
		mouseY = window.event.clientY + document.body.scrollTop;
	}else
	{
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
}

function makeVisible(divId)
{
	document.getElementById(divId).style.height = globalimage.height+ 20;
	document.getElementById(divId).style.width = globalimage.width;
	document.getElementById(divId).style.visibility = "visible";
}
 
function showdiv(evt, divId, img, text)
{
	openRllover = null;

	var src = img;
	var border = 1; //kleiner rahmen
	
	globalimage = new Image();
	globalimage.src = img;
	globalimage.border = border;
	
	myDiv = document.getElementById(divId);
	
	myDiv.style.width = 80;
	myDiv.style.height = 40;
	
	if(myDiv.firstChild)
		myDiv.removeChild(myDiv.firstChild);
		
	myDiv.appendChild(document.createTextNode("Bild wird geladen..."));	
	myDiv.style.background = "white";
	myDiv.style.border = 1;
	myDiv.style.bordercolor = "black";
	myDiv.style.left = mouseX - 30;
	myDiv.style.top = mouseY - 20;
	
	makeVisible(divId);
	setTimeout("showpicture('"+divId+"','" + text + "')",10);
}

function wait(evt, divId, img, time, text)
{	
	if (!text) text = "";
	openRllover = divId;	
	window.setTimeout("check('"+evt+"', '"+divId+"', '"+img+"','" + text + "')", time);
	
}
	
function check(evt, divId, img, text)
{
	if (openRllover == divId)
	{
		if (!text) text = "";
		showdiv(evt, divId, img, text);
	}
}
  
function hidediv(id)
{
	document.getElementById(id).style.visibility = "hidden";
}

function disableopen()
{
	openRllover = null;
}

function showpicture(divId, text)
{	
	if(globalimage.complete)
	{
		var topofdoc = document.body.scrollTop;	
		var clickx = mouseX; 
		var clicky = mouseY;
		
		clicky = clicky - topofdoc;
		
		var Page = {
			pageHeight : function()
			{
				return self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
			},
			pageWidth : function()
			{
				return self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
			},
			pageSize: function() {
				return { width: this.pageWidth(), height: this.pageHeight() };
			}
		};

		var width = globalimage.width;
		var height = globalimage.height;
		var frameHeight=null;
		
		if(self.innerHeight)
			frameHeight = self.innerHeight;
		else
			frameHeight = document.body.offsetHeight;

		var x = 0;
		var y = 0;

		var bereich = 0;
		
		//oben links
		if(clickx < Page.pageWidth()/2 && clicky < (Page.pageHeight() - topofdoc)/2)
		{
			x = clickx - 50;
			y = clicky - 50;
			if(height > frameHeight - clicky)
				y = frameHeight - height - MINDISTANCE;
			if(width > (Page.pageWidth() - clickx))
				x = Page.pageWidth() - width - MINDISTANCE;
			if(x < MINDISTANCE)
				x = MINDISTANCE;
			if(y < MINDISTANCE)
				y = MINDISTANCE;
		}
		
		//oben rechts
		if(clickx > Page.pageWidth()/2 && clicky < (Page.pageHeight() - topofdoc)/2)
		{
			x = clickx - width + 50;
			y = clicky - 50;
			if(height > frameHeight - clicky)
				y = frameHeight - height - MINDISTANCE;
			if(width > (Page.pageWidth() - clickx))
				x = Page.pageWidth() - width - MINDISTANCE;
			if(x < MINDISTANCE)
				x = MINDISTANCE;
			if(y < MINDISTANCE)
				y = MINDISTANCE;		
		}
		
		//unten links
		if(clickx < Page.pageWidth()/2 && clicky > (Page.pageHeight() - topofdoc)/2)
		{
			x = clickx - 50;
			y = clicky - height + 50;
			if(height > frameHeight - clicky)
				y = frameHeight - height - MINDISTANCE;
			if(width > (Page.pageWidth() - clickx))
				x = Page.pageWidth() - width - MINDISTANCE;
			if(x < MINDISTANCE)
				x = MINDISTANCE;
			if(y < MINDISTANCE)
				y = MINDISTANCE;
		}
		
		//unten rechts
		if(clickx > Page.pageWidth()/2 && clicky > (Page.pageHeight() - topofdoc)/2)
		{
			x = clickx - width + 50;
			y = clicky - height + 50;
			
			if(height > frameHeight - clicky)
				y = frameHeight - height - MINDISTANCE;
			if(width > (Page.pageWidth() - clickx))
				x = Page.pageWidth() - width - MINDISTANCE;
			if(x < MINDISTANCE)
				x = MINDISTANCE;
			if(y < MINDISTANCE)
				y = MINDISTANCE;
		}
		
		document.getElementById(divId).style.left = x;
		document.getElementById(divId).style.top = y + topofdoc;
		
			
		myDiv = document.getElementById(divId);
		if(myDiv.firstChild)
		{
			myDiv.removeChild(myDiv.firstChild);
		}		
		if(myDiv.firstChild)
		{
			myDiv.removeChild(myDiv.firstChild);
		}		
		myDiv.appendChild(globalimage);
		
		myTextDiv = document.createElement("div");
		if (text && text!="")
			myTextDiv.innerHTML = text;
		myDiv.appendChild(myTextDiv);
		
		setTimeout("makeVisible('"+divId+"')",10);
		return;
	}
	setTimeout("showpicture('"+divId+"')",10);
	
}