
/* ---------------------------------------------
 * Handles controls and timed banner rotation
 * for a jQuery based carousel with thumbnails
 * and text contained in the banner.
 * ---------------------------------------------*/
jQuery(document).ready(function () {

	var carousel = jQuery('.bannerCarousel');
	if(carousel.length > 0) {
		var activeBannerCarouselItem = 0;
		var thumbnails = carousel.find('.thumbnail');
		var banners = carousel.find('.display > img'); 
		var texts = carousel.find('.display > .content');
		var swapIndex = 0;
		
		if(!window.carousel || !window.carousel.timer) {
			if(!window.carousel) {
				window.carousel = {interval:6000, timer:0};
			}
		} else {
			clearTimeout(window.carousel.timer);
		}
		
		// Track clicks of links in banner carousel banners through Google Analytics
		carousel.find('.display > .content a').click(function () {
			var link = jQuery(this);
			var position = jQuery(this).parents('.bannerCarousel').eq(0).find('.controls .thumbnail.selected').index() + 1;
			widgetUsed("BannerCarouselLinkClick", ("Banner " + position + ": " + link.text()));
		});
		
		/* --------------------------------------------- 
		 * Intervene in the automatic looping, 
		 * set the active item to the item that was clicked, 
		 * run banner loop to swap to the active item and reset the timer,
		 * continue the auto-loop process 
		 * --------------------------------------------- */
		jQuery(".bannerCarousel .controls .thumbnail").click(function() {
			var thumbnail = jQuery(this);
			var carousel = jQuery("#banner .bannerCarousel");
			activeBannerCarouselItem = thumbnail.index();
			var title = carousel.find('.display > .content').eq(activeBannerCarouselItem).find('h3').text();
			clearTimeout(window.carousel.timer);
			
			// Track carousel item clicks through Google Analytics
			if(!thumbnail.hasClass("selected")) {
				widgetUsed("BannerCarouselItemClick", ("Item " + (activeBannerCarouselItem + 1) + ": " + title));
			}

			bannerCarouselChange();
		});
		bannerCarouselChange();
	}

	/* ---------------------------------------------
	 * Changes the displayed banner and active thumbnail item 
	 * to the one at the given index. This does not affect the timer.
	 * --------------------------------------------- */
	function setBanner (index) {
		swapIndex = index;
		thumbnails.each(swapThumbnails);
	}
	
	function swapThumbnails(current) {
		var currentThumbnail = thumbnails.eq(current);
		var currentBanner = banners.eq(current);
		var currentText = texts.eq(current);
		if(current == swapIndex) {
			currentThumbnail.addClass("selected");
			currentBanner.fadeIn(1000);
			jQuery('.bannerCarousel').children('.content').replaceWith(currentText.clone(true));
		} else {
			currentThumbnail.removeClass("selected");
			currentBanner.fadeOut(1000);
		}
	}
	
	
	/* --------------------------------------------- 
	 * Reset the timer and switch to whichever banner has been
	 * designated as the active item. Increment the active item 
	 * --------------------------------------------- */
	function bannerCarouselChange() {
		var thumbnails = carousel.find('.thumbnail');
		setBanner(activeBannerCarouselItem);
		activeBannerCarouselItem++;
		if(activeBannerCarouselItem >= thumbnails.length) {
			activeBannerCarouselItem = 0;
		}
		clearTimeout(window.carousel.timer);
		if(window.carousel.interval > 0) {
			window.carousel.timer = setTimeout(bannerCarouselChange, window.carousel.interval);
		}
	}

});









/* ---------------------------------------------
 * Handles extra carousel controls, pagination 
 * and banner rotation for a jQuery based
 * jCarousel
 * ---------------------------------------------*/

// difference between position/index in carousel and position/index in DOM.
var carouselIndexAdjust = 0;

// Used to calculate "carouselItemsPerPage"
var carouselItemWidth = 210;

// The number of carousel items that are visible at a time
var carouselItemsPerPage;

/* ---------------------------------------------
 * Activates the carousel with an option to autoscroll. 
 * autoScrollDelay is the number of seconds to wait between scrolling to the next carousel item.
 * if autoScrollDelay is 0 autoscroll will be disabled.
 * --------------------------------------------- */
function initCarousel(autoScrollDelay) {
	var carousel = jQuery(".carousel").jcarousel({
		scroll: 1,
		start: 1,
		initCallback: carousel_initCallback,
		itemVisibleOutCallback: updateCarouselPage,
		wrap: "circular",
		auto: autoScrollDelay
	});
}


/***
 * Called after the carousel is loaded
 * Determines the number of visible carousel items based on the width of the carousel
 * Sets the click to scroll behavior for carousel items
 * Sets the click to scroll behavior for pagination dots
 */
function carousel_initCallback(carousel) {
jQuery('.carousel-group').css('visibility', 'visible');
	carouselItemsPerPage = Math.floor(parseInt(jQuery('.jcarousel-clip-horizontal').width()) / carouselItemWidth);
	var domainAndPath = window.location.href.substring(window.location.href.indexOf(".") + 1);
	jQuery('.carousel-pages a').bind('click', function () {
		carousel.stopAuto();
		carousel.scroll((jQuery(this).index() * carouselItemsPerPage) + 1);
		widgetUsed("CarouselPageClick", ("To Carousel Page " + (jQuery(this).index() + 1) + ": " + domainAndPath));
		jQuery('.carousel-pages a').removeClass('active');
		jQuery(this).addClass('active');
		return false;
	});
	carousel.first = 1;
	updateCarouselPage(carousel);
	jQuery('.jcarousel-prev').click(function () {
		widgetUsed("CarouselPrevClick", ("From Item " + carousel.first + ": " + domainAndPath));
	});
	jQuery('.jcarousel-next').click(function () {
		widgetUsed("CarouselNextClick", ("From Item " + carousel.first + ": " + domainAndPath));
	});
	
	jQuery('.carousel-banner > div > a').click(function() {
		var index = jQuery(this).parent().index() + 1;
		widgetUsed("CarouselBannerClick", ("Banner " + index + ": " + domainAndPath));
	});
	
	jQuery('.carousel-item-box').click(function () {
		carousel.stopAuto();
		var index = 0;
		var linked = (jQuery(this).parent('li').length == 0);
		if(linked) {
			index = jQuery(this).parent().parent().index() + 1 - carouselIndexAdjust
		} else {
			index = (jQuery(this).parent().index() + 1 - carouselIndexAdjust);
		}
		var title = jQuery(this).find('.caption h3').text();
		var indexToDisplay = (((index - 1) % carousel.size()) + 1);
		widgetUsed("CarouselItemClick", ("To Item " + indexToDisplay + ": " + domainAndPath));
		if(!linked) {
			carousel.scroll(index);
		}
	});
};

/***
 * Called whenever a carousel item that was visible moves out of view
 * The current item range is checked
 * The active carousel item is set and styled
 * The banner for the active carousel item is made visible if one exists
 * The pagination dots are updated
 */
function updateCarouselPage(carousel) {

	// The index of current carousel item (zero-based index)
	var index = carousel.first - 1;
	// The number of selectable carousel items
	var size = carousel.size();
	
	if (index >= size) {
		// Carousel has scrolled too far to the right. Jump back to the beginning.
		carousel.scroll((index % size) + 1, false);

	} else if (index < 0) {
		// Carousel has scrolled too far to the left. Jump back to the end.
		if (jQuery('.jcarousel-list li').size() > size) {
			carouselIndexAdjust = 1;
		}
		index = (index % size) + size;
		carousel.scroll((index % size) + 1, false);
	} else {
		// Carousel has moved, but is neither too far to the left or right. set the current active item and update the page dots.
		var page = Math.floor(index / carouselItemsPerPage) % Math.ceil(size / carouselItemsPerPage);
		
		// Set active item
		jQuery('.jcarousel-list li').each(function (i) {
			if (i == index + carouselIndexAdjust) {
				jQuery(this).addClass('active');

			} else {
				jQuery(this).removeClass('active');
			}
		});

		//set active banner
		jQuery('.carousel-banner > div').each(function (i) {
			if(i == index) {
				jQuery(this).addClass('active');
				if(jQuery.browser.msie) {
					jQuery(this).show();
				} else {
					jQuery(this).fadeIn();
				}
			} else {
				jQuery(this).removeClass('active');
				if(jQuery.browser.msie) {
					jQuery(this).hide();
				} else {
					jQuery(this).fadeOut();
				}
			}
		});

		// Set active page of items
		jQuery('.carousel-pages a').each(function (p) {
			if (p == page) {
				jQuery(this).addClass('active');
			} else {
				jQuery(this).removeClass('active');
			}
		});
		carousel.startAuto();
	}
}
