﻿// ------------------------------------------------------- //
// CLASSE: MultiplePanel
// Rappresenta un elenco di elementi con passaggio sfumato //
// ------------------------------------------------------- //
function MultiplePanel(containerID)
{
    this.items = new Array();
    this.activeIndex = 0;
    
    if (!getObject(containerID))
    {
        return;
    }
    
    // Registra il contenitore
    this.containerID = containerID;
    this.containerObject = getObject(this.containerID);
   
    // Inizializza i singoli elementi
    this.initItems();
    
    // Imposta i valori di partenza dei pannelli
    this.startItems();
}

// Inizializza i singoli elementi del controllo
MultiplePanel.prototype.initItems = function()
{
    var items = getObjects(this.containerObject, 'div');

    // Cicla tra tutti gli elementi
    for (var i = 0; i < items.length; i++)
    {
        var item = items[i];
        
        // Aggiunge quelli trovati all'array
        if (item.className == 'panel') {
            item.id = this.containerID + '_' + i;
			this.items.push(new SinglePanel(this.containerID, item.id));
        }
    }
};

MultiplePanel.prototype.startItems = function()
{
    // Nasconde tutti gli elementi tranne il primo
    if (this.items.length > 1)
    {
        var firstItem = this.activeItem = this.items[0];
        var position = getOffset(firstItem.controlObject);
        var positionX = position['left'];
        var positionY = position['top'];
        var width = firstItem.controlObject.offsetWidth;
        var height = firstItem.controlObject.offsetHeight;
        
        // Cicla attraverso tutti gli elementi, assegna le stesse proprietà e li nasconde
        for (var i = 1; i < this.items.length; i++)
        {
            var item = this.items[i];

            item.controlObject.style.left = positionX;
            item.controlObject.style.top = positionY;
            item.controlObject.style.width = width;
            item.controlObject.style.height = height;
            item.controlObject.style.display = 'none';
        }
        
        // Avvia il timer
        this.switchInterval = window.setInterval('MultiplePanels[\'' + this.containerID + '\'].hideCurrentPanel()', 10000);
    }
};

// Nasconde il pannello attuale
MultiplePanel.prototype.hideCurrentPanel = function()
{
    // Nasconde il pannello attivo
    this.items[this.activeIndex].hide();
};

MultiplePanel.prototype.showNextPanel = function()
{
    // Sceglie l'elemento successivo ripartendo se necessario dall'inizio dell'array
    var nextIndex = (this.activeIndex + 1 < this.items.length ? this.activeIndex + 1 : 0);
    
    // Mostra il pannello successivo
    this.items[nextIndex].show();
    
    // Registra il nuovo indice
    this.activeIndex = nextIndex;
};

// ------------------------------- //
// CLASSE: SinglePanel             //
// Rappresenta un singolo pannello //
// ------------------------------- //
function SinglePanel(containerID, itemID)
{
    this.containerID = containerID;
	this.itemID = itemID;
	this.initItem();
}

// Inizializza l'elemento
SinglePanel.prototype.initItem = function()
{
	this.controlObject = getObject(this.itemID);
	this.controlObject.containerID = this.containerID;
	this.controlObject.unselectable = true;
	
	// Memorizza il collegamento per impostare il click sull'oggetto
	var anchor = getObjects(this.controlObject, 'a')[0];
	if (anchor)
	{
	    this.controlObject.style.cursor = 'pointer';
	    this.controlObject.title = 'Clicca per leggere il servizio';
	    this.controlObject.onclick = function(e)
	    {
	        window.location.href = anchor.href;
	    };
	}
};

// Nasconde l'elemento
SinglePanel.prototype.hide = function()
{
	this.slideOut(10);
	this.isOpened = false;
};
	
// Visualizza l'elemento
SinglePanel.prototype.show = function()
{
	if (this.isOpened)
	{
		return;
	}
	
	this.controlObject.style.display = '';
	this.controlObject.style.opacity = 0;
	
	this.slideIn(0);
	this.isOpened = true;
};

// Timer che visualizza progressivamente il pannello
SinglePanel.prototype.slideIn = function(opacity) {
	if (opacity < 10) 
	{
		opacity += 1;
		
		this.controlObject.style.opacity = opacity / 10;
		this.controlObject.style.filter = 'alpha(opacity=' + opacity * 10 + ')';
		this.slideInInterval = window.setTimeout('MultiplePanels[\'' + this.containerID + '\'].items[' + MultiplePanels[this.containerID]. activeIndex + '].slideIn(' + opacity + ')', 0);
	}
	else
	{
		this.stopSlideIn();
	}
};

// Timer che nasconde progressivamente il pannello
SinglePanel.prototype.slideOut = function(opacity) {
	if (opacity > 0) 
	{
		opacity -= 1;
		
		this.controlObject.style.opacity = opacity / 10;
		this.controlObject.style.filter = 'alpha(opacity=' + opacity * 10 + ')';
		this.slideOutInterval = window.setTimeout('MultiplePanels[\'' + this.containerID + '\'].items[' + MultiplePanels[this.containerID]. activeIndex + '].slideOut(' + opacity + ')', 0);
	}
	else
	{
		this.stopSlideOut();
	}
};

// Ferma la visualizzazione
SinglePanel.prototype.stopSlideIn = function() {
	clearInterval(this.slideInInterval);
	this.controlObject.style.display = 'block';
	this.controlObject.style.opacity = 1;
	this.controlObject.style.filter = 'alpha(opacity=100)';
};

// Ferma la sparizione
SinglePanel.prototype.stopSlideOut = function() {
	clearInterval(this.slideOutInterval);
	this.controlObject.style.display = 'none';
	this.controlObject.style.opacity = 0;
	this.controlObject.style.filter = 'alpha(opacity=0)';
	
	// Richiama la visualizzazione del pannello successivo
	MultiplePanels[this.containerID].showNextPanel();
};