/*

	----------------------------------------------------------------------------------------------------
	Content-Slider based on Accessible News Slider
	----------------------------------------------------------------------------------------------------
	
	Author of Accessible News Slider:
	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 : "normal",
		slideBy : 2,
		view_all : "view all",
		view_less : "view less",
		show_headline : true,
		show_view : true,
		show_totals : true
    }, 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( ".next > a", $this );
	if (settings.show_headline == true) {
		var headlinestr = "<p class=\"view_all\"><span>"+settings.headline+"</span>";
		if ( settings.show_totals == true ) {
			headlinestr = headlinestr+" - "+li.length+" total";
		}
		if ( settings.show_view == true && li.length > settings.slideBy ) {
			headlinestr = headlinestr+" ( <a href=\"#\">"+settings.view_all+"</a> )";
		}
		headlinestr = headlinestr+"</p>";
			
		$next.parent().after( [ headlinestr ].join( "" ) );
	}
	if ( li.length > settings.slideBy ) {
		var $back = jQuery( ".back > a", $this );
		var liWidth = parseInt(jQuery( li[0] ).css("width"));
		var animating = false;
		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;
				}
			}
			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;
				}
			}
			return false;
		});
		
		$next.css( "display", "block" );

		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() == settings.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( settings.view_less );
				}
			} else {
				if ( !skip_to_news ) {
					jQuery( this ).html( settings.view_all );
				}
				
				ul.css( "width", ( li.length * liWidth ) );
				$next.css( "display", "block" );
			}
			return false;
		});
	}
};
