var Rolling = function(target){
	this.timer = null; 			//interval Å¸ÀÌ¸Ó
	this.timerNextPause = null;
	
	this.direction = 1; 		//¹æÇâ
	this.gapTime = 10; 			//ÀÌµ¿µô·¹ÀÌ
	this.gapMove = 1; 			//ÀÌµ¿°£°Ý
	this.gapNextPause = 0; 	//´ÙÀ½ ´ë»ó µ¿ÀÛ¿¡ ´ëÇÑ µô·¹ÀÌ, 0ÀÌ¸é »ç¿ë¾ÈÇÔ.
	
	this.target = null;
	this.targetW = 0;
	this.chilNodesW = 0;
	
	this.started = false; 		//ÇöÀç µ¿ÀÛÁßÀÎ°¡?
	this.paused = false; 		//ÇöÀç Àá½Ã ¸ØÃã ÁßÀÎ°¡?
	this.nextPaused = false; 	//´ÙÀ½ °³Ã¼°¡ º¸ÀÏ¶§ Àá½Ã ¸ØÃã ÁßÀÎ°¡?
	this.checkStart = true; 	//start ÇÒ¶§ ÀÚ½Ä³ëµåµéÀÇ ³Êºñ¸¦ Ã¼Å©ÇØ¼­ start°¡´ÉÇÑÁö Ã¼Å©, ¾øÀ¸¸é ¹«Á¶°Ç µ¿ÀÛ!
	
	if(target){
		this.init(target);
	}
}
Rolling.prototype = {
	//ÃÊ±âÈ­, Å¸°ÙÀ» ÁöÁ¤ÇÏ°í Å¸°ÙÀÇ child¸¦ ÃÊ±âÈ­½ÃÅ²´Ù.
	'init':function(target){
		this.target = target;

	}
	//--- Å¸°Ù ¼³Á¤ ÃÊ±âÈ­
	,'initChild':function(child){
		child.style.margin = '0';
	}
	//--- ¹æÇâ°ú ÀÚ½Ä ¼³Á¤ ÃÊ±âÈ­
	,'setDirection':function(direction){
		this.changeDirection(direction);
		var tNode = [];
		//--- Å¸°Ù ÃÊ±âÈ­
		this.target.style.overflow = 'hidden';
		this.target.style.position = 'relative';
		this.target.onmouseover = function(thisC){
									return function(){
										thisC.onmouseover()
									}
								}(this);
		this.target.onmouseout = function(thisC){
									return function(){
										thisC.onmouseout()
									}
								}(this);
		
		if(this.direction == 1 || this.direction == 3) {
			this.target.style.whiteSpace = 'normal';
			this.targetW = this.target.offsetWidth;
			
			for(var child = this.target.firstChild 	; child 	; child = child.nextSibling) {
				if(child.nodeType==1){	
					child.style.margin = 0;
					child.style.display = 'block';
					this.chilNodesW += child.offsetWidth;
				}else{
					tNode.push(child);
				}
			}
		} else if(this.direction == 2 || this.direction == 4) {
			this.target.style.whiteSpace = 'nowrap';
			this.targetW = this.target.offsetHeight;
			
			for(var child = this.target.firstChild 	; child 	; child = child.nextSibling) {
				if(child.nodeType==1){	
					child.style.margin = 0;
					child.style.display = 'inline-block';
					child.style.cssText += ';/display:inline'; //FOR IE6,7
					this.chilNodesW += child.offsetHeight;
				}else{
					tNode.push(child);
				}
			}
		}
		for(var i=0,m=tNode.length;i<m;i++){
			tNode[i].parentNode.removeChild(tNode[i]); //ÅÃ½ºÆ® ³ëµå Á¦°Å
		}
		tNode = null;
	}
	//--- ¹æÇâ º¯°æ
	,'changeDirection':function(direction){
		this.direction = direction
	}
	//--- Ã³À½³ëµå °¡Á®¿À±â
	,'getFirst':function(){
		return this.target.firstChild;
	}
	//--- ¸¶Áö¸·³ëµå °¡Á®¿À±â
	,'getLast':function(){
		return this.target.lastChild;
	}
	//--- µ¿ÀÛ
	,'_act':function(){
		if(this.paused || this.nextPaused){return;}
		switch(this.direction){
			case 1:this._act_up();		break;
			case 2:this._act_right(); 	break;
			case 3:this._act_down(); 	break;
			case 4:this._act_left();	break;
		}
	}
	//--- µ¿ÀÛ À§ (1)
	,'_act_up':function(){
		//---ÃÊ±âÈ­
		var n = this.getFirst();
		var mt = Math.abs(parseInt(n.style.marginTop));
		var mtg = mt+this.gapMove
		var h = n.offsetHeight;
		if(mtg>=h){
			this.target.appendChild(n);
			this.initChild(n);
			this.nextPause();
			this._act();
			return;
		}else{
			n.style.marginTop = '-'+mtg.toString()+'px';
		}
		//document.title = n.style.marginTop;
	}
	//--- µ¿ÀÛ ¿À¸¥ÂÊ (2)
	,'_act_right':function(){
		//---ÃÊ±âÈ­
		var n = this.getFirst();
		var ml = Math.abs(parseInt(n.style.marginLeft));
		var mlg = ml-this.gapMove
		if(mlg<=0){
			var l = this.getLast();
			var w = l.offsetWidth;
			l.style.marginLeft = '-'+w+'px';
			this.target.insertBefore(l,n); //¸¶Áö¸· ³ëµå¸¦ ¸Ç Ã³À½À¸·Î
			
			this.initChild(n);
			this.nextPause();
			this._act();
			return;
		}else{
			n.style.marginLeft = '-'+mlg.toString()+'px';
		}
		//document.title = n.style.marginLeft;
	}
	//--- µ¿ÀÛ ¾Æ·¡(3)
	,'_act_down':function(){
		//---ÃÊ±âÈ­
		var n = this.getFirst();
		var mt = Math.abs(parseInt(n.style.marginTop));
		var mtg = mt-this.gapMove;
		
		if(mtg<=0){
			var l = this.getLast();
			var h = l.offsetHeight;
			l.style.marginTop = '-'+h+'px';
			this.target.insertBefore(l,n); //¸¶Áö¸· ³ëµå¸¦ ¸Ç Ã³À½À¸·Î
			this.initChild(n);
			this.nextPause();
			this._act();
			return;
		}else{
			n.style.marginTop = '-'+mtg.toString()+'px';
		}
		//document.title = n.style.marginTop;
	}
	//--- µ¿ÀÛ ¿ÞÂÊ (4)
	,'_act_left':function(){
		//---ÃÊ±âÈ­
		var n = this.getFirst();
		var ml = Math.abs(parseInt(n.style.marginLeft));
		var mlg = ml+this.gapMove
		var w = n.offsetWidth;
		if(mlg>=w){
			this.target.appendChild(n);
			this.initChild(n);
			this.nextPause();
			this._act();
			return;
		}else{
			n.style.marginLeft = '-'+mlg.toString()+'px';
		}
		//document.title = n.style.marginLeft;
	}
	//---  ·Ñ¸µ½ÃÀÛ
	,'start':function(){
		if(!this.started && this.startAble()){
			this.nextPause();
			this.timer = setInterval(function(thisC){return function(){thisC._act()}}(this),this.gapTime);
			this.started = true;
		}
	}
	//--- ·Ñ¸µ ¸ØÃã
	,'stop':function(){
		clearInterval(this.timer);
		this.started = false;
	}
	//--- ·Ñ¸µÀ» ½ÃÀÛÇÒ ¼ö ÀÖ´Â°¡? ¿Ü°¢ÀÇ 1.5¹è¸¸Å­ÀÇ ÀÚ½Ä ³ëµå°¡ ÀÖ¾î¾ßÇÑ´Ù.
	,'startAble':function(){
		if(!this.checkStart){return true;}
		if(this.chilNodesW > (this.targetW*2)){
			return true;
		}
		return false;
	}
	//--- pause(boolÀÌ true¸é ¸ØÃá´Ù. false¸é ¸ØÃãÀÌ ÇØÁ¦)
	//stop°ú Â÷ÀÌÁ¡Àº clearIntervalÀ» ÇÏÁö ¾Ê´Â´Ù´Â °Í!
	,'pause':function(bool){
		this.paused = bool?true:false;
	}
	//--- ´ÙÀ½ °´Ã¼°¡ º¸ÀÏ¶§ Àá½Ã ¸ØÃá´Ù.
	,'nextPause':function(){
		if(this.nextPaused){
			clearTimeout(this.timerNextPause);
		}
		this.nextPaused = true;
		this.timerNextPause = setTimeout(function(thisC){
								return function(){
									thisC.nextPaused = false;
								}
							}(this)
							,this.gapNextPause);
	}
	//--- ¸¶¿ì½º ¿À¹ö ÀÌº¥Æ®, º¸Åë Àá½Ã ¸ØÃá´Ù.
	,'onmouseover':function(){
		this.pause(true);
	}
	//--- ¸¶¿ì½º ¾Æ¿ô ÀÌº¥Æ®, º¸Åë Àá½Ã ¸ØÃáÀ» ÇØÁ¦ÇÑ´Ù.
	,'onmouseout':function(){
		this.pause(false);
	}
	//--- À§·Î ³Ñ±è
	,'up':function(){
		var n = this.getFirst();
		var l = this.getLast();
		this.target.appendChild(n);	
		this.initChild(n);
		this.nextPause();
	}
	//--- ¾Æ·¡·Î ³Ñ±è
	,'down':function(){
		var n = this.getFirst();
		var l = this.getLast();
		this.target.insertBefore(l,n); //¸¶Áö¸· ³ëµå¸¦ ¸Ç Ã³À½À¸·Î
		this.initChild(l);
		this.initChild(n);
		this.nextPause();
	}
	//--- ¿ÞÂÊÀ¸·Î ³Ñ±è, up°ú µ¿ÀÛÀÌ °°´Ù.
	,'left':function(){
		this.up();
	}
	//--- ¿À¸¥ÂÊÀ¸·Î ³Ñ±è , down°ú µ¿ÀÛÀÌ °°´Ù.
	,'right':function(){
		this.down();
	}
}
