(function($) {

$.fn.newsSlideshow = function(options) {
	var settings = jQuery.extend({
		interval: 4000,
		animationTime: 1000,
		animationType: 'horizontal'
	}, options);
	return this.each(function() {
	
		function startTimer() {
			clearTimeout(timeout);
			timeout = setTimeout(function() {
				move();
			}, settings['interval']);
		}
	
		function move(to) {
			var moveto = (to == null ? current+1 : to);
			switch (settings['animationType']) {
				case 'horizontal':
					$ul.animate({
						marginLeft: -moveto*w
					}, {
						duration: settings['animationTime'],
						queue: false,
						complete: function() { end(moveto); }
					});
				break;
				case 'vertical':
					$ul.animate({
						marginTop: -moveto*h
					}, {
						duration: settings['animationTime'],
						queue: false,
						complete: function() { end(moveto); }
					});
				break;
				case 'fade':
					$elements.eq(current).fadeOut(settings['animationTime'], function() { end(moveto); });
				break;
			}
		}
		
		function end(to) {
			if (to == n) {
				switch (settings['animationType']) {
					case 'horizontal':
						$ul.css('margin-left', '0px');
					break;
					case 'vertical':
						$ul.css('margin-top', '0px');
					break;
					case 'fade':
						$elements.show();
					break;
				}
				to = 0;
			}
			$('a[rel='+current+']', $dots).toggleClass('ui-icon-radio-on').toggleClass('ui-icon-radio-off');
			$('a[rel='+to+']', $dots).toggleClass('ui-icon-radio-off').toggleClass('ui-icon-radio-on');
			current = to;
			if (hovered) return false;
			startTimer();
		}
		
		var timeout;
		var current = 0;
		var hovered = false;
		var $container = $(this);
		var $ul = $('ul', $container);
		var $elements = $('li', $ul);
		var n = $elements.size();
		var w = $elements.width();
		var h = $elements.height();
		$('li:first', $ul).clone().appendTo($ul);
		switch (settings['animationType']) {
			case 'horizontal':
				$elements.css('float', 'left');
				$ul.width((n+1)*w);
			break;
			case 'fade':
				$ul.css('position', 'relative');
				$elements.css({position: 'absolute', top: '0px', left: '0px'}).css('z-index', function(index) {
					return 10*n - index*10;
				});
			break;
		}
		var menu = '<ul><li><a class="ui-icon ui-icon-radio-off" href="javascript:void(0)" rel="0"></a></li>';
		for (var i=1;i<n;i++) {
			menu += '<li><a class="ui-icon ui-icon-radio-on" href="javascript:void(0)" rel="'+i+'"></a></li>';
		}
		menu += '</ul>';
		$dots = $('<div class="slideshowNews_dots">');
		$dots.html(menu).hide().appendTo($container).css('margin-left', '-'+($dots.width()/2)+'px').show();
		startTimer();
		$container.mouseenter(function() {
			hovered = true;
			clearTimeout(timeout);
		}).mouseleave(function() {
			hovered = false;
			startTimer();
		});
		$dots.click(function(e) {
			$target = $(e.target);
			if (!$target.is('a')) { return false; }
			e.preventDefault();
			clearTimeout(timeout);
			$ul.stop(true);
			move(parseFloat($target.attr('rel')));
		});
	});
};
})(jQuery);
