
jQuery.fn.simpleSlider = function(settings) {
	settings = jQuery.extend({
		container: '.slider-container',
		item: '.slider-item',
		speed : 'normal',
		slideBy : 1
    }, settings);
    return this.each(function() {
		jQuery.fn.simpleSlider.run(jQuery(this), settings);
    });
};

jQuery.fn.simpleSlider.run = function( $this, settings ) {

	var container = jQuery(settings.container, $this );
	
	var items = container.children();
	
	if ( items.length > settings.slideBy ) {
		
		
		$this.before(
			'<p class="slider-list-back"><a href="#" title="Back">&laquo; back</a></p>'+
			'<p class="slider-list-next"><a href="#" title="Next">next &raquo;</a></p>'
		);
		
		
		var $next = $this.siblings('.slider-list-next');
		var $back = $this.siblings('.slider-list-back');
		var itemWidth = jQuery(items[0]).width();
		
		var animating = false;
		
		container.css('width', (items.length * itemWidth));
		
		var moveLeft = function() {
			if (!animating) {
				animating = true;
				offsetLeft = parseInt(container.css('left')) - (itemWidth * settings.slideBy);
				if (offsetLeft + container.width() > 0) {
					$back.css('display', 'block');
					container.animate({
						left: offsetLeft
					}, settings.speed, function() {
						if (parseInt(container.css('left')) + container.width() <= itemWidth * settings.slideBy) {
							$next.css('display', 'none');
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			return false;
		}
		
		var moveRight = function() {
			if (!animating) {
				animating = true;
				offsetRight = parseInt( container.css('left') ) + ( itemWidth * settings.slideBy );
				if ( offsetRight + container.width() <= container.width() ) {
					$next.css('display', 'block');
					container.animate({
						left: offsetRight
					}, settings.speed, function() {
						if ( parseInt( container.css('left') ) == 0 ) {
							$back.css('display', 'none');
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			return false;
		}
		
		$back.click(moveRight);
		$next.click(moveLeft);
		
		container.mousewheel(function(a, b) {
			if(b > 0) {
				moveRight();
			}else if (b < 0) {
				moveLeft();
			}
			return false;
		});
		
		$next.css('display', 'block');
		
		/*$next.css( "display", "block" )
			.parent().after( [ "<p class=\"view_all\">", settings.headline, " - ", li.length, " total ( <a href=\"#\">view all</a> )</p>" ].join( "" ) );*/
		
		/*jQuery( ".view_all > a, .skip_to_news > a", $this ).click(function() {
			var skip_to_news = ( jQuery( this ).html() == "Skip to News" );
			if ( jQuery( this ).html() == "view all" || skip_to_news ) {
				ul.css( "width", "auto" ).css( "left", "0" );
				$next.css( "display", "none" );
				$back.css( "display", "none" );
				if ( !skip_to_news ) {
					jQuery( this ).html( "view less" );
				}
			} else {
				if ( !skip_to_news ) {
					jQuery( this ).html( "view all" );
				}
				ul.css( "width", ( li.length * liWidth ) );
				$next.css( "display", "block" );
			}
			return false;
		});*/
	}
};