/*
----------------------------------------------------------------------------------------------------
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 : "Aktuelle Themen",
		speed : "slow",
		slideShowInterval: 5000,
			// delay before slide show begins
			slideShowDelay: 5000,
		slideBy : 3
	}, 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();
	var current = 1;
	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 ) {
				animating = true;
				offsetLeft = parseInt( ul.css( "left" ) ) - ( liWidth * settings.slideBy );
				if ( offsetLeft + ul.width() > 0 ) {
					current++;
					jQuery('.count').html( settings.headline + " " + (((current * settings.slideBy) - settings.slideBy) + 1) + " - "+ ((current * settings.slideBy) > li.length ? li.length:(current * settings.slideBy) ) + " von " + li.length);
					$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 ) {
				animating = true;
				offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * settings.slideBy );
				if ( offsetRight + ul.width() <= ul.width() ) {
					current--;
					if(current !== 1) {
						jQuery('.count').html( settings.headline + " " + ((current * settings.slideBy) - settings.slideBy) + " - " + current * settings.slideBy + " von " + li.length);
					} else {
						jQuery('.count').html( settings.headline + " " + (((current * settings.slideBy) - settings.slideBy) + 1) + " - "+ current * settings.slideBy + " von "+ li.length);
					}		
					$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();
		jQuery('#list').after( [ "<div class=\"view_all\"><span class=\"count\">", " </span></div>" ].join( "" ) );
		if(current !== 1) {
			jQuery('.count').html( settings.headline + " " + (((current * settings.slideBy) - settings.slideBy) + 1) + " - " + current * settings.slideBy + " von " + li.length);
		} else {
			jQuery('.count').html( settings.headline + " " + (((current * settings.slideBy) - settings.slideBy) + 1) + " - "+ current * settings.slideBy + " von "+ li.length);
		}
		jQuery( ".back").appendTo('.view_all');
		jQuery( ".next").appendTo('.view_all');
		var date = jQuery('<div></div>');
		var firstimg = jQuery( "ul li:eq(0) img", $this );
		// date.appendTo("#newsimg").html("<h2 class='textpink'>Aktuelle Themen</strong></h2>");
		img = jQuery('<img></img>')
		img.appendTo("#newsimg");
		img.attr('src', firstimg.attr('src'));
		img.attr('width', '312');
		img.attr('height', '99');
		//var firstlink = firstimg.find('a.title').attr('href');
		//img.wrap('<a href="' + firstlink + '"></a>');
		para = jQuery('<div></div>');
		para.appendTo("#newsimg");
		var firstli = jQuery( "#newsslider ul li:eq(0)");
		para.html("<h1 class='headline'>" +  jQuery('a.title', firstli ).text() + "</h1>" + "<p id='paraText'>" + jQuery('.description', firstli).html() + "</p>");
		firstli.addClass('selected');
		
		jQuery('#newsslider ul li:odd').addClass('odd');  
    	jQuery('#newsslider ul li:even').addClass('even');  

		li.hover(
			function () {
				li.removeClass('selected')
				var current = jQuery(this);
				current.addClass('selected');
				img.attr('src', current.find('img').attr('src'));
				//var link = current.find('a').attr('href');
				//img.wrap('<a href="' + link + '"></a>');
				//para.html("<h1 class='headline'><a href='" + link + "'>" +  jQuery('.title', current).text() + "</a></h1>" + "<p id='paraText'>" + jQuery('.description', current).html() + "</p>");
				para.html("<h1 class='headline'>" +  jQuery('.title', current).text() + "</h1>" + "<p id='paraText'>" + jQuery('.description', current).html() + "</p>");
			},
			function () {
				var current = jQuery(this);
				current.parent().css('backgroundColor', 'transparent');
			}
		);
	}
};
