/**
* Sputnik Carousel
* by Tim Broddin
* version 1.0
* http://www.sputnikweb.be
**/
(function($){	
	$.fn.sputnikcarousel = function(options) {
		var e = $(this);
		var active_index = 0;
		if(e.length) {
			var panes = $('.pane', e);
			var active_pane = $(panes[0]);
			active_pane.fadeIn();
			var counter = $('<div />').addClass('carousel_counter');
			var page = 1;
			panes.each(function() { 
				counter.append($('<div />').addClass('carousel_counter_b').attr('id', 'carousel_counter_' + page).html(page).bind('click', function() { scrollToPane($(this).html()); }));	
				page++;
			});
			e.append(counter);
			$('#carousel_counter_1').addClass('active');
			if(panes.length > 1) {
				var intr = setInterval(function() { normalRotation(); }, 6000);
			}
			function normalRotation() {
				$('.carousel_counter_b').removeClass('active');
				$(panes[active_index]).fadeOut(1000); 
				if(active_index + 1 == panes.length) {
					active_index = -1;
				}
				$(panes[active_index+1]).fadeIn(1000); 
				active_index++; 
				$('#carousel_counter_' + (active_index+1)).addClass('active');
			}
			function scrollToPane(page) {
				clearInterval(intr);
				$('.carousel_counter_b').removeClass('active');
				$(panes[active_index]).fadeOut(1000); 
				$(panes[(page-1)]).fadeIn(1000); 
				active_index = page-1;
				$('#carousel_counter_' + page).addClass('active');
				intr = setInterval(function() { normalRotation(); }, 6000);				
				
			}
			return this; 	
		}
	};	
})(jQuery);
