// JavaScript Document
var IMAGE_NAME_INDEX = 0;
var IMAGE_URL_INDEX = 1;

/** Global image index of big picture */
var bigImageIndex = -1;
/** Global index of link of big picture */
var bigLinkIndex = -1;
/** Link index in place where function showThumbnails() is called */
var globalLinkIndex = -1;
/* Image index in place where function showThumbnails() is called */
var globalImageIndex = -1;

/**
* Creates one thumbnail cell.
*/
function createThumbnail(imageIndex, cellWidth, allign, vallign) {
	var result = false;
	
	document.writeln("<td align=" + allign + " vallign=" + vallign + " width=" + cellWidth + "%>\n");
	
	if (imageIndex < images.length) {
		var imageName = images[imageIndex][IMAGE_NAME_INDEX];
	
		document.writeln("<a onMouseOver=\"showImage(" + (imageIndex + globalImageIndex) + 
			")\"><img name='" + imageName + "' src='" + THUMB_PATH + "/" + imageName + "'></img></a>\n");
		result = true;
	}
		
	document.writeln("</td>\n");
	
	return result;
}

/**
* Generates full view of thumbnails and big picture.
*/
function generateSlideshowView() {
	var cellWidth = (1 / cols) * 100;
	var verticalImagesCount = Math.floor((images.length - (2 * cols)) / 2);
	var imageCreated = false;
	var imageIndex = 0; // Index of processing picture from array
	
	globalImageIndex = document.images.length;
	globalLinkIndex = document.links.length;	
	if (verticalImagesCount <= 0) {
		verticalImagesCount = 1;
	}
	
	document.writeln("<table cellspacing=" + spaceBetweenCells + " cellpadding=0>");
	
	// Pierwsza linijka
	document.writeln("<tr>");
	for (i = 0; i < cols; i++) {
		
		if (i == 0) {
			imageCreated = createThumbnail(imageIndex, cellWidth, "right", "bottom");
		} else if (i == cols - 1) {
			imageCreated = createThumbnail(imageIndex, cellWidth, "left", "bottom");
		} else {
			imageCreated = createThumbnail(imageIndex, cellWidth, "center", "bottom");
		}

		if (imageCreated) {
			imageIndex++;
		}
		
	}
	document.writeln("</tr>");
	
	var first = false;
	
	for (i = 0; i < verticalImagesCount; i++) {
		document.writeln("<tr>");	
		
		imageCreated = createThumbnail(imageIndex, cellWidth, "right", "center");
		if (imageCreated) {
			imageIndex++;
		}
		
		if (!first) {
			document.writeln("<td align=center colspan=" + (cols - 2) + " rowspan=" + verticalImagesCount + ">");
			if (!disableLinking) {
				document.writeln("<a href=''>");
			}
			document.writeln("<img width=" + bigWidth + " height=" + bigHeight + " src='" + BIG_PATH + "/" + startImage + "'>");
			if (!disableLinking) {
				document.writeln("</a>");
			}
			document.writeln("</td>");
			bigImageIndex = imageIndex + globalImageIndex;
			bigLinkIndex = globalLinkIndex;
			imageIndex++;
			first = true;
		}
				
		imageCreated = createThumbnail(imageIndex, cellWidth, "left", "center");
		if (imageCreated) {
			imageIndex++;
		}
		
		document.writeln("</tr>");
	}
	
	// Ostatnia linijka
	document.writeln("<tr>");
	for (i = 0; i < cols; i++) {
		
		if (i == 0) {
			imageCreated = createThumbnail(imageIndex, cellWidth, "right", "top");
		} else if (i == cols - 1) {
			imageCreated = createThumbnail(imageIndex, cellWidth, "left", "top");
		} else {
			imageCreated = createThumbnail(imageIndex, cellWidth, "center", "top");
		}
		
		if (imageCreated) {
			imageIndex++;
		}
	}
	document.writeln("</tr>");
	
	document.writeln("</table>");
}

/**
* Function that shows big picture in the center and also change the url for the big
* picture if linkingFunctionality is enabled.
*/
function showImage(pageImageIndex) {
	var onImage = document.images[pageImageIndex];

	document.images[bigImageIndex].src = BIG_PATH + "/" + onImage.name;
	document.images[bigImageIndex].width = bigWidth;
	document.images[bigImageIndex].height = bigHeight;
	
	if (disableLinking) {
		return;
	}
	
	var url = images[pageImageIndex - globalImageIndex][IMAGE_URL_INDEX];
	
	if (url != null && url != "") {
		document.links[bigLinkIndex].href = url;
	} else {
		document.links[bigLinkIndex].href = "";
	}
}
