var ACTIVE_SLIDE = null;

function Slide(el, dir, foto_list, order){

    this.active = true;
    
    this.browser = function(){
        var ua = navigator.userAgent, b="Unknown";   
        if(ua.indexOf("MSIE")!= -1) b="IE";
        if(ua.indexOf("Firefox")!= -1) b="FF";
        if(ua.indexOf("Opera")!= -1) b="OP";
        if(ua.indexOf("Safari")!= -1) b="SA";
        return b;       
    }
    
    this.getNext = function(){ 
        var nb = this.activeBox === this.Box1 ? this.Box2 : this.Box1;
        return nb;   
    };
    
    this.setNext = function(){    
        var ni = this.PN + 1 < this.TF ? this.PN + 1 : 0, nb = this.getNext();        
        var _img = nb.getElementsByTagName('img')[0];
        this.PN = ni;
        
        if(this.browser() == 'IE'){   
            var image = document.createElement("img");
                image.setAttribute("src", this.path + this.FL[ni]);
                nb.replaceChild(image, _img);        
        }else{
            _img.setAttribute('src', this.path + this.FL[ni]);
        }
    };
    
    this.opacityIE = function(b, v){b.style.filter='progid:DXImageTransform.Microsoft.Alpha(opacity=' + v + ')';};    
    this.opacity   = function(b, v){b.style.opacity= v/100;};
    
    
    this.play = function(){
        var t = this;
        if(this.active) 
            setTimeout(function(){t.crossfade(t.getNext(), 99, 1)}, 5000);
    };
    
    this.stop = function(){}
    
    this.crossfade = function(b, O, I){
        if(!this.active) return;
        var t = this;
        this.setOpacity(this.activeBox, O);
        this.setOpacity(b, I);
        
        O -= 1;
        I += 1;
        
        if(O) setTimeout(function(){t.crossfade(b, O, I)}, 10);     
        else{
            this.setOpacity(this.activeBox, 0);
            this.setOpacity(b, 100);
            this.activeBox = b;
            this.setNext();
            this.play();
        }
    };   

    this.Box = document.getElementById(el);
    this.path = dir + "/";
    this.FL = foto_list;
    this.TF = this.FL.length;
    this.PN = order ? 0 : Math.ceil(this.TF * Math.random());
    this.setOpacity = this.browser() =='IE' ? this.opacityIE : this.opacity;    
    this.Box1 = document.createElement("div");
    this.Box2 = document.createElement("div");
    this.foto1 = document.createElement("img");
    this.foto2 = document.createElement("img");
    
    this.Box1.style.position = "absolute";
    this.Box2.style.position = "absolute";

    this.Box1.appendChild(this.foto1);
    this.Box2.appendChild(this.foto2);
    
    this.Box.innerHTML = "";
    this.Box.appendChild(this.Box1);
    this.Box.appendChild(this.Box2);
    
    this.activeBox = this.Box1;
    this.setOpacity(this.Box2, 0);
    
    this.foto1.setAttribute('src', this.path + this.FL[this.PN]);
    this.setNext();    
}

function getAziendaSlide(){
    var azienda1 = new Array(), azienda2 = new Array();
    for(i=1; i<7; i++)azienda1.push("azienda" + i + ".jpg");
    for(i=7; i<15; i++)azienda2.push("azienda" + i + ".jpg");
                             
    var AS1 = new Slide("azienda1", "images/azienda", azienda1, 'from_0');
    AS1.foto1.setAttribute('src', 'images/azienda/azienda1.jpg');
    AS1.play();
    
    var AS2 = new Slide("azienda2", "images/azienda", azienda2);
    setTimeout(function(){AS2.play()}, 3000);
    ACTIVE_SLIDE = new Array(AS1, AS2);
}

function getCorsiSlide(){
    var fotoCorsi = new Array("corsi1.jpg", "corsi2.jpg", "corsi3.jpg", 
                    "1.jpg", "2.jpg", "3.jpg", "4.jpg",  "5.jpg", "6.jpg");
    var CS = new Slide("corsiSlide", "images/corsi", fotoCorsi);
    CS.play();
    ACTIVE_SLIDE = CS;    
}

function getPiantaSlide(){
    var Pianta1 = new Array("pianta1.jpg", "pianta2.jpg", "pianta3.jpg"), Pianta2 = new Array();
    var PS1 = new Slide("pianta1", "images/origini", Pianta1, 'from_0');
    PS1.foto1.setAttribute("src", "images/origini/pianta1.jpg");
    PS1.play();
    
    for(i=4; i<10; i++)Pianta2.push("pianta" + i + ".jpg");
    var PS2 = new Slide("pianta2", "images/origini", Pianta2);
    PS2.play();
    ACTIVE_SLIDE = new Array(PS1, PS2);
}

function getRaccoltaSlide(){
    var Raccolta1 = new Array("raccolta1.jpg", "raccolta2.jpg", "raccolta3.jpg");
    var RS1 = new Slide("raccolta1", "images/origini", Raccolta1);
    RS1.play();
    
    var Raccolta2 = new Array("raccolta4.jpg", "raccolta5.jpg", "raccolta6.jpg");
    var RS2 = new Slide("raccolta2", "images/origini", Raccolta2);
    RS2.play();
    ACTIVE_SLIDE = new Array(RS1, RS2);
}
