/*
** Float an image on the page.
** Copyright Dolphin Classical
*/

/*
** Global scope
*/
var floatingimage;
var floatingdiv;
var thimage;
var	image = new Image();
var thpos = new Array();
var deltah = 1; // computed later
var deltaw = 6;
var deltat = 5; // time delay


/*
** Setup for floating the image. The rest will
** be done from the timer callback.
**
** ARGUMENTS:
**  imgurl .... the url of the image to display
**  thindex ... to form the name of the thumbnail image 
**              (to get the position of the float)
**  visible ... are we displaying or hidding the floating image?
*/
function float_image(imgurl, thindex, legendtext, visible)
{
	// Get floating image
    floatingimage = document.getElementById('floatingimage');
	if (!floatingimage)
		return;
	
	// Get div in which we display the float
    floatingdiv = document.getElementById('floatingimagediv');
	if (!floatingdiv)
		return;

	// Get thumbnail image (to position the float)
	thimage = document.getElementById('thimage_' + thindex);
	if (!thimage)
		return;

	// Are we hiding the image?
	if (visible == false)
	{
		/* Hide the div */
		floatingdiv.style.display = "none"; 
		return;
	}
		
	// Create image object (to access dimensions, ...)
	image.src = imgurl;
	
	// Assign image to float
	floatingimage.src = imgurl;
	
	// Find where to float the image on the page (absolute)
	thpos = findPos(thimage);
	thpos[0] = parseInt(thpos[0]); // remove eventual 'px' at the end
	thpos[1] = parseInt(thpos[1]);
	
	// Resize floating image - Start with 0 width and height 
	floatingimage.width = 0;
	floatingimage.height = 0;
	
	// Position floating image div
	floatingdiv.style.left = thpos[0] + parseInt(thimage.width) - parseInt(image.width) + 'px'; 
	floatingdiv.style.top = thpos[1] + 'px';

	// Text for the legend
    var legend = document.getElementById('floatlegend');
	if (legendtext)
		legend.innerHTML = legendtext + '<br>(Click image to close it)';
	else
		legend.innerHTML = '';
	

	// Display it 
	floatingdiv.style.display = "block"; 
	
	// The zoom animation is done here
	setTimeout("resize()", deltat);
}


/*
** Progressive resizing of the floating image.
*/
function resize()
{
	// Get dimensions
	var flwidth = parseInt(floatingimage.width);
	var flheight = parseInt(floatingimage.height);
	var imwidth = parseInt(image.width);
	var imheight = parseInt(image.height);
	
	// Get image ratio
	var ratio = imwidth / imheight;
	
	// Are we done? 
	if (flwidth < imwidth && flheight < imheight)
	{
		// Delta for animation
		deltah = deltaw / ratio;
		if (deltah < 1)
			deltah = 1;
			
		// Resize image
		floatingimage.width += deltaw;
		floatingimage.height += deltah;
		
		// Sanity check
		if (floatingimage.width > imwidth)
			floatingimage.width = imwidth;
		if (floatingimage.height > imheight)
			floatingimage.height = imheight;
		
		// Repeat
		setTimeout(resize, deltat);
	}
	else
	{
		// Just to be safe
		floatingimage.width = imwidth;
		floatingimage.height = imheight;
	}
}


/*
** Find the obsolute position of an element on the page.
**
** ARGUMENT:
** The object of which we want to get the absolute position.
*/
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];
}

