﻿
function MySlider(parentDiv, className, name) {

    this.transitionTime = 3000;
    this.name = name;

    this.next = function() {
        var num = this._currentDivNum + 1
        if (num == this._contentDivs.length) {
            num = 0;
        }
        this.change(num);
    };
    this.previous = function() {
        var num = this._currentDivNum - 1
        if (num == -1) {
            num = this._contentDivs.length - 1;
        }
        this.change(num);
    };

    this.change = function(newNum) {
        if (newNum != this._currentDivNum) {
            this.hideDiv(this._contentDivs[newNum], false);
            this.hideDiv(this._contentDivs[this._currentDivNum], true);
            this._currentDivNum = newNum;
            if(this.timer){
                clearTimeout(this.timer);
                this.timer = setTimeout(this.name+".next()", this.transitionTime);
            }
        }
    };

    this.hideDiv = function(div, hide) {
        if(div){
            if (hide) {
                div.style.display = 'none';
            } else {
                div.style.display = 'block';
            }
        }
    }

    this._parentDivId = parentDiv;

    // initialisation
    var divs = document.getElementById(parentDiv).getElementsByTagName("div");
    this._contentDivs = [];
    this._currentDivNum = 0;
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].className == className) {
            this._contentDivs.push(divs[i])
        }
    }
    if (this._contentDivs.length > 0) {
        this._contentDivs[0].style.position = "absolute";
        for (var i = 1; i < this._contentDivs.length; i++) {
            this.hideDiv(this._contentDivs[i], true);
            this._contentDivs[i].style.position = "absolute";
        }
        if(name){
            this.timer = setTimeout(this.name+".next()", this.transitionTime);
        }
    }
    // end initialisation
}