(function($){  
	$.fn.featurizer = function(options){   
		var container = this;  
		var selected_feature;
		var feature_height;
		var total_features;
		var current_feature_index = 0;
		var interval_id;
		var nextWhich = -1;
		var transitioning = false;
		
		$(container).addClass("featurizer-container");
		
		var settings = {
			'interval': 6000,
			'fade_duration':600,
			'marker': 'featurizer/images/featurizer_dot.png',
			'selected_marker': 'featurizer/images/featurizer_dot_selected.png',
			'feature_width': 300
    };
    
    options = $.extend({}, settings, options );
    
    //set up the featured images
    var feature_images = $(container).children();
    
    $(feature_images).addClass("featurizer-image");
    $(feature_images).first().addClass("featurizer-selected")
    
    selected_feature = $(feature_images).first();
    $(feature_images).wrapAll('<div class="featurizer-images" />');
		//add in the dots
		dots_html = '<div class="featurizer-before-markers"><!-- --></div><div class="featurizer-markers">';
		var marker_image;
		var marker_class;
		total_features = feature_images.length
		for(i=0;i<total_features;i++) {
			marker_image = options.selected_marker;
			if(i==0) {
				marker_image = options.selected_marker;
				marker_class = "featurizer-marker-selected";
			} else {
				marker_class = "featurizer-marker";
			}
			
			last_image = $(feature_images[i]);
			dots_html = dots_html + '<a href="#" class="'+marker_class+'" id="marker_'+i+'"><img src="'+marker_image+'" /></a>';
		}
		dots_html = dots_html + "</div>"
		container.append(dots_html);
		$(".featurizer-marker img").fadeTo(1,.5);
		$(".featurizer-markers").css({"width":options.feature_width});
		$(".featurizer-marker").click(function() {
			var marker_index = $(this).attr("id").replace("marker_","");
			selectFeature(parseInt(marker_index));
			return false;
		});
		$(".featurizer-marker-selected").click(function() {
			var marker_index = $(this).attr("id").replace("marker_","");
			selectFeature(parseInt(marker_index));
			return false;
		});
		
		//start the timer
		interval_id = setTimeout( function() { timedAdvance(0) }, (options.interval+options.fade_duration) );
		
		function selectFeature(which) {
			if(transitioning) {
				nextWhich = which;
				return;
			}
			if(current_feature_index == which) {
				return;
			}
			if(interval_id) {
				clearTimeout(interval_id);
			}	
			current_feature_index = which;
			interval_id = setTimeout( function() { timedAdvance(which) }, options.interval+options.fade_duration,which );
			transitioning = true;
			selected_dot = $(".featurizer-markers").children()[which];
			$(selected_dot).children("img").fadeTo(options.fade_duration,1);
			$(".featurizer-marker-selected img").fadeTo(options.fade_duration,.5);
			old_selected_dot = $(".featurizer-marker-selected");
			old_selected_dot.removeClass("featurizer-marker-selected");
			old_selected_dot.addClass("featurizer-marker");
			$(selected_dot).removeClass("featurizer-marker");
			$(selected_dot).addClass("featurizer-marker-selected");
			selected_feature = $(feature_images[which]);
			selected_feature.css({"z-index":99});

			$(".featurizer-selected").fadeOut(options.fade_duration,function() {
				$(this).removeClass("featurizer-selected");
				$(this).css({"z-index":50});
				$(this).fadeIn(1);
				selected_feature.addClass("featurizer-selected");
				selected_feature.css({"z-index":100});
				transitioning = false;
				nextTransition();
			});
			
		}
		
		function timedAdvance(current_index) {
			if((current_index+1) >= total_features) {
				selectFeature(0);
			} else {
				selectFeature(current_index+1);
			}
		}
		
		function nextTransition() {
			if(nextWhich > -1) {
				selectFeature(nextWhich);
				nextWhich = -1;
			}
		}
	};  
 })(jQuery);
