// --------------------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------------------
// behaviours - Rotates through carousel items until user clicks;
// depends: 	jQuery library;
// author:		nr;
// --------------------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------------------

(function carousel() {
	
	$(".carousel").each(function() {
		
		var $this = $(this);
		
		function selectCarouselItem(index) {
			var $anchor = $this.find('.carouselControl a:eq(' + index + ')');
			var $itemToShow = $($anchor.attr('rel'));
			$anchor.parent().siblings().children().removeClass('selected').end().end().end().addClass('selected');
			$itemToShow.siblings('.carouselItem').fadeOut('slow').end().fadeIn('slow');
		}
		
		function showCarouselItem() {
			var $itemToShow = $($(this).attr('rel'));
			$(this).parent().siblings().children().removeClass('selected').end().end().end().addClass('selected');
			$itemToShow.siblings('.carouselItem').fadeOut('slow').end().fadeIn('slow');
			return false;
		}
		
		$this.find('.carouselControl a').click(showCarouselItem);
		
		var currentIndex = 0;
		var rotating = setInterval(function() {
			selectCarouselItem(++currentIndex % 3);
		}, 7000);
	
		$this.find('.carouselControl a').one("click", function() {
			clearInterval(rotating);
		});
		
		$this.one("click", function() {
			clearInterval(rotating);
		}); 
		
	});
	
})();

// --------------------------------------------------------------------------------------------------------------------------------
// --- end of file --- 
// -------------------------------------------------------------------------------------------------------------------------------
