/*  Accessible News Slider
    --------------------------------------------------------------
    Author:	Brian Reindel ( http://blog.reindel.com/ )
	Modified by: Wex ( http://www.wexcode.com/ )
*/

jQuery.fn.accessNews = function( settings ) {
	settings = jQuery.extend({
        speed : "normal",
		slideBy : 1
    }, settings);
    return this.each(function() {
		jQuery.fn.accessNews.run( jQuery( this ), settings );
    });
};
jQuery.fn.accessNews.run = function( $this, settings ) {
	var ul = jQuery( "ul:eq(0)", $this );
	var li = ul.children();
	if ( li.length > settings.slideBy ) {
		var $next = jQuery( "#arrow-right > a", $this );
		var $back = jQuery( "#arrow-left > a", $this );
		var liWidth = 555;
		var animating = false;
		var currentPos = 0;
		ul.css( "width", ( li.length * liWidth ) );
		$next.click(function() {
			if ( !animating ) {
				animating = true;
				offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
				if ( offsetLeft + ul.width() > 0 ) {
					$back.css( "display", "block" );
					ul.animate({
						left: offsetLeft
					}, settings.speed, function() {
						if ( parseInt( ul.css( "left" ) ) + ul.width() <= liWidth * settings.slideBy ) {
							$next.css( "display", "none" );
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			currentPos = currentPos + 1;
			$(".view-all").attr("id", "page-" + currentPos);
			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" );
					ul.animate({
						left: offsetRight
					}, settings.speed, function() {
						if ( parseInt( ul.css( "left" ) ) == 0 ) {
							$back.css( "display", "none" );
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			currentPos--;
			$(".view-all").attr("id", "page-" + currentPos);
			return false;
		});
	}
};