//
// COMMUNITOR SiteWare 2.0 Site Server
// Copyright by COMMUNITOR Internetservice GmbH, 2001-2009
//


/*******************************************/
/* Bild-Überblendanimation-Klasse SWslider */
/*******************************************/
var SWimageSlider = Class.create();
Object.extend(SWimageSlider.prototype, {
  initialize: function(selector) {
    this.options = Object.extend({
      imagesarea: '.featurepicarea',
      animationDuration: 1
    }, arguments[1] || { });
    this.options.selector = selector;
    this.images = new Array();
    this.shown = 0;
    this.buttons = false;
    Event.observe(window,'load',this.setup.bindAsEventListener(this));
  },
  setup: function() {
    this.container = $(document.getElementsByTagName("body")[0]).select(this.options.selector)[0];
    this.image_container = this.container.select(this.options.imagesarea)[0];
    this.image_container.makePositioned().update("<br />");
    this.draw();
  },
  draw: function() {
    if (!this.image_container) return;
    // draw images
    this.images.each(this.drawImage.bind(this));
    // update buttons
    if (!this.buttons) {
      if (this.images.length > 1) {
        this.container.select('.btnNext').invoke(
          'observe',
          'click',
          this.show.bindAsEventListener(this,{position:'next'})
        );
        this.container.select('.btnPrev').invoke(
          'observe',
          'click',
          this.show.bindAsEventListener(this,{position:'previous'})
        );
        this.buttons = true;
      } else {
        this.container.select('.btnNext').invoke('hide');
        this.container.select('.btnPrev').invoke('hide');
      }
    }
  },
  drawImage: function(imageObj,pos) {
    if (Object.isElement(this.images[pos].div))
      return;
    var link,image = new Element('IMG'),
        container = new Element('DIV');
    image.src = imageObj.src;
    image.alt = imageObj.text;
    image.setStyle({width: imageObj.width+'px', height: imageObj.height+'px'});
    if (imageObj.link) {
      link = new Element('A');
      link.href = imageObj.link;
      link.target = imageObj.target;
      link.appendChild(image);
      image = link;
    }
    image.title = imageObj.text;
    container.setStyle({position:'absolute',top:'0px',left:'0px',display:(pos == this.shown) ? 'block' : 'none'});
    container.appendChild(image);
    this.image_container.appendChild(container);
    this.images[pos].div = container;
  },
  add: function(src,width,height,text,link,target) {
    this.images.push({
      src: src.applyBaseHref(window.location.href),
      width: width,
      height: height,
      text: text.htmlEntityDecode(),
      link: link.substring(6,link.length-1),
      /*link: link,*/
      target: target || '_self',
      div: null
    });
    this.draw();
  },
  show: function(e,options) {
    e.stop();
    var nr = this.shown;
    if (!options || !options.position)
      nr = 0;
    else if (options.position == 'next') {
      ++nr;
      if (nr >= this.images.length)
        nr = 0;
    } else if (options.position == 'previous') {
      --nr;
      if (nr < 0)
        nr = this.images.length - 1;
    }
    if (nr == this.shown)
      return;
    // blend over
    new Effect.Parallel([
      new Effect.Opacity(this.images[this.shown].div, { sync: true, from: 1, to: 0, transition: Effect.Transitions.linear }),
      new Effect.Opacity(this.images[nr].div,         { sync: true, from: 0, to: 1, transition: Effect.Transitions.linear })
    ], {
      afterSetup:  Element.show.bind(this,this.images[nr].div),
      afterFinish: Element.hide.bind(this,this.images[this.shown].div),
      duration: this.options.animationDuration
    });
    this.shown = nr;
  }
});