// Slideshow class
var SimpleSlideshow = new Class({
  options: {
    showDuration: 6000,
    tweenDuration: 1000,
    showTOC: true,
    tocWidth: 14,
    tocClass: 'toc',
    tocActiveClass: 'toc-active',
    startpageTeaser : false
  },
  Implements: [Options,Events],
  initialize: function(container,elements,options) {
    //settings
    this.container = $(container);
    this.elements = $$(elements);
    this.currentIndex = 0;
    this.interval = '';
    if(this.options.showTOC) this.toc = [];

	this.setOptions(options);
    
    //assign
    this.elements.each(function(el,i) {
    
      // dw::start
      if(this.options.startpageTeaser == true) {
	      var link = el;
	      link.setStyle('display', 'block');
          link.setStyle('width', '100%');
          link.setStyle('height', '450px');  
        }
      // dw::end

      if(this.options.showTOC) {
        this.toc.push(new Element('a',{
          href: '#',
          'class': this.options.tocClass + '' + (i == 0 ? ' ' + this.options.tocActiveClass : ''),
          events: {
            click: function(e) {
              if(e) e.stop();
              this.stop();
              this.show(i);
            }.bind(this)
          },
          styles: {
            right: ((this.elements.length - i) * (this.options.tocWidth + 10)),
            top: 400
          }
        }).inject(this.container));
      }
      if(i > 0) el.set('opacity',0);
    },this);
    
    //events
    this.container.addEvents({
      mouseenter: function() { this.stop(); }.bind(this),
      mouseleave: function() { this.start(); }.bind(this)
    });

  },
  show: function(to) {
  	this.elements[this.currentIndex].set('tween', {duration: this.options.tweenDuration});
    this.elements[this.currentIndex].tween('opacity',0);
    if(this.options.showTOC) this.toc[this.currentIndex].removeClass(this.options.tocActiveClass);
    this.elements[this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex+1 : 0))].tween('opacity',1);
    if(this.options.showTOC) this.toc[this.currentIndex].addClass(this.options.tocActiveClass);
  },
  start: function() {
    this.interval = this.show.bind(this).periodical(this.options.showDuration);
  },
  stop: function() {
    $clear(this.interval);
  }
});


window.addEvent('domready',function() {
  
  // Animate logo
  var slideshow_2 = new SimpleSlideshow('logo_container_left','$(".logo_morph")', {
    showDuration: 6000,
  	tweenDuration : 5000,
  	showTOC : false,
  });
  slideshow_2.start();
  
  // Move active language's flag on top
  $$('.mod_changelanguage ul').set('id', 'container');
  $$('.mod_changelanguage ul li.active').set('id', 'activeItem');

  var activeItemCopy = $('activeItem').clone(true, true);

  $('activeItem').destroy();
  activeItemCopy.inject($('container'), 'top');
  
  $$('.mod_changelanguage ul li').removeClass('first');
  $$('.mod_changelanguage ul li').removeClass('last');

});
