// ############################# //
// PhotoGallery 1.0              //
// Zetaneon Framework            //
// Copyright Daniel Messner 2011 //
// ############################# //


// Fotogallery anzeigen
function PhotoGallery(sID, sTarget, iPicture) {
	// Prüfen ob die Variabeln einen Inhalt haben
	if (sID != "" && sTarget != "") {
		// Liste aller Bilder erstellen
		PhotoGallery_list(sID);
		
		// Prüfen ob das zu öffnende Bild existiert
		if (!PhotoGallery_item_src[iPicture].lenght) { // Das produziert zwar einen Fehler in JS wenn das Bild nicht existiert, aber der Code wird auch nicht weiter ausgeführt
			// Fenster für Fotogallery öffnen
			ZetaneonUIWindow(sTarget, 1);
			
			// ID der Gallerie
			PhotoGallery_ID = sID;
			// ID des Fensters
			PhotoGallery_Window = sTarget;
			// Aktuelle Position im Bild-Listen-Array
			PhotoGallery_current = iPicture;
			
			// Sonderbehandlung für IE7-
			if (checkBrowserName('MSIE')) {
				if (getInternetExplorerVersion() < 8) {
					// Debug für Bildergallerie Anzeige
					document.getElementById("ZetaneonUIWindow_" + sTarget).style.display = "block";
				}
			}
			
			// Inhalt ins Fenster schreiben
			PhotoGallery_update(PhotoGallery_item_src[iPicture]);
		}
	}
}

// Vorheriges Bild anzeigen
function PhotoGallery_previous() {
	// Zähler des neuen Bildes herausfinden
	iPrevious = PhotoGallery_current - 1;
	
	// Prüfen ob der Anfang der Gallery (Bild 1) noch nicht erreicht wurde
	if (iPrevious >= 0) {
		// Aktuelle Position neu bestimmen
		PhotoGallery_current--;
		
		// Neues Foto einsetzen
		PhotoGallery_update(PhotoGallery_item_src[iPrevious]);
	}
}

// Nächstes Bild anzeigen
function PhotoGallery_next() {
	// Zähler des neuen Bildes herausfinden
	iNext = PhotoGallery_current + 1;
	
	// Prüfen ob das Ende der Gallery (letztes Bild) noch nicht erreicht wurde
	if (iNext <= PhotoGallery_total) {
		// Aktuelle Position neu bestimmen
		PhotoGallery_current++;
		
		// Neues Foto einsetzen
		PhotoGallery_update(PhotoGallery_item_src[iNext]);
	}
}

// Buttons animieren
function PhotoGallery_button(iButton, iOpa) {
	if (iButton == 0) {
		fadeNow(PhotoGallery_Window + "_previous", iOpa);
	}
	if (iButton == 1) {
		fadeNow(PhotoGallery_Window + "_next", iOpa);
	}
}

// Schreibt den Inhalt des Fensters mit dem angegebenen Bild neu
function PhotoGallery_update(sPath) {
	// Maximalgröße bestimmen
	iMaxWidth  = window.innerWidth  * 0.8;
	iMaxHeight = window.innerHeight * 0.8;
	
	// Prüfen ob ein Bildtext eingefügt werden soll
	sText = PhotoGallery_item_alt[PhotoGallery_current];
	if (sText == null || sText == "") sText = "";
	else sText = '<div class="PhotoGallery_text" style="max-width:' + iMaxWidth + 'px;">' + sText + '</div>';
	
	// Sichtbarkeit prüfen
		// Previous Button
		if ((PhotoGallery_current - 1) < 0) sPrevious = ' style="visibility:hidden"';
		else sPrevious = "";
		// Next Button
		if ((PhotoGallery_current + 1) > PhotoGallery_total) sNext = ' style="visibility:hidden"';
		else sNext = "";
		
		// Sonderbehandlung für IE7-
			if (checkBrowserName('MSIE')) {
				if (getInternetExplorerVersion() < 8) {
					// Navigationspfeile nie anzeigen
					sPrevious = ' style="display:none"';
					sNext = ' style="display:none"';
				}
			}
	
	// Neues Bild anzeigen
	ZetaneonUIWindowContent(PhotoGallery_Window, '<div class="PhotoGallery_close"><span onclick="ZetaneonUIWindow(\'' + PhotoGallery_Window + '\', 0)">Schließen</span></div><div id="' + PhotoGallery_Window + '_previous" class="PhotoGallery_previous"' + sPrevious + '><img alt="" onclick="PhotoGallery_previous()" onmouseover="PhotoGallery_button(0, 1)" onmouseout="PhotoGallery_button(0, 0.75)" src="kernel/frameworks/PhotoGallery/previous.png" /></div><img id="' + PhotoGallery_Window + '_img" alt="" src="' + sPath + '" style="max-width:' + iMaxWidth + 'px; max-height:' + iMaxHeight + 'px;" /><div id="' + PhotoGallery_Window + '_next" class="PhotoGallery_next"' + sNext + '><img alt="" onclick="PhotoGallery_next()" onmouseover="PhotoGallery_button(1, 1)" onmouseout="PhotoGallery_button(1, 0.75)" src="kernel/frameworks/PhotoGallery/next.png" /></div>' + sText);
	
	// Navigationsbuttons anpassen
		// Höhe des Bildes
		iHeight = document.getElementById(PhotoGallery_Window + "_img").offsetHeight;
		
		// Padding ausrechnen
		iPadding = (iHeight / 2) - 31;
		iHeight = iHeight - iPadding + "px";
		iPadding += "px";
		
		// Divs anpassen
		document.getElementById(PhotoGallery_Window + "_previous").style.height     = iHeight;
		document.getElementById(PhotoGallery_Window + "_previous").style.paddingTop = iPadding;
		document.getElementById(PhotoGallery_Window + "_next").style.height         = iHeight;
		document.getElementById(PhotoGallery_Window + "_next").style.paddingTop     = iPadding;
		
		// Transparenz
		fadeNow(PhotoGallery_Window + "_previous", 0.75);
		fadeNow(PhotoGallery_Window + "_next", 0.75);
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = false; // Return value assumes failure.
  if (navigator.appName == "Microsoft Internet Explorer")
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

// Liste der Bilder als Array mit Beschreibungstext erstellen
function PhotoGallery_list(sID) {
	// Prüfen ob die angegebene ID vorhanden ist
	if (document.getElementById(sID)) {
		// Arbeitsvariabeln erstellen
		PhotoGallery_item_src = new Array();
		PhotoGallery_item_alt = new Array();
		iC = 0;
		
		// Prüfen ob es sich um einzelnes Bild oder einen Bilderdiv handelt
		if (document.getElementById(sID).nodeName == "IMG") {
			PhotoGallery_item_src[0] = document.getElementById(sID).getAttribute("src"); // Pfad des Bildes
			PhotoGallery_item_alt[0] = document.getElementById(sID).getAttribute("alt"); // Beschreibungstext
			PhotoGallery_total = 0;
		}
		else {
			// Alle Fotos in der Liste in einen Array einlesen
			aPhotos = document.getElementById(sID);
			
			// Liste erstellen
			do {
				PhotoGallery_item_src[iC] = aPhotos.getElementsByTagName("img")[iC].getAttribute("src"); // Pfad des Bildes
				PhotoGallery_item_alt[iC] = aPhotos.getElementsByTagName("img")[iC].getAttribute("alt"); // Beschreibungstext
				
				iC++; // Arbeitszähler erhöhem
			}
			
			// Schleifenbedingung prüfen
			while (aPhotos.getElementsByTagName("img")[iC]);
			
			// Anzahl der Einträge
			PhotoGallery_total = iC - 1;
		}
	}
}
