
function Encyclopedia(type, encyclopediaBackground, articleBackground)
{
	if (!Encyclopedia.prototype.init) {
		Encyclopedia.prototype.init = _init;
		Encyclopedia.prototype.setLetterId = _setLetterId;
		Encyclopedia.prototype.getActiveStyle = _getActiveStyle;
		Encyclopedia.prototype.getLogo = _getLogo;
		Encyclopedia.prototype.getLogoDivStart = _getLogoDivStart;
		Encyclopedia.prototype.getAlphaNavigationFrame = _getAlphaNavigationFrame;
		Encyclopedia.prototype.loadPage = _loadPage;
		Encyclopedia.prototype.addSearchString = _addSearchString;
		Encyclopedia.prototype.getBackgroundStyle = _getBackgroundStyle;
		Encyclopedia.prototype._getElement = __getElement;
		Encyclopedia.prototype._getStyleAttribute = __getStyleAttribute;
		Encyclopedia.prototype._ns4OnLoad = __ns4OnLoad;
		Encyclopedia.prototype.showBackToOverviewLink = _showBackToOverviewLink;
		Encyclopedia.prototype.testProperties = _testProperties;
		Encyclopedia.prototype.getDebugMessage = _getDebugMessage;
		Encyclopedia.prototype.addDebugMessage = _addDebugMessage;

		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';
		}
		Encyclopedia.prototype.dhtmlType = _dhtmlType;
	}
	
	this.debugMessage = '';

	this.init(type, encyclopediaBackground, articleBackground);
}

function _init(type, encyclopediaBackground, articleBackground)
{
	if (!type) return;
	
	this.type = type;

	if (type == 'encyclopedia') {
		this.isEncyclopedia = 1;
		this.backgroundImage = encyclopediaBackground;

		var encyclopediaPath = location.pathname;

		// Opera 5 Bug: 'location.pathname' returns the same value
		// as 'location.href', i.e. pathname with query string appended.
		var position = encyclopediaPath.indexOf('?');
		if (position != -1) {
			encyclopediaPath = encyclopediaPath.substr(0, position);
		}

		// Complete the path if called without 'index.html':
		if (encyclopediaPath.search(/\.html$/) == -1) {
			if (encyclopediaPath.charAt(encyclopediaPath.length - 1) == '/') {
				encyclopediaPath += 'index.html';
			}
			else {
				encyclopediaPath += '/index.html';
			}
		}

		var found = encyclopediaPath.match(/^(.*\/lexikon\/.*\/)([^\/]+)$/);

		if (found && found[1] && found[2]) {
			this.encyclopediaDirectory = found[1];
			this.encyclopediaFilename = found[2];
			this.encyclopediaPath = this.encyclopediaDirectory + this.encyclopediaFilename;
		}
	}
	else if (type == 'article') {
		var searchstring = location.search.substring(1);
		
		var found;
		
		if (searchstring) {
			found = searchstring.match(/^([a-z])-([0-9]+)-(.*\/lexikon\/.*\/)([^\/]+)$/);
		}
		
		if (found && found[1] && found[2] && found[3] && found[4]) {
			this.activeLetter = found[1];
			this.lcActiveLetter = this.activeLetter.toLowerCase();
			this.ucActiveLetter = this.activeLetter.toUpperCase();

			this.isLetterStart = found[2];
			this.encyclopediaDirectory = found[3];
			this.encyclopediaFilename = found[4];
			this.encyclopediaPath = this.encyclopediaDirectory + this.encyclopediaFilename;
			this.isEncyclopediaArticle = 1;
			this.backgroundImage = encyclopediaBackground;
		}
		else {
			this.backgroundImage = articleBackground;
			this.activeLetter = '';
		}
	}
	else if (type == 'alpha') {
		this.isAZNavigation = 1;
	}
	else {
		return;
	}

	if (!this.encyclopediaPath) return;

	this.alphaNavigationId = 'azNav';
	this.alphaNavigationPath = this.encyclopediaDirectory + this.alphaNavigationId + '.html';

	this.backToOverviewId = 'backToOverview';
	
	return 1;
}

// Called from encyclopedia.
function _setLetterId(letter, isLetterStart)
{
	this.activeLetter = letter || '';
	this.lcActiveLetter = this.activeLetter.toLowerCase();
	this.ucActiveLetter = this.activeLetter.toUpperCase();

	this.isLetterStart = isLetterStart || 0;
}

function __getElement(id, d)
{
	if (!d) d = document;
	// Does the variable contain a document?
	if (!d.domain) return;

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

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;
}

// Test function for property display.
function _testProperties(flag)
{
	if (!flag) return;
	
	alert ('type = ' + this.type + '\n'
		+ 'encyclopediaDirectory = ' + this.encyclopediaDirectory + '\n'
		+ 'encyclopediaFilename = ' + this.encyclopediaFilename + '\n'
		+ 'encyclopediaPath = ' + this.encyclopediaPath + '\n'
		+ 'backgroundImage = ' + this.backgroundImage + '\n'
		+ 'activeLetter = ' + this.activeLetter + '\n'
		+ 'isLetterStart = ' + this.isLetterStart + '\n'
		+ 'isEncyclopedia = ' + this.isEncyclopedia + '\n'
		+ 'isEncyclopediaArticle = ' + this.isEncyclopediaArticle + '\n');
}

// Test function for display of debug messages.
function _getDebugMessage()
{
	alert(this.debugMessage);
}

// Test function for adding debug messages.
function _addDebugMessage(string)
{
	this.debugMessage += string + '\n';
}

// Called from A-Z navigation and encyclopedia..
function _getActiveStyle(color)
{
	if (!color) color = '#ffffff';
	if (!this.activeLetter) return '';

	return '<style type="text/css">'
		+ '#' + this.lcActiveLetter + 'Letter {color:' + color + ';}'
		+ '</style>';
}

// Called from A-Z navigation
function _loadPage(letter, absolutePage)
{
	if (!letter) letter = this.lcActiveLetter;
	
	if (!this.encyclopediaPath) return void 0;

	var lcLetter = letter.toLowerCase();
	
	if (this.lcActiveLetter == lcLetter) {
		// Case 'isEncyclopedia' is not needed
		// anymore. Encyclopedia A-Z navigation
		// does not use javascript.
		if (this.isEncyclopedia) {
			if (this.isLetterStart) {
				return void 0;
			}
			else {
				var filename;
				if (absolutePage == 1) filename = 'index.html';
				else {filename = 'index' + absolutePage + '.html';}

				parent.location.href = this.encyclopediaDirectory + filename;
				return void 0;
			}
		}
		else {
			parent.location.href = this.encyclopediaPath;
			return void 0;
		}
	}
	else {
		var filename;
		if (absolutePage == 1) filename = 'index.html';
		else {filename = 'index' + absolutePage + '.html';}

		parent.location.href = this.encyclopediaDirectory + filename;
		return void 0;
	}
}

// Called from article and encyclopedia.
function _addSearchString(href)
{
	if (!href.match(/var/)) {
		if (this.isEncyclopedia || this.isEncyclopediaArticle) {
			if (!(this.activeLetter && this.encyclopediaPath)) return href;
		
			// Prevent appending of multiple searchstrings on double clicks.
			if (href.indexOf('?') != -1) return href;
		
			return href + '?' + this.activeLetter + '-'
				+ this.isLetterStart + '-'
				+ this.encyclopediaPath;
		}
		return href;
	} else {
		EVAL(href);
	}
}

// Called from article and encyclopedia.
function _getBackgroundStyle()
{
	if (!this.backgroundImage) return '';
	
	return '<style type="text/css">'
		+ '.bg_content {background-image:url(' + this.backgroundImage + ');'
		+ 'background-repeat:no-repeat;}'
		+ '</style>';
}

// Called from article and encyclopedia.
function _getAlphaNavigationFrame(left,top,width,height)
{
	if (!this.alphaNavigationPath) return '';
	if (!(this.isEncyclopedia || this.isEncyclopediaArticle)) return '';

	var id = this.alphaNavigationId;
	var dimensions = 'left:' + left + 'px;top:' + top + 'px;'
		+ 'width:' + width + 'px;height:' + height + 'px;'

	if (this.dhtmlType == 'NS4') {
		return '<div id="' + id + '" style="position:absolute;' + dimensions + '">'
			+ '</div>';
	}
	else if (this.dhtmlType != 'none') {
		return '<div style="position:absolute;' + dimensions + '">'
			+ '<iframe src="' + this.alphaNavigationPath + '" '
			+ 'id="' + id + '" name="' + id + '" scrolling="no" '
			+ 'width="' + width + '" height="' + height + '" '
			+ 'marginwidth="0" marginheight="0" frameborder="0">'
			+ '</iframe>'
			+ '</div>';
	}
	else {
		return '';
	}
}

// Called from article.
function _getLogoDivStart(left,top,width,height,xOffset,id)
{
	if (this.isEncyclopediaArticle) left -= xOffset;
	
	var dimensions = 'left:' + left + 'px;top:' + top + 'px;'
		+ 'width:' + width + 'px;height:' + height + 'px;'

	return '<div id="' + id + '" style="position:absolute;' + dimensions + '; visibility:visible">';
}

// Obsolete. Kept for compatibility with old documents.
// Called from article.
function _getLogo(left,top,width,height,xOffset,id,src,alt)
{
	if (!src) return '';
	if (!alt) alt = '';
	
	if (this.isEncyclopediaArticle) left -= xOffset;
	
	var dimensions = 'left:' + left + 'px;top:' + top + 'px;'
		+ 'width:' + width + 'px;height:' + height + 'px;'

	return '<div id="' + id + '" style="position:absolute;' + dimensions + '; visibility:visible">'
		+ '<img src="' + src + '" width="' + width + '" height="' + height +'" alt="' + alt + '" border="0"></div>';
}

// Called from A-Z navigation.
function _showBackToOverviewLink(d)
{
	if (!d) return;
	if (!this.isEncyclopediaArticle) return;

	var styleAttrib = this._getStyleAttribute(this._getElement(this.backToOverviewId, d));
	
	if (styleAttrib) styleAttrib.visibility = 'visible';
}

// Not used. Could be called from article and encyclopedia.
// Unfortunately, there is no way to support NS 4.x due to
// various problems with document.write, Ttyle, nested divs ...
// Disabled.
function __ns4OnLoad()
{
	return;
	if (!this.dhtmlType == 'NS4') return;
	if (!this.alphaNavigationPath) return;
	
	var navLayer = this._getElement(this.alphaNavigationId);
	if (!navLayer) return;
	
	navLayer.src = this.alphaNavigationPath;
}


