window.addEvent( 'domready', function(){
  
  $('newscontainer-nonjs').set('styles', { 'display': 'none' }); //hide non-js news-teaser
  $('newscontainer').set('styles', { 'display': 'block' }); //show js news-teaser
  
  NewsTeaserAnimation = new NewsTeaserAnimationObj(500);	
  NewsTeaserAnimation.posts[NewsTeaserAnimation.currentIdx].set('styles', { 'display': 'block' });
  var animationLoop = NewsTeaserAnimate.periodical(15000);
  
  /*NewsTeaserAnimation.posts.each(function(item){

  	alert('item:' + item);

  });
  */
});

function NewsTeaserAnimationObj(fade_time) {
		this.currentIdx = 0;
		this.posts = $$( '#newscontainer .latestitem' );
		this.fade_time = fade_time;
}

function NewsTeaserAnimate() {
	//Fade out current post
	
	var fadeOut = new Fx.Tween(NewsTeaserAnimation.posts[NewsTeaserAnimation.currentIdx], {duration: NewsTeaserAnimation.fade_time, wait: true});

	fadeOut.start('opacity',0).chain(function() {
		NewsTeaserAnimation.posts[NewsTeaserAnimation.currentIdx].set('styles', { 'display': 'none' });
		//Fade in next Post
		NewsTeaserAnimation.currentIdx++;
		
		if (NewsTeaserAnimation.currentIdx >= NewsTeaserAnimation.posts.length) NewsTeaserAnimation.currentIdx = 0;
		
		NewsTeaserAnimation.posts[NewsTeaserAnimation.currentIdx].set('styles', { 'display': 'block' });
		NewsTeaserAnimation.posts[NewsTeaserAnimation.currentIdx].setStyle('opacity', 0);
		var fadeIn = new Fx.Tween(NewsTeaserAnimation.posts[NewsTeaserAnimation.currentIdx],  {duration: NewsTeaserAnimation.fade_time, wait: true});
		fadeIn.start('opacity',1);
		}
	);
	
	
	
}
