(function($){  
	$.fn.topSlider = function() {
		var changePicture = function(obj, windowDiv, imagesDiv, a) {
			var img = $(a.attr('href'));
			var position = img.position();
			windowDiv.animate({ scrollLeft: img.offset().left - imagesDiv.offset().left }, 300);
			$('li', obj).removeClass('active');
			a.parent().addClass('active');
		};
		
		return this.each(function() {
			var obj = $(this);
			var windowDiv = $('div.window', obj);
			var imagesDiv = $('div.images', obj);
			var images = $('img', imagesDiv);	
			var quantity = $('a', obj).length;
			var height = 0;
			var width = 0;
			
			images.each(function() {
				height = Math.max(height, $(this).outerHeight());
				width += $(this).outerWidth();
			});
			imagesDiv.width(width);
			imagesDiv.height(height);

			images.load(function() {
				height = 0;
				width = 0;
				images.each(function() {
					height = Math.max(height, $(this).outerHeight());
					width += $(this).outerWidth();
				});
				imagesDiv.width(width);
				imagesDiv.height(height);
			});
			
			$(window).load(function() {
				windowDiv.everyTime(13000, 'pictureChanger', function(i) {
					changePicture(obj, windowDiv, imagesDiv, $('ol li:nth-child(' + (i % quantity + 1) + ') a', obj));
				});
			});
			
			$('a', obj).click(function() {
				windowDiv.stopTime();
				changePicture(obj, windowDiv, imagesDiv, $(this));
				windowDiv.oneTime(30000, function() {
					windowDiv.everyTime(13000, 'pictureChanger', function(i) {
						changePicture(obj, windowDiv, imagesDiv, $('ol li:nth-child(' + (i % quantity + 1) + ') a', obj));
					});
				});
				return false;
			});
		});
	}
})(jQuery);
