
function PictureGallery(srcList, altList, widthList, heightList,
	legendId, imgId, divId, numId, copyrightId,
	imgDivLeft, imgDivTop, imgMaxWidth, imgMaxHeight)
{
	this.srcList    = srcList;
	this.altList    = altList;
	this.widthList  = widthList;
	this.heightList = heightList;
	
	this.length = srcList.length;
	this.last   = this.length - 1;
	
	this.images = new Array(this.length);
	this.loaded = new Array(this.length);

	this.legendId    = legendId;
	this.imgId       = imgId;
	this.divId       = divId;
	this.numId       = numId;
	this.copyrightId = copyrightId;
	
	this.imgDivLeft   = imgDivLeft;
	this.imgDivTop    = imgDivTop;
	this.imgMaxWidth  = imgMaxWidth;
	this.imgMaxHeight = imgMaxHeight;
	
	this.i = 0;
	
	if (!PictureGallery.prototype.init) {
		PictureGallery.prototype.changeImage = _changeImage;
		PictureGallery.prototype.changeLayer = _changeLayer;
		PictureGallery.prototype.browse = _browse;
		PictureGallery.prototype.loadImage = _loadImage;
		PictureGallery.prototype._getStyleAttribute = __getStyleAttribute;
		PictureGallery.prototype._getElement = __getElement;
		PictureGallery.prototype._getNestedElement = __getNestedElement;
		PictureGallery.prototype._visibilityValue = __visibilityValue;
		PictureGallery.prototype._getImgTag = __getImgTag;
		PictureGallery.prototype._getNextIndex = __getNextIndex;
		PictureGallery.prototype._adjustDivPosition = __adjustDivPosition;
		PictureGallery.prototype._errorAlert = __errorAlert;
		PictureGallery.prototype.rollover = _rollover;
		PictureGallery.prototype.loadMouseOverButtons = _loadMouseOverButtons;
		
		var _dhtmlType = 'none';
		if (window.opera && (!document.createElement || !document.createTextNode)) {
			_dhtmlType = 'OP';
		}
		else if (document.getElementById) {
			_dhtmlType = 'DOM';
		}
		else if (document.all) {
			_dhtmlType = 'MS';
		}
		else if (document.layers) {
			_dhtmlType = 'NS4';
		}
		PictureGallery.prototype.dhtmlType = _dhtmlType;
		PictureGallery.prototype.init  = 1;
	}
	
	this.loadImage(0);
	this.loadImage(this._getNextIndex());
}

function __getNextIndex()
{
	var next;
	if (this.i == this.last) {
		next = this.last;
	}
	else {
		next = this.i + 1;
	}
	return next;
}

function _loadImage(index)
{
	if (this.loaded[index]) return;

	this.images[index] = new Image();
	this.images[index].src = this.srcList[index];
	this.loaded[index] = 1;
}
	
function _changeLayer(id)
{
	for (var j = 0; j < this.length; j++) {
		var styleAttrib = this._getStyleAttribute(this._getElement(id + j));

		if (!styleAttrib) continue;
		
		if (this.i == j) {
			styleAttrib.visibility = this._visibilityValue('visible');
		}
		else {
			styleAttrib.visibility = this._visibilityValue('hidden');
		}
	}
}

function _changeImage()
{
	var div = this._getElement(this.divId);
	
	if (!div) {
		this._errorAlert();
		return;
	}
	
	this._adjustDivPosition(div);
	
	var width  = this.widthList[this.i];
	var height = this.heightList[this.i];
	
	if (this.dhtmlType == 'DOM') {
		var img = document.createElement('img');
		img.setAttribute('src', this.images[this.i].src);
		img.setAttribute('alt', this.altList[this.i] || '');
		img.setAttribute('title', this.altList[this.i] || '');
		img.setAttribute('id', this.imgId);
		img.setAttribute('border', 0);
		if (width) img.setAttribute('width', width);
		if (height) img.setAttribute('height', height);
		
		var oldImg = this._getElement(this.imgId);
		if (!oldImg) {
			div.appendChild(img);
		}
		else {
			oldImg.parentNode.replaceChild(img, oldImg);
		}

		return;
	}
	
	if (this.dhtmlType == 'OP') {
		var img = this._getElement(this.imgId);
		if (!img) {
			this._errorAlert();
			return;
		}
		img.setAttribute('src', this.images[this.i].src);
		img.setAttribute('alt', this.altList[this.i] || '');
		img.setAttribute('title', this.altList[this.i] || '');
		img.setAttribute('id', this.imgId);
		img.setAttribute('border', 0);

		if (width || height) {
			// Helps with Opera 6, but not with Opera 5.
			var styleAttrib = this._getStyleAttribute(img);
			if (styleAttrib) {
				if (width)  styleAttrib.width = width;
				if (height) styleAttrib.height = height;
			}
		}
		
		return;
	}
	
	var imgTag = this._getImgTag();

	if (this.dhtmlType == 'MS') {
		div.innerHTML = imgTag;
	}
	else if (this.dhtmlType == 'NS4') {
		div.document.open();
		div.document.write(imgTag);
		div.document.close();
	}
}

function _browse(direction)
{
	if (this.length == 0) return;
	
	if (this.dhtmlType == 'none') {
		alert ('Dein Browser unterstützt leider nicht das Blättern ' +
			'in der Bildergalerie.');
		return;
	}
	
	if (!direction) direction = '';
	
	if (direction == 'last') {
		if (this.i == this.last) return;
		this.i = this.last;
	}
	else if (direction == 'next') {
		if (this.i == this.last) return;
		if (++this.i > this.last) this.i = this.last;
	}
	else if (direction == 'previous') {
		if (this.i == 0) return;
		if (--this.i < 0) this.i = 0;
	}
	else {
		if (this.i == 0) return;
		this.i = 0;
	}
	
	this.loadImage(this.i);
	
	this.changeImage();
	this.changeLayer(this.legendId);
	this.changeLayer(this.numId);
	this.changeLayer(this.copyrightId);

	this.loadImage(this._getNextIndex());
}

function __adjustDivPosition(div)
{
	if (!div) return;
	
	var divStyle = this._getStyleAttribute(div);
	
	if (divStyle) {
		divStyle.left = parseInt(this.imgDivLeft + (this.imgMaxWidth - this.widthList[this.i]) / 2);
		divStyle.top  = parseInt(this.imgDivTop  + (this.imgMaxHeight - this.heightList[this.i]) / 2);
	}
}

function __errorAlert(msg)
{
	if (!msg) {
		msg = 'Es ist ein Fehler aufgetreten.\n' + 
			'Blättern in der Bildergalerie ist leider nicht möglich.';
	}
	alert (msg);
}

function __getStyleAttribute(element)
{
	if (!element) return;
	if (this.dhtmlType == 'DOM') return element.style
	if (this.dhtmlType == 'MS')  return element.style;
	if (this.dhtmlType == 'OP')  return element.style
	if (this.dhtmlType == 'NS4') return element;
}

function __getElement(id)
{
	if (this.dhtmlType == 'DOM') return document.getElementById(id);
	if (this.dhtmlType == 'MS')  return document.all[id];
	if (this.dhtmlType == 'OP')  return document.getElementById(id);
	if (this.dhtmlType == 'NS4') return document[id];
}

function __getNestedElement(outerId, innerId)
{
	if (this.dhtmlType == 'NS4') {
		var outer = document[outerId];
		if (outer) return outer.document[innerId];
		return;
	}
	return this._getElement(innerId);
}

function __visibilityValue(what)
{
	if (this.dhtmlType == 'NS4') {
		if (what == 'hidden')  return 'hide';
		if (what == 'visible') return 'show';
	}
	return what;
}

function __getImgTag()
{
	var dimensions = '';
	if (this.widthList[this.i]) {
		dimensions += ' width="' + this.widthList[this.i] + '"';
	}
	if (this.heightList[this.i]) {
		dimensions += ' height="' +  this.heightList[this.i] + '"';
	}
	
	var img = '<img src="' + this.images[this.i].src +
		'" alt="' + this.altList[this.i] +
		'" title="' + this.altList[this.i] +
		'" id="' + this.imgId + '"' +
		dimensions + ' border="0">';

	return img;
}

function _rollover(num, path, divId, imgId, action)
{
	var id;
	
	if (action == 'over')     id = 'a';
	else if (action == 'out') id = 'i';
	else {return};

	var img = this._getNestedElement(divId + num, imgId + num);
	
	img.src = path + id + num + '.gif';
}

function _loadMouseOverButtons(count, path)
{
	images = new Array();
	for (i=1; i<=count; i++) {
		images[i] = new Image();
		images[i].src = path + 'a' + i + '.gif';
	}
}


