// Define own namespace
if(typeof fyyr == 'undefined')
	fyyr = new Object();

fyyr.home = (function(){

	
	return {		
		
		slideSwitch : function() {
		    var $active = $('.toparea .image img.active');
		
		    if ( $active.length == 0 ) $active = $('.toparea .image img:last');
		
		    // use this to pull the images in the order they appear in the markup
		    var $next =  $active.next().length ? $active.next()
		        : $('.toparea .image img:first');
		
		    // uncomment the 3 lines below to pull the images in random order
		    
		    // var $sibs  = $active.siblings();
		    // var rndNum = Math.floor(Math.random() * $sibs.length );
		    // var $next  = $( $sibs[ rndNum ] );
		
		
		    $active.addClass('last-active');
		
		    $next.css({opacity: 0.0})
		        .addClass('active')
		        .animate({opacity: 1.0}, 1000, function() {
		            $active.removeClass('active last-active');
		        });
		},
		
		
		init : function() {
			
			// Generate home page event tabs
			
			$("#events").addClass("tabbed");
			$("#events h3.past").addClass("active");
			$("#events ul.upcoming").hide();
			$("#events h3").click(function(e) {
				if(!$(this).hasClass("active")) {
					
					// Deactive previous tab
					var previous = $("#events h3.active");
					previous.removeClass("active");
					var previousContentClass = previous[0].className;
										
					// Active new tab
					var contentClass = this.className;
					$(this).addClass("active");
					var newHeight = $("#events ul."+contentClass).height() + ($("#events").height() - $("#events ul."+previousContentClass).height());
					
					// Animate hiding and showing
					$("#events ul."+previousContentClass).fadeOut(300, function() {
						$("#events ul."+contentClass).fadeIn(300, function() {
							$("#events").css({height:"auto"});
						});
					});
					
					$("#events").animate({height:newHeight+"px"}, 300);
					
				}
			});
		    
		    setInterval( "fyyr.home.slideSwitch()", 5000 );
			
		}
	
	}


})();

$(document).ready(function() {
	fyyr.home.init();
});
