
/*
----------------------------------------------------------------------------------------------------
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 ) {
	var ul = jQuery( "ul:eq(0)", $this );
	var li = ul.children();
	/*
	if ( li.length < settings.slideBy ) {
		settings.slideBy = li.length-1;
	}
	*/
	li.click(function(){
		location.href=jQuery('a',$this).attr('href');
	});
	jQuery('.container',$this).click(function(){
		location.href=jQuery('a',$this).attr('href');
	});
	
	jQuery('h2',$this).each(function(index){
		jQuery(this).attr('title',jQuery(this).text());
	});
	
	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 ) {
			animating = true;
			offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
			if ( offsetLeft + ul.width() <= 0 ) {
				offsetLeft = 0;
			}
			$back.show();
			ul.animate({
			left: offsetLeft
			}, settings.speed, function() {
				jQuery('li:eq('+Math.abs(offsetLeft)/liWidth+')',ul).trigger('mouseover');
				animating = false;
			});
			var start = Math.abs(offsetLeft/liWidth)+1;
			jQuery('.count',jQuery($this)).html(start+'-'+(start+settings.slideBy-1)+ ' of '+li.length);
		}
		return false;
	});
	$back.click(function() {
		if ( !animating ) {
			animating = true;
			offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * settings.slideBy );
			if ( offsetRight + ul.width() > ul.width() ) {
				offsetRight = -(liWidth * (Math.floor(jQuery('li',ul).length / settings.slideBy) * settings.slideBy));
			}
			$next.show();
			ul.animate({
			left: offsetRight
			}, settings.speed, function() {
				var nselect = Math.min((Math.abs(offsetRight)/liWidth) + settings.slideBy - 1,jQuery('li',ul).length-1);
				jQuery('li:eq('+nselect+')',ul).trigger('mouseover');
				animating = false;
			});
			var start = Math.abs(offsetRight/liWidth)+1;
			jQuery('.count',jQuery($this)).html(start+'-'+(start+settings.slideBy-1)+ ' of '+li.length);
		}
		return false;
	});
	
	$next.show();
	jQuery(".description").hide();
	jQuery($this).append('<div class="view_all"><span class="count">1-'+settings.slideBy + ' of '+ li.length+ '</span></div>');
	jQuery( ".back",$this).appendTo('.view_all');
	jQuery( ".next",$this).appendTo('.view_all');
	var date = jQuery('<div></div>');
	var firstimg = jQuery( "ul li:eq(0) img", $this );
	img = jQuery('<img></img>')
	img.appendTo(".container",$this);
	img.attr('src', firstimg.attr('rel'));
	para = jQuery('<div></div>');
	para.appendTo(".container",$this);
	var firstli = jQuery( " ul li:eq(0)",$this);
	para.html("<h2>" +  jQuery('h2', firstli ).attr('title') + "</h2>" + "<p class='paraText'>" + jQuery('p.description', firstli).html() + "</p>"+jQuery('a',firstli).clone().wrap('<div>').parent().html());
	firstli.addClass('selected');
	//jQuery( '.container .paraText' ).trimLines( { 'maxLines': 3});
	li.hover(function () {
		li.removeClass('selected')
		var current = jQuery(this);
		current.addClass('selected');
		img.attr('src', current.find('img').attr('rel'));
		para.html("<h2>" +  jQuery('h2', current).attr('title') + "</h2>" + "<p class='paraText'>" + jQuery('p.description', current).html() + "</p>"+jQuery('a',current).clone().wrap('<div>').parent().html());
		//jQuery( '.container .paraText' ).trimLines( { 'maxLines': 3});
	});
	
	var timeout = 0;
	switchNext = function testing(){
		idx = jQuery('li',ul).index(jQuery('.selected'));
		next = idx + 1;
		if(next >= li.length){
			next = 0;
			if ( !animating ) {
				animating = true;
				ul.animate({
				left: 0
				}, settings.speed, function() {
					animating = false;
				});
			}
		}else if(next % settings.slideBy == 0){
			$next.trigger('click');
		}
		jQuery('li:eq('+next+')',ul).trigger('mouseover');
		jQuery('li:eq('+next+')',ul).trigger('mouseout');
		timeout = setTimeout(switchNext,8000);
	}
	timeout = setTimeout(switchNext,8000);
	
	$this.mouseover(function(){
		clearTimeout(timeout);
	});
	$this.mouseout(function(){
		timeout = setTimeout(switchNext,8000);
	});
	
};

