(function($){
	$.fn.extend({
		topScroller:function(o){
			o = jQuery.extend({
				baseSpan  :  5,
				baseWidth :105,
				setTime   :100,
				setTimeF  : 25
			}, o);
			return this.each(function(){
				var obj       = $(this);
				var tempDirec = 'L';
				var objLi     = obj.find("li");

				objLi.each(function(){
					var tempPos  = $(this).prevAll().length;
					var tempLeft = (tempPos * o.baseWidth);
					$(this).css('left',tempLeft);
				});

				obj.find("ul").wrapInner('<div id="wrapScroll"></div>');
				var objOri = $("#wrapScroll");
				var copyText    = $("#wrapScroll").html();
				objOri.after( '<div id="wrapScrollCopy">' + copyText + '</div>' );
				var liCnt = $("#wrapScroll").find("li").length;
				var objCpy = $("#wrapScrollCopy");

				objOri.css("left", 0).css("position","absolute");
				objCpy.css("left", (0 - ( o.baseWidth * liCnt ))).css("position","absolute");

				var timeCnt  = 0;
				var tempTime = o.setTime;

				var hogeInterval = setInterval(function(){
					tempSlid( o.baseSpan ,tempTime ,o.baseWidth ,tempDirec ,liCnt );
					timeCnt++;
				},tempTime);

				obj.find("li").hover( function () {
					objOri.stop();
					objCpy.stop();
					clearInterval(hogeInterval);
				}, function () {
					objOri.stop();
					objCpy.stop();
					clearInterval(hogeInterval);
					tempTime = o.setTime;
					if( tempDirec === 'R' ){
						hogeInterval = setInterval(function(){
							tempSlid( o.baseSpan ,tempTime ,o.baseWidth ,tempDirec ,liCnt );
							timeCnt--;
						},tempTime);
					} else {
						hogeInterval = setInterval(function(){
							tempSlid( o.baseSpan ,tempTime ,o.baseWidth ,tempDirec ,liCnt );
							timeCnt++;
						},tempTime);
					}
				});

				obj.children('span.s_Lbtn').hover( function () {
					objOri.stop();
					clearInterval(hogeInterval);
					tempTime = o.setTimeF;
					tempDirec = 'L';
					hogeInterval = setInterval(function(){
						tempSlid( o.baseSpan ,tempTime ,o.baseWidth ,tempDirec ,liCnt );
						timeCnt++;
					},tempTime);
				}, function () {
					objOri.stop();
					clearInterval(hogeInterval);
					tempTime = o.setTime;
					tempDirec = 'L';
					hogeInterval = setInterval(function(){
						tempSlid( o.baseSpan ,tempTime ,o.baseWidth ,tempDirec ,liCnt );
						timeCnt++;
					},tempTime);
				});
				obj.children('span.s_Rbtn').hover( function () {
					objOri.stop();
					clearInterval(hogeInterval);
					tempTime = o.setTimeF;
					tempDirec = 'R';
					hogeInterval = setInterval(function(){
						tempSlid( o.baseSpan ,tempTime ,o.baseWidth ,tempDirec ,liCnt );
						timeCnt--;
					},tempTime);
				}, function () {
					objOri.stop();
					clearInterval(hogeInterval);
					tempTime = o.setTime;
					tempDirec = 'R';
					hogeInterval = setInterval(function(){
						tempSlid( o.baseSpan ,tempTime ,o.baseWidth ,tempDirec ,liCnt );
						timeCnt--;
					},tempTime);
				});

			});
		}
	});
})(jQuery);


function tempSlid ( tn1,tn2,tn3,tn4,tn5 ) {
// baseSpan ,setTime ,baseWidth ,tempDirec ,liCnt

	var objOri = $("#wrapScroll");
	var objCpy = $("#wrapScrollCopy");

	var oriPos = objOri.css("left").replace( "px", "" );
	var cpyPos = objCpy.css("left").replace( "px", "" );
	oriPos = Number(oriPos);
	cpyPos = Number(cpyPos);

	var LLimit = 0 - ((tn3 * tn5) * 1.5);
	var RLimit = ( tn3 * tn5 / 2 );

	if(      oriPos > cpyPos & cpyPos < 0 ){ cpyPos = oriPos - (tn3 * tn5); }
	else if( oriPos > cpyPos & oriPos > 0 ){ oriPos = cpyPos + (tn3 * tn5); }
	else if( oriPos < cpyPos & oriPos < 0 ){ oriPos = cpyPos - (tn3 * tn5); }
	else if( oriPos < cpyPos & cpyPos > 0 ){ cpyPos = oriPos + (tn3 * tn5); }

	if( oriPos < LLimit ){ oriPos = cpyPos + (tn3 * tn5); objOri.css("left",oriPos); }
	if( cpyPos < LLimit ){ cpyPos = oriPos + (tn3 * tn5); objCpy.css("left",cpyPos); }
	if( oriPos > RLimit ){ oriPos = cpyPos - (tn3 * tn5); objOri.css("left",oriPos); }
	if( cpyPos > RLimit ){ cpyPos = oriPos - (tn3 * tn5); objCpy.css("left",cpyPos); }

	if( tn4 === 'L' ){
		objOri.stop().animate({ left : (oriPos - tn1) },{ duration : tn2   , easing: "linear" });
		objCpy.stop().animate({ left : (cpyPos - tn1) },{ duration : tn2   , easing: "linear" });
	} else {
		objOri.stop().animate({ left : (oriPos + tn1) },{ duration : tn2   , easing: "linear" });
		objCpy.stop().animate({ left : (cpyPos + tn1) },{ duration : tn2   , easing: "linear" });
	}
}



