(function($) {
	$.fn.jqListScroll = function(options) {
		var element = this;

		var conf = $.extend({
			//初期表示タブ
			scrollLength:		"1",
			relativeBox: 		".relativeBox",
			handler:			".handler",
			nextBt:				".next a",
			prevBt:				".prev a",
			dotsId:				"dots",
			dotsActive:			"active",
			ScrollSpeed:		700,
			interval:			"4000",
			toStartClass:		"toStart",
			toEndClass:			"toEnd"
		}, options);
		
		
		var jqListScrollTimer;
		// リストの個数を取得
		var listSize = $(conf.relativeBox + " " + "li",element).size();
		// 動作条件:エレメントが2以上
		if(listSize < 2){
			$(conf.handler,element).remove();
			return this
		}else{
			$(element).append('<ul id="' + conf.dotsId + '">');
			
			$(conf.relativeBox + " " + "li",element).each(function(){
				$("#" + conf.dotsId ,element).append('<li>&nbsp;</li>');
			});
			
			$("#"  + conf.dotsId + " li:first",element).addClass(conf.dotsActive).addClass("first");
			$("#"  + conf.dotsId + " li:last",element).addClass("last");
		}

		// Quart Easing
		jQuery.easing.quart = function (x, t, b, c, d) {
			return -c * ((t=t/d-1)*t*t*t - 1) + b;
		};

		// Setting
		$(conf.relativeBox + " " + "li",element).clone().appendTo(conf.relativeBox + " " + "ul",element);
		var scrollPx = $(conf.relativeBox + " " + "li",element).innerWidth() * conf.scrollLength;
		var scrollPoint = scrollPx * listSize;
		$(conf.relativeBox,element).stop().animate({scrollLeft: scrollPoint},0);
		
		// Next Bt Click
		$(conf.nextBt,element).click(function(){
			nextEvent();
			setTimer();
			return false;
		});
		
		// Prev Bt Click
		$(conf.prevBt,element).click(function(){
			prevEvent();
			setTimer();
			return false;
		});
		
		$(conf.relativeBox + " " + "li",element).hover(function(){
			clearInterval(jqListScrollTimer);
		},function(){
			setTimer();
		});

		// Set Timer
		var setTimer = function () {
			jqListScrollTimer = setInterval(function(){
				nextEvent();
				clearInterval(jqListScrollTimer);
				setTimer();
			},conf.interval);
		}
		
		// Next Event
		var nextEvent = function () {
			$(conf.relativeBox + " " + "li:first",element).appendTo(conf.relativeBox + " " + "ul",element);
			scrollPoint -= scrollPx;
			$(conf.relativeBox,element).stop().animate({scrollLeft: scrollPoint},0);
			
			if($("#" + conf.dotsId + " .active",element).hasClass("last")){
				$("#" + conf.dotsId + " .active",element).removeClass(conf.dotsActive);
				$("#"  + conf.dotsId + " li:first",element).addClass(conf.dotsActive);
			}else{
				$("#" + conf.dotsId + " .active",element).removeClass(conf.dotsActive).next().addClass(conf.dotsActive);
			}

			scrollPoint += scrollPx;
			scrollMove();

			clearInterval(jqListScrollTimer);
		}

		// Prev Event
		var prevEvent = function () {
			$(conf.relativeBox + " " + "li:last",element).prependTo(conf.relativeBox + " " + "ul",element);
			scrollPoint += scrollPx;
			$(conf.relativeBox,element).stop().animate({scrollLeft: scrollPoint},0);
			
			if($("#" + conf.dotsId + " .active",element).hasClass("first")){
				$("#" + conf.dotsId + " .active",element).removeClass(conf.dotsActive);
				$("#"  + conf.dotsId + " li:last",element).addClass(conf.dotsActive);
			}else{
				$("#" + conf.dotsId + " .active",element).removeClass(conf.dotsActive).prev().addClass(conf.dotsActive);
			}

			scrollPoint -= scrollPx;
			scrollMove();
			
			clearInterval(jqListScrollTimer);
		}

		// Scroll Action
		var scrollMove = function(){
			$(conf.relativeBox,element).stop().animate({scrollLeft: scrollPoint},{
				duration: conf.ScrollSpeed,
				easing: "quart"
			});
		};
		
		// Load Timer Set
		setTimer();

		return this;
    };
})(jQuery);



$(function(){
	$(".jqListScroll").jqListScroll();
});

