//Class ScrollEffect:

function ScrollEffect(target_dom, duration_ms, refresh_ms){
	//Generals variables:
	this.target = target_dom;
	this.initPosition = null;
	this.actualPosition = this.initPosition;
	this.distance = null;
	this.initTime = 0;
	this.timeActual = this.initTime;
	this.duration = duration_ms;
	this.refresh = refresh_ms;
	
	this.temp = null;
	this.isTempActive = false;

	//Specific of this scroll variables:
	this.actualStep = 1;
	this.sectionsWidth = null;
	this.numberSections = null;
	this.wasInitScroll = false;
	
	this.fixDuration = function(){
		if(this.duration < this.refresh) {throw("duration must be greater than refresh time")}
		this.duration = (this.duration - (this.duration % this.refresh));
	};
	
	(function initialice(){
		if((this.duration == undefined) || (this.duration <= 0)) {throw("duration must a number > 0")};
		if((this.refresh == undefined) || (this.refresh <= 0)) {throw("refresh must a number > 0")};		
		this.fixDuration();
	}).call(this);	
	
	this.getTargetLocation = function(){
		var left_attr = this.target.style.left;
		left_num = Number(left_attr.substring(0,left_attr.length - 2));
		return left_num;	
	};
	
	this.easeOut = function(t, b, c, d){		
		t = t/d-1;
		return c*((t)*t*t*t*t + 1) + b;
	};
	
	//---------------------------------------------
	//Generic Animation:
	
	this.animate = function(distance_param, initPosition_x){
		//pre-animation:
		if(this.isTempActive){return};

		if(distance_param !== undefined) {this.distance = distance_param};
		if(this.distance === null) {throw("distance is required to animate")};
		if(this.distance === 0) {throw("distance 0 is error")};		
		
		if(initPosition_x !== undefined){
			this.initPosition = initPosition_x;
			this.target.style.left = this.initPosition + "px";
			this.actualPosition = this.initPosition;
		};
		if(this.initPosition === null){
			this.initPosition = this.getTargetLocation();
			this.actualPosition = this.initPosition;
		};	
		
		this.startAnimation();
	};
	
	this.startAnimation = function(){
		var this_int = this;	
		function call_doAnimation(){
			this_int.doAnimation();
		};
		this.temp = window.setInterval(call_doAnimation, this.refresh);		
		this.isTempActive = true;			
	};
	
	this.doAnimation = function(){
		this.actualPosition = this.easeOut(this.timeActual, this.initPosition, this.distance, this.duration);
		this.target.style.left = this.actualPosition + "px";
				
		if(this.timeActual < this.duration){
			this.timeActual += this.refresh;
		} else {
			this.finishAnimation();
		};
	};
	
	this.finishAnimation = function(){
		window.clearInterval(this.temp);
		
		this.initPosition = this.actualPosition;
		this.distance = null;
		this.timeActual = this.initTime;
		
		this.temp = null;
		this.isTempActive = false;		
	};

	//---------------------------------------------
	//Specific aplication to Scroll:
	
	this.initScroll = function(widthElement, maxElements, numeric_dom){
		if((widthElement == undefined) || (widthElement <= 0)) {throw("width scroll element must be > 0")};	
		this.sectionsWidth = widthElement;
		if((maxElements == undefined) || (maxElements <= 0)) {throw("quantity of sections must be > 0")};			
		this.numberSections = maxElements;
		this.numericControl_dom = numeric_dom;
		this.wasInitScroll = true;
	};	

	this.doScroll = function(type_str, value_num){
		if(this.isTempActive){return};	
		if(!this.wasInitScroll){throw("Is obligatory run the initScroll method before of doScroll method")};			

		var newDistance = 0;
		var distanceSteps = 0;
		
		if(type_str == "numericButton"){
			distanceSteps = value_num - this.actualStep;
			if(distanceSteps == 0){return};
			newDistance = (-1)* (distanceSteps) * this.sectionsWidth;
			this.actualStep = value_num;
		};
		
		var nextStep = 0;
		
		if(type_str == "scrollArrow"){
			nextStep = value_num + this.actualStep;
			if((nextStep == 0)||(nextStep == this.numberSections + 1)){return};
			newDistance = (-1) * value_num * this.sectionsWidth;		
			this.actualStep = nextStep;			
		};
		
		switch(this.actualStep){
			case 1:
				this.numericControl_dom.style.backgroundPosition = "0px 0px"
				break;
			
			case 2:
				this.numericControl_dom.style.backgroundPosition = "0px -40px"			
				break;			
			
			case 3:
				this.numericControl_dom.style.backgroundPosition = "0px -80px"			
				break;			
		};
		
		this.animate(newDistance);
	};
};
