(function($, undefined){

	$(function(){
		init();
	});
	
	var 
	SHOW       = 3,
	ITEM_W     = 187,
	PAUSE_TIME = 3000,
	
	$base, $list, $item, $next, $prev,
	itemLength, protect, autoTimer, current;

	function init(){
		$base = $('#banner_list');
		$list = $('div.item_wrapper>ul', $base);
		$item = $list.children();
		$next = $('li.next>a', $base);
		$prev = $('li.prev>a', $base);
		itemLength = $list.children().length;
		current = 0;
		
		checkDisable();
		unlinkBanner();
		setEvent();
	}
	
	function moveBanner(next){
		if(protect){
			return;
		}
		protect = true;
		var goal  = (next+SHOW <= itemLength) ? next : 0,
		    goalX = goal * ITEM_W * -1,
		    diff  = Math.abs(current - goal),
				speed = (diff === 1) ? 450 : 180 * diff;
		$list.animate({left:goalX}, speed, 'easeOutCirc', function(){
			current = goal;
			protect = false;
			checkDisable();
		});		
	}	
	
	function checkDisable(){
		if(current === 0) {
			$prev.addClass('disable');
		}
		else {
			$prev.removeClass('disable');
		}
		if(current+SHOW >= itemLength) {
			$next.addClass('disable');
		}
		else {
			$next.removeClass('disable');
		}
	}
	
	function unlinkBanner(){
		$item
			.children('a[href=#]')
			.children()
				.unwrap();
	}
	
	function setEvent(){
		$next.click(function(){
			if(!$(this).is('.disable')){
				moveBanner(current+1);
			}
			return false;
		});
		$prev.click(function(){
			if(!$(this).is('.disable')){
				moveBanner(current-1);
			}
			return false;
		});
	}

}(jQuery));
