﻿//****************************************************************
//----------------------------------------------------------------
// Fading Slide Show
// ver 1.0
// Author: Samuele Carassai
//----------------------------------------------------------------
//
// API (user):
//
//****************************************************************

JSCSlideShow = function (target) {
    // Private
    var _parent = this;
    
    var _intervalId = null;
    var _transitionTime = 25;
    var _waitingTime = 4000;
    var _images = new Array();
    var _running = true;
    var _current = 0;
    var _inTransaction = false;

    // Next function is <so_xfade> revisited    var _xfade = function () {	            _inTransaction = true;		_stopInterval();		        var nIndex = (_images[_current + 1]) ? _current + 1 : 0;        cOpacity = _images[_current].xOpacity;        nOpacity = _images[nIndex].xOpacity;        cOpacity -= .05;        nOpacity += .05;        _images[nIndex].style.display = 'block';        _images[_current].xOpacity = cOpacity;        _images[nIndex].xOpacity = nOpacity;        _setOpacity(_images[_current]);        _setOpacity(_images[nIndex]);        if(cOpacity <= 0) {	        _images[_current].style.display = 'none';	        _current = nIndex;	                    if (_parent.onChange != null) _parent.onChange();                        _inTransaction = false;	        _startInterval();        } else            window.setTimeout(_xfade, _transitionTime);                };
    var _setOpacity = function (obj) {	    if(obj.xOpacity > .99) {		    obj.xOpacity = .99;		    return;	    }	    obj.style.opacity = obj.xOpacity;	    obj.style.MozOpacity = obj.xOpacity;	    obj.style.filter = 'alpha(opacity=' + (obj.xOpacity * 100) + ')';    };        var _create = function (target) {
        if (!document.getElementById) return ;		if (typeof(target) == "string") target = document.getElementById(target);
		if (target == null) return ;
		
	    _images = target.getElementsByTagName('img');	    for (var index = 0; index < _images.length; index ++) {	        target.style.position = "relative";	        	        _images[index].style.display = "none";	        _images[index].style.position = "absolute";	        _images[index].style.top = 0;	        _images[index].style.left = 0;	        _images[index].xOpacity = 0;	    };	    	    if (_images.length > 0) {	        _images[0].style.display = 'block';	        _images[0].xOpacity = .99;            	        _startInterval();	    };    };
    
    var _stopInterval = function () {
        if (_intervalId != null) {
            window.clearInterval(_intervalId);
            _intervalId = null;
        };
    };
    
    var _startInterval = function () {
        if (_running) 
            _intervalId = window.setInterval(_xfade, _waitingTime);
    };
    
    // Publiche
    this.stop = function () {
        _running = false;
        _stopInterval();
    };
    
    this.start = function () {
        _running = true;
        _startInterval();
    };
    
    this.showImage = function (imageIndex) {
        if (imageIndex >= 0 && imageIndex < _images.length && _inTransaction == false) {
            for (var index = 0; index < _images.length; index ++) {
	            _images[index].style.display = "none";	            _images[index].style.position = "absolute";	            _images[index].xOpacity = 0;            };

	        _current = imageIndex;	        _images[_current].style.display = 'block';	        _images[_current].xOpacity = .99;	        _setOpacity(_images[_current]);	    };    };
    
    this.getCurrentImageIndex = function () {
        return _current;
    };
    
    // Costruttore
    _create(target);
};