/*

	----------------------------------------------------------------------------------------------------
	Accessible News Slider
	----------------------------------------------------------------------------------------------------
	
	Author:
	Brian Reindel
	
	Author URL:
	http://blog.reindel.com

	License:
	Unrestricted. This script is free for both personal and commercial use.

*/

jQuery.fn.accessNews = function( settings ) {
	settings = jQuery.extend({
        headline : "Top Stories",
        speed : "slow",
		    slideBy : 4,
		    backBtn: "#back",
		    nextBtn: "#next"
    }, settings);
    return this.each(function() {
		jQuery.fn.accessNews.run( jQuery( this ), settings );
    });
};
jQuery.fn.accessNews.run = function( $this, settings ) {
	jQuery( ".javascript_css", $this ).css( "display", "none" );
	var ul = jQuery( "ul:eq(0)", $this );
	var li = ul.children();

	var $next = jQuery( settings.nextBtn, $this );
	var $back = jQuery( settings.backBtn, $this );
	var liWidth = jQuery( li[0] ).width();
	var animating = false;
	ul.css( "width", ( li.length * liWidth ) );

	if ( li.length > settings.slideBy ) {
		$next.click(function() {
			if ( !animating ) {
				animating = true;
				offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
				if ( offsetLeft + ul.width() > 0 ) {
					//$back.css( "display", "block" );
					$back.removeClass( "disabled" );
					ul.animate({
						left: offsetLeft
					}, settings.speed, function() {
						if ( parseInt( ul.css( "left" ) ) + ul.width() <= liWidth * settings.slideBy ) {
							//$next.css( "display", "none" );
							$next.addClass( "disabled" );
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
		$back.click(function() {
			if ( !animating ) {
				animating = true;
				offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * settings.slideBy );
				if ( offsetRight + ul.width() <= ul.width() ) {
					//$next.css( "display", "block" );
					$next.removeClass( "disabled" );
					ul.animate({
						left: offsetRight
					}, settings.speed, function() {
						if ( parseInt( ul.css( "left" ) ) == 0 ) {
							//$back.css( "display", "none" );
							$back.addClass( "disabled" );
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
		//$next.css( "display", "block" )
		//$back.css( "display", "none" )
		$next.removeClass( "disabled" );
		$back.addClass( "disabled" );
	}
	else
	{
		//$next.css( "display", "none" )
		//$back.css( "display", "none" )
		$back.addClass( "disabled" );
		$back.addClass( "disabled" );
	}
};
