// JavaScript Document
var activeStory = 1;
var storyCount;
var banners;

function openStoryByIndex(num) {
	if (num != activeStory) {
		jQuery(banners).find(".CarouselBg:nth-child(" + activeStory + ")").css("z-index", "1");
		jQuery(banners).find(".CarouselBg:nth-child(" + num + ")").css("z-index", "2");

		jQuery(banners).find(".CarouselBg:nth-child(" + num + ")").fadeIn("slow");
		jQuery(banners).find(".CarouselBg:nth-child(" + activeStory + ")").fadeOut("slow");

		activeStory = num;
	}
}

function nextStory() {
	if (activeStory != storyCount) {
		openStoryByIndex(activeStory + 1);
	} else {
		openStoryByIndex(1);
	}
}

function prevStory() {
	if (activeStory != 1) {
		openStoryByIndex(activeStory - 1);
	} else {
		openStoryByIndex(storyCount);
	}
}

function rotate() {
	jQuery('.CarouselWrapper a.CarouselNext').click();
}

function initCarouselBg(objDoc, rotation) {
	jQuery('.CarouselBgWrapper>.CarouselBg:first-child').show();

	var obj = jQuery(objDoc);
	banners = jQuery(obj).find(".CarouselBgWrapper");
	
	// rotation time (ms) 
	var run = setInterval('rotate()', rotation);

	storyCount = jQuery(banners).find(".CarouselBg").length;
	
	jQuery('.CarouselWrapper a.CarouselNext').click(function () {
		nextStory();
		clearInterval(run);
		run = setInterval('rotate()', rotation);
		return false;
	})
	jQuery('.CarouselWrapper a.CarouselPrev').click(function () {
		prevStory();
		clearInterval(run);
		run = setInterval('rotate()', rotation);
		return false;
	});
}

