/**
 * Class AnemaGallery
 *
 * Galerie verwaltet Bilder.
 *
 * imageCreatorFunction: erzeugt die DOM-Elemente für die Bilder;
 *			bekommt das Image-Objekt übergeben und setzt dort die
 *			notwendigen Properties (welche das sind, ist in AnemaGallerImage
 *			dokumentiert.
 *
 * Handler/Hooks (self bezeichnet das betroffene AnemaGallery-Objekt):
 *		- onDisplayHandler( self, image )
 *		- onCloseHandler( self )
 *
 *
 */

function AnemaGallery( imageCreatorFunction/*, displayHandler=null, closeHandler=null*/ ){
	if( typeof imageCreatorFunction != "function" ) throw "[AnemaGallery] constructor: imageCreatorFunction not a function";
	this.imageCreatorFunction = imageCreatorFunction;

	this.onDisplayHandler = null;
	if( arguments[1] ){
		this.onDisplayHandler = arguments[1];
		if( typeof this.onDisplayHandler != "function" ) throw "[AnemaGallery] constructor: displayHandler not a function";
	}
	this.onCloseHandler = null;
	if( arguments[2] ){
		this.onCloseHandler = arguments[2];
		if( typeof this.onCloseHandler != "function" ) throw "[AnemaGallery] constructor: closeHandler not a function";
	}

	this.images = new Array();
	this.currentIndex = -1;
	this.domElement = null;
	this.attached = false;
	this.size = 0;	// Muss nicht gleich images.length sein, weil nicht immer alle gebraucht werden.
	this.thumbsLoaded = 0;	// Anzahl der fertig geladenen Bilder.
}

/**
 * Liefert das Bild am gewünschten Index.
 * Der optionale Parameter gibt an, ob der Index zyklisch
 * berechnet werden soll, also wenn zu groß, wieder auf 0 springt
 * oder wenn zu klein auf den letzten Index.
 * Defaultmäßig wird zyklisch gerechnet.
 *
 */
AnemaGallery.prototype.getImageAtIndex = function( index/*, cycle=true*/ ){
	var cycle = (arguments[1]===false) ? false : true;	// true ist der Default.
	if( typeof index != "number" ) throw "[AnemaGallery] getImageAtIndex: index not a number (" + index + ")";

	if( index < 0 )
		if( cycle ){
			index = this.getSize() - 1;
		}else{
			throw "[AnemaGallery] getImageAtIndex: negative index (" + index + ")";
		}
	if( index >= this.getSize() ){
		if( cycle ){
			index = 0;
		}else{
			throw "[AnemaGallery] getImageAtIndex: index out of bounds (" + index + ")";
		}
	}

	this.currentIndex = index;

	return this.images[index];
};

/**
 * Liefert null, wenn keine Bilder in der Galerie.
 * Bei Überlauf wird das erste Bild zurückgeliefert.
 */
AnemaGallery.prototype.getNextImage = function(){
	if( this.images.length == 0 ) return null;
	return this.getImageAtIndex( this.currentIndex + 1 );
};

/**
 * Liefert null, wenn keine Bilder in der Galerie.
 * Bei Unterlauf wird das letzte Bild zurückgeliefert.
 */
AnemaGallery.prototype.getPreviousImage = function(){
	if( this.images.length == 0 ) return null;
	return this.getImageAtIndex( this.currentIndex - 1 );
};

AnemaGallery.prototype.attachToDomElement = function( obj ){
	if( typeof obj == "string" ) obj = document.getElementById( obj );
	if( !obj ) throw "[AnemaGallery] attachToDomElement: DOM element does not exist";
	this.domElement = obj;
	for( var i=0; i<this.images.length; i++ ){
		// detach and reattach if need be
		var img = this.images[i];
		if( img.getStatus() == "attached" ) img.detachFromDOM();
		img.attachToDOM();
	}
	this.attached = true;
};

AnemaGallery.prototype.detachFromDOM = function(){
	for( var i=0; i<this.images.length; i++ ){
		var img = this.images[i];
		img.detachFromDOM();
	}
	this.attached = false;
};


/**
 * Übernimmt einen Array von Objekten, deren Properties
 * die Werte für die gleichnamigen Properties der Image-Objekte
 * darstellen.
 * Welche davon Pflicht und welche optional siehe AnemaGalleryImage.js
 *
 * Existieren bereits Bilder in der Galerie, werden ihre Daten ausgetauscht.
 * Überzählige Bilder werden aus dem DOM entfernt. Sind zu wenige, neue erzeugt.
 *
 * Ist der optionale Parameter attachAtOnce true, werden die Bilder
 * sofort erzeugt und in den DOM-Baum eingehängt, sonst muss das
 * durch einen separaten Aufruf von gallery.attachToDomElement()
 * geschehen.
 *
 */
AnemaGallery.prototype.createGalleryFromData = function( imageDataArray/*, attachAndActivateAtOnce=false */ ){
	var i = 0;
	var attachAndActivateAtOnce = (arguments[1] === true);
	if( !this.imageCreatorFunction ) throw "[AnemaGallery] createGalleryFromData: missing imageCreatorFunction";
	if( typeof this.imageCreatorFunction != "function" ) throw "[AnemaGallery] createGalleryFromData: imageCreatorFunction not a function";
	//if( this.images.length > 0 ) this.clear();

	// Erst alten Inhalt aus dem DOM nehmen.
	for( var i=0; i<this.images.length; i++ ){
		this.images[i].detachFromDOM();
	}
	this.thumbsLoaded = 0;	// Setze Zähler für geladene Thumbs zurück.
	this.size = imageDataArray.length;
	this.currentIndex = 0;

	for( i=0; i<imageDataArray.length; i++ ){
		var data = imageDataArray[i];
		var img = null;
		if( i < this.images.length ){
			// Lade Daten in existentes Bild.
			img = this.images[i];
			img.loadData( data );
			img.activate();
		}else{
			// Erzeuge neue Einträge.
			img = new AnemaGalleryImage( data, this.imageCreatorFunction, this );
			if( attachAndActivateAtOnce ){
				img.activate();
			}
			this.images.push( img );
		}
	}

	// Nimm überzählige Bilder aus dem DOM.
	while( i < this.images.length ){
		this.images[i].hide();
		this.images[i].detachFromDOM();
		i++;
	}
};

AnemaGallery.prototype.hide = function(){
	for( var i=0; i<this.images.length; i++ ){
		this.images[i].hide();
	}
};

AnemaGallery.prototype.clear = function(){
	for( var i=0; i<this.images.length; i++ ){
		this.images[i].detachFromDOM();
	}
	this.images.length = 0;
};


// Setzt den onDisplayHandler und liefert den alten zurück.
AnemaGallery.prototype.setDisplayHandler = function( displayHandler ){
	var old = null;
	if( typeof displayHandler != "function" ) throw "[AnemaGallery] setDisplayHandler: displayHandler not a function";
	this.onDisplayHandler = displayHandler;
	old = this.onDisplayHandler;
	return old;
};

// Setzt den onCloseHandler und liefert den alten zurück.
AnemaGallery.prototype.setCloseHandler = function( closeHandler ){
	var old = null;
	if( typeof closeHandler != "function" ) throw "[AnemaGallery] setCloseHandler: closeHandler not a function";
	old = this.onCloseHandler;
	this.onCloseHandler = closeHandler;
	return old;
};

/**
 * Hier gibt's as is nix zu tun.
 * Wir rufen deshalb nur den onCloseHandler auf, falls vorhanden.
 *
 */
AnemaGallery.prototype.close = function(){
	if( this.onCloseHandler ) this.onCloseHandler( this );
};

AnemaGallery.prototype.displayImageAtIndex = function( index ){
	if( this.onDisplayHandler ) this.onDisplayHandler( this, this.getImageAtIndex(index) );
};

AnemaGallery.prototype.displayNextImage = function(){
	if( this.onDisplayHandler ) this.onDisplayHandler( this, this.getNextImage() );
};

AnemaGallery.prototype.displayPreviousImage = function(){
	if( this.onDisplayHandler ) this.onDisplayHandler( this, this.getPreviousImage() );
};

AnemaGallery.prototype.getDomElement = function(){
	return this.domElement;
};

AnemaGallery.prototype.getSize = function(){
	return this.size;
};

AnemaGallery.prototype.getLoadedThumbsCount = function(){
	return this.thumbsLoaded;
};




AnemaGallery.prototype.xx = function(){
	throw "[AnemaGallery] xx not yet implemented";
};
