(function($){

$.fn.feature = function(options) {
	// Extend our default options with those provided.
	var opts = $.extend({}, $.fn.feature.defaults, options);

	// Local Variables
	var items = $(this).find("div.entry");
	var indicatorholder = $(this).find('#indicators p');
	var indicators = [];
	var timeoutID = null;
	var current_index = 0;
	var current_indicator = 0;
	var learnmore = $(this).find("div#learnmore a.learnmore");
	var hoverflag = false;
	var parent = this;
	
	var init = function() {

		var counter = 0;
		items.each(function(){

			var element = $('<a class="dot"><span>' + counter + '</span></a>').appendTo(indicatorholder);
			element.index = counter;
			indicators.push(element);

			element.click(function(e){
				e.preventDefault();
				parent.forceshow_item(element.index);
			});

			if (counter === 0) {
				element.addClass('first');
			};

			if (counter === items.length-1) {
				element.addClass('last');
			};

			counter++;

		});

		parent.bind('mouseenter', function(){
			hoverflag = true;
		});

		parent.bind('mouseleave', function(){
			hoverflag = false;
		});

		parent.highlight_indicator();
		parent.start_rotate();
	};

	this.show_item = function(index, func) {
		this.hide_item(current_index);
		
		if(index >= items.length) {
			index = 0;
		}
		
		if(index < 0) {
			index = items.length-1;
		} 
		
		// Set the current index to the new value.
		current_index = index;
		
		parent.highlight_indicator();
	
		parent.settarget($(items[index]).attr('rel'));
	
		$(items[index]).animate({ 
			opacity: 'show'
		}, opts.animation_duration);
	};

	this.forceshow_item = function(index) {
		parent.forcehide_item(current_index);

		current_index = index;

		parent.highlight_indicator();
		parent.settarget($(items[index]).attr('rel'));
		$(items[index]).show();
	};

	this.forcehide_item = function(index) {
		$(items[index]).hide();
	};

	this.hide_item = function(index) {
		// Check if the index actually exists.
		if(!items[index]) {
			index = 0;
		} 
		
		$(items[index]).animate({ 
			opacity: 'hide'
		}, opts.animation_duration);
	};

	this.show_next = function() {
		this.show_item(current_index + 1);
	};

	this.show_previous = function() {
		this.show_item(current_index - 1);
	};

	this.highlight_indicator = function() {
		indicators[current_indicator].removeClass('current');
		indicators[current_index].addClass('current');
		current_indicator = current_index;	
	};

	this.settarget = function(target) {
		learnmore.attr('href', '#' + target);
	};

	this.rotate = function() {

		if (hoverflag) { return; };

		if(opts.pause == true) {
			return;
		}

		// success
		this.show_next();

	};

	this.start_rotate = function() {
		var self = this;
		timeoutID = window.setInterval(function(){ self.rotate(); }, opts.rotate_delay);
	};

	this.stop_rotate = function() {
		window.clearInterval(timeoutID);
	};

	init();

	return(this);
};

// override these globally if you like (they are all optional)
$.fn.feature.defaults = {
	rotate_delay:			5000,
	pause:				false,
	show_controls:			false,
	animation_duration:		1500,
	bullet_off:			"/media/images/white_bullet.gif",
	bullet_on:			"/media/images/yellow_bullet.gif"
};

})(jQuery);

$(document).ready(function(){

	$('#rotator').feature();

});

