(function($){
	
	if ($('body#home').length > 0) {
		
		var banner_interval	= 8000;
		var interval;
		
		// Homepage banner images
		$('#feature .banner-nav a').click(function() {
			if ($(this).hasClass('active')) {
				return false;
			}
			
			// Stop auto-rotate
			window.clearInterval(interval);
			
			// Get the banner
			var $new_banner		= $(this).next().children('a');
			
			// Swaperoo
			go_to_image($new_banner);
			
			return false;
		});
		
		var go_to_image = function($new) {
			// Get the old banner
			var $old	= $('#banner a');
			
			// If new isn't provided, we'll pick one
			if (! $new || typeof $new == 'undefined') {
				var $parent	= $('#feature .banner-nav a.active').closest('li').next().find('.banner a');
				var $new	= $parent.clone();
				if ($new.length == 0) {
					$parent	= $('#feature .banner-nav li:first .banner a');
					$new	= $parent.clone();
				}
			} else {
				var $parent	= $new
				$new		= $new.clone();
			}
			
			// Un-activate the old and activate the new
			$parent.closest('ul').find('.active').removeClass('active').end().end().closest('li').children('a').addClass('active')
			
			// Place the new banner "behind" the current banner
			$old.after($new);
			
			// Move the new banner over
			$new.css('left', '961px').animate({left: 0}, function() {
				// Replace the semi-transparent overlay text
				$('#feature p.banner-caption').html($parent.closest('li').children('p.caption').html());
				
				// Remove the old banner
				$old.remove();
			});
		};
		
		// Banner image rotatomatic
		interval = setInterval(go_to_image, banner_interval);
		
		// Don't rotate during hover
		$('#feature').hover(function() {
			window.clearInterval(interval)
		}, function() {
			interval = setInterval(go_to_image, banner_interval);
		});
		
	}
	
	if ($('#minical').length > 0) {
		var init_minicals	= function() {
			// Create holder divs for prev and next minicals
			var $prev_div	= $('<div id="minical_prev"></div>').hide();
			var $next_div	= $('<div id="minical_next"></div>').hide();
			$('#minical').append($prev_div).append($next_div);
			
			load_minical('prev');
			load_minical('next');
		}
		var load_minical	= function(which) {
			var url	= $('#mc_'+which+'_month').attr('href').replace('events', 'minical');
			$.get(url, function(d) {
				$('#minical_'+which).html(d);
				$('#mc_'+which+'_month').css('visibility', 'visible');
			});
		}
		
		init_minicals();
				
		// Assign click action
		$('#mc_prev_month, #mc_next_month').live('click', function() {
			$('#mc_prev_month, #mc_next_month').css('visibility', 'hidden');
			if ($(this).attr('id') == 'mc_prev_month') {
				$('#minical').html($('#minical_prev').html())
				init_minicals();
			} else {
				$('#minical').html($('#minical_next').html());
				init_minicals();
			}
			
			return false;
		});
	} 
	
})(window.jQuery);
