// ³Ñ¾î¿Â µ¥ÀÌÅÍ¸¦ ·Ñ¸µ ¾Ö´Ï¸ÞÀÌ¼Ç ÇÏ´Â Å¬·¡½º
var Rolling = $Class({
	
	_area : null,
	_options : null,
	_reserved_options : null,
	
	_waiter : null,
	_mover : null,
	
	_percent : 0,
	_fromTo : null,
	_paused : true,

	$init : function() {
		
	},
	
	touch : function(oArea, oOptions) {
		
		this._area = $(oArea); // ½ºÅ©·ÑÇØ¾ß ÇÏ´Â ¿µ¿ª ÀúÀå

		// ¿É¼Ç ÀúÀå
		this._options = {};
		/* JINDO extend */(function(s,a){for(var x in a){s[x]=a[x];}return s;})(this._options, oOptions || {});
		
		this.scrollTo(0);
		
		if (!this._options.suspend)
			this.resume();
	},
	
	scrollTo : function(nPx) {
		
		nPx = nPx % this._options.size;
		if (nPx < 0) nPx = this._options.size + nPx;
		
		this._setScrollPos(nPx);
		
	},
	
	scrollBy : function(nPx) {

		this.scrollTo(this._getScrollPos() + nPx);
		
	},
	
	_setScrollPos : function(nPx) {
		
		this._area[this._options.direction == Rolling.VERTICAL ? "scrollTop" : "scrollLeft" ] = nPx;
	},
	
	_getScrollPos : function() {
		
		return this._area[this._options.direction == Rolling.VERTICAL ? "scrollTop" : "scrollLeft" ];
	},
	
	_applyReservedOptions : function() {
		
		if (this._reserved_options) { // ¿¹¾àµÈ ¿É¼ÇÀÌ ÀÖÀ¸¸é Àû¿ë
			/* JINDO extend */(function(s,a){for(var x in a){s[x]=a[x];}return s;})(this._options, this._reserved_options || {});
			this._reserved_options = null;
		}
	},
	
	_startWaiting : function() {
		
		this._applyReservedOptions();
		
		this._stopWaiting();
		this._stopMoving();

		if (this._paused) return;
		
		this._waiter = window.setTimeout($Fn(this._startMoving, this).bind(), this._options.freeze);
	},
	
	_stopWaiting : function() {
		if (this._waiter) window.clearTimeout(this._waiter);
		this._waiter = null;	
	},
	
	_startMoving : function() {
		
		this._applyReservedOptions();
		
		this._percent = 0;
		
		this._fromTo = [];
		
		this._fromTo[0] = this._getScrollPos();
		this._fromTo[1] = this._options.moveSize;
		
		this._stopWaiting();
		this._stopMoving();
		this._mover = window.setInterval($Fn(this._doMoving, this).bind(), this._options.moveInterval);
	},
	
	_stopMoving : function() {
		if (this._mover) window.clearInterval(this._mover);
		this._mover = null;	
	},
	
	_doMoving : function() {
		
		// ¾ÆÁ÷ ¿òÁ÷ÀÏ È½¼ö°¡ ³²¾Ò´ÂÁö·Î '±â´Ù¸®±â' ¸¦ ½ÃÀÛÇÒ°ÇÁö ÆÇ´Ü
		if (this._percent >= 1) {
			this._startWaiting();
			return;
		}
		
		this._percent += this._options.moveStep;
		this._percent = Math.round(this._percent * 10000) / 10000;

		if (this._percent >= 1) this._percent = 1;
		this.scrollTo(this._fromTo[0] + this._fromTo[1] * this._options.effect(this._percent));
		
	},

	resume : function() {
		this._paused = false;
		if (!this._mover) this._startWaiting();
	},
	
	pause : function() {
		this._stopWaiting();
		this._paused = true;
	},
	
	getIndex : function() {
		var nPos = this._getScrollPos();
		return Math.round(nPos / this._options.moveSize);
	},
	
	movePrev : function() {
		
		var nIndex = this.getIndex();
		var nPos = (nIndex - 1) * this._options.moveSize;
		
		this.scrollTo(nPos);
		this._startWaiting();
	},

	moveNext : function() {

		var nIndex = this.getIndex();
		var nPos = (nIndex + 1) * this._options.moveSize;
		
		this.scrollTo(nPos);
		this._startWaiting();
	},
	
	setOptions : function(oOptions) {
		
		this._reserved_options = {}
		this._reserved_options = /* JINDO extend */(function(s,a){for(var x in a){s[x]=a[x];}return s;})(this._reserved_options, oOptions || {});
		
		if (this._mover == null) {
			this._startMoving();
		}
		
		/*		
		effect : Effect.slower, // Á¡Á¡ ´À¸®°Ô ¿òÁ÷ÀÌ´Â È¿°ú »ç¿ë
		move : { // ¿òÁ÷ÀÏ¶§´Â 100ÇÈ¼¿¾¿ ¿òÁ÷ÀÌ°í 10msec ¸¶´Ù 1% ¾¿ ÁøÇàÇØ¶ó
			size : 100,
			step : 0.01,
			interval : 10
		},
		freeze : 1500, // ¸ØÃçÀÖÀ»¶§´Â 1.5ÃÊµ¿¾È ¸ØÃçÀÖ¾î¶ó
		*/
		
	}

});

Rolling.VERTICAL = 1;
Rolling.HORIZONTAL = 2;

var Effect = { //°¢ ÇÁ·ÎÆÛÆ¼¿¡ µû¶ó¼­ ·Ñ¸µ ¼Óµµ Á¶ÀýÀÌ µÊ.
	linear : function(v) { return v; },
	faster : function(v) { return v * v * v * v; },
	slower : function(v) { var v = Math.sin(v * (Math.PI / 2)); return v * v * v * v; }
};


// µ¥ÀÌÅÍ¸¦ Add ÇÏ´Â Å¬·¡½º
nhn.AddData = $Class({
	nAddItem : 0,
	oData : [],
	
	$init : function(sId, oData) {
		this.elOLId = $(sId);
		for(var x in oData)		this.oData[x] = oData[x];
		this._addData();
	},
	
	// ÀÌ¹ÌÁö ÅÂ±×¿Í °æ·Î ¼Ó¼º(src)À» Æ÷ÇÔÇÏ°í ÀÖÀ¸¸é true ¹ÝÈ¯, Æ÷ÇÔÇÏ°í ÀÖÁö ¾ÊÀ¸¸é ÅØ½ºÆ®·Î ÀÎ½ÄÇÏ°í false ¹ÝÈ¯.
	_judgeData : function(nIdx) {
		var sImgStr;		
		
		if(nIdx < this.oData.length) {
			sImgStr = this.oData[nIdx].sTag;
			if ((sImgStr.indexOf("img") && sImgStr.indexOf("src")) != -1) {
				return true;
			}
			else {
				return false;
			}
		}
	},
	
	// µ¥ÀÌÅÍ Å¸ÀÔ¿¡ µû¶ó ·Ñ¸µ ÄÁÅ×ÀÌ³Ê¿¡ Ãß°¡, ÀÌ¹ÌÁö µ¥ÀÌÅÍ°¡ ³Ñ¾î¿À¸é Å©±â¸¦ ÄÁÅ×ÀÌ³ÊÀÇ Å©±â¿¡ ¸Â°Ô Á¶Á¤
	_addData : function() {
		for(var i=0; i<this.oData.length; i++) {
			var oInnerLi = $("<li>");
			$Element(oInnerLi).attr("id", 'list' + i);
			$Element(this.elOLId).append(oInnerLi);
			$Element(oInnerLi).html(this.oData[i].sTag);

			$A(cssquery("img", this.elOLId)).forEach(function(o){
				$Element(o).css ({
					height : (parseInt($Element(oInnerLi).css('height'))-normal.hResize) + 'px',
					width : (parseInt($Element(oInnerLi).css('width'))-normal.wResize) + 'px'
				});
			});
		}		
	}
}).extend(Rolling);