/*  Preview Image

    Author : SBT (Scientific Brain Training) Company - GLATIGNY Jérôme

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

var previewBox;

function initPreviews(start)
{
	var elements = start.getElementsByTagName("img");
	for( var i = elements.length - 1; i >= 0; i-- )
	{
		if( elements[i].className == "imgPreview" )
		{
			elements[i].className = "";
			elements[i].onmouseover = function(){showPreview(this);}
		}
	}
}

function showPreview(elem)
{
	if( (previewBox == null) || typeof(previewBox) == "undefined" )
	{
		previewBox = document.createElement('div');
		previewBox.className = "previewbox";
		previewBox.onmouseout = function(){ hidePreview(); }
		// Change here the content of the preview box
		previewBox.innerHTML = "<div class=\"content\"><a href=\"#\"><img alt=\"\" src=\"\" border=\"0\"/></a></div></div>";
		previewBox.style.display = "none";
		document.body.appendChild(previewBox);
	}
	
	previewBox.firstChild.firstChild.href = elem.parentNode.href;
	var img = previewBox.firstChild.firstChild.firstChild;
	img.src = elem.src;
	img.alt = elem.alt;
		
	var pos = findPos(elem);
	
	// Show the previewbox before place it (in order to read the offsetWidth and offsetHeight values)
	previewBox.style.display = "block";
	
	var leftPos = pos[0] - parseInt( (previewBox.offsetWidth - elem.offsetWidth) / 2 );
	if( leftPos < 0) leftPos = 0;
	previewBox.style.left = leftPos + "px";
	previewBox.style.top = pos[1] - parseInt( (previewBox.offsetHeight - elem.offsetHeight) / 2 ) + "px";
}

function hidePreview()
{
	previewBox.style.display = "none";
}

function findPos(elem) {
	var leftPos = 0;
	var topPos = 0;
	while( elem != null )
	{		
		leftPos += elem.offsetLeft;
		topPos += elem.offsetTop;
		elem = elem.offsetParent;
	}
	return [leftPos,topPos];
} 