var Slider = function(intNumElem, strPrefix, strMovimento) {
    this.numElementi = intNumElem;
    this.prefix = strPrefix;
    this.curElemento = 0;
    this.timing = 5;
    this.slidingTime = 0.3;
    this.timer = null;
    this.movimento = strMovimento;
}

Slider.prototype.moveOut = function(num, index, velocita, accelerazione, intervallo) {
    totMove = index * (velocita * accelerazione);
    if (this.movimento == 'destrasinistra') document.getElementById(this.prefix + '_slide' + num).style.left = -totMove + '%';
    if (this.movimento == 'bassoalto') document.getElementById(this.prefix + '_slide' + num).style.top = -totMove + '%';
    if (totMove < 100) setTimeout('mySliding.moveOut(' + num + ',' + (index + 1) + ',' + velocita + ',' + (accelerazione - 0.02) + ',' + intervallo + ')', intervallo);
    else {
        switch (this.movimento) {
            case 'destrasinistra':
                document.getElementById(this.prefix + '_slide' + num).style.left = '';
                document.getElementById(this.prefix + '_slide' + num).style.right = '-100%';
                break;
            case 'bassoalto':
                document.getElementById(this.prefix + '_slide' + num).style.top = '';
                document.getElementById(this.prefix + '_slide' + num).style.bottom = '-130%';
        }
    }
}

Slider.prototype.moveIn = function(num, index, velocita, accelerazione, intervallo) {
    totMove = 100 - (index * velocita * accelerazione);
    if (totMove < 0) totMove = 0;
    if (this.movimento == 'destrasinistra') document.getElementById(this.prefix + '_slide' + num).style.right = -totMove + '%';
    if (this.movimento == 'bassoalto') document.getElementById(this.prefix + '_slide' + num).style.bottom = -totMove + '%';
    if (totMove > 0) setTimeout('mySliding.moveIn(' + num + ',' + (index + 1) + ',' + velocita + ',' + (accelerazione - 0.02) + ',' + intervallo + ')', intervallo);
    else this.curElemento = num;
}

Slider.prototype.next = function(nextEl) {
	if (nextEl <= 0) toShow = this.curElemento + 1;
	else toShow = nextEl;
	if (toShow > this.numElementi) toShow = 1;
	if (toShow != this.curElemento) {
		//parametri per velocita di scorrimento
		intervallo = 10;
		spazio = 100;
		tempo = this.slidingTime * (1000 / intervallo);
		velocita = spazio / tempo; //percentuale di spostamento
		if (this.curElemento > 0) this.moveOut(this.curElemento, 1, velocita, 2, intervallo);
		this.displayPag(toShow);
		this.moveIn(toShow, 1, velocita, 2, intervallo);
	}

}

Slider.prototype.showElement = function(num) {
    if (num != this.curElemento) {
        this.stopSliding();
        switch (this.movimento) {
            case 'destrasinistra':
                document.getElementById(this.prefix + '_slide' + this.curElemento).style.right = '';
                document.getElementById(this.prefix + '_slide' + this.curElemento).style.left = '';
                break;
            case 'bassoalto':
                document.getElementById(this.prefix + '_slide' + this.curElemento).style.bottom = '';
                document.getElementById(this.prefix + '_slide' + this.curElemento).style.top = '';
                break;
        }
        for (i = 1; i <= this.numElementi; i++) {
            if (i != this.curElemento) {
                switch (this.movimento) {
                    case 'destrasinistra':
                        document.getElementById(this.prefix + '_slide' + i).style.right = '-100%';
                        document.getElementById(this.prefix + '_slide' + i).style.left = '';
                        break;
                    case 'bassoalto':
                        document.getElementById(this.prefix + '_slide' + i).style.bottom = '-130%';
                        document.getElementById(this.prefix + '_slide' + i).style.top = '';
                        break;
                }
            }
        }
        this.next(num);
        this.startSliding();
    }
}

Slider.prototype.displayPag = function(num) {
	for(i = 1; i <= this.numElementi; i++){
	    //document.getElementById(this.prefix + 'e_pag_slider' + i).removeAttribute("class");
	    document.getElementById(this.prefix + '_pag_slider' + i).className = '';
	}
	document.getElementById(this.prefix + '_pag_slider' + num).className = 'current';
}

Slider.prototype.startSliding = function() {
    this.timer = setInterval('mySliding.next(-1)', (this.timing + this.slidingTime) * 1000);
}

Slider.prototype.stopSliding = function() {
	clearInterval(this.timer);
}

var mySliding;

function initSlider(intNumElem, strPrefix, strMovimento) {
    if (document.getElementById(strPrefix) != null) {
        mySliding = new Slider(intNumElem, strPrefix, strMovimento);
        mySliding.next(-1);
        if (mySliding.numElementi > 1) mySliding.startSliding();
    }
}
