var currentBoxID = "";
var currentBoxIDLarge = "";
var currentBoxIDTimeout = "";
var currentBoxIDTimeoutID = "";
var currentBoxIDTimeoutID2 = "";
var ie4 = document.all;
var ope = navigator.userAgent.indexOf("Opera");
var _isInternetExplorer = isInternetExplorer();
var _isInternetExplorerVer8 = isInternetExplorerVer8();
//var _isNetscape = isNetscape();
var mouseX = 0;
var mouseY = 0;

initMouseListener();

/**
 * Attach the mousePosition-function to the eventListener.
 */
function initMouseListener()
{
	if (window.Event)
	  document.captureEvents(Event.MOUSEMOVE)
	document.onmousemove = mousePosition;
}

/**
 * Saves the mouseposition [X and Y coordinates] in the two Javascript-variables 'mouseX' and 'mouseY'
 */
function mousePosition(e)
{
	if (!e)
	  var e = window.event;
		  
	if ( _isInternetExplorer && (! _isInternetExplorerVer8 ) )
	{
		if (e.pageX || e.pageY) 	
		{
			mouseX = e.pageX;
			mouseY = e.pageY;
		}
		else if (e.clientX || e.clientY)
		{
			mouseX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			mouseY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
	}
	else
	{
		mouseX = (window.Event) ? e.pageX : event.clientX;
		mouseY = (window.Event) ? e.pageY : event.clientY;
		
		if(ie4 && (ope < 0) && (! isInternetExplorerVer8() ) )
		{
			mouseY += document.body.scrollTop;
		}
	}
	
	mouseX += 10;
	mouseY -= 10;
	
	/* nodig ivm nieuwe div-layout */
	mouseX -= 130;
}

/**
 * Shows the box / popup with the given boxID and display the corresponding image. If an other box / popup
 * is showed, it will be closed before showing the other one.
 */
function showBox( boxID, image )
{
	var largeBox = false;
	if (boxID.indexOf("large") > 0)
	  largeBox = true;
	  
	if ( (currentBoxIDTimeout == boxID) && (largeBox) && (currentBoxIDTimeoutID != ""))
	{
		clearTimeout( currentBoxIDTimeoutID );
		clearTimeout( currentBoxIDTimeoutID2 );
		currentBoxIDLarge = boxID;
	}
	else if ( ! ((currentBoxID == boxID) || (currentBoxIDLarge == boxID)) )
	{
		if ( largeBox && (currentBoxIDLarge != "") && (currentBoxIDLarge != boxID) )
		  hideBox( currentBoxIDLarge ) 
		else if ( (! largeBox) && (currentBoxID != "")  && (currentBoxID != boxID) )
		  hideBox( currentBoxID );
	
		/* toon eerst de loading-img bij largeBox */
		if (largeBox)
		  getLayer( boxID + '_i').src = "Data/collectie/gformat/gformat_loading.jpg";
		getLayer( boxID + '_i' ).src = image;

		/* maak de popup 'visible' en zet de Y-coordinaat op muiscursor-hoogte, en X-coordinaat (ongeveer) centreren */
		boxObj = getLayer( boxID );
		boxObj.style.top  = '' + (checkMouseCoordinates( 'Y', mouseY, boxObj.offsetHeight ) - 10) + 'px'; 
		if (largeBox)
		  boxObj.style.left = '' + checkMouseCoordinates( 'X', mouseX, boxObj.offsetWidth ) + 'px';
		else 
		  boxObj.style.left = '' + (checkMouseCoordinates( 'Xcenter', mouseX, boxObj.offsetWidth )) + 'px';		
		boxObj.style.visibility = "visible";
		boxObj = null;	
	
		if (largeBox)
		  currentBoxIDLarge = boxID;
		else
		  currentBoxID = boxID;
	}
	 
	currentBoxIDTimeout = "";
	currentBoxIDTimeoutID = "";
}

function setImageSource( imgID, image )
{
	if ( getLayer(imgID) != null )
	  getLayer(imgID).src = image;
}

function checkBoxHeight( boxID, textLayer )
{
	var boxHeight = getLayer( boxID ).offsetHeight;
	var textHeight = getLayer( textLayer ).offsetHeight;
	
	if ( textHeight > (boxHeight - 30 ))
	  getLayer( boxID + '_container' ).style.height = '' + (boxHeight + (textHeight - boxHeight) + 25) + 'px'; 
}

/**
 * Hides the current box / popup
 */
function hideBox( boxID, sender )
{
	var largeBox = false;
	var timeoutLength = 1;
	
	if (boxID.indexOf("large") >= 0)
	  largeBox = true;
	
	/* set the optional time-out-time for triggering hideBox. this timeout is cleared by an new
	   onmouseover event within the timeout-time */
	if (largeBox && (boxID == sender))
	{
		timeoutLength = 300;
		currentBoxIDTimeout = boxID;
	}
	else
	{
		currentBoxIDTimeout = "";
	}

	/* timeout is only set with a 'largeBox' */
	if (largeBox)
	{
		currentBoxIDTimeoutID2 = setTimeout("getLayer( '"+boxID+"_i' ).src = 'Data/collectie/gformat/gformat_loading.jpg';", timeoutLength);
		currentBoxIDLarge = "";
	}
	else
	{
		getLayer( boxID + '_i' ).src = "";
		currentBoxID = "";
	}
	
	currentBoxIDTimeoutID = setTimeout("getLayer( '"+boxID+"' ).style.visibility = 'hidden';", timeoutLength);
	setTimeout( "currentBoxIDTimeoutID = '';", (timeoutLength - 10) );
	setTimeout( "currentBoxIDTimeoutID2 = '';", (timeoutLength - 10) );
}

/**
 * Returns the body element, the location is dependent on the type of browser, like IE, FF, Netscape or Opera.
 */
function truebody()
{
	return ((!window.opera && document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body)
}

/**
 * Checks of the X or Y coordinate of the mouse is acceptable as upper-left corner for an object, e.g. popup.
 * Otherwise, the coordinate will be shifted to left or to top, so that it is totally visible in the page.
 */
function checkMouseCoordinates( XorY, XorY_value, objectWidthOrHeight )
{
	//alert('vars: |' + XorY + '| _ |' + XorY_value + '| _ |' + objectWidthOrHeight + '|' )
	var docWidth  = (document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth - 15);
	var docHeight = (document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight));
	var returnValue = XorY_value;

	
	if ( XorY == 'X' )
	{
		if ( (docWidth - XorY_value - 125) < objectWidthOrHeight)
		  returnValue = XorY_value - objectWidthOrHeight; // Move to the left side of the cursor
		else 
		  returnValue = XorY_value;
	}
	else if ( XorY == 'Xcenter' )
	{
		returnValue = ( (docWidth - objectWidthOrHeight) / 2) - 230;
	}
	else
	{
		if ( (docHeight - XorY_value) < objectWidthOrHeight)
		  returnValue = XorY_value - Math.max(0,(10 + objectWidthOrHeight + XorY_value - docHeight - truebody().scrollTop));
		else
		  returnValue = XorY_value;
	}
	//alert('XorY = |' + XorY + '| result = |' + returnValue + '|');
	return returnValue;
}

function isInternetExplorer()
{
	var browser	= navigator.appName;

	if ((browser == "Microsoft Internet Explorer") || ("MSIE"))
	  return true
	else
	  return false;
}

function isInternetExplorerVer8()
{
	var browser_version	= navigator.appVersion;
	var msieAcht		= browser_version.indexOf("MSIE 8");

	if ( msieAcht > 1 )
	  return true
	else
	  return false;
}

function isNetscape()
{
	var browser		= navigator.appName;
	var b_version	= navigator.appVersion;
	var version		= parseFloat(b_version);

	if (browser == "Netscape")
	  return true
	else
	  return false;
}
