(function($) {
	
	$.fn.reverse = [].reverse; 
	
	$.fn.extend({
        joeNews: function(options) {
		
			var defaults = {
			
			};
			
			return this.each(function() {
				
				var options = $.extend(defaults, options);
				
				// Initial vars
				var obj = $(this),
					prev = $('[data-for="' + $(this).attr('id') + '"]').find('.prev'),
					next = $('[data-for="' + $(this).attr('id') + '"]').find('.next'),
					items = $(this).find('> li'),
					currentIndex = 0;
					
				items.not(':first').addClass('closed');
				
				function move_to_item(newIndex) {
		
					if(obj.is(':animated')) {
						return false;
					}
	
					var newItem = items.eq(newIndex);
					var newOffset = newItem.position().top;
					
					items.fadeTo(200, 0.2, function() {
						$(this).addClass('closed').css('opacity','');
						obj.stop().animate({
							top: parseInt(newOffset * -1)
						}, {
							duration: 1000,
							easing: 'easeOutExpo',
							complete: function() {
								if(head.opacity) {
									newItem.fadeTo(200, 1, function() {
										$(this).removeClass('closed').css('opacity','');
									});
								}
								else {
									$(this).removeClass('closed');
								}
							}
						});
					});
					
					currentIndex = newIndex;
					
					next.add(prev).removeClass('disabled');
					
					if(currentIndex == (items.length-1)) {
						 next.addClass('disabled');
					}
				
					if(currentIndex == 0) {
						prev.addClass('disabled');
					}
					
				}
				
				next.add(prev).bind('click', function(e) {
					e.preventDefault();
				});
								
				next.parent('li').click(function(e) {
					if($(this).find('a').hasClass('disabled')) return false;
					move_to_item(currentIndex + 1);
					return true;
				});
				
				prev.parent('li').click(function(e) {
					if($(this).find('a').hasClass('disabled')) return false;
					move_to_item(currentIndex - 1);
					return false;
				});
				
				obj.delegate('li.closed', 'click', function(e) {
					e.preventDefault();
					move_to_item($(this).index());
					return false
				});
				
				items.find('.detail').each(function() {
					$(this).data('fullHeight', $(this).outerHeight());
					$(this).height(0);
				});
				
				items.find('.read').click(function(e) {
					var detail = $(this).siblings('.detail');
					if(!detail.height()) {
						detail.animate({
							height: detail.data('fullHeight')
						}, {
							duration: 500,
							easing: 'easeOutExpo'
						});	
						
						//$(this).html('Read less');
						
					}
					else {
						detail.animate({
							height: 0
						}, {
							duration: 500,
							easing: 'easeOutExpo'
						});	
						//$(this).html('Read more');
					}
				});
				
					var callback = function(e, delta) {
					    
					    //console.log(e.pageX, e.pageY, delta);
						
					    // optionally you can prevent default behavior
					    e.stopPropagation();
					    e.preventDefault();
					    
					    if(delta < 0) {
					    	next.parent('li').trigger('click');
					    }
					    else {
					    	prev.parent('li').trigger('click');
					    }
					    
					};
					
					$('.news-body').mousewheel(callback);
				
				
			});	
			
			
		
		}
    });
})(jQuery);

