/* -----------------------------------------------------------------
 *                       Aveghe Thumbnail Viewer
 * -----------------------------------------------------------------
 * Aveghe Thumbnail Viewer: your picture are great!
 * Copyright (C) 2009 Elia Contini
 * 
 * Aveghe Thumbnail Viewer 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.
 * 
 * Aveghe Thumbnail Viewer 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 Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

var exitMessage = 'Per chiudere premi ESC o fai click';
var loadingMessage = 'sto caricando ...';

//----------------- no modify beyond here -----------------
var imagesLink = document.getElementsByTagName('a');
var AvegheThumbnailViewer = document.createElement('div');
var image = null;

// AvegheThumbnailViewer div creation
AvegheThumbnailViewer.setAttribute('id', 'AvegheThumbnailViewer');
AvegheThumbnailViewer.setAttribute('style', 'display: block;' +
											'position: fixed;' +
											'left: -1000px;' +
											'top: -1000px;'
);
document.body.appendChild(AvegheThumbnailViewer);
	
// setting onclick event for every thumbnail
for(i = 0; i < imagesLink.length; i++)
	{
		if(imagesLink[i].rel == 'thumbnail')
			imagesLink[i].setAttribute('onclick', "return show('" + i + "')");
	}

// show an image
function show(index)
	{
		image = new Image();
		image.alt = imagesLink[index].getElementsByTagName('img')[0].alt;
		
		AvegheThumbnailViewer.innerHTML = loadingMessage;
		
		calculatePosition();
		
		image.onload = function(e)
						{
							AvegheThumbnailViewer.innerHTML = '<img src="' + image.src + '" width="' + image.width + '" height="' + image.height + '" alt="' + image.alt + '" />' +
							  '<p>' + exitMessage + '</p>';
							
							calculatePosition();
							
							//event listener registration
							AvegheThumbnailViewer.onclick = close; // closing by mouse
							document.onkeyup = close;              // closing by keyboard
							window.onresize = calculatePosition;   // position by window resize
						};				
		image.src = imagesLink[index].href;
		
		return false;
	}

// calculate the position of Aveghe Thumbnail Viewer within the browser window
function calculatePosition()
	{
		AvegheThumbnailViewerWidth = AvegheThumbnailViewer.offsetWidth;
		AvegheThumbnailViewerHeight = AvegheThumbnailViewer.offsetHeight;
		
		windowWidth = 0;
		windowHeight = 0;
		
		if(navigator.userAgent.indexOf('MSIE') == -1)
			{
				windowWidth = window.innerWidth;
				windowHeight = window.innerHeight;
			}
		else
			{
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			}
		
		positionX = (windowWidth / 2) - (AvegheThumbnailViewerWidth / 2);
		positionY = (windowHeight / 2) - (AvegheThumbnailViewerHeight / 2);
		
		AvegheThumbnailViewer.setAttribute('style', 'display: none;' +
												'position: fixed;' +
												'left: ' + positionX + 'px;' +
												'top: ' + positionY + 'px;'
									  );
		if(AvegheThumbnailViewer.style.display == 'none' && image != null)
			AvegheThumbnailViewer.style.display = 'block';
	}

// close Aveghe Thumbnail Viewer
function close(e)
	{
		// ESC key pressed or click
		eventCode = null;
		eventType = null;
		if(navigator.userAgent.indexOf('MSIE') == -1)
			{
				eventCode = e.which;
				eventType = e.type;
			}
		else
			{
				eventCode = window.event.keyCode;
				eventType = window.event.type;
			}
		
		if(eventCode == 27 || eventType == 'click')
			{
				AvegheThumbnailViewer.style.display = 'none';
				AvegheThumbnailViewer.innerHTML = '';
				image = null;
			}
	}