window.addEvent('load',function() {
  /* settings */
  var showDuration = 6000;
  var container = $('slideshow-container');
  var slidehowslider = $('slideshow-slider');
  
  var base_height = container.getStyle('height').toInt();  
  
  var bump = container.getProperty("bump");


  /* get the first container and clone it, so that we have a 
     dummy container at the end of the slideshow */	 
  var first_child = container.getElement('.juka_dyn_teaser');
  var clone = first_child.clone().inject('slideshow-slider');  
  
  var slide_divs = container.getElements('.juka_dyn_teaser');   
  var currentIndex = 0;
  container_offset = 190;
  var next_left = 0;
  var interval;
  
  /* init left position of slideshow slider */
  $('slideshow-slider').setStyle('left', '0px');
  
  /* get the highest element of slide_divs array and 
     set the height of the slideshow to the height of
	 the highest element */
	 
  var max_size = 0;
  slide_divs.each(function(item, index){
    var this_size = item.getSize();
	max_size = max_size < this_size.y ? this_size.y : max_size;	
  });

  /* set all containers to the height of the highest element */
  
  if(max_size > base_height && bump == "true") {
    $$('.juka_dyn_teaser').setStyle('height', max_size);
    var myFx = new Fx.Tween(container,{
	  transition: 'bounce:out',
	    duration: 1000		  		
	  });	
    myFx.start('height',max_size + 2);
  }
  else {
    $$('.juka_dyn_teaser').setStyle('height', base_height - 2);
  }
  
  //$('slideshow-container').setStyle('border-bottom', 'none');  
  
  /* worker */
  var show = function() { 
    /* calculate new offset */

	if(currentIndex == slide_divs.length - 1) {
	  next_left = 0;   
	}
	else {
	  next_left = eval($('slideshow-slider').getStyle('left').toInt() - container_offset);
	}

		/* move slideshow slider */
		var myTween = new Fx.Tween(slidehowslider,{
		  transition: 'bounce:out',
		  duration: 1000,
		  onComplete: function(){
			currentIndex = currentIndex < slide_divs.length - 1 ? currentIndex + 1 : 0; 
			/* if we have the dummy element we jump to the first image */			
			if(currentIndex == slide_divs.length - 1) {
			  $(slidehowslider).setStyle('left', '0');
			  currentIndex = 0;
			  //$clear(interval);
			  //start_animation();
			} 			
		  }
		});		
		myTween.start('left',next_left);	
  };
  
  var start_animation = function() {
    interval = show.periodical(showDuration); 
  }
  
  /* start once the page is finished loading */
  start_animation();



$('slideshow-container').addEvents({
  mouseenter: function() {
    //temporarily stop
    $clear(interval);
  },
  //restart animation on mouseout
  mouseleave: start_animation
});
 
});


   


   
