(function($) {
	var imgs = $('#homepage_feature_mainbox img'),
		height = 0;
	imgs.each(function(i, img) {
		if(img.height > height) {
			height = img.height;
		}
	});
	$('#homepage_feature_mainbox').css({
		height: height + 'px'
	});
	imgs.css({
		position: 'absolute',
		top: 0,
		left: 0
	});

	var slideshowEnabled = true, slideshowTimer, slideshow = function() {
		var currentActiveNav = $('#homepage_feature_nav .active'),
			navItems = $('#homepage_feature_nav li'),
			currentIndex = navItems.index(currentActiveNav),
			nextIndex = currentIndex == navItems.length - 1 ? 0 : currentIndex + 1,
			newActiveNav = $(navItems[nextIndex]);

		changeFeature(newActiveNav);

		if(slideshowEnabled) {
			slideshowTimer = setTimeout(slideshow, 4000);
		}
	};
	if(slideshowEnabled) {
		slideshowTimer = setTimeout(slideshow, 4000);
	}

	var changeFeature = function(changeTo, click) {
		var currentActiveNav = $('#homepage_feature_nav .active'),
			newActiveNav = $(changeTo),
			currentActiveImg = $('#homepage_feature_mainbox .active'),
			newActiveImg = $('#' + newActiveNav.attr('id').replace(/^homepage_feature_nav/, 'homepage_feature_img'));

		if(click) {
			clearTimeout(slideshowTimer);
			slideshowEnabled = false;
		}

		if(currentActiveNav[0] == newActiveNav[0] && currentActiveImg[0] == newActiveImg[0]) {
			return;
		}

		currentActiveNav.removeClass('active');
		newActiveNav.addClass('active');

		currentActiveImg.fadeOut(250 * (click ? 1 : 2), function() {
			currentActiveImg.removeClass('active');
		})
		newActiveImg.fadeIn(250 * (click ? 1 : 2), function() {
			newActiveImg.addClass('active');
		});
	};

	$('#homepage_feature_nav li').live('click', function(e) {
		changeFeature($(this), true);
	});
})(jQuery);