
/*
----------------------------------------------------------------------------------------------------
Accessible News Slider
----------------------------------------------------------------------------------------------------
Author:
Brian Reindel, modified and adapted by Andrea Ferracani

Author URL:
http://blog.reindel.com, http://www.micc.unifi.it/ferracani

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
}, 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 leftpos=0;
ul.css( "width", ( li.length * liWidth ) );
$next.click(function() {
	if ( !animating ) {
		//alert($(this).attr("rel"));
		nextLi=parseInt($(this).attr("rel"));
		
		
		if(li.length > (nextLi + 4)){
			$(this).attr("rel",nextLi + 5);
			//$(".count").html(""+nextLi + " - " +( nextLi + 4)+" of total");
			$(".count").html(""+nextLi + " - " +( nextLi + 4)+"");
			
			$next.attr("rel",nextLi + 5);
			$back.attr("rel", parseInt($next.attr("rel")-5));
			
			
		}else{
			$(this).attr("rel",1);
			$(".count").html(""+nextLi + " - " +li.length+"");
			
			$next.attr("rel",1);
			$back.attr("rel",parseInt(li.length-4));
			
		}
		
		offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
		
		if(offsetLeft == -1275) {
		   offsetLeft=0;
		}
		
		animating = true;
		
		if ( offsetLeft + ul.width() > 0 ) {
			$back.show();
			ul.animate({
			left: offsetLeft
			}, settings.speed, function() {
			if ( parseInt( ul.css( "left" ) ) + ul.width() <= liWidth * settings.slideBy ) {
			}
			animating = false;
			});
		} else {
			animating = false;
		}
	}
return false;
});


$back.click(function() {
if ( !animating ) {

if(parseInt($back.attr("rel")) >1){
	$(".count").html(""+ parseInt($back.attr("rel")-5) + " - " + (parseInt($back.attr("rel")) -1) +"");
	
	$next.attr("rel",parseInt($next.attr("rel")-5));
	$back.attr("rel",parseInt($next.attr("rel")-5));
}
else{
	$(".count").html(""+ '1' + " - " + '5' +"");
	
	$next.attr("rel",6);
	$back.attr("rel",1);

}



animating = true;
offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * settings.slideBy );
if ( offsetRight + ul.width() <= ul.width() ) {
$next.show();
ul.animate({
left: offsetRight
}, settings.speed, function() {
if ( parseInt( ul.css( "left" ) ) == 0 ) {

}
animating = false;
});
} else {
animating = false;
}
}
return false;
});

$next.show();
jQuery(".description").hide();
var firstli = jQuery( "#newsslider ul li:eq(0)");
jQuery('#list').after( [ "<div class=\"view_all\"><span class=\"count\">", settings.headline, " ", li.length, "</span></div>" ].join( "" ) );
jQuery( ".back").appendTo('.view_all');
jQuery( ".next").appendTo('.view_all');
$next.attr("rel",6);
$back.attr("rel",0);
var date = jQuery('<div></div>');
var firstimg = jQuery( "ul li:eq(0) img", $this );
date.appendTo("#container").html("");
img = jQuery('<img></img>')
img.appendTo("#container");
img.attr('src', firstimg.attr('src'));
img.attr('width', 125);
img.attr('height', 140);
img.addClass("imgborder");

para = jQuery('<div></div>');
para.appendTo("#container");

para.html("<h1>" +  jQuery('a.docname', firstli ).attr("title") + "</h1>" + "<p id='paraText'>" + jQuery('p.description', firstli).html() + "</p>");
firstli.addClass('selected');
li.hover(
function () {
li.removeClass('selected')
var current = jQuery(this);
current.addClass('selected');
img.attr('src', current.find('img').attr('src'));
img.addClass("imgborder");
img.attr('width', 125);
img.attr('height', 140);
para.html("<h1>" +  jQuery('.docname', current).attr("title") + "</h1>" + "<p id='paraText'>" + jQuery('.description', current).html() + "</p>");
},
function () {
current.parent().css('backgroundColor', 'transparent');
}
);
}
};

