/** 
 * @fileoverview gallery.js: Display a gallery
 *
 * @author Andreas Schosser {@link http://www.galayxn.de/ www.galayxn.de}
 */

/**
 * Class for displaying a gallery of elements (images)
 *
 * @param {int} maxWidth (optional) maximum width of big image
 * @param {int} maxMag (optional) magnification factor
 * @param {int} startMag (optional) magnification factor of first image
 * @param {string} thumbnailDir (optional) directory containing the thumbnails
 * @param {string} galleryPrefix (optional) prefix to display multiple galleries on one page
 * @param {string} page (optional) Seite, auf der die Galerie eingebunden ist
 * @version 0.3
 * @author Andreas Schosser
 * @constructor
 */
function Gallery(maxWidth, maxMag, startMag, thumbnailDir, galleryPrefix, page) {
	/**
	 * maximum width of magnified image
	 * @type int
	 */
	var maxWidthDefault = 600;

	var maxMagDefault = 4;

	if (!maxWidth) {
		maxWidth = maxWidthDefault;
	}

	if (!maxMag) {
		maxMag = maxMagDefault;
	}

	if (!thumbnailDir) {
		var thumbnailDir = "thumb";
	}
	
	if (!galleryPrefix) {
		galleryPrefix = '';
	}

	if (!page) {
		page = '';
	}

	var objPrefix = galleryPrefix + "img_";

	var bigID = 0;

	var offset = 0;	


	/**
	 * contains elements to be displayed in gallery
	 * @type array of objects
	 */
	this.items = new Array;
	var i = 0;
	while (document.getElementById(objPrefix + i)) {
		var item = new Array;
		item[0] = new Image(objPrefix + i);
		item[1] = new Image(objPrefix + i);
		var itemSmall = item[0];
		var itemBig = item[1];
		var src = new Path(itemSmall.src);
		if (i == bigID) {
			var srcArray = new Array(src.dirFull, thumbnailDir, src.filename);
			itemSmall.src = srcArray.join("/");
			if (startMag) {
				var mag = startMag;
			} else {
				var mag = magnification(itemBig.width);
			}
			itemSmall.width = itemBig.width / mag;
			itemSmall.height = itemBig.height / mag;
			itemSmall.showTitle();
		} else {
			var srcArray = new Array(src.dirTrunc, src.filename);
			itemBig.src = srcArray.join("/");
			var mag = magnification(itemSmall.width * maxMag);
			itemBig.width = itemSmall.width * mag;
			itemBig.height = itemBig.height * mag;
		}
		if (document.getElementById(objPrefix + i).parentNode.href) {
			item[2] = document.getElementById(objPrefix + i).parentNode.href;
		}
		this.items[i] = item;
		++i;
	}	
	
	function magnification (width) {
		var nw = Math.min(maxWidth, width);
		return (maxMag * nw / width);
	}
	
	/**
 	 * This function displays a magnified version of an image with a given id
 	 *
 	 * @param {int} id ID of the image to be displayed
 	 * @returns false
 	 * @author Andreas Schosser
 	 */
	this.show = function(imgNo) {
		id = (imgNo + offset) % this.items.length;
		// display thumbnail first
		this.items[bigID][0].replace(this.items[id][0]);
		
		// display the big image
		this.items[bigID][0].replace(this.items[id][1]);
		
		// show alt tag
		this.items[id][1].showTitle();

		// replace link of big image
		if (document.getElementById(this.items[bigID][0].id).parentNode.href) {
			document.getElementById(this.items[bigID][0].id).parentNode.href = this.items[id][2];
		}

		for (i = 0; i < this.items.length; ++i) {
			newindex = (i + id) % this.items.length;
		
			if (i != bigID) {
				// display thumbnail
				this.items[i][0].replace(this.items[newindex][0]);
			}
		}
		offset = (imgNo + offset) % this.items.length;
		return false;
	}

	return this;
}

/**
 * Returns values of an image-object
 *
 * @param {string} id id of the image
 * @returns image-object
 * @author Andreas Schosser
 * @constructor
 */
function Image(id) {

	/**
	 * id of the image as defined in html-page
	 * @type string
	 */
	this.id = id;

	/**
	 * uri of the image
	 * @type string
	 */
	this.src = document.getElementById(id).src;

	/**
	 * text within alt-tag
	 * @type string
	 */
	this.title = document.getElementById(id).alt;

	/**
	 * width of image
	 * @type int
	 */
	this.width = document.getElementById(id).width;

	/**
	 * height of image
	 * @type int
	 */
	this.height = document.getElementById(id).height;

	/**
	 * this function replaces the image with another one
	 *
	 * @param {object} obj Object that replaces the actual image
	 * @author Andreas Schosser
	 */
	this.replace = function(obj) {
		if (document.getElementById(this.id)) {
			document.getElementById(this.id).width = obj.width;
			document.getElementById(this.id).height = obj.height;
			document.getElementById(this.id).src = obj.src;
			document.getElementById(this.id).alt = obj.title;
		}
	}
	
	/**
	 * this function displays the content of the alt-tag if an object
	 * 'desc' exists within the page
	 *
	 * @author Andreas Schosser
	 */
	this.showTitle = function() {
		if (document.getElementById('desc')) {
			if (this.title) {
				document.getElementById('desc').innerHTML = this.title;
			} else {
				document.getElementById('desc').innerHTML = "&nbsp;";
			}
		}
	}
	
	return this;
}

/**
 * @param {string} path
 * @returns path-Object
 * @author Andreas Schosser
 * @constructor
 */
function Path(path) {
	var delim = "/";
	var elems = path.split(delim);

	this.path = path;
	this.filename = elems[elems.length - 1];
	elems.length = elems.length -1;
	this.dirFull = elems.join(delim);
	elems.length = elems.length -1;
	this.dirTrunc = elems.join(delim);
	return this;
}


