$.fn.slider = function() {
		return this.each(function() {
			var $rotator = $(this), 
				settings = { speed: 5750, startPosition: 0 },
				$navigation = $('<ul class="navigation"></ul>'),
				$items = $rotator.find('> li');
				siteWidth = $('body').width();

			if ($items.length > 1) {
				$items.each(function(i) {
					$navigation.append('<li><a class="image-index" href="#"><span>' + (i + 1) +'</span></a></li>');
				});

				totalWidth = 0;
				height = 0;
				$items.width(siteWidth);
				$items.each(function() {
						oldheight = $(this).height();
						if (oldheight > height) {
							height = oldheight;
						};
					totalWidth = totalWidth + $(this).width();
				});
				$rotator.width(totalWidth).height(height);

				$rotator.parent().append($navigation);
			}	
			
			$navigation
				.find('a.image-index')
					.hover(
						function() {
							pause();
							show($navigation.find('li a.image-index').index(this));
						},
						function(){
							play();
						}
					)
					.end()
				.find('a')
					.click(function() { return false; });

			show(settings.startPosition);
			play();

			/*
			 * Sliding
			 */
			var intervalId;

			function show(index) {
				$('ul').data('currentIndex', index);
				offset = $items.filter(function(i) { return i === index; }).position().left;				
				$rotator.animate({
					left: -offset
				}, 1000);
				
				$navigation
					.find('a')
						.removeClass('rotator-current')
						.filter(function(i) { return i === index; })
							.addClass('rotator-current');
			}

			function slide() {
				currentIndex = $('ul').data('currentIndex');

				var newIndex = (currentIndex || 0) + 1;
				if (newIndex > ($items.length - 1) || newIndex < 0) {
					newIndex = 0;
				}
				show(newIndex);

			}
			function pause() {
				clearInterval(intervalId);
			}

			function play() {
				intervalId = setInterval(slide, settings.speed);
			}
		});
	};
$(function(){
	$('#main-container #wrapper #body-container #contentBody-container .rotator').slider();
});
