﻿var timerFoto = 0;
var speed = 5000;
var i = 0;
var transin = 0.1;
var transout = 100;
var imatge;

function fadeout() {
    
    imatge.style.opacity = transout / 100;
    imatge.style.filter = 'alpha(opacity = ' + transout + ')';
    transout -= 20;
    if (transout >= 3)
        window.setTimeout(fadeout, 10);
    else {
        imatge.onload = new function(){ window.setTimeout(fadein, 100); };  
        imatge.src = imatges[index];
        imatge.style.opacity = 0;
        imatge.style.filter = 'alpha(opacity = 0)';
    }
}

function fadein() {

    imatge.style.opacity = transin / 100;
    imatge.style.filter = 'alpha(opacity = ' + transin + ')';
    transin += 20;
    if (transin <= 100)
        window.setTimeout(fadein, 10);
    else {
        imatge.style.opacity = 1;
        imatge.style.filter = 'alpha(opacity = 100)';
        window.setTimeout(canviafoto, 5000);
    }
}

function canviafoto() {
    imatge = document.getElementById('fotoPortada');
    index++;
    if (index >= imatges.length) index = 0;
    transout = 100;
    window.setTimeout(fadeout, 10);
}

index = 0;
document.getElementById('fotoPortada').src = imatges[index];
timerFoto = window.setTimeout(canviafoto, speed);

