
var SPARTA;

if (!SPARTA) {
    SPARTA = {};
}

SPARTA.wgImageSlideShow = function (spec) {

    // the object
    var that = {};
    
    // private fields
    var doms = spec.doms;
    var time1 = spec.time1;
    var time2 = spec.time2;
    
    var current = 0;
    
    for(var i=0; i<doms.length; i++)
        doms[i].opacity = 0.0;
    
    function setOpacity (dom)
    {
        if (dom.opacity > .99) 
            dom.opacity = .99;
 
        dom.style.opacity = dom.opacity;
        dom.xOpacity = dom.opacity;
        dom.style.MozOpacity = dom.opacity;
        dom.style.filter = "alpha(opacity=" + (dom.opacity*100) + ")";
    }
    
    function fadeHandler() 
    {
        var other = current + 1;
        if (other >= doms.length) other = 0;
        
        var opacity1 = doms[current].opacity;
        var opacity2 = doms[other].opacity;
        
        opacity1 -= 0.05;
        opacity2 += 0.05;
        
        doms[current].opacity = opacity1;
        doms[other].opacity = opacity2;
        doms[other].style.display = "block";   
        
        setOpacity(doms[current]);
        setOpacity(doms[other]);
        
        if (opacity1 < 0.01)
        {
            doms[current].style.display = "none";   
            doms[current].opacity = 0.0;
            doms[other].opacity = 1.0;
            
            // switch over
            current = other;
            if (that.onSwitchOver != null) {
                that.onSwitchOver(doms[current]);
            }
                    
            setTimeout(fadeHandler, time1);
        }
        else
        {
            setTimeout(fadeHandler, 50);
        }
    }
    
    that.onSwitchOver = null;
    
    that.randomize = function() {
        current = Math.floor(doms.length * Math.random());
    };
    
    that.start = function() {
        doms[current].opacity = 1.0;
        doms[current].style.display = "block";   
        
        if (that.onSwitchOver != null) {
            that.onSwitchOver(doms[current]);
        }
        
        setTimeout(fadeHandler, time1);
    };
    
    return that;

}