(function($){
	$(function(){
		$('#features .feature h4 em'
		+',.t_home .mod_search'
		+',.mod_house_sidebar img'
		).ifixpng();
	});

	$(function(){
		var ZINDEX_HIDDEN = 1;
		var ZINDEX_CURRENT = 2;
		var ZINDEX_TRANSITION = 3;
		var ROTATION_INTERVAL = 7000;
		var TRANSITION_LENGTH = 500;
		
		var allFeatures = $('#features .feature');
		var allFeatureLinks = $('.featuresMenu a');
		
		var isCurrent = function(){ return $(this).css('z-index') == ZINDEX_CURRENT; };
		var isNotCurrent = function(){ return !isCurrent.apply(this); };
		
		var getCurrentFeature = function(){
			var currentFeature = allFeatures.filter(isCurrent);
			//Shouldn't happen, but just in case.
			if (currentFeature.length < 1) {
				currentFeature = allFeatures.eq(0);
			}
			return currentFeature;
		};
		
		var getCurrentFeatureLink = function(){
			var currentFeature = getCurrentFeature();
			var currentFeatureLink = $('.featuresMenu a[href$=#' + currentFeature[0].id + ']');
			return currentFeatureLink;
		};
		
		var animateFeatureMenuIndicatorTo = function(link){
			allFeatureLinks.removeClass('selected');
			$(link).addClass('selected');
		};
		
		var showFeature = function(featureToShow) {
			//Normalize the features' state so that exactly one is showing, with no animations
			allFeatures
				.stop()
				.filter(isNotCurrent)
				.css({'z-index': ZINDEX_HIDDEN})
				.show()
				.fadeTo(0,1)
				;
				
			var current = getCurrentFeature();
			featureToShow = $(featureToShow);
			
			if (current[0].id != featureToShow[0].id){
				//Set feature to show as current, with old current on top
				current.css({'z-index': ZINDEX_TRANSITION});
				featureToShow.css({'z-index': ZINDEX_CURRENT}).show();
				
				//Start menu transition, since featureToShow is now technically "current"
				animateFeatureMenuIndicatorTo(getCurrentFeatureLink());
				
				//Do fade
				current
					.fadeOut(TRANSITION_LENGTH)
					.queue(function(){
						$(this).css({'z-index': ZINDEX_HIDDEN});
						$(this).dequeue();
					})
					.show()
					;
				if ($.browser.msie && $.browser.version.substr(0,1) == '7')
				{
					$(featureToShow).get(0).style.removeAttribute('filter');
				}
			}
		};
	
		//Start rotator (save a reference for later)
		$('#features').data('rotator', 
			window.setInterval(function(){
				var nextFeature = getCurrentFeature().next();
				if (nextFeature.length < 1) {
					nextFeature = allFeatures.eq(0);
				}
				showFeature(nextFeature);
			}, ROTATION_INTERVAL)
		);
		
		//Menu actions
		allFeatureLinks
			.click(function(){
				//Stop automatic rotation
				window.clearInterval($('#features').data('rotator'));

				var id = this.href.match(/#.*/)[0];
				showFeature(id);
				return false;
			})
			;
	});

	/* Locations Map */
	if ($.browser.msie && $.browser.version.substr(0,1) == '6')
	{
		//Doesn't work well enough in IE6, disable
	}
	else
	{
	$(function(){
		var ZINDEX_HIDDEN = 1;
		var ZINDEX_CURRENT = 2;
		var ZINDEX_TRANSITION = 3;
		var TRANSITION_LENGTH = 200;
		
		var allLocations = $('#locationMap .location');
		
		var isCurrent = function(){ return $(this).hasClass('selectedLocation'); };
		var isNotCurrent = function(){ return !isCurrent.apply(this); };
		
		var getCurrentLocation = function(){
			var currentLocation = allLocations.filter(isCurrent);
			//Shouldn't happen, but just in case.
			if (currentLocation.length < 1) {
				currentLocation = allLocations.eq(0);
				currentLocation.addClass('selectedLocation');
			}
			return currentLocation;
		};
		
		/* normalize initial state (kill default styles) */
		allLocations
			.filter(isNotCurrent)
			.stop()
			.show()
			.css({'left': '282px'})
			;
		
		var showLocation = function(locationToShow) {
			//Normalize the features' state so that exactly one is showing, with no animations
			var currentLocation = getCurrentLocation();
			locationToShow = $(locationToShow);
			allLocations
				.stop()
				.hide()
				.css({'left': '282px'})
				;

			locationToShow.addClass('selectedLocation');
			
			locationToShow
				.show()
				.animate({'left': 0}, TRANSITION_LENGTH, 'swing')
				;
		};
	
		//Menu actions
		$('#locationLinks a,.location h5 a')
			.click(function(){
				var id = this.href.match(/#.*/)[0];
				showLocation(id);
				return false;
			})
			;
	});
	}
})(jQuery);