jQuery.fn.extend({
	mbanner: function () {
		
		var $this = $(this),
			title = $this.find('.title p'),
			titleLink = title.find('a'),
			images = $this.find('.img img'),
			total = images.length,
			currentIdx = 0,
			toggleDelay = 5,
			autorotateInterval; // in seconds
		
		var startAutorotate = function () {
			
			autorotateInterval = setInterval(function () {

				var nextIdx = (currentIdx+1) % total;

				$(images[currentIdx]).fadeOut();
				$(images[nextIdx]).fadeIn();

				title.animate({
					marginLeft: "-" + parseInt(title[0].offsetWidth) + 'px'
				}, "fast", "swing", function () {
					
					titleLink
						.html(images[nextIdx].title)
						.attr('href', images[nextIdx].parentNode.href);
					
					title.animate({
						marginLeft: 0
					}, "fast", "swing", function () {
						currentIdx = nextIdx;
					});

				});

			}, toggleDelay * 1e3);
					
		}
		
		var stopAutorotate = function () {
			clearInterval(autorotateInterval);
		}
		
		$this
			.mouseover(function () {
				stopAutorotate()
			})
			.mouseout(function () {
				startAutorotate()
			});
		
		startAutorotate();
		
	}
}) 

