window.addEvent('domready', function() {
  var carousel = $('carousel');
  var read_more = $('content_se') ? 'Läs mer' : 'Lue lisää tästä';
  var carouselindex = 0;
 	var carouselcount = $('carouselhelper').get('class');

  
  if(carousel) {
  var lis = carousel.getElements('.products li');
  
  lis.set('morph', {duration: 250});
  
  lis.addEvent('click', function() {
    carousel.getElement('.link').setStyle('display', 'none').setStyle('opacity', 0);
    
    lis.morph({width: 140, height: 140, marginTop: 60});
    this.morph({width: 212, height: 212, marginTop: 10});
    carouselindex = this.getElement('.product').get('alt');
    
    carousel.getElement('.products').morph({
      left: carousel.getSize().x / 2 - 101 - (((this.getAllPrevious(':not(.hidden)').length) * 140))
    });
  
    carousel.getElement('.link').set('html', '<b>'+this.get('title')+'</b><br />'+read_more+' »').set('href', this.get('href'));
    
    (function() {
      carousel.getElement('.link').setStyle('opacity', 0).setStyle('display', 'block').morph({opacity: 1});
    }).delay(500);
  });
  
  $$('.rightbtn').addEvent('click', function() {
	  
  if (carouselindex==0) {
  	var destination = carousel.getElement('.carousel-2');
  }
  else {
   var startingpoint = carousel.getElement('.carousel-'+carouselindex);
	 var destination = startingpoint.getNext(':not(.hidden)'); 		
	}; 
 		carouselindex = destination.getElement('.product').get('alt');
 		
   	lis.morph({width: 140, height: 140, marginTop: 60});   	
   	destination.morph({width: 212, height: 212, marginTop: 10});
   	 
    carousel.getElement('.products').morph({
      left: carousel.getSize().x / 2 - 101 - (((destination.getAllPrevious(':not(.hidden)').length) * 140))
    });
    
    carousel.getElement('.link').set('html', '<b>'+destination.get('title')+'</b><br />'+read_more+' »').set('href', destination.get('href'));
    
    (function() {
      carousel.getElement('.link').setStyle('opacity', 0).setStyle('display', 'block').morph({opacity: 1});
    }).delay(250);

  });
  
  
   $$('.leftbtn').addEvent('click', function() {
   
  if (carouselindex==0) {
  	var destination = carousel.getElement('.carousel-2');
  }
  else {  	  	
    var startingpoint = carousel.getElement('.carousel-'+carouselindex)
	 var destination = startingpoint.getPrevious(':not(.hidden)'); 		
 	};
 		carouselindex = destination.getElement('.product').get('alt');
 		
   	lis.morph({width: 140, height: 140, marginTop: 60});   	
   	destination.morph({width: 212, height: 212, marginTop: 10});
   	 
    carousel.getElement('.products').morph({
      left: carousel.getSize().x / 2 - 101 - (((destination.getAllPrevious(':not(.hidden)').length) * 140))
    });
    
    carousel.getElement('.link').set('html', '<b>'+destination.get('title')+'</b><br />'+read_more+' »').set('href', destination.get('href'));
    
    (function() {
      carousel.getElement('.link').setStyle('opacity', 0).setStyle('display', 'block').morph({opacity: 1});
    }).delay(250);
  });
  
  if(Browser.Engine.trident) {
    $(document.body).addClass('ie');
    
    carousel.getElements('img:not(.product)').each(function(img) {
      img.set('src', img.get('src').replace('.png', '-ie.png'));
    });
  }
  
  
  
  var categories = carousel.getElements('.categories a');
  
  categories.addEvent('click', function() {
    carousel.getElement('.link').setStyle('display', 'none').setStyle('opacity', 0).set('text', '');
    
    categories.removeClass('selected');
    this.toggleClass('selected');
    
    var cl = this.get('rel');
    
    lis.morph({opacity: 0});
    (function() {
      var count = 0;
      
      lis.each(function(li) {
        li.setStyles({width: 140, height: 140, marginTop: 60});
        
        if(li.hasClass(cl) || !cl) {
          count++;
          li.removeClass('hidden').morph({opacity: 1});
        }
        else {
          li.addClass('hidden');
        }
      });
      
      carousel.getElement('.products').setStyle('left', (742-(count*140))/2);
    }).delay(500);
  });
  }
});
