// Gestione namespace

var Namespace = {};
var eDott = {};
var titleObj = null;
var lastStart = new Date();

Namespace.ensure = function(name)
{
    if (!name || !name.length)
        return null;

    var levels = name.split(".");
    var currentNS = eDott;

    for (var i=(levels[0] == "eDott") ? 1 : 0; i<levels.length; ++i) {
        currentNS[levels[i]] = currentNS[levels[i]] || {};
        currentNS = currentNS[levels[i]];
    }

    return currentNS;
};
// Inizializzazione controllo per questa sezione
Namespace.ensure('eDott.WebUI');

eDott.WebUI['eDottTV'] = {
    k_CueAreaMaxWidthDelta: 40,
    k_CueAreaExpansionLengthMs: 1500,
    k_CueAreaExpansionFrequencyMs: 100,
    k_CueAreaExpansionDueTimeMs: 0,
    k_ExpandWidthFxExpMultiplier: 10,
    
    m_currentCueSection: null,
    
    // Gestisce gli eventi scatenati dal player Flash.
    
    HandlePlayerAction: function(action, data)
    {
        var elContainer = document.getElementById('idVideoIndice');
			//alert(action + ' ' + data);
        switch (action)
        {
        		case 'chapterStart':
        		{	        			
	    			
        			//inizio capitolo
        			strNumero = data
        			strListItemName = 'video_' + strNumero;
        			oldStrListItemName = '';
        			if (titleObj)
	        		{
	        			//torno al colore standard
	        			titleObj.className = '';	
	        			oldStrListItemName = titleObj.id;	
	        		}
	        			        			
	        		titleObj = document.getElementById(strListItemName);
	        		if (titleObj)
        			{
        				//imposto il colore per evidenziare
        				titleObj.className = 'sel';      				
        			}	
	        			
	        		//Segna il click dell'utente
	        		/*C'è un bug in firefox, per cui viene chiamato sue volte l'evento ChapterStart
	        		Per risolverlo, faccio il log solo se sue eventi sullo stesso capitolo vengono invocati almeno
	        		a un secondo uno dall'altro
	        		*/
	        		var adesso = new Date();
	        		var gap = adesso.getTime() - lastStart.getTime();
	    			if (oldStrListItemName!=strListItemName || (oldStrListItemName==strListItemName && gap>1000))
	    				lastStart = adesso;
	        		
					break;	
        		
        		}	
            case 'cueSectionStart':
            {                          
    			
                if (this.m_currentCueSection)
                    this.m_currentCueSection.style.display = 'none';

                this.m_currentCueSection = document.getElementById(data);
                
                if (this.m_currentCueSection)
                {
                    this.m_currentCueSection.style.display = 'block';

                    // Espansione dell'area cue

                    this.ExpandCueArea(true);
                }
                else
                    this.ExpandCueArea(false);
                
                
                break;
            }

            case 'cueSectionEnd':
            case 'stop':
            {
                // Contrazione dell'area cue

                if (this.m_currentCueSection)
                {
                    this.ExpandCueArea(false);
                }
                   
                break;
            }
        }
    },
    
    m_Expanding: true,
    m_ExpansionStartedAt: 0,
    
    ExpandCueArea: function(bExpand)
    {

        // Setup della procedura

        this.m_Expanding = bExpand;
        this.m_ExpansionStartedAt = new Date().getTime();
        
        // Avvio della procedura di espansione/contrazione
        
        var _this = this;
        window.setTimeout(function()
        {
            _this.ExpandCueAreaRecursive();
        }, this.k_CueAreaExpansionDueTimeMs);
    },
    
    ExpandCueAreaRecursive: function()
    {
        var bExpand = this.m_Expanding;

        // Recupera gli elementi con cui interagire

        var elSlideContainer = document.getElementById('idVideoIndice');
        
        // Calcola il tempo trascorso dall'inizio dell'effetto
        
        var iElapsedMs = (new Date().getTime() - this.m_ExpansionStartedAt);

        if (iElapsedMs >= this.k_CueAreaExpansionLengthMs)
        {
            // La procedura è terminata per eccesso di duetime

            if (this.m_Expanding)
            {
                //elSlideContainer.className = 'CueContainer';
            }
            else
            {
                //elSlideContainer.className = 'AbstractContainer';
                
                if (this.m_currentCueSection)
                {
                    this.m_currentCueSection.style.display = 'none';
                    this.m_currentCueSection = null;
                }
            }
        }
        else
        {
            // Calcolo del quoziente del tempo relativo all'effetto di espansione
            
            var fRelativeTimePosition = iElapsedMs / this.k_CueAreaExpansionLengthMs;
            
            // Recupera la posizione del contenitore di slide

            var position = Position.get(elSlideContainer);
            
            // Imposta la posizione e la larghezza del contenitore delle cue section

            Position.set(elCueContainer, position.left, position.top + 5);
            elCueContainer.style.width = '' + this.ExpandWidthFx(position, fRelativeTimePosition) + 'px';
            elCueContainer.style.display = 'block';

            // Continua la procedura di espansione/contrazione
            
            var _this = this;
            window.setTimeout(function()
            {
                _this.ExpandCueAreaRecursive();
            }, this.k_CueAreaExpansionFrequencyMs);
        }
    },
    
    ExpandWidthFx: function(oSlideContainerPosition, fRelativeTimePosition)
    {
        var iMaxWidth = (oSlideContainerPosition.width + this.k_CueAreaMaxWidthDelta);
        var iWidthShift = Math.floor(iMaxWidth * Math.exp(-fRelativeTimePosition * this.k_ExpandWidthFxExpMultiplier));

        return this.m_Expanding ? iMaxWidth - iWidthShift : iWidthShift;
    }
};