$(document).ready(function(){
	features.init();
});

var features = function(){
	return {
		timeout: 			null,
		duration: 			5 * 1000,
		trigger: 			'click',
		triggerReturn:		false,
		items: 				null,
		links: 				null,

		init: function(){
			features.items = $('#features #features-images');
			features.links = $('#features #features-links');
			
			if(features.items.length == 0 || features.links.length == 0)
				return false;
			
			features.links.find('a').live(features.trigger, function(){
				features.change(this);
				return features.triggerReturn;
			});
			
			features.timeout = setTimeout('features.change()',features.duration);
		},

		change: function(over) {
			clearTimeout(features.timeout);

			if(over === undefined) {
				var i = 0;
				var total = features.links.find('li').length - 1;
				var current = 0;
				var next = 0;

				features.links.find('li').each(function(){
					if($(this).hasClass('current'))
						current = i;

					i++;
				});

				if(current != total)
					next = current + 1;

				over = $(features.links.find('li')[next]).find('a');
			}

			var $this = $(over);
			var id = $this.attr('class');
			var top = $this.parent().position().top;
			var $next_image = features.items.find('#'+id);
			var $current_image = features.items.find('.current');

			if($this.parent().hasClass('current'))
				return false;

			features.items.stop(false, true);

			$current_image.fadeOut(function(){
				$current_image.removeClass('current');

				$next_image.fadeIn(function(){
					$next_image.addClass('current');
				});
			});

			$next_link = $this;
			$current_link = features.links.find('.current a');

			$next_link.parent().addClass('current');
			$current_link.parent().removeClass('current');

			features.timeout = setTimeout('features.change()',features.duration);
		}
	}
}();
