;(function($) {

	var feature_obj, nav, items, timeout;
	
	$.feature = {		
		defaults: {
			delay: 5000,
			animtime: 1200,
			mode: 'slide'
		}	
	};

	$.fn.extend({
		
		feature: function(options) {
	
			options = $.extend({}, $.feature.defaults, options);
		
			return this.each(function() {
				feature_obj = $(this);
				nav = feature_obj.find('ul.nav');
				items = feature_obj.find('ul.items');
				
				$.data(feature_obj, "feature", options);

				if (items.find('li').length > 1) {

					create_pagination();
					create_pagination_actions();	
				
					init_items();		
					feature_action(0);										
				

					start_timeout();		
				}	
			})
									
		} // end function
	});
	
	function settings(element) {
		return $.data(element, "feature");
	}
	
	function create_pagination() {
		var news_count = items.find('li').length;

		nav.empty();
				
		for(i=1; i < (news_count + 1); i++) {
			nav.append('<li><a href="#news_'+ i +'" title="Read this news item">'+ i +'</a></li>');
		}		
	}

	function create_pagination_actions() {
		var nav_anchors = nav.find('li a');
		var width = feature_obj.width();
		
		nav_anchors.click(function() {
			var index = $(this).text() - 1;
			
			clearInterval(timeout);
			feature_action(index);				
		});
	}
		
	function init_items() {	
		switch(settings(feature_obj).mode) {
			case "slide":		
				var item_count = items.find('li').length;
				var width = items.find('li').outerWidth();
				
				items.width(item_count * width);		
				
				break;
			case "fade":
				items.find('li').each(function(i) {
					$(this).css({
						display: 'none',
						position: 'absolute',
						top:      0,
						left:     0,
						opacity:  0
					});
				})
				items.find('li:first').css({
					display: 'block',
					opacity: 1
				});
				break;
			default:		
		}
	}
	
	function feature_action(index) {
		switch(settings(feature_obj).mode) {
			case "slide":
				var width = items.find('li').outerWidth();
				var pos = index * width * -1;
				
				items.animate({
					left: pos
				}, settings(feature_obj).animtime, 'swing');	
				
				
				
				break;
			case "fade":
				var current_index  = (nav.find('li.selected a').length > 0) ?  nav.find('li.selected a').text() - 1 : nav.find('li').length - 1;
				var move_to_index  = index;
												
				//items.find('li:eq('+ index +')').css({ opacity: 1, zIndex: index });
				items.find('li:eq('+ move_to_index  +')').css({ zIndex: index + 1 }).show().animate({ opacity: 1 }, settings(feature_obj).animtime / 2, 'swing');				
				items.find('li:eq('+ current_index  +')').css({ zIndex: index }).animate({ opacity: 0 }, settings(feature_obj).animtime, 'linear', function() { $(this).hide(); });				
				
				break;
			default:
		}
		
		nav.find('li').removeClass('selected');
		nav.find('li:eq('+ index +')').addClass('selected');
	}
	
	function start_timeout() {
		timeout = setInterval(function() {
			var current_index = nav.find('li.selected a').text();
			var item_count    = nav.find('li').length;
			var new_index;
			
			if (current_index < item_count) {
				new_index = current_index++;
			} else {
				new_index = 0;
			}
			feature_action(new_index);
			
		}, settings(feature_obj).delay);
	}

})(jQuery);
