/**
 * @author Briley
 */
$(function() {
	// Set up the homepage slideshow
	HomeSlideshow.init($('#slideshow'), $('#slideshow img').remove().css('visibility', 'visible'));
	
	// Set up "announcements" animator
	$('#announcements a.header').click(toggleAnnouncements);
	setTimeout(toggleAnnouncements, 1000);
});
function toggleAnnouncements() {
	var $ann = $('#announcements');
	var $bkg = $('#announcementsBackground');
	
	if ($ann.hasClass('up')) {
		$bkg.animate({top:320}, {duration:500, easing:'easeInBack'});
		$ann.animate({paddingTop:360}, {
			duration: 500,
			easing: 'easeInBack',
			complete: function(){
				$ann.removeClass('up').addClass('down');
			}
		});
	}
	else {
		$bkg.animate({top:60}, {duration:500, easing:'easeOutBack'});
		$ann.animate({paddingTop:100}, {
			easing: 'easeOutBack',
			duration: 500,
			complete: function(){
				$ann.removeClass('down').addClass('up');
			}
		});
	}
	return false;
}
var HomeSlideshow = {
	$gallery: null,
	$images:  null,
	index: 0,
	interval: 4000,
	animSpeed: 2000,
	
	init: function($gal, $set) {
		var H = HomeSlideshow;
		H.$gallery = $gal;
		H.$images = $set;
		
		// Advance
		HomeSlideshow.advance();
	},
	
	advance: function() {
		var $img = HomeSlideshow.$images.eq(HomeSlideshow.index);
		// Add new image
		$img.css('opacity', 0).appendTo(HomeSlideshow.$gallery);
		// If not loaded, wait until loaded
		if (!$img[0].complete) $img.load(function() { HomeSlideshow.fadein($img); });
		// Otherwise go ahead and fade it in
		else HomeSlideshow.fadein($img);
		
		// Increment index
		HomeSlideshow.index++;
		if (HomeSlideshow.index == HomeSlideshow.$images.length) HomeSlideshow.index = 0;
	},
	
	fadein: function($img) {
		$img.animate({opacity:1}, {duration:HomeSlideshow.animSpeed, easing:'easeInSine', complete:function() {
			setTimeout(HomeSlideshow.advance, HomeSlideshow.interval);
		}});
	}
};
