/*

	----------------------------------------------------------------------------------------------------
	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 : "normal",
		slideBy : 1
    }, 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();
	

	if ( li.length > settings.slideBy ) {
		
		var $next = jQuery( ".next > a", $this );
		var $back = jQuery( ".back > a", $this );
		var $pause = jQuery( ".pause > a", $this );
		var liWidth = jQuery( li[0] ).width();
		var timer = null;
		var stopped = false;
		
		ul.css( "width", ( li.length * liWidth ) );
		
		function increaseOne()
		{
			if ( parseInt( ul.css( "left" ) ) + ul.width() <= liWidth * settings.slideBy ) {ul.css( "left", 0 );}
			offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
			if ( offsetLeft + ul.width() > 0 ) {ul.animate({left: offsetLeft}, settings.speed);} 
			if(!stopped)
			{
				clearTimeout(timer);
				start();
			}
		}
		
		function start() 
		{
			timer=setTimeout(increaseOne,10000);
			stopped=false;
		}
		
		function decreaseOne()
		{
			if ( parseInt( ul.css( "left" ) ) == 0 ) {ul.css( "left", liWidth * settings.slideBy-ul.width());}
			offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * settings.slideBy );
			if ( offsetRight + ul.width() <= ul.width() ) {ul.animate({left: offsetRight}, settings.speed);}
			if(!stopped)
			{
				clearTimeout(timer);
				start();
			}
		}
		function stop() 
		{
			if (!(stopped)) 
			{
				clearTimeout(timer);
				stopped=true;
			}
		}

		function stopstart() 
		{
			if (stopped) {
				start();
			} else {
				stop();
			}
		}		
		function initEvents()
		{
			$next.click( increaseOne);
			$back.click( decreaseOne);
			$pause.click( stopstart);
			start();
		}
		
		var x;
		x=$(document);
		x.ready(initEvents);
		
				
							
		//$next.css( "display", "block" )
		//	.parent().after( [ "<p class=\"view_all\">", settings.headline, " - ", li.length, " total ( <a href=\"#\">view all</a> )</p>" ].join( "" ) );
		$next.css( "display", "block" )
			.parent().after(  [""].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;
		});
	}
};