// Gestione namespace

var Namespace = {};
var eDott = {};
var titleObj = null;

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)
    {
        //alert(action + ' ' + data);
        var elContainer = document.getElementById('idVideoIndice');
			//alert(action + ' ' + data);
        switch (action)
        {
        		case 'chapterStart':
        		{
        			//inizio capitolo
        			strNumero = String(data).substring(String(data).length,String(data).length-1);
        			strListItemName = 'titolo' + strNumero;
        			/*
        			if (!(titleObj) || (titleObj && titleObj.id!=strListItemName))
        			{
        			*/	
	        			if (titleObj)
	        			{
	        				//torno al colore standard
	        				titleObj.style.backgroundColor = '#F7FAFA';	
	        				titleObj.style.backgroundImage = 'url(/images/freccia_piccola.gif)';
	        			}
	        			
	        			titleObj = document.getElementById(strListItemName);
	        			if (titleObj)
	        			{
	        				//imposto il colore per evidenziare
	        				titleObj.style.backgroundColor = '#D7E7E7';
	        				titleObj.style.backgroundImage = 'url(/images/freccia_piccola_over.gif)';
	        			}	
					/*
					}
					*/
					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');
        var elCueContainer = document.getElementById('RADAR_CueSectionContainer');
        
        // 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
            {
                elCueContainer.style.display = 'none';
                //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;
    }
};