﻿
var config = {
    darkback: null,
    area: null,

    width: '',
    height: '',
    Configuracao_Velocidade: null,
    Configuracao_Servidor: null,

    setScreenSize: function (w, h) {
        config.width = w;
        config.height = h;
    },

    activate: function () {

        trace("setting config servidores ... ");

        var t = document.createElement("text");
        var s = " <div id='WebCasters_Config_Back' "
            + "style=\"background-image:url('/images/darkback.png');background-repeat:repeat;"
            + "position: absolute;top:0px;left:0px;width:" + config.width + ";height:" + config.height + ";display:none;z-index:1000\">"
            + "<div id='WebCasters_Config_Area' style='background-color:white;width:300px;height:300px;margin-top:10px;margin-left:auto;margin-right:auto;'></div></div>";
        t.innerHTML = s;
        document.body.appendChild(t);

        indirectRun(config._activate2);
    },

    _activate2: function () {
        config.darkback = document.getElementById('WebCasters_Config_Back');
        config.area = document.getElementById('WebCasters_Config_Area');

        // initialize configuration controls
        var tb = new StringBuilder();

        tb.append("<table width=280 border=0 style='font-family:sans-serif'>");
        tb.append("<tr><td>&nbsp;</td><td><b>Configura&ccedil;&otilde;es</b></td></tr>");
        tb.append("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>");

        tb.append(config.contentVelocidades());
        tb.append(config.contentServidor());

        tb.append("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>");
        tb.append("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>");
        tb.append("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>");
        tb.append("<tr><td>&nbsp;</td><td><input type='button' value='Fechar' id='Config_CloseBtn'/></td></tr>");
        tb.append("</table>");
        config.area.innerHTML = tb.toString();
        // 

        indirectRun(config.addEvents);
    },

    addEvents: function () {

        //configuração de velocidades
        config.addEventsVelocidades();
        config.addEventsServidores();

        simpleAddEvent(document.getElementById('Config_CloseBtn'), 'click', config.close);

        trace("setting config servidores ... OK");
    },

    // CONFIGURAÇÃO DE VELOCIDADES
    contentVelocidades: function () {
        return "<tr><td>Velocidade:</td><td><select id='Config_Velocidades'></select></td></tr>"
    },

    addEventsVelocidades: function () {

        var selectObj = config.Configuracao_Velocidade
    	    = document.getElementById('Config_Velocidades');

        while (selectObj.options.length > 0) {
            selectObj.remove(0);
        }

        var newOpt = document.createElement('option');
        newOpt.text = 'Automática';
        newOpt.value = '';

        try { selectObj.add(newOpt, null); /* standards compliant */ }
        catch (ex) { selectObj.add(newOpt); /* IE only */ }


        if (visualizador.Transmissao.Velocidade1 > 0) {

            var newOpt1 = document.createElement('option');
            newOpt1.text = Math.floor(visualizador.Transmissao.Velocidade1 / 1000) + ' Kbps';
            newOpt1.value = visualizador.Transmissao.Velocidade1 + '';

            try { selectObj.add(newOpt1, null); }
            catch (ex) { selectObj.add(newOpt1); }

        }

        if (visualizador.Transmissao.Velocidade2 > 0) {

            var newOpt2 = document.createElement('option');
            newOpt2.text = Math.floor(visualizador.Transmissao.Velocidade2 / 1000) + ' Kbps';
            newOpt2.value = visualizador.Transmissao.Velocidade2 + '';

            try { selectObj.add(newOpt2, null); }
            catch (ex) { selectObj.add(newOpt2); }


        }

        if (visualizador.Transmissao.Velocidade3 > 0) {

            var newOpt3 = document.createElement('option');
            newOpt3.text = Math.floor(visualizador.Transmissao.Velocidade3 / 1000) + ' Kbps';
            newOpt3.value = visualizador.Transmissao.Velocidade3 + '';

            try { selectObj.add(newOpt3, null); }
            catch (ex) { selectObj.add(newOpt3); }

        }

        config.Configuracao_Velocidade.selectedIndex = streamManager.idxVelocidadeSelecionada;

        simpleAddEvent(config.Configuracao_Velocidade, 'change',
            function () {
                trace('config.onConfiguracaoVelocidadeChange()');
                config.onConfiguracaoVelocidadeChange();
            }
        );

    },

    onConfiguracaoVelocidadeChange: function () {
        if (config.Configuracao_Velocidade == null) {
            return;
        }

        streamManager.idxVelocidadeSelecionada = config.Configuracao_Velocidade.selectedIndex;
        streamManager.velocidadeSelecionada = config.Configuracao_Velocidade.value;

        config.retryStream(streamManager.GetStream);


    },



    // SELEÇÃO MANUAL DE SERVIDORES
    contentServidor: function () {
        return "<tr><td>Servidor:</td><td><select id='Config_Servidores'></select></td></tr>";
    },

    addEventsServidores: function () {
        var selectObj = config.Configuracao_Servidor
    	    = document.getElementById('Config_Servidores');

        var servidores = streamManager.GetListaServidores();

        if (!servidores || !config.Configuracao_Servidor) {
            trace("WARNING: lista de servidores indisponível.");
            indirectRun(config.addEventsServidores);
            return;
        }

        // limpa o componente
        while (selectObj.options.length > 0) {
            selectObj.remove(0);
        }

        var opt0 = document.createElement('option');
        opt0.text = "Automático";
        opt0.value = 'a';
        try { selectObj.add(opt0, null); }
        catch (ex) { selectObj.add(opt0); }

        // popula o componente
        for (var i = 0; i < servidores.rows.length; i++) {
            var opt = document.createElement('option');
            opt.text = servidores.rows[i].Nome;
            opt.value = i;

            try { selectObj.add(opt, null); }
            catch (ex) { selectObj.add(opt); }
        }

        config.updateServidor();

        simpleAddEvent(config.Configuracao_Servidor, 'change',
            function () {
                trace('config.onConfiguracaoServidorChange()');
                config.onConfiguracaoServidorChange();
            }
        );

    },

    updateServidor: function () {
        for (var i = 0; i < config.Configuracao_Servidor.options.length; i++) {
            if (config.Configuracao_Servidor.options[i].value == streamManager.idxServidor) {
                config.Configuracao_Servidor.selectedIndex = i;
                return;
            }

        }

    },

    onConfiguracaoServidorChange: function () {
        if (config.Configuracao_Servidor == null) {
            return;
        }

        var id = config.Configuracao_Servidor.options[config.Configuracao_Servidor.selectedIndex].value;

        if (id == 'a') {
            trace("setting streamManager.idxServidor to AUTO");
            config.retryStream(function () {
                streamManager.SetServerById(0);
            });
            return;
        }


        trace("setting streamManager.idxServidor to " + id);

        config.retryStream(function () {
            streamManager.SetServerById(id);
        });
    },



    show: function () {
        config.updateServidor();

        wcplayer.visible(false);

        config.darkback.style.display = "block";
    },

    close: function () {
        wcplayer.visible(true);
        config.darkback.style.display = "none";

    },


    retryStream: function (fn) {
        //recarrega o stream com o parâmetro da velocidade             
        var current = wcplayer.current();
        indirectRun(fn);
        wcplayer.offset = current;
        // coloca o offset para voltar mais ou menos onde o cara
        // está assitindo
    }


};


