/***
 * Skript fuer einfache Bildergalerien zum durchblaettern
 * @version: $Id: galerie.js 37 2008-12-10 19:58:18Z andy $
 */

/**
 * Erzeugt eine neue Galerie
 * @param string $parent_id ID des Elements, in das die Galerie geladen wird
 * @param array $pics Bildpfade
 * @param string $next_pic Pfad zum Bild zum Weiterblaettern
 * @param string $prev_pic Pfad zum Bild zum Zurueckblaettern
 */
function galerie( parent_id, pics, next_pic, prev_pic ) {
  // Bildobjekte schon mal anlegen (werden dadurch in den Speicher geladen)
  this.bilder = Array();
  for( i=0; i<pics.length; i++ ) {
    this.bilder[i] = new Image();
    this.bilder[i].src = pics[i];
  }
  this.parent = document.getElementById( parent_id );
  this.pos = 0;
  
  // erst mal Platz fuer die Galerie schaffen
  this.removeAllChildNodes( this.parent );
  
  // Element fuer das Bild anlegen
  this.img = document.createElement('img');
  // und gleich Bild anzeigen (Webkitfix)
  this.img.src = this.bilder[0].src;
  
  // Die Steuerelemente hinzufuegen
  var tools = document.createElement('div');
  tools.className = 'jsgalerie_controls';
  
  this.nextbutton = document.createElement( 'input' );
  this.nextbutton.type = 'image';
  this.nextbutton.className = 'jsgalerie_next';
  this.nextbutton.galerie = this;
  this.nextbutton.onclick = galerie_next;
  this.nextbutton.title = "Nächstes Bild";
  this.nextbutton.src = next_pic;

  this.prevbutton = document.createElement( 'input' );
  this.prevbutton.type = 'image'; 
  this.prevbutton.src = prev_pic;
  this.prevbutton.className = 'jsgalerie_prev';
  this.prevbutton.galerie = this;
  this.prevbutton.onclick = galerie_prev;
  this.prevbutton.title = "Vorheriges Bild";
  
  this.postext = document.createTextNode( '1/' + this.bilder.length );
  
  tools.appendChild( this.prevbutton );
  tools.appendChild( this.nextbutton );
  tools.appendChild( this.postext );
  this.parent.appendChild( tools );

  this.parent.appendChild( this.img );
  this.refresh_buttons();
}

galerie.prototype.removeAllChildNodes = function( parent ) {
  while( parent.childNodes.length > 0 ) {
    parent.removeChild( parent.childNodes[0] );
  }
}

galerie.prototype.showFirstImage = function() {
  this.jumpto(0);
}

galerie.prototype.showimage = function( image ) {
  this.img.src = image.src;
  this.img.width = image.width;
  this.img.height = image.height;
}

galerie.prototype.next = function() {
  if( this.pos+1 < this.bilder.length ) {
    this.showimage( this.bilder[ ++this.pos ] );
  }
  this.refresh_buttons();
}

galerie.prototype.prev = function() {
  if( this.pos > 0 ) {
    this.showimage( this.bilder[ --this.pos ] );
  }
  this.refresh_buttons();
}

galerie.prototype.refresh_buttons = function() {
  if( this.pos+1 >= this.bilder.length ) {
    this.nextbutton.style.display = 'none';
  } else {
    this.nextbutton.style.display = 'block';
  }
  
  if( this.pos <= 0 ) {
    this.prevbutton.style.display = 'none';
  } else {
    this.prevbutton.style.display = 'block';
  }
  
  this.postext.data = this.pos+1 + '/' + this.bilder.length;
}

galerie.prototype.jumpto = function( index ) {
  this.pos = index;
  this.showimage( this.bilder[ index ] );
  this.refresh_buttons();
}

galerie.prototype.jumptofile = function( filename ) {
  for( var i=0; i<this.bilder.length; i++ ) {
    if( this.bilder[ this.pos ].src == filename )
      this.pos = i;
      this.showimage( this.bilder[i] ); 
      this.refresh_buttons();
      break;
    }
}
/* Ende der Klasse Galerie */

/* Eventlistener */
function galerie_next() {
  this.galerie.next();
}
function galerie_prev() {
  this.galerie.prev();
}