(function($){
	$.rotator = {
		defaults: {
			headerImages: new Array(),
			featureImages: new Array(),
			interval: 6000,
			headerHeight: 301,
			featureHeight: 290
		}
	};
	$.fn.extend({
		rotator : function(options){
			// ---------------------------------------------------------------
			
			// set options
			options = $.extend($.rotator.defaults, options);
			
			// initialize variables
			var headerIndex = 0;
			var headerLength = options.headerImages.length;
			var headerShouldAnimate = headerLength > 1;
			
			var featureIndex = 0;
			var features = $('#featureContent .product');
			var featureLength = $(features).length;
			var featureShouldAnimate = featureLength > 2;
			
			var timer = null;
			var activeHeaderLink = null;
			var disableInteraction = false;
			
			// load the first header image
			if (headerLength) {
				$('#headerContent').append(getHeaderHTML('headerImageA', 0));
			}
			
			// build header navigation if there is more than one header image
			if (headerShouldAnimate) {
				$('#headerNavigationWrapper').append('<div id="headerNavigation"></div>');
				// load the second header image
				$('#headerContent').append(getHeaderHTML('headerImageB', 1));
				// create header navigation
				for (var i = 0; i < headerLength; i++) {
					$('#headerNavigation').append('<a href="#" id="headerLink'+i+'">'+(i+1)+'</a>');
				}
				// attach click events to the header navigation
				$('#headerNavigation a').each(function(i){
					// active the first header link
					if (i == 0) {
						activeHeaderLink = $(this);
						activeHeaderLink.addClass('active');
					}
					// attach events
					$(this).click(function(){
						// if the header or feature is not animating 
						// and the link clicked is not the active link...
						if ( ! disableInteraction && i != headerIndex) {
							// stop the timer
							clearTimeout(timer);
							// if the next header image that is loaded is not the one that is requested...
							if (i != getNextHeaderIndex()) {
								// remove the header image that was suppose to be next...
								$('#headerImageB').remove();
								// and load the new header image
								$('#headerContent').append(getHeaderHTML('headerImageB', i));
							}
							// reset the index
							headerIndex = i;
							// animate to the requested header image
							animateHeader(true);
						}
						// prevent page jumping
						return false;
					});
				});
			}
			
			// reveal the first feature product
			if (featureLength) {
				showFeature('featureProductA', 0);
				// reveal the second feature product if needed
				if (featureLength > 1) {
					featureIndex ++;
					showFeature('featureProductB', 1);
				}
			}
			
			// reveal the third feature product if there is more than two features
			if (featureShouldAnimate) {
				showFeature('featureProductC', 2);
			}
			
			// start timer
			timer = setTimeout(timerComplete, options.interval);
			
			// ---------------------------------------------------------------
			
			function timerComplete(){
				if (headerShouldAnimate) {
					headerIndex = getNextHeaderIndex();
					animateHeader();
				} else if (featureShouldAnimate) {
					featureIndex = getNextFeatureIndex();
					animateFeature();
				}
			}
			
			// ---------------------------------------------------------------
			
			function animateHeader(clickFromNavigation){
				disableInteraction = true;
				// animate to the next header image
				$('#headerContent').animate({top:'-'+options.headerHeight+'px'}, 1000, 'easeOutQuad', function(){
					disableInteraction = false;
					// deactive previous navigation link
					activeHeaderLink.removeClass('active');
					// active current navigation link
					activeHeaderLink = $('#headerLink'+headerIndex);
					activeHeaderLink.addClass('active');
					// remove header image
					$('#headerImageA').remove();
					// reposition content
					$('#headerContent').css({top:'0px'});
					// swap ids
					$('#headerImageB').attr('id', 'headerImageA');
					// load the next header image
					$('#headerContent').append(getHeaderHTML('headerImageB', getNextHeaderIndex()));
					// if this header animation was triggered by the timer and the feature needs to animate...
					// else don't animate the feature and restart the timer.
					if ( ! clickFromNavigation && featureShouldAnimate) {
						featureIndex = getNextFeatureIndex();
						animateFeature();
					} else {
						timer = setTimeout(timerComplete, options.interval);
					}
				});
			}
			function getNextHeaderIndex(){
				return (headerIndex == headerLength - 1) ? 0 : headerIndex + 1;
			}
			function getHeaderHTML(id, index){
				return '<div id="'+id+'"><img src="'+options.headerImages[index]+'" /></div>';
			}
			
			// ---------------------------------------------------------------
			
			function animateFeature(){
				disableInteraction = true;
				// fade out the top feature product
				$('#featureProductA').animate({opacity:0}, 500, function(){
					// animate to the next feature product
					$('#featureContent').animate({top:'-'+options.featureHeight+'px'}, 1000, 'easeOutQuad', function(){
						disableInteraction = false;
						// remove the top feature product
						$('#featureProductA').css({display:'none', opacity:100});
						$('#featureProductA').attr('id', '');
						// reposition content
						$('#featureContent').css({top:'0px'});
						// swap ids
						$('#featureProductB').attr('id', 'featureProductA');
						$('#featureProductC').attr('id', 'featureProductB');
						// reveal the next feature product
						showFeature('featureProductC', getNextFeatureIndex());
						// restart timer
						timer = setTimeout(timerComplete, options.interval);
					});
				});
			}
			function getNextFeatureIndex(){
				return (featureIndex == featureLength - 1) ? 0 : featureIndex + 1;
			}
			function showFeature(id, index){
				$(features).eq(index).attr('id', id);
				$(features).eq(index).css({display:'block'});
			}
			
			// ---------------------------------------------------------------
		}
	});
})(jQuery);
