/* $Id: slideshow.js,v 1.3 2007/08/21 12:45:01 oliver Exp $ */

function imgLoad() {
  var slide = galleryImages[galleryPosition];
  imgPreload.src = slide.normal;
  imgPreload.onload = function() {
    imgDisplay();
  }
  imgPreload.onerror = function() {
    imgNext();
  }
}

function imgDisplay() {
  var elem = $('myGallery').getElementsBySelector('div.imageDisplay').first();
  var slide = galleryImages[galleryPosition];
  elem.getElementsBySelector('a').first().href = slide.large;
  $('slideInfo').innerHTML = slide.title+'&nbsp;'+(galleryPosition+1)+'/'+(galleryImages.length);
  elem.setStyle({
    backgroundImage: 'url('+slide.normal+')'
  });
}

function imgNext() {
  galleryPosition += 1;
  if(galleryPosition >= galleryImages.length) galleryPosition = 0;
  imgLoad();
}

function imgPrev() {
  galleryPosition -= 1;
  if(galleryPosition < 0) galleryPosition = galleryImages.length - 1;
  imgLoad();
}

function imgPlay() {
  imgInterval = window.setInterval('imgNext()', gallerySlideDuration);
}

function imgStop() {
  window.clearInterval(imgInterval);
}

/*
var SlideShow = Class.create();

SlideShow.prototype = {
  initialize: function(container,slideDuration) {
    this.slideDuration = slideDuration/1000;
    this.container = container;
    this.position = 0;
    this.slides = new Array();
    this.pe = null;

    container.getElementsBySelector('div.imageElement').each(function(elem) {
      var slide = {
        title: elem.getElementsBySelector('h3').first().innerHTML,
        href: elem.getElementsBySelector('a').first().href,
        //thumb: elem.getElementsBySelector('img.thumbnail').first().src,
        full: elem.getElementsBySelector('img.full').first().src
      };
      this.slides.push(slide);
      elem.remove();
    }.bind(this));

    this.status();
    this.load();
    this.play();
  },

  load: function() {
    var slide = this.slides[this.position];
    var imgPreload = new Image();
    imgPreload.src = slide.full;
    imgPreload.onload = function() {
      this.display();
    }.bind(this);
    imgPreload.onerror = function() {
      alert('error with '+this.src);
      this.nextItem();
    }.bind(this);
  },
  
  display: function() {
    var elem = this.container.getElementsBySelector('div.imageDisplay').first();
    var slide = this.slides[this.position];
    elem.getElementsBySelector('a').first().href = slide.href;
    $('slideInfo').innerHTML = slide.title+'&nbsp;'+(this.position+1)+'/'+(this.slides.length);
    elem.setStyle({
      backgroundImage: 'url('+slide.full+')'
    });
  },

  nextItem: function() {
    this.position += 1;
    if(this.position >= this.slides.length) this.position = 0;
    this.load();
  },

  prevItem: function() {
    this.position -= 1;
    if(this.position < 0) this.position = this.slides.length - 1;
    this.load();
  },
  
  play: function() {
    if(this.pe == null) {
      this.pe = new PeriodicalExecuter((this.nextItem).bind(this),this.slideDuration);
    }
  },

  stop: function() {
    if(this.pe != null) {
      this.pe.stop();
      this.pe = null;
    }
  },
  
  status: function() {
  }
};
*/
