        // Cool sliding by ABTOMAT 2010 Version 0.21


function Slider()
{
    this.tape = null;
    this.backButton = null;
    this.nextButton = null;
    this.limit = 4;
    this.mode = 1; // Mode:
    // 0 - Horisontal
    // 1 - Vertical
    this.movePeriod = 10;
    this.speed = 0.20;
    this.minSpeed = 20;
    this.step=100; // length of one frame

    this.position = 0;
    this.targetPosition = 0;

    this.triggerMode = 1;
    // 0 - discrete
    // 1 - continuous

    this.moving = 0;

    this.direction = 0;
    this.movePeriod = 5;
    this.acceleration = 0.2;
    this.resist = 0.975;
    this.moveSpeed = 0;
    this.maxSpeed = 10;

    this.pages = Array();
}

Slider.prototype.setMode = function (value)
{
    this.mode = value;
}

Slider.prototype.setSpeed = function (value)
{
    this.speed = value;
}

Slider.prototype.setTriggerMode = function (value)
{
    this.triggerMode = value;
}

Slider.prototype.setLimit = function (value)
{
    this.limit = value-2;
}

Slider.prototype.setStep = function (value)
{
    this.step = value;
}

Slider.prototype.setTape = function (node)
{
    if(node !=null)
    {
        this.tape = node;
        this.setCoord(0);
        this.tape.style.width = (this.limit+2)*(this.step)+"px";
    }
    else
    {
        alert("Tape you are trying to set is null!");
    }
}

Slider.prototype.move = function()
{
    if(this.moving == 1)
    {

        //if(this.acceleration*this.direction < 0) alert(0);

        this.speed -= this.acceleration*this.direction;
        if(this.speed <= -this.maxSpeed)
        {
            this.speed = -this.maxSpeed;
        }
        else
        {
            if(this.speed >= this.maxSpeed)
            {
                this.speed = this.maxSpeed;
            }
        }
        
        this.speed *= this.resist;

        this.setCoord(this.getCoord() +this.speed);
        var sli = this;
        if(this.getCoord() < -this.getCoordLimit())
        {
            this.setCoord(-this.getCoordLimit());
            this.moving = 0;
            this.speed = 0;
        }
        else
        {
            if(this.getCoord() > 0)
            {
                this.setCoord(0);
                this.moving = 0;
                this.speed = 0;
            }
            else
            {
                setTimeout(function(){sli.move()}, this.movePeriod);
            }
        }
    }
}

Slider.prototype.setTapeById = function (id)
{
    var node = document.getElementById(id);
    if(node !=null)
    {
        this.setTape(node);
    }
    else
    {
        alert("Tape with id \""+id+"\" you are trying to set is null!");
    }
}

Slider.prototype.setPages = function (value, tag)
{
    if(typeof(tag)=="undefined") tag = "*";
    var pages, i;
    var slider = this;
    
    if(value !=null)
    {
        if(typeof(value)=="string")
        {
            pages = document.getElementById(value);
        }
        else if(typeof(value)=="object")
        {
            pages = value;
        }
        else
        {
            alert("Pages must be HTML element or its id!");
            return;
        }
        this.pages = pages.getElementsByTagName(tag)
        for(i=0; i<this.pages.length; i++)
        {
            this.pages[i].onclick = function(kuda)
            {
                return function(){slider.goToSlide(kuda); return false;}
            }(i);
        }
    }
    else
    {
        alert("Pages \""+id+"\" you are trying to set are null!");
    }
}

Slider.prototype.setBackButton = function (node)
{
    if(node !=null)
    {
        this.backButton = node;
        var slider = this;
        if(this.triggerMode)
        {
            this.backButton.onmouseover = function()
            {
                slider.direction = -1;
                if(slider.moving != 1)
                {
                    slider.moving = 1;
                    slider.move();
                }

            }
            this.backButton.onmouseout = function()
            {
                slider.direction = 0;
                if(slider.moving != 1)
                {
                    slider.moving = 1;
                    slider.move();
                }

            }
        }
        else
        {
            var bb = this.backButton;
            this.backButton.onclick = function()
            {
                slider.backSlide();
            }
            this.backButton.onmouseover = function()
            {
                bb.className = "bover";
            }
            this.backButton.onmousedown = function()
            {
                bb.className = "bpress";
            }
            this.backButton.onmouseup = function()
            {
                bb.className = "bover";
            }
            this.backButton.onmouseout = function()
            {
                bb.className = "breg";
            }
        }
    }
    else
    {
        alert("Back button you are trying to set is null!");
    }
}

Slider.prototype.setBackButtonById = function (id)
{
    node = document.getElementById(id);
    if(node !=null)
    {
        this.setBackButton(node);
    }
    else
    {
        alert("Back button with id \""+id+"\" you are trying to set is null!");
    }
}

Slider.prototype.setNextButton = function (node)
{
    if(node !=null)
    {
        this.nextButton = node;
        var slider = this;
        if(this.triggerMode)
        {
            this.nextButton.onmouseover = function()
            {
                slider.direction = 1;
                if(slider.moving != 1)
                {
                    slider.moving = 1;
                    slider.move();
                }

            }
            this.nextButton.onmouseout = function()
            {
                slider.direction = 0;
                if(slider.moving != 1)
                {
                    slider.moving = 1;
                    slider.move();
                }
                
            }
        }
        else
        {
            var nb = this.nextButton;
            this.nextButton.onclick = function()
            {
                slider.nextSlide();
            }
            this.nextButton.onmouseover = function()
            {
                nb.className = "nover";
            }
            this.nextButton.onmousedown = function()
            {
                nb.className = "npress";
            }
            this.nextButton.onmouseup = function()
            {
                nb.className = "nover";
            }
            this.nextButton.onmouseout = function()
            {
                nb.className = "nreg";
            }
        }
    }
    else
    {
        alert("Next button you are trying to set is null!");
    }
}

Slider.prototype.setNextButtonById = function (id)
{
    var node = document.getElementById(id);
    if(node !=null)
    {
        this.setNextButton(node);
    }
    else
    {
        alert("Next button with id \""+id+"\" you are trying to set is null!");
    }
}

Slider.prototype.setCoord = function(coord)
{
    if (this.tape!=null)
    {
        if (this.mode == 0)
        {
            this.tape.style.left = coord+"px";
        }
        else
        {
            this.tape.style.top = coord+"px";
        }

    }
    else
    {
        alert("Tape is null! You must set it before trying to move it!");
    }
}

Slider.prototype.getCoord = function()
{
    if (this.tape!=null)
    {
        if (this.mode == 0)
        {
            return (parseInt(this.tape.style.left));
        }
        else
        {
            return (parseInt(this.tape.style.top));
        }

    }
    else
    {
        alert("Tape is null! You must set it before trying to get it's coordinate!");
        return (null);
    }
}

Slider.prototype.getCoordLimit = function()
{
    return (this.limit*this.step);
}

Slider.prototype.nextSlide = function()
{
    if(this.targetPosition<=this.limit)
    {
        this.targetPosition++;
        this.goToSlide(this.targetPosition);
    }
    else
    {
        this.targetPosition = 0;
        this.goToSlide(this.targetPosition);
    }
}

Slider.prototype.backSlide = function()
{
    if(this.targetPosition>0)
    {
        this.targetPosition--;
        this.goToSlide(this.targetPosition);
    }
    else
    {
        this.targetPosition = this.limit+1;
        this.goToSlide(this.targetPosition);
    }
}

Slider.prototype.goToSlide = function(number)
{   
    if(number>this.limit+1) number = this.limit+1;
    var slider = this;
    this.targetPosition = number;
    setTimeout(function(){slider.sliding()}, this.movePeriod);
    var i;
    for(i=0; i<this.pages.length;i++)
    {
        ASRemoveClass(this.pages[i],"active");
    }
    if(this.pages[number]!=null) ASAddClass(this.pages[number],"active");
}

Slider.prototype.sliding = function()
{
    direction = -this.targetPosition*this.step - this.getCoord();
    if (direction!=0) direction += (direction/Math.abs(direction))*this.minSpeed;

    if(Math.abs(direction*this.speed)<8)
    {
        this.setCoord(-this.targetPosition*this.step);
    }
    else
    {
        var slider = this;
        this.setCoord(this.getCoord() + direction*this.speed);
        setTimeout(function(){slider.sliding()}, this.movePeriod);
    }
}

function ASAddClass(el, newclass)
{
    var classes=Array();
    var i;
    var k = 0;
    var newClassName = "";
    for (i=0; i<el.className.length; i++)
    {
        if(el.className[i]==" "||i==el.className.length-1)
        {
            if(k-i!=0)
            {
                if(i==el.className.length-1)
                {
                    classes.push(el.className.substr(k, el.className.length-k+1));
                }
                else
                {
                    classes.push(el.className.substr(k, i-k));
                }
            }
            k = i+1;
        }
    }
    for(i=0; i<classes.length; i++)
    {
        if(classes[i]==newclass) return;
    }

    classes.push(newclass);
    for(i=0; i<classes.length; i++)
    {
        if(i!=0) newClassName += " ";
        newClassName += classes[i];
    }

    el.className = newClassName;
}

function ASRemoveClass(el, remclass)
{
    var classes=Array();
    var i;
    var k = 0;
    var newClassName = "";
    for (i=0; i<el.className.length; i++)
    {
        if(el.className.substr(i,1)==" "||i==el.className.length-1)
        {
            if(k-i!=0)
            {
                if(i==el.className.length-1)
                {
                    classes.push(el.className.substr(k, el.className.length-k+1));
                }
                else
                {
                    classes.push(el.className.substr(k, i-k));
                }
            }
            k = i+1;
        }
    }

    for(i=0; i<classes.length; i++)
    {
        if(classes[i]!=remclass)
        {
            if(i!=0) newClassName += " ";
            newClassName += classes[i];
        }
    }
    el.className = newClassName;
}
