/*

	----------------------------------------------------------------------------------------------------
	Accessible News Slider
	----------------------------------------------------------------------------------------------------
	
	Author:
	Brian Reindel
	
	Author URL:
	http://blog.reindel.com

	License:
	Unrestricted. This script is free for both personal and commercial use.

*/
var slide_current = 1;
var slide_last = 1;
var slide_timeout = false;

jQuery.fn.accessNews = function( settings ) {
	settings = jQuery.extend({
        headline : "Top Stories",
        speed : "normal",
		slideBy : 2
    }, 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 liWidth = jQuery( li[0] ).width();
		var animating = false;
		ul.css( "width", ( li.length * liWidth ) );
		$next.click(function() {
			if ( !animating ) {
				stopAutoSlide();
				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;
						updateSlideCurrent();
						startAutoSlide();
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
		$back.click(function() {
			if ( !animating ) {
				stopAutoSlide();
				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;
						updateSlideCurrent();
						startAutoSlide();
					});
				} else {
					animating = false;
				}
			}
			return false;
		});
		$next.css( "display", "block" ).parent().after( [ "" ].join( "" ) );
	}
};

function updateSlideCurrent() {
	var ul = jQuery( "ul:eq(0)", "#example_1" );
	var li = ul.children();
	var liWidth = jQuery( li[0] ).width();
	var r_width = parseInt(ul.width()) + parseInt(ul.css( "left" ));
	var l_width = parseInt(ul.width()) - r_width;

	slide_last = slide_current;
	slide_current = l_width / liWidth;
	slide_current = slide_current + 1;
	changeSlideBigImage();
}

function changeSlideBigImage() {
	jQuery("#featured-big"+slide_last).hide();
	jQuery("#featured-big"+slide_current).fadeIn("slow");
}

function showSlideBig(oid) {
	slide_last = slide_current;
	slide_current = oid;
	changeSlideBigImage();
}

function autoSlide() {
	var ul = jQuery( "ul:eq(0)", "#example_1" );
	var $next = jQuery( ".next > a", "#example_1" );
	var $back = jQuery( ".back > a", "#example_1" );

	var li = ul.children();
	var liWidth = jQuery( li[0] ).width();
	var animating = false;
	var slideBy = 1;
	var slideSpeed = "normal";

	ul.css( "width", ( li.length * liWidth ) );

	if ( !animating ) {
		animating = true;
		offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * slideBy );
		if ( (offsetLeft + ul.width()) <= 0 ) { offsetLeft = 0; }

		$back.css( "display", "block" );
		ul.animate({
			left: offsetLeft
		}, slideSpeed, function() {
			if ( parseInt( ul.css( "left" ) ) + ul.width() <= liWidth * slideBy ) {
				$next.css( "display", "none" );
			}
			animating = false;
			updateSlideCurrent();
		});
	}
	$next.css( "display", "block" ).parent().after( [ "" ].join( "" ) );

	slide_timeout = setTimeout("autoSlide()", slide_speed);
}

function startAutoSlide() {
  slide_timeout = setTimeout("autoSlide()", slide_speed);
}

function stopAutoSlide() {
  clearTimeout(slide_timeout);
}

