/*

	----------------------------------------------------------------------------------------------------
	Accessible News Slider - CUSTOMIZED VERSION
	----------------------------------------------------------------------------------------------------
	
	Author:
	Brian Reindel
	
	Author URL:
	http://blog.reindel.com

	License:
	Unrestricted. This script is free for both personal and commercial use.

	Customization for automatic news sliding by Alessandro Perrone - 24th September 2009

*/

jQuery.fn.accessNews = function(settings){
	settings = jQuery.extend({
        headline : "News",
        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;
		var beginAgain = false;
		var automatic = false;
		var initTimer = true;
		
		//console.log(initTimer);

		ul.css("width", (li.length * liWidth));

		function forward(automatic){
			if (!animating){
				// if automatic mode calls the interval again
				if(automatic)
					theInterval();
				else
					theInterval(15);

				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");
							beginAgain = true;
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
		}
		
		$next.click(function(){
			forward(false);
			return false;
		});

		$back.click(function(){
			backward(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");
					ul.animate({
						left: offsetLeft
					}, settings.speed, function(){
						if(parseInt( ul.css( "left" )) + ul.width() <= liWidth * settings.slideBy ){
							$next.css("display", "none");
							beginAgain = true;
						}
						animating = false;
					});
				} else {
					animating = false;
				}
			}
			return false;
		});

		$back.click(function(){
			if(!animating){
				animating = true;
				if(!beginAgain)
					offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * settings.slideBy );
				else
					offsetRight = 0;
				//alert(offsetRight);
				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");
							beginAgain = false;
						}
						animating = false;
					});
				}else{
					animating = false;
				}
			}
			return false;
		});
		*/

		function backward(automatic){
			if(!animating){
				// if automatic mode calls the interval again
				if(automatic)
					theInterval();
				else
					theInterval(15);

				animating = true;
				if(!beginAgain || !automatic)
					offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * settings.slideBy );
				else
					offsetRight = 0;

				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");
							beginAgain = false;
						}
						animating = false;
					});
				}else{
					animating = false;
				}
			}
		}

		//$next.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( "" ) );
			//.parent().after( [ "<p class=\"view_all\"> ( <a href=\"#\">visualizza tutte</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;
		});

		theInt = null;

		theInterval = function(secs){
			
			if(secs !== undefined)
				ms = secs * 1000;
			else
				ms = 5000;

			clearInterval(theInt);

			theInt = setInterval(function(){
				if(!beginAgain){
					forward(true);
				}else{
					backward(true);
				}
			}, ms);
		};

		if(initTimer){
			initTimer = false;
			theInterval();
		}
		
		//theInterval();

	}
};