var RotationTimeouts = new Array();
var RotationInterval = 6000;
var FadeTime = 400;
var timer = null;

$(window).ready(function() {
	if ($('#rotation-wrapper').length > 0) {
		$('#bullets').width(28 * $('#rotation-wrapper').children().length);
		for (var i=0; i<$('#rotation-wrapper').children().length; i++) {
			$('#bullets').append('<div id="bullet-' + i + '" class="bullet">&nbsp;</div>');
		}
		$('#bullets').children().first().addClass('active');
		$('#bullets').children().each(function(index) {
			$(this).click(function() {
				SwitchImage(index);
			});
		});

		timer = setTimeout(RotateImage, RotationInterval);
	}
});

function SwitchImage(i) {
	if (timer)
		clearTimeout(timer);

	$('#rotation-wrapper').fadeTo(FadeTime, 0, function() {
		var active = $('#rotation-wrapper').children('.active');
		$(active).removeClass('active');
		$('#promo-' + (i+1)).addClass('active');
		$('#rotation-wrapper').fadeTo(FadeTime, 1);

		var activeNow = $('#bullets').children('.active');
		$(activeNow).removeClass('active');
		$("#bullet-" + i).addClass('active');
	});

	//timer = setTimeout(RotateImage, RotationInterval);
}

function RotateImage() {
	$('#rotation-wrapper').fadeTo(FadeTime, 0, function() {
		var first = $('#rotation-wrapper').children().first();
		$(first).removeClass('active');
		$('#rotation-wrapper').append(first);
		$('#rotation-wrapper').children().first().addClass('active');

		var activeNow = $('#bullets').children('.active');
		$(activeNow).removeClass('active');
		var activeNext = $(activeNow).next();
		if (activeNext.length != 0) {
			$(activeNext).addClass('active');
		} else {
			$('#bullets').children().first().addClass('active');
		}

		$('#rotation-wrapper').fadeTo(FadeTime, 1);
	});

	timer = setTimeout(RotateImage, RotationInterval);
}

