//****************************************************************
// Paket jsgallery09a
// Version 1.0.0.0
//
// JavaScript-Paket für die Anzeigesteuerung einer Bildergalerie
//
// Copyright REMATEC Datentechnik GmbH 2009
//****************************************************************

// Variablen
var jsgallery09a_obj;	// Zur Ansprache des auf der Website definierten Objektes der Klasse jsgallery09a

// Definition des Objektes zur Überblend-Steuerung
function jsgallery09a(ProgramDir, ImageDir, ImageFrontID, ImageBackID, BuBackID, BuFwdID, FadeSpeed){

	// Dieses Objekt in Standardvariable speichern
	jsgallery09a_obj = this;
	// Objekt-Attribute definieren
	this.ProgramDir = ProgramDir;
	if ((this.ProgramDir.length > 0) && (this.ProgramDir.substr(this.ProgramDir.length, 1) != "/")){
		this.ProgramDir += "/";
	}
	this.ImagePath = ImageDir;
    if ((this.ImagePath.length > 0) && (this.ImagePath.substr(this.ImagePath.length, 1) != "/")){
        this.ImagePath += "/";
    }
	this.ImageFront = "#" + ImageFrontID;
	this.ImageBack = "#" + ImageBackID;
	this.bu_Back = "#" + BuBackID;
	this.bu_Fwd = "#" + BuFwdID;
	this.ImgNames = new Array();
	this.ImgNamesLoaded = false;
	this.actualImg = 0;
	this.activeImage = "Front";
	this.FadeSpeed = FadeSpeed;
	
	// Initialisierung der Überblendsteuerung des aktuellen Objektes
	this.initialize = function(){
		with(this){
			// Bildwechsel-Funktionen binden an die Links zum Bildwechsel
			$(bu_Back).click(function(){jsgallery09a_obj.go_backward();});
			$(bu_Fwd).click(function(){jsgallery09a_obj.go_forward();});
			// Event setzen zum Überblenden nach fertigem Bild-Laden
			$(ImageFront).load(function(){jsgallery09a_obj.doFade()});
			$(ImageBack).load(function(){jsgallery09a_obj.doFade()});
			// besorge die Bildnamen
			$.getJSON(encodeURI(ProgramDir + "jimagelist.php?imagedir=" + ImagePath + "&baseurl=" + document.URL), function(data,textStatus){jsgallery09a_obj.GetImageNames(data, textStatus);});
		}	
	}
	
	// nächstes Bild laden
	this.loadNextImg = function(){
		var NewPic;
		with(this){
			if (ImgNamesLoaded){
				if (activeImage == "Front"){
					NewPic = ImageBack;
					activeImage = "Back";
				}
				else {
					NewPic = ImageFront;
					activeImage = "Front";
				}
				$(NewPic).attr("src", ImagePath + ImgNames[actualImg]);
			}
		}
	}
	
	// zeige das vorherige Bild an
	this.go_backward = function(){
		with(this){
			$(bu_Back).blur();
			if (ImgNamesLoaded){
				actualImg--;
				if (actualImg < 0){
					actualImg = ImgNames.length - 1;
				}
				loadNextImg();
			}
		}
	}
	
	// zeige das nächste Bild an
	this.go_forward = function(){
		var NewPic;
		with(this){
			$(bu_Fwd).blur();
			if (ImgNamesLoaded){
				actualImg++;
				if (actualImg >= ImgNames.length){
					actualImg = 0;
				}
				loadNextImg();
			}
		}
	}
	
	// lade die Bilderliste und schalte die Buttons sichtbar
	this.GetImageNames = function(data, textStatus){
		with(this){
			ImgNames = data;
			if (ImgNames.length > 0){
				ImgNamesLoaded = true;
				$("div.jsgallerybuttons").css("visibility","visible");
			}
		}
	}
	
	//stoße das Überblenden an
	this.doFade = function(){
		with(this){
			if (activeImage == "Front"){
				$(ImageFront).fadeIn(FadeSpeed);
			}
			else {
				$(ImageFront).fadeOut(FadeSpeed);
			}
		}
	}
}

// Initialisierung nach fertigem Laden der Seite durchführen
$(document).ready(function(){jsgallery09a_obj.initialize();});

