isMSIE = /*@cc_on!@*/false; 
if(!isMSIE)
{
	trace = function(obj){
  	console.log(obj)
  }
}


String.prototype.getNum = function()
{
	return this.replace(/[^\-.0-9]/g , "") * 1
}

function RollSlider(obj)
{
	var self = this;
	this.stage = obj.stage;
	this.children = obj.children;
	this.cells = this.children.getElementsByTagName(obj.child_tag);
	this.cells_original = [];
	this.cell_size = obj.cell_size;
	this.rnum = 0;
	this.lnum = 0;
	this.timer = false;
	this.auto_position = false;
	this.min_length = obj.min_length ? obj.min_length : 6;
	
	
	
	this.rightButton = obj.rightButton;
	this.rightButton.onmousedown = function(e){self.rightSlide(e)};
	this.leftButton = obj.leftButton;
	this.leftButton.onmousedown = function(e){self.leftSlide(e)};

	this._init();
	
}

RollSlider.prototype._rnumCount = function()
{
	this.rnum = this.rnum >= this.cells_original.length ? 1 : this.rnum + 1 ;
}

RollSlider.prototype._lnumMinus = function()
{
	this.lnum = this.lnum < 1 ? this.cells_original.length - 1 : this.lnum - 1 ;
}

RollSlider.prototype._init = function()
{
	if(this.cells.length <= this.min_length)
	{
		this.rightButton.style.display = "none";
		this.leftButton.style.display = "none";
	}
	
	this._resize();
	for(var i = 0 ; i < this.cells.length ; i++)
	{
		this.cells_original.push(this.cells[i].cloneNode(true));
	}
	
	
}

RollSlider.prototype._resize = function()
{
	this.stage.style.width = this.cell_size * this.cells.length + "px";
}

RollSlider.prototype.rightSlide = function(e)
{
	if(this.auto_position) return false;
	var self = this;
	var tp = this.children.style.left.getNum() - this.cell_size ;
	var x = this.children.style.left.getNum();

	this._rnumCount();	
	var clone = this.cells_original[this.rnum -1].cloneNode(true);
	this.children.appendChild(clone);
	this._resize();
	

	if(this.timer) clearInterval(this.timer);
	this.timer = setInterval(function(){
		x += Math.floor( ( tp - x ) / 5 );
		self.children.style.left = x + "px";
		if (x == tp) {
			clearInterval(self.timer);
			self._complete("right");
		}
	} , 30);
}

RollSlider.prototype.leftSlide = function(e)
{
	if(this.auto_position) return false;
	var self = this;
	this.children.style.left = this.children.style.left.getNum() - this.cell_size + "px";
	var tp = this.children.style.left.getNum() + this.cell_size ;
	var x = this.children.style.left.getNum();
	
	this._lnumMinus();
	var clone = this.cells_original[this.lnum].cloneNode(true);
	
	this.children.insertBefore(clone , this.cells[0]);
	
	this._resize();
	
	
	if(this.timer) clearInterval(this.timer);
	this.timer = setInterval(function(){
		x += Math.ceil( ( tp - x ) / 5 );
		self.children.style.left = x + "px";
		if (x == tp) {
			clearInterval(self.timer);
			self._complete("left");
		}
	} , 30);
	
}


RollSlider.prototype._complete = function(derection)
{
	var self = this;
	var delta = Math.abs(this.children.style.left.getNum() % this.cell_size) ;

	if(delta > 0)
	{
		this.auto_position = true;
		if(derection == "right")
		{
			delta -= this.cell_size;
			var x = this.children.style.left.getNum();
			var tp = x + delta;
			
			this.timer = setInterval(function(){
				x += Math.floor( ( tp - x ) / 5 );
				self.children.style.left = x + "px";
				if (x == tp) {
					clearInterval(self.timer);
					self.auto_position = false;
					self._cleaning();
				}
			} , 30);
	
	
		}else
		{
			var x = this.children.style.left.getNum();
			var tp = x + delta ;

			this.timer = setInterval(function(){
				x += Math.ceil( ( tp - x ) / 5 );
				self.children.style.left = x + "px";
				if (x == tp) {
					clearInterval(self.timer);
					self.auto_position = false;
					self._cleaning();
					
				}
			} , 30);
		}
	}else
	{
		self._cleaning();
	}
	
}

RollSlider.prototype._cleaning = function()
{
	if(this.cells.length > this.cells_original.length * 10)
	{
		this.auto_position = true;
		var self = this;
		var x = this.children.style.left.getNum();

		for(var i = 0 ; i < this.cells_original.length ; i++)
		{
			if(this.cells[i].className == "first")
			{
				var tp = - (i * this.cell_size);
				break;
			}			
			
		}


		this.timer = setInterval(function(){
			x += Math.ceil( ( tp - x ) / 3 );
			self.children.style.left = x + "px";
			
			if (Math.ceil( ( tp - x ) / 3 ) == 0) {
				
				clearInterval(self.timer);
				self.auto_position = false;
				self.children.style.left = "0px";
				
				
				while(self.cells.length > 0)
				{
					self.children.removeChild(self.cells[self.cells.length -1]);
				}
				
				for(var i = 0 ; i < self.cells_original.length ; i++)
				{
					self.children.appendChild(self.cells_original[i].cloneNode(true));
				}
				
				self.rnum = 0;
				self.lnum = 0;
			}
		} , 30);
		
		
	}
}


