jQuery.noConflict();

jQuery(document).ready(function($){
    // Do jQuery stuff using $
   $('.teaser.animate div.view').each(function() {
	    var $container = $(this);
		var $views = $container.children();
		var currentView = 0, oldView = 0;

		var hiddenPosition = 0;
		$views.each(function() {
			if (hiddenPosition < $(this).height())
				hiddenPosition = $(this).height();
		});
		
		$container.css('height', hiddenPosition);
		hiddenPosition += 10;
		$views.css({'top': hiddenPosition, 'position': 'absolute' });
		
		$views.eq(currentView).css('top', 0);

		var viewCount = $views.length;
		var pause;
		var rotateInProgress = false;
		var teaserRotate = function() {
			if (!rotateInProgress) {
				rotateInProgress = true;
				pause = false;
				currentView = (oldView + 1) % viewCount;
				$views.eq(oldView).animate(
					{top: -hiddenPosition }, 'slow', function(){
						$(this).css('top', hiddenPosition);
					});
				$views.eq(currentView).animate(
					{top: 0}, 'slow', function(){
						rotateInProgress = false;
						if (!pause) {
							pause = setTimeout(teaserRotate, 5000);
						}
					});
				oldView = currentView;
			}	
		};

		if (!pause) {
			pause = setTimeout(teaserRotate, 5000);
		}
			
		$container.hover(function() {
			clearTimeout(pause);
			pause = false;
		}, function() {
			if (!pause) {
				pause = setTimeout(teaserRotate, 250);
			}
		});
	});
});

