jQuery.fn.extend({
	inbanner: function () {
		
		var $this = $(this),
			images = $this.find('.img img'),
			total = images.length,
			titleElement = $this.find('.title a'),
			prevElement = $this.find('.controls .prev a'),
			nextElement = $this.find('.controls .next a'),
			currentIdx = 0,
			toggleDelay = 5,
			autorotateInterval; // in seconds
		
		var startAutorotate = function () {
			
			autorotateInterval = setInterval(function () {
				scrollNext();				
			}, toggleDelay * 1e3);
					
		}
		
		var stopAutorotate = function () {
			clearInterval(autorotateInterval);
		}
		
		var scrollNext = function () {
			
			var nextIdx = (currentIdx+1) % total;
			
			$(images[currentIdx]).fadeOut();
			$(images[nextIdx]).fadeIn();
			
			titleElement[0].href = images[nextIdx].parentNode.href;
			titleElement.html(images[nextIdx].title);
			
			currentIdx = nextIdx;
			
		}
		
		var scrollPrev = function () {
			
			var prevIdx = (currentIdx == 0) ? total - 1 : currentIdx - 1;
			
			$(images[currentIdx]).fadeOut();
			$(images[prevIdx]).fadeIn();
			
			titleElement[0].href = images[prevIdx].parentNode.href;
			titleElement.html(images[prevIdx].title);
			
			currentIdx = prevIdx;
			
		}
		
		$this
			.mouseover(function () {
				stopAutorotate()
			})
			.mouseout(function () {
				startAutorotate()
			});
		
		prevElement.click(scrollPrev);
		nextElement.click(scrollNext);
		
		startAutorotate();
		
	}
}) 

