﻿// ------------------------------------------------------- //
// CLASSE: Accordion
// Rappresenta un elenco di elementi mutualmente esclusivi //
// ------------------------------------------------------- //
function Accordion(containerID)
{
    this.items = new Array();
    this.activeItem = null;
    
    if (!getObject(containerID))
    {
        return;
    }
    
    // Registra il contenitore
    this.containerID = containerID;
    this.containerObject = getObject(this.containerID);
   
    // Inizializza i singoli elementi
    this.initItems();
}

// Inizializza i singoli elementi del controllo
Accordion.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 == 'item') {
            item.id = this.containerID + '_' + i;
			this.items[item.id] = new AccordionItem(this.containerID, item.id);
        }
    }
}

// -------------------------------------- //
// CLASSE: AccordionItem                  //
// Rappresenta un elemento dell'accordion //
// -------------------------------------- //
function AccordionItem(containerID, itemID)
{
    this.containerID = containerID;
	this.itemID = itemID;
	this.initItem();
}

// Inizializza l'elemento
AccordionItem.prototype.initItem = function()
{
	this.controlObject = getObject(this.itemID);
	this.controlObject.containerID = this.containerID;
	this.controlObject.unselectable = true;
	this.controlObject.onmouseover = AccordionItemEvents.prototype.item_onmouseover;
	this.controlObject.style.cursor = 'default';
	
	// Cerca il pannello da visualizzare
	if (getObjects(this.controlObject, 'p').length == 1)
	{   
		this.panelObject = getObjects(this.controlObject, 'p')[0];
		this.panelObject.id = this.itemID + '_panel';
		this.panelID = this.panelObject.id;
		this.panelHeight = Math.floor(this.panelObject.offsetHeight - ((10 * this.panelObject.offsetHeight) / 100));
	}
	
	// Nasconde l'elemento
	this.hide();
};

// Nasconde l'elemento
AccordionItem.prototype.hide = function()
{
	this.slideOut(this.panelHeight, 10);
	this.isOpened = false;
};
	
// Visualizza l'elemento
AccordionItem.prototype.show = function()
{
	if (this.isOpened)
	{
		return;
	}
	
	if (Accordions[this.containerID].activeItem != null)
	{
	    Accordions[this.containerID].activeItem.hide();
	}
	
	this.panelObject.style.display = '';
	this.panelObject.style.height = '0px';
	this.panelObject.style.opacity = 0;
	this.clipInterval = Math.ceil(this.panelHeight / 10);
	this.opacityInterval = Math.ceil(10 / this.clipInterval);
	
	this.slideIn(0, 0);
	this.isOpened = true;
	
	// Imposta questo come oggetto attivo
	Accordions[this.containerID].activeItem = this;
};

// Timer che visualizza progressivamente il pannello
AccordionItem.prototype.slideIn = function(clipY, opacity) {
	if (clipY < this.panelHeight - this.clipInterval) 
	{
		clipY += this.clipInterval;
		opacity += this.opacityInterval;
		
		this.panelObject.style.height = clipY + 'px';
		this.panelObject.style.opacity = opacity / 10;
		this.panelObject.style.filter = 'alpha(opacity=' + opacity * 10 + ')';
		this.slideInInterval = window.setTimeout('Accordions[\'' + this.containerID + '\'].items[\'' + this.itemID + '\'].slideIn(' + clipY + ', ' + opacity + ')', 0);
	}
	else
	{
		this.stopSlideIn();
	}
};

// Timer che nasconde progressivamente il pannello
AccordionItem.prototype.slideOut = function(clipY, opacity) {
	if (clipY > 0 - this.clipInterval) 
	{
		clipY -= this.clipInterval;
		opacity -= this.opacityInterval;
		
		this.panelObject.style.height = clipY + 'px';
		this.panelObject.style.opacity = opacity / 10;
		this.panelObject.style.filter = 'alpha(opacity=' + opacity * 10 + ')';
		this.slideOutInterval = window.setTimeout('Accordions[\'' + this.containerID + '\'].items[\'' + this.itemID + '\'].slideOut(' + clipY + ', ' + opacity + ')', 0);
	}
	else
	{
		this.stopSlideOut();
	}
};

// Ferma la visualizzazione
AccordionItem.prototype.stopSlideIn = function() {
	clearInterval(this.slideInInterval);
	this.panelObject.style.display = 'block';
	this.panelObject.style.height = 'auto';
	this.panelObject.style.opacity = 1;
	this.panelObject.style.filter = 'alpha(opacity=100)';
};

// Ferma la sparizione
AccordionItem.prototype.stopSlideOut = function() {
	clearInterval(this.slideOutInterval);
	this.panelObject.style.display = 'none';
	this.panelObject.style.height = '0px';
	this.panelObject.style.opacity = 0;
	this.panelObject.style.filter = 'alpha(opacity=0)';
};

// ------------------------------------------------- //
// FUNZIONE: AccordionItemEvents                     //
// Rappresenta gli eventi degli elementi dell'elenco //
// ------------------------------------------------- //
function AccordionItemEvents() { };

// Gestisce il passaggio su un elemento
AccordionItemEvents.prototype.item_onmouseover = function(e)
{
	e = getEvent(e);
	
	Accordions[this.containerID].items[this.id].show();
}