var currentPhoto = null;
var stepPhoto    = 20;

function showPhoto(item)
{
	var photo    = findChildWithClassName(item, 'photo');
	var vignette = findChildWithClassName(item, 'vignette');
	var targetY  = photo.offsetWidth;
	
	photo.style.width  = vignette.offsetWidth + 'px';
	photo.style.marginLeft = '0px';
	photo.style.marginTop  = '0px';
	photo.setAttribute('targetY', targetY);
	
	if( photo.complete )
	{
		zoomPhoto(item, photo, vignette, targetY);
	}
	else
	{
		photo.onload = function() {zoomPhoto(item, photo, vignette, targetY)};
	}
}

function zoomPhoto(item, photo, vignette, targetY)
{
	if( photo.getAttribute('save') != null && photo.getAttribute('save') != '')
	{
		photo.style.marginLeft = photo.getAttribute('save');
		photo.setAttribute('save', null);
	}
	
	if( currentPhoto != null && currentPhoto != item)
	{
		var pphoto    = findChildWithClassName(currentPhoto, 'photo');
		var pvignette = findChildWithClassName(currentPhoto, 'vignette');
		var ptargetY  = pphoto.offsetWidth;
		pphoto.style.zIndex = 19;
		
		unzoomPhoto(currentPhoto, pphoto, pvignette, ptargetY, item);
		
		currentPhoto = null;
	}
	
	
	var w = photo.offsetWidth + stepPhoto;
	var l, t ;
	if( photo.style.marginLeft == '' || photo.style.marginLeft == '-3000px' )
	{
		l = 0;
	}
	else
	{
		l = parseInt(photo.style.marginLeft);
	}
	
	if( photo.style.marginTop == '' )
	{
		t = 0;
	}
	else
	{
		t = parseInt(photo.style.marginTop);
	}
	
	l = l - stepPhoto/1.2;
	t = t - stepPhoto/1.1;
	
	photo.style.width = w + 'px';
	photo.style.marginLeft = l + 'px';
	photo.style.marginTop = t + 'px';
	photo.style.zIndex = 20;
	
	if( w < targetY )
	{
		setTimeout(function() {zoomPhoto(item, photo, vignette, targetY)}, 1);
	}
	else
	{
		item.onclick = function() {unzoomPhoto(item, photo, vignette, targetY)};
		currentPhoto = item;
		
		var desc    = findChildWithClassName(item, 'desc');
		if( desc != null )
		{
			desc.style.marginTop = (photo.offsetHeight / 2 + 66) + 'px';
			desc.style.marginLeft = '-139px';
			desc.style.width = (photo.offsetWidth  - 20) + 'px';
		}
	}
}	

function unzoomPhoto(item, photo, vignette, targetY)
{
	var w = photo.offsetWidth - stepPhoto;
	
	var desc    = findChildWithClassName(item, 'desc');
	if( desc != null )
	{
		desc.style.marginLeft = '-3000px';
	}

	var l = parseInt(photo.style.marginLeft) + stepPhoto/4.3;
	var t = parseInt(photo.style.marginTop) + stepPhoto/3.7;
	
	photo.style.width = w + 'px';
	photo.style.marginLeft = l + 'px';
	photo.style.marginTop = t + 'px';
	
	if( w > vignette.offsetWidth )
	{
		setTimeout(function() {unzoomPhoto(item, photo, vignette, targetY)}, 1);
	}
	else
	{
		if( w != vignette.offsetWidth )
		{
			photo.style.width = vignette.offsetWidth + 'px';
		}
		
		var tY = photo.getAttribute('targetY');
		
		item.onclick = function() {
			photo.style.width  = vignette.offsetWidth + 'px';
			photo.style.marginLeft = '0px';
			photo.style.marginTop  = '0px';
			photo.setAttribute('save', null);
			zoomPhoto(item, photo, vignette, tY);
		}
		photo.setAttribute('save', photo.style.marginLeft);
		photo.style.marginLeft = '-3000px';
	}
}	

function findChildWithClassName(elt, className)
{
	var c = null;
	
	for(var i = 0; i < elt.childNodes.length; i ++ )
	{
		var child = elt.childNodes[i];
		
		if( child.nodeType != 1 )
		{
			continue;
		}
		
		if( child.className == className )
		{
			return child;
		}
		
		c = findChildWithClassName(child, className);
		
		if( c != null )
		{
			break;
		}
		
	}
	
	return c;
}