/**
 * SilverStripe.com javascript interactions
 *
 * @copyright 2010 SilverStripe LTD
 */

(function($) {
	$(document).ready(function() {
		
		
		
		/** --------------------------------------------------------------------
		 *
		 * Rotating banner functionality. Automatically cycles through the slides
		 * and provides the logic for the pagiation
		 *
		 * -------------------------------------------------------------------- */
		$(".banner-pagination").show();
		$(".banner-pagination a").click(function() {
			if($(this).hasClass('selected')) return false;
			
			$(".banner-pagination a.selected").removeClass('selected');
			
			// find out new index
			var newBanner = $(".banner-images div.banner").eq($(this).text() - 1);
	
			$(".banner-images .selected").fadeOut('slow',function() { 
				$(this).removeClass('selected');
				newBanner.fadeIn('slow').addClass('selected');
			});
			
			$(this).addClass('selected');
			
			return false;
		});
		
		$(".banner-showcase").hover(function() {
			$(".banner-pagination").stopTime();
		}).mouseleave(function() {
			cycleBanners();
		});
		
		if($(".banner-pagination a").length > 1) {
			cycleBanners();
		}
		
		/* stop any scrolling banner */
		$("a.lightbox").click(function() {
			$(".banner-pagination").addClass('lightbox-opened').stopTime();

			return false;
		});
		
		function cycleBanners() {
			if($(".banner-pagination").hasClass('lightbox-opened')) return false;
			
			$(".banner-pagination").everyTime(7500, function() {
				if($(".banner-pagination a.selected").next("a").length > 0) {
					$(".banner-pagination a.selected").next("a").click();
				}
				else {
					$(".banner-pagination a:first").click();
				}
			});
		}

	})
	
})(jQuery);

