


jQuery(document).ready(function($) {

	
	
	// Add 'has-js' class to body tag
	$("body").addClass('has-js');
	
	
	
	// Latest Feed
	if ( $("div.latest-feed ul li").length > 0 ) {
		$("div.latest-feed ul li").each(function() {
			$(this).css( 'opacity', 0 );
			$(this).css( 'display', 'none' );
		});
		$("div.latest-feed ul li:first").css( 'display', 'block' );
		$("div.latest-feed ul li:first").addClass( 'selected' );
		$("div.latest-feed ul li:first").fadeTo( 300, 1 );
		
		var latest_feed_int = window.setInterval(function() {
			$("div.latest-feed ul li.selected").fadeTo( 300, 0, function() {
				$(this).css( 'display', 'none' );
				$(this).removeClass( 'selected' );
				if ( $(this).next().length > 0 ) {
					$(this).next().css( 'display', 'block' );
					$(this).next().addClass( 'selected' );
					$(this).next().fadeTo( 300, 1 );
				} else {
					$("div.latest-feed ul li:first").css( 'display', 'block' );
					$("div.latest-feed ul li:first").addClass( 'selected' );
					$("div.latest-feed ul li:first").fadeTo( 300, 1 );
				}
			} );
		}, 5000);
		
	}
	
	
	
	// Easing
	// Borrowed from jQuery easing plugin
	// http://gsgd.co.uk/sandbox/jquery.easing.php
	$.easing.easeInOutQuint = function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	};
	
	
	
	// SUSD Gallery
	if ( $("div.susd-gallery div.susd-gallery-scrollable p.susd-gallery-image").length > 0 ) {
		
		var gallery    = $("div.susd-gallery");
		var scrollable = $("div.susd-gallery div.susd-gallery-scrollable");
		var scrollcontents = $("div.susd-gallery div.susd-gallery-scrollable div.susd-gallery-scrollcontents");
		
		var total_width = 0;
		var total_images = 0;
		
		$("div.susd-gallery-scrollable p.susd-gallery-image:last").css('marginRight', '0px');
		$("div.susd-gallery-scrollable p.susd-gallery-image").each(function() {
			total_width += $(this).width() + 1; // has 1px margin
			total_images++;
			$(this).attr('id', 'susd-gallery-image-' + total_images);
		});
		total_width--;
		
		if ( total_images == 0 ) {
			gallery.css('display', 'none');
		} else {
		
			$.data(gallery, 'total_width', total_width);
			$.data(gallery, 'total_images', total_images);
			$.data(gallery, 'current_image', 1);
			
			scrollable.width(gallery.width());
			scrollcontents.width(total_width);
			
			gallery.css('position', 'relative');
			
			gallery.prepend('<div class="susd-gallery-next susd-gallery-nav"><a href="#susd-gallery" rel="next">Next</a></div>');
			gallery.prepend('<div class="susd-gallery-prev susd-gallery-nav"><a href="#susd-gallery" rel="prev">Previous</a></div>');
			
			if ( total_images <= 1 ) {
				$("div.susd-gallery-next").css('opacity', 0);
			}
			$("div.susd-gallery-prev").css('opacity', 0);
			
			$("div.susd-gallery div.susd-gallery-nav a").click(function(e) {
				
				var n = $.data(gallery, 'current_image');
				
				$(this).attr('rel') == 'prev' ? n-- : n++;
				
				if ( n > $.data(gallery, 'total_images') ) {
					n = $.data(gallery, 'total_images');
				}
				if ( n < 1) {
					n = 1;
				}
				
				if ( n == 1 ) {
					$("div.susd-gallery-prev").fadeTo(500, 0);
				} else {
					$("div.susd-gallery-prev").fadeTo(500, 1);
				}
				
				if ( n == $.data(gallery, 'total_images') ) {
					$("div.susd-gallery-next").fadeTo(500, 0);
				} else {
					$("div.susd-gallery-next").fadeTo(500, 1);
				}
				
				$.data(gallery, 'current_image', n);
				
				$("div.susd-gallery div.susd-gallery-scrollable").queue([]).stop();
				$("div.susd-gallery div.susd-gallery-scrollable").scrollTo('#susd-gallery-image-' + n, 1000, {easing:'easeInOutQuint'});
				return false;
				
			});
			
		}
		
	}
	
	
	
});


