﻿
function animatedglider(divId, animatetime, maxright) {
    this.divId = divId
    this.divObj = document.getElementById(divId)
    this.timelength = animatetime
    this.contentwidth = parseInt(this.divObj.style.width)
    this.contentheight = parseInt(this.divObj.style.height)
    this.maxsize = maxright - 0
    this.contentLeft = 0;

    this.startLeft = this.contentLeft
    this.targetLeft = this.contentLeft
    
    var thisobj = this
}

animatedglider.prototype.left = function(distance) {
//this.contentLeft = this._min(0, this._max(-this.maxsize, (this.contentLeft - distance)));
//this.divObj.style.left = this.contentLeft + "px"
var thisobj = this
this.startTime = new Date().getTime()
this.startLeft = this.contentLeft
if (this.runtimer != null)
    this.targetLeft = this._min(0, this._max(-this.maxsize, (this.targetLeft - distance)))
else
    this.targetLeft = this._min(0, this._max(-this.maxsize, (this.contentLeft - distance)))
this.runtimer = setTimeout(function() { thisobj._slideengine("na") }, 10)
}
animatedglider.prototype.right = function(distance) {
//this.contentLeft = this._min(0, (this.contentLeft + distance));
//this.divObj.style.left = this.contentLeft + "px"
var thisobj = this
this.startTime = new Date().getTime()
this.startLeft = this.contentLeft
if (this.runtimer != null)
    this.targetLeft = this._min(0, (this.targetLeft + distance))
else
    this.targetLeft = this._min(0, (this.contentLeft + distance))
this.runtimer = setTimeout(function() { thisobj._slideengine("na") }, 10)
}

animatedglider.prototype._min = function(a, b) {
return (a < b) ? a : b
}
animatedglider.prototype._max = function(a, b) {
return (a > b) ? a : b
}

// unfinished
animatedglider.prototype._slideengine = function(direction) {
    var elapsed = new Date().getTime() - this.startTime //get time animation has run
    var thisobj = this;
    var distancepercent;
    
    //if (direction == "left") {
        if (elapsed < this.timelength) {
            distancepercent = animatedglider.curveincrement(elapsed / this.timelength)
            this.contentLeft = this.lerp(this.startLeft, this.targetLeft, distancepercent);
            this.divObj.style.left = this.lerp(this.startLeft, this.targetLeft, distancepercent) + "px";
            this.runtimer = setTimeout(function() { thisobj._slideengine(direction) }, 10)
        }
    //}
}

animatedglider.prototype.lerp = function(a, b, percent) {
    return (a * (1 - percent) + b * percent);
}

animatedglider.curveincrement=function(percent){
	return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
}
