(function(){
	$.fn.centerImage = function(){
		return $(this).each(function(){
			if($(this).find('div img').height() > $(this).find('div').height()){
				var top = -1*(parseInt($(this).find('div img').height())/2);
				
				$(this).find('div').css('top','50%');
				$(this).find('div img').css('top',top);
			}
			
			if($(this).find('div img').width() > $(this).find('div').width()){
				var left = -1*(parseInt($(this).find('div img').width())/2);
				
				$(this).find('div').css('left','50%');
				$(this).find('div img').css('left',left);
			}
		});
	}
	
	$.fn.carousel = function(){
		return $(this).each(function(){
			var carousel = $(this);
			
			
			carousel.find('li').centerImage();
			carousel.find('li:not(:last) img').hide();
			
			var switchImages = function(){
				var pres = carousel.find('li:last');
				var next = pres.prev();
					next.find('img').show();
				
				pres.delay(3000,function(){
					pres.find('img').fadeOut(1000,function(){
						pres.prependTo(carousel);
						switchImages();
					});
				});
			}
			
			switchImages();
		});
	};
	
	$(document).ready(function(){
		$('ul.carousel-image').carousel();
	});
})(jQuery);
