window.addEvent('domready', function() {
  if($('slider') != null) {
    i = 0;
    images = [];
    currentImage = 0;
    dots = $$('div.sliderdot');
    $$('#slider img').each(function(imgElm) {
      if(!imgElm.hasClass('infoicon')) {
        var dot = dots[i];
        dot.set('rel', i);
        
        dot.addEvent('click', function(e) {
          if(e != null) {
            e.preventDefault();
          }
          var elmNr = this.get('rel');
          setImage(elmNr);
        });
        
        if(i > 0) {
          imgElm.setStyle('opacity', 0);
          dot.addClass('inactive');
        } else {
          imgElm.setStyle('opacity', 1);
          imgElm.setStyle('z-index', 96);
          var imgText = imgElm.get('rel');
          if(imgText != null) {
            $('slidertext').set('html', imgText);
          } 
        }
        imgElm.store('number',i);
        images[i] = imgElm;
        i++;
      }
    });
    
    
    function nextImage() {
      currentImage++;
      if(currentImage > (images.length-1)) {
        currentImage = 0;
      }
      imgElm = images[currentImage];
      imgElm.setStyle('z-index', 97);
      
      var myFx = new Fx.Tween(imgElm, {
          duration: 'normal',
          property: 'opacity',
          onComplete: function() {
            this.setStyle('z-index', 96);
            images.each(function(img) {
              if(img != this) {
                img.setStyle('z-index', 90);
                img.setStyle('opacity', 0);
              }
              dots.addClass('inactive');
              var activeNr = this.retrieve('number');
              var dot = dots[activeNr];
              dot.removeClass('inactive');
            }.bind(this));
            var imgText = this.get('rel');
            if(imgText != null) {
              $('slidertext').set('html', imgText);
            } 
          }.bind(imgElm)
      });
      myFx.start('1');
    }
    function setImage(nr) {
      if(nr == null) {
        return false;
      }
      if(periodicalTimer != null) {
        clearInterval(periodicalTimer);
      }
      currentImage = nr;
      if(currentImage > (images.length-1)) {
        currentImage = 0;
      }
      imgElm = images[currentImage];
      imgElm.setStyle('z-index', 97);
      
      var myFx = new Fx.Tween(imgElm, {
          duration: 'normal',
          property: 'opacity',
          onComplete: function() {
            this.setStyle('z-index', 96);
            images.each(function(img) {
              if(img != this) {
                img.setStyle('z-index', 90);
                img.setStyle('opacity', 0);
              }
              dots.addClass('inactive');
              var activeNr = this.retrieve('number');
              var dot = dots[activeNr];
              dot.removeClass('inactive');
            }.bind(this));
            var imgText = this.get('rel');
            if(imgText != null) {
              $('slidertext').set('html', imgText);
            } 
            periodicalTimer = nextImage.periodical(4000);
          }.bind(imgElm)
      });
      myFx.start('1');
    }
    
    periodicalTimer = nextImage.periodical(4000);
  }
  
  if($('menu') != null) {
    $$('#menu a').each(function(linkElm) {
      parentElm = linkElm.getParent('li');
      parentElm.addEvent('mouseenter', function(e) {
        if(e != null) {
          e.preventDefault();
        }
        this.addClass('over');
      });
      
      parentElm.addEvent('mouseleave', function(e) {
        if(e != null) {
          e.preventDefault();
        }
        this.removeClass('over');
      });
      
      parentElm.addEvent('click', function(e) {
        //window.open(this.get('href'));
        window.location = this.get('href');
      }.bind(linkElm));
    });
  }
  
  if($$('.homeMediumBlock').length > 0) {
    $$('.homeMediumBlock').each(function(block) {
      var colorBar = block.getElement('div.colorBar');
      if(colorBar != null) {
        block.addEvent('mouseenter', function(e) {
          var myFx = new Fx.Tween(this, {
            duration: 'short',
            transition: Fx.Transitions.linear.easeInOut,
            property: 'width'
          });
          myFx.start('310px');
        }.bind(colorBar));
        block.addEvent('mouseleave', function(e) {
          var myFx = new Fx.Tween(this, {
            duration: 'short',
            transition: Fx.Transitions.linear.easeInOut,
            property: 'width'
          });
          myFx.start('32px');
        }.bind(colorBar));
      }
    });
  }  
});

