/**
 * @requires jQuery 1.4.1+
 * @requires jQuery Easing Plugin
 *
 */

$(document).ready(function() {

	function setSlideList( wrap, auto ){
		// settings
	
		// 最初の待ち時間
		var firstDelay	= 4000;

		// 動き終わり～動き始め
		var interval	= 6000;

		// 動くのにかける時間
		var duration	= 500;
		var duration2	= 300;

		// 動きの種類
		var easing	= 'easeInOutExpo';

		// 動く幅
		var width = 700;
		
		//対象リスト
		var tgtList;
	
		// for Automation
		var enableAutomation = auto;
		var $wrapArea = $('.inner', wrap);
		var $list = $('.slide', wrap);
	
		var num = $('li', $list).size();
		var cur = 0;

		// 要素が三つ以下なら矢印を表示しない(機能もない)
		if( num <= 1 ) return false;
	
		function resetListStyles() {
			$list.css({
				'width'	: ( width * num * 3 ) + 'px',
				'left'	: ( width * num * -1 ) + 'px'
			});
		}

		resetListStyles();

		var $even = $('li', $list); 
		$even.clone().appendTo($list);
		$even.clone().prependTo($list);
	
		var $_prev = $('<p class="slidelist_arrow slidelist_prev"><a href="#" class="png_bg">前へ</a></p>').appendTo($wrapArea);
		var $_next = $('<p class="slidelist_arrow slidelist_next"><a href="#" class="png_bg">次へ</a></p>').appendTo($wrapArea);
	
		var $prev = $('a', $_prev);
		var $next = $('a', $_next);
	
		$.each([$prev, $next], function(i,v){
			v.click(function(e) { e.preventDefault(); });
		});
		
		$prev.click(function() {
			moveList(-1, false);
		});
		$next.click(function() {
			moveList(+1, false);
		});
		
		
	
		var _timer;
		function setListInterval(int) { 
			clearInterval(_timer);
			_timer = setInterval(function() { moveList(+1, true); }, int);
		};
	
		function moveList(n, rotation) {
			cur += n;
			$list.stop(true, true).animate({left: '-=' + width * n + 'px'}, duration, easing, function() {
				if ( n > 0 ) {
					var $img = $('li:lt(' + n + ')', this);
					$img.clone().appendTo($list);
				} else {
					var $img = $('li:gt(' + ( num + n - 1 ) + ')', this);
					$img.clone().prependTo($list);
				}
				$img.remove();
				resetListStyles();
			});
			
			var tgtList = $('#slide_area li');
			
			//-add fade
			$(tgtList).stop(true, true).animate({
				opacity: 0
			},{
				duration: duration2,
				easing: "swing",
				complete: function(){
					setTimeout(function() {
						$(tgtList).animate({opacity: 1}, duration2);
					}, 400);
				}
			});
			
		
			if( enableAutomation ) setListInterval( interval + duration );
			
		};
	
		if( enableAutomation ) setListInterval(firstDelay);
	}
	setSlideList($('#slide_area'), true);
});




