/*

	----------------------------------------------------------------------------------------------------
	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 : 3,
		NbSlides: 4
    }, 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 ActualSlide=1;
	if ( li.length > settings.slideBy ) {
		var $next = jQuery( ".next > a", $this );
		var $back = jQuery( ".back > a", $this );
		var $compteur1=jQuery( ".compteur1", $this );
		var $compteur2=jQuery( ".compteur2", $this );
		var $compteur3=jQuery( ".compteur3", $this );
		var $compteur4=jQuery( ".compteur4", $this );
		//var liWidth = jQuery( li[0] ).width();
		var liWidth = 167;
		var animating = false;
		ul.css( "width", ( li.length * liWidth ) );
		$compteur1.click(function() {
		
			if ( !animating ) {
				animating = true;
				offsetLeft = 0;
				if ( offsetLeft + ul.width() > 0 ) {
					ActualSlide=1;
					$back.css( "display", "block" );
					$compteur3.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					$compteur1.css("background","url(images/home-v3/compteur-hover.png) no-repeat center top");
					$compteur2.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					$compteur4.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					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;
		});
		$compteur2.click(function() {
		
			if ( !animating ) {
				animating = true;
				offsetLeft =  -( liWidth * settings.slideBy );
				if ( offsetLeft + ul.width() > 0 ) {
					ActualSlide=2;
					$back.css( "display", "block" );
					$compteur3.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					$compteur1.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					$compteur2.css("background","url(images/home-v3/compteur-hover.png) no-repeat center top");
					$compteur4.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					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;
		});
		$compteur3.click(function() {
		
			if ( !animating ) {
				animating = true;
				offsetLeft =  -( liWidth * settings.slideBy )*2;
				if ( offsetLeft + ul.width() > 0 ) {
					ActualSlide=3;
					$back.css( "display", "block" );
					$compteur3.css("background","url(images/home-v3/compteur-hover.png) no-repeat center top");
					$compteur1.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					$compteur2.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					$compteur4.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					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;
		});
			$compteur4.click(function() {
		
			if ( !animating ) {
				animating = true;
				offsetLeft =  -( liWidth * settings.slideBy )*3;
				if ( offsetLeft + ul.width() > 0 ) {
					ActualSlide=4;
					$back.css( "display", "block" );
					$compteur3.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					$compteur1.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					$compteur2.css("background","url(images/home-v3/compteur.png) no-repeat center top");
					$compteur4.css("background","url(images/home-v3/compteur-hover.png) no-repeat center top");
					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;
		});
		$next.click(function() {
			if ( !animating ) {
				animating = true;
				offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
				if ( offsetLeft + ul.width() > 0 ) {
					$back.css( "display", "block" );
					var goToSlide=ActualSlide+1;
					for(i=1;i<=settings.NbSlides;i++){
						if(i==goToSlide){
						$toto=eval("$compteur"+i);
						$toto.css("background","url(images/home-v3/compteur-hover.png) no-repeat center top");
						}
						else{
							$toto=eval("$compteur"+i);
							$toto.css("background","url(images/home-v3/compteur.png) no-repeat center top");
						}
					}
					ActualSlide++;
					
					ul.animate({
						left: offsetLeft
					}, settings.speed, function() {
						if ( parseInt( ul.css( "left" ) ) + ul.width() <= liWidth * settings.slideBy ) {
							//$next.css( "display", "none" );
						}
						animating = false;
					});
				} else {
					offsetLeft = 0;
					animating = false;
					goToSlide=1;
					for(i=1;i<=settings.NbSlides;i++){
						if(i==goToSlide){
						$toto=eval("$compteur"+i);
					$toto.css("background","url(images/home-v3/compteur-hover.png) no-repeat center top");
						}
						else{
							$toto=eval("$compteur"+i);
							$toto.css("background","url(images/home-v3/compteur.png) no-repeat center top");
						}
					}
					ActualSlide=1;
					ul.animate({
						left: offsetLeft
					}, settings.speed, function() {
						
						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" );
					var goToSlide=ActualSlide-1;
					for(i=1;i<=settings.NbSlides;i++){
						if(i==goToSlide){
						$toto=eval("$compteur"+i);
						$toto.css("background","url(images/home-v3/compteur-hover.png) no-repeat center top");
						}
						else{
							$toto=eval("$compteur"+i);
							$toto.css("background","url(images/home-v3/compteur.png) no-repeat center top");
						}
					}
					ActualSlide--;
					
					ul.animate({
						left: offsetRight
					}, settings.speed, function() {
						
						animating = false;
					});
				} else {
				
					animating = false;
					offsetLeft =  -( liWidth * settings.slideBy )*(parseInt(settings.NbSlides-1));
					
					goToSlide=settings.NbSlides;
					for(i=1;i<=settings.NbSlides;i++){
						if(i==goToSlide){
						$toto=eval("$compteur"+i);
					$toto.css("background","url(images/home-v3/compteur-hover.png) no-repeat center top");
						}
						else{
							$toto=eval("$compteur"+i);
							$toto.css("background","url(images/home-v3/compteur.png) no-repeat center top");
						}
					}
					ActualSlide=settings.NbSlides;
					
					ul.animate({
						left: offsetLeft
					}, settings.speed, function() {
						
						animating = false;
					});
				}
			}
			return false;
		});
		$compteur1.css("background","url(images/home-v3/compteur-hover.png) no-repeat center top")
		$back.css( "display", "block" )
		$next.css( "display", "block" )
			.parent().after( [ "<p class=\"view_all\">", settings.headline, " - ", li.length, " total ( <a href=\"#\">view all</a> )</p>" ].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;
		});
	}
};