function imagegallery2_init()
{
dojo.require("dojo.fx");
dojo.addOnLoad( function() {
	var gallerybutton = dojo.byId("gallerybutton");
	if(gallerybutton)
	{
		dojo.connect(gallerybutton, "onclick", function(evt) {
			var galleryframe = dojo.byId("galleryframe");
			galleryframe.style.opacity = 0.0;
	    	galleryframe.style.visibility = "visible";
			dojo.fx.chain([
				dojo.fadeIn({node:galleryframe}),
				dojo.animateProperty({
					node:galleryframe,
					duration:800,
					properties: {
						width: {start: '0', end: '1000'}
					}
				}),
				dojo.animateProperty({
					node:galleryframe,
					duration:800,
					properties: {
						height: {start: '0', end: '750'}
					}
				})
			]).play();
		});
	}
	
	var imageEl = dojo.byId("currentimage");
	if(imageEl)
	{
		dojo.connect(imageEl, "load", function(evt) {
			imagegallery2_resizeimage(imageEl);
		});
		// and force call on page load:
		imagegallery2_resizeimage(imageEl);
	}
});

}


function imagegallery2_open(/*String*/ url)
{
	var win = window.open(url,'imagegallery2',"toolbar=no, width=1150, height=800, directories=no, status=no, scrollbars=yes, resizable=yes, menubar=no");
	win.focus();
	return false;
}

function imagegallery2_resizeimage(/*element*/ currentimageEl)
{
	if(isIE)
	{
		var maxWidth = 480;
		var maxHeight = 320;
		// resize... as this thing does not interpret max-width/max-height:
		
		var newImg = new Image();
		newImg.src = currentimageEl.src;
		var oldWidth = newImg.width;
		var oldHeight = newImg.height;
		if(oldWidth == 0 || oldHeight == 0)
		{ return; } // seems to happen sometimes...
		
		var widthFactor = maxWidth / oldWidth;
		var heightFactor = maxHeight / oldHeight;
		var resizeFactor = Math.min(widthFactor, heightFactor);
		var newWidth = oldWidth * resizeFactor;
		var newHeight = oldHeight * resizeFactor;
		
	//alert("width="+oldWidth+" height="+oldHeight+" wFactor="+widthFactor+" hFactor="+heightFactor+" newWidth="+newWidth+" newHeight="+newHeight);
	
		currentimageEl.width = newWidth;
		currentimageEl.height = newHeight;
		currentimageEl.style.visibility = 'visible';
	}
}


function displayimage(/*int*/ imageid)
{
	var currentimageEl = dojo.byId("currentimage");
	if(isIE)
	{
		currentimageEl.style.visibility = 'hidden';
	}
	currentimageEl.src='imageshow?format=Big&id='+imageid;
	//imagegallery2_resizeimage(currentimageEl);
	
	currentimage=imageid;
}

function getCurrentIndex(/*int*/ imageid)
{
	for(var i=0; i<galleryimagelist.length; i++)
	{
		if(galleryimagelist[i] == imageid)
		{
			return i;
		}
	}
	
	return -1;
}

function nextimage()
{
	var currentIndex = getCurrentIndex(currentimage);

	// current image not found:
	if(currentIndex == -1)
	{ return; }
	
	// nothing to do if last image:
	if(currentIndex == galleryimagelist.length)
	{ return; }

	currentimage = galleryimagelist[currentIndex+1];
	//alert("currentIndex="+currentIndex+" currentimage="+currentimage);
	var currentimageEl = dojo.byId("currentimage");
	if(isIE)
	{
		currentimageEl.style.visibility = 'hidden';
	}
	currentimageEl.src='imageshow?format=Big&id='+currentimage;
	//imagegallery2_resizeimage(currentimageEl);
}

function previousimage()
{
	var currentIndex = getCurrentIndex(currentimage);

	// current image not found:
	if(currentIndex == -1)
	{ return; }

	// nothing to do if first image:
	if(currentIndex == 0)
	{ return; }

	currentimage = galleryimagelist[currentIndex-1];
	var currentimageEl = dojo.byId("currentimage");
	if(isIE)
	{
		currentimageEl.style.visibility = 'hidden';
	}
	currentimageEl.src='imageshow?format=Big&id='+currentimage;
	//imagegallery2_resizeimage(currentimageEl);
}
