// JavaScript-Funktionen fuer die Bilder-Galerie
// Copyright (C) 2005-2007 Alexander Mueller
// Autor: Alexander Mueller
// Web:   http://www.EvoComp.de/
// Datei: galerie.js
// The copyright notice must stay intact for use!
// You can obtain this and other scripts at http://www.EvoComp.de/scripts/skripte.html
//
// This program is distributed in the hope that it will be useful,
// but without any warranty, expressed or implied.

var Photos = new Array();
var anzahl_beschreibungsfelder = 1;
var index_erstes_bild = 0;
var index_grosses_bild = 0;
var anzahl_bilder = 0;

function addPhoto (thumbnail, bild, alt, beschreibung) {
	Photos[Photos.length] = new Object();
	Photos[Photos.length - 1]["datei"] = thumbnail;
	Photos[Photos.length - 1]["datei_gross"] = bild;
	Photos[Photos.length - 1]["alt"] = alt;
	Photos[Photos.length - 1]["beschreibung"] = beschreibung;
	if (addPhoto.arguments.length > 4) {
		if (anzahl_beschreibungsfelder < addPhoto.arguments.length - 3)
			anzahl_beschreibungsfelder = addPhoto.arguments.length - 3;
		for (i = 4; i < addPhoto.arguments.length + 1; i++)
			Photos[Photos.length - 1]["beschreibung" + (i - 2)] = addPhoto.arguments[i];
	}
}

function initialisiereGalerie () {
	if (Photos.length > 0) {
		anzahl_bilder = Photos.length;
		if (bildindex_beim_laden > anzahl_bilder)
			bildindex_beim_laden = anzahl_bilder;
		if (anzahl_thumbnails > anzahl_bilder)
			anzahl_thumbnails = anzahl_bilder;
		if (anzahl_thumbnails < 1)
			anzahl_thumbnails = 1;

		erzeuge_thumbnails ();
		erzeuge_beschreibungsfelder ();
		thumbnails_auffrischen ();
		window.setInterval ("ausrichten ()", 5);
		if (bildindex_beim_laden)
			grosses_bild_anzeigen (bildindex_beim_laden);
	}
}

function convert_to_utf8 (text) {
/*	text = text.replace (/Ä/g, "\u00c4");
	text = text.replace (/Ü/g, "\u00dc");
	text = text.replace (/Ö/g, "\u00d6");
	text = text.replace (/ß/g, "\u00df");

	text = text.replace (/ü/g, "\u00fc");
	text = text.replace (/ä/g, "\u00e4");
	text = text.replace (/ö/g, "\u00f6");
	return text;*/
}

function zaehle_thumbnails () {
	var tn = 0;

	for (i = 0; i < document.images.length; i++)
		if ((document.images[i].id).match (/^thumbnail[0-9]+$/))
			tn++;
	return tn;
}

function erzeuge_beschreibungsfelder () {
	if (anzahl_beschreibungsfelder > 1)
		for (i = 0; i < anzahl_beschreibungsfelder; i++) {
			feld = document.createElement ("span");
			feld.id = "beschriftung" + (i + 1);
			feld.appendChild (document.createTextNode (""));
			document.getElementById ("beschriftung").appendChild (feld);
		}
}

function erzeuge_thumbnails () {
	for (i = 0; i < anzahl_thumbnails; i++) {
		eintrag = document.createElement ("li");
		link = document.createElement ("a");
		link.href = "javascript:grosses_bild_anzeigen (" + (i + 1) + ")";
		bild = document.createElement ("img");
		bild.border = "0";
		bild.id = "thumbnail" + (i + 1);
		bild.className = "thumbnail";
		bild.onmouseover = sethover;
		bild.onmouseout = unsethover;

		link.appendChild (bild);
		eintrag.appendChild (link);

		document.getElementById ("thumbs").appendChild (eintrag);
	}
}

function getIndexOfSelectedThumbnail (id) {
	if (/thumbnail([0-9]+)/.exec (id))
		return parseInt (RegExp.$1);
	return 0;
}

function sethover (event) {
	this.className = "thumbnail_hover";
}

function unsethover (event) {
	if (index_erstes_bild + getIndexOfSelectedThumbnail (this.id) - 1 == index_grosses_bild)
		this.className = "thumbnail_selected";
	else
		this.className = "thumbnail";
}

function ausrichten () {
	for (i = 0; i < anzahl_thumbnails; i++) {
		document.getElementById ('thumbnail' + (i + 1)).vspace = (thumb_groesse - document.getElementById ('thumbnail' + (i + 1)).height) / 2;
		document.getElementById ('thumbnail' + (i + 1)).hspace = (thumb_groesse - document.getElementById ('thumbnail' + (i + 1)).width) / 2;
	}
	document.getElementById ('zurueck').vspace = (thumb_groesse - document.getElementById ('zurueck').height) / 2;
	document.getElementById ('pgup').vspace = (thumb_groesse - document.getElementById ('pgup').height) / 2;
	document.getElementById ('weiter').vspace = (thumb_groesse - document.getElementById ('weiter').height) / 2;
	document.getElementById ('pgdown').vspace = (thumb_groesse - document.getElementById ('pgdown').height) / 2;
}

function thumbnails_auffrischen () {
	for (i = 0; i < anzahl_thumbnails; i++) {
		document.getElementById ('thumbnail' + (i + 1)).src = Photos[index_erstes_bild + i]["datei"];
		document.getElementById ('thumbnail' + (i + 1)).alt = Photos[index_erstes_bild + i]["alt"];
		document.getElementById ('thumbnail' + (i + 1)).title = Photos[index_erstes_bild + i]["alt"];
		if (index_grosses_bild == index_erstes_bild + i)
		    document.getElementById ('thumbnail' + (i + 1)).className = "thumbnail_selected";
		else
		    document.getElementById ('thumbnail' + (i + 1)).className = "thumbnail";
	}

	if (index_erstes_bild > 0) {
		document.getElementById ('zurueck').src = pfad_zu_navbildern + "/back.gif";
	}
	else {
		document.getElementById ('zurueck').src = pfad_zu_navbildern + "/first.gif";
	}
	if (index_erstes_bild + anzahl_thumbnails < anzahl_bilder) {
		document.getElementById ('weiter').src = pfad_zu_navbildern + "/forward.gif";
	}
	else {
		document.getElementById ('weiter').src = pfad_zu_navbildern + "/last.gif";
	}

	if (!document.getElementById ('thumb_beschriftung').firstChild)
		document.getElementById ('thumb_beschriftung').appendChild (document.createTextNode (""));
	tnstr = thumbnail_string.replace (/%index_erster%/i, (index_erstes_bild + 1));
	tnstr = tnstr.replace (/%index_letzter%/i, (index_erstes_bild + anzahl_thumbnails));
	tnstr = tnstr.replace (/%anzahl_bilder%/i, anzahl_bilder);
	document.getElementById ('thumb_beschriftung').firstChild.data = tnstr;
}

function zurueck () {
	if (index_erstes_bild > 0) {
		index_erstes_bild--;
		thumbnails_auffrischen ();
	}
}

function weiter () {
	if (!(index_erstes_bild + anzahl_thumbnails > anzahl_bilder - 1)) {
		index_erstes_bild++;
		thumbnails_auffrischen ();
	}
}

function pgup () {
	if (index_erstes_bild - anzahl_thumbnails > 0)
		index_erstes_bild = index_erstes_bild - anzahl_thumbnails;
	else
		index_erstes_bild = 0;
	thumbnails_auffrischen ();
}

function pgdown () {
	if (index_erstes_bild + 2 * anzahl_thumbnails < anzahl_bilder)
		index_erstes_bild = index_erstes_bild + anzahl_thumbnails;
	else
		index_erstes_bild = anzahl_bilder - anzahl_thumbnails;
	thumbnails_auffrischen ();
}

function grosses_bild_anzeigen (i) {
	index_grosses_bild = index_erstes_bild + i - 1;
	grosses_bild_auffrischen ();
}

function beschriftungsstring_ersetzen (beschriftung) {
	tstr = beschriftung.replace (/%index_gross%/i, (index_grosses_bild + 1));
	tstr = tstr.replace (/%anzahl_bilder%/i, anzahl_bilder);
	if (anzahl_beschreibungsfelder == 1)
		tstr = tstr.replace (/%beschreibung%/i, Photos[index_grosses_bild]["beschreibung"]);
	return tstr;
}

function grosses_bild_auffrischen () {
	document.getElementById ('gross').src = Photos[index_grosses_bild]["datei_gross"];
	document.getElementById ('gross').alt = Photos[index_grosses_bild]["alt"];
	document.getElementById ('gross').title = Photos[index_grosses_bild]["alt"];

	if (!document.getElementById ('fotonr').firstChild)
		document.getElementById ('fotonr').appendChild (document.createTextNode (""));
		document.getElementById ('fotonr').firstChild.data = (index_grosses_bild + 1) + " / " + anzahl_bilder;

	if (anzahl_beschreibungsfelder == 1) {
		if (!document.getElementById ('beschriftung').firstChild)
			document.getElementById ('beschriftung').appendChild (document.createTextNode (""));
		document.getElementById ('beschriftung').firstChild.data = beschriftungsstring_ersetzen (bild_beschriftung);
	}
	else {
		for (i = 0; i < anzahl_beschreibungsfelder; i++) {
			if (typeof Photos[index_grosses_bild]["beschreibung" + (i == 0 ? "" : (i + 1))] != "undefined")
				document.getElementById ('beschriftung' + (i + 1)).firstChild.data = beschriftungsstring_ersetzen (Photos[index_grosses_bild]["beschreibung" + (i == 0 ? "" : (i + 1))]);
			else
				document.getElementById ('beschriftung' + (i + 1)).firstChild.data = "";
		}
	}

	if (index_grosses_bild > 0)
		document.getElementById ('zurueck_gross').src = pfad_zu_navbildern + "/back.gif";
	else
		document.getElementById ('zurueck_gross').src = pfad_zu_navbildern + "/first.gif";

	if (anzahl_bilder > index_grosses_bild + 1)
		document.getElementById ('weiter_gross').src = pfad_zu_navbildern + "/forward.gif";
	else
		document.getElementById ('weiter_gross').src = pfad_zu_navbildern + "/last.gif";
	thumbnails_auffrischen ();
}

function zurueck_gross () {
	if (index_grosses_bild > 0) {
		index_grosses_bild--;
		grosses_bild_auffrischen ();
	}
}

function weiter_gross () {
	if (index_grosses_bild + 1 < anzahl_bilder) {
		index_grosses_bild++;
		grosses_bild_auffrischen ();
	}
}


