﻿
var cycles = new Array();
var navTabs = new Array();
var curCycle = 0;
var cycleCount = 0;
var firstCount = 0;
var preventRecurse = false;
var userInteraction = false;
var autoClick = false;
var cycleSpeed = 5000;

function SlideController(currSlideElement, nextSlideElement, options, forwardFlag) {
	if (preventRecurse || userInteraction)
		return cycleSpeed;
	if (firstCount < cycleCount) {
		firstCount++;
		return cycleSpeed;
	}
	if (curCycle == options.cycleIndex && options.nextSlide == 1) {
		preventRecurse = true;
		cycles[curCycle].cycle('pause');
		curCycle++;
		if (curCycle >= cycleCount)
			curCycle = 0;
		cycles[curCycle].cycle(0);
		autoClick = true;
		navTabs[curCycle].click();
		autoClick = false;
		cycles[curCycle].cycle('resume', false);
		preventRecurse = false;
		return cycleSpeed;
	}
	return cycleSpeed;
}

$(document).ready(function () {
	// Show first tab.
	$('#FrontPageSlides div.FrontPageSlideNavTab:first').addClass('FrontPageSlideNavTabSelected');
	// Show first slide.
	$('#FrontPageSlides div.FrontPageSlideShow:first').css('display', 'block');
	// Setup slide tab navigation.
	$('#FrontPageSlideNav div.FrontPageSlideNavTab').each(function (index) {
		var i = index;
		navTabs[i] = $(this);
		$(this).click(function () {
			if (!autoClick) {
				userInteraction = true;
				var j;
				for (j = 0; j < cycles.length; j++) {
					preventRecurse = true;
					cycles[j].cycle('pause');
					preventRecurse = false;
				}
			}
			$('#FrontPageSlideNav div.FrontPageSlideNavTab').removeClass('FrontPageSlideNavTabSelected');
			$(this).addClass('FrontPageSlideNavTabSelected');
			$('#FrontPageSlides div.FrontPageSlideShow').hide();
			$('#FrontPageSlides div.FrontPageSlideShow:eq(' + i + ')').css('display', 'block');
		});
	});
	// Setup each slideshow's navigation.
	$('.FrontPageSlideShow').each(function (index) {
		$(this).find('.SlideNumbers').each(function (index) {
			$(this).find('.SlideNumber').each(function (index) {
				var i = index;
				$(this).click(function () {
					$(this).closest('.FrontPageSlideShow').cycle(i);
				});
			});
		});
	});
	// Setup cycle.
	$('#FrontPageSlides div.FrontPageSlideShow').each(function (index) {
		cycleCount++;
		$(this).cycle({
			//fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			//sync: 0
			timeout: cycleSpeed,
			fx: 'none',
			timeoutFn: SlideController,
			firstTime: true,
			cycleIndex: index
			//prev: '#FrontPageSlidePrev',
			//next: '#FrontPageSlideNext'
		});
		if (index > 0)
			$(this).cycle('pause');
		cycles[index] = $(this);
	});
});

