(function() {
    var layout = $('#serverList'),
        l = function(selector) {
            return layout.find(selector);
        },
        list = l('.list'),
        servers = (function() {
            var path = '../log/server.json',
                content = Fso.read(path),
                list;

            if (!content) {
                return;
            }

            list = new Function('return ' + content)();

            return list;
        }()) || {
            "服务器1": "116.31.99.244:8008",
            "服务器2": "119.63.47.244:8008",
            "服务器3": "139.196.23.239:408",
            "服务器4": "115.29.171.41:408",
            "服务器5": "182.92.172.67:508",
            "服务器6": "115.29.171.41:480",
            "服务器7": "139.196.23.239:804",
            "服务器8": "112.124.53.91"
        },
        template = list.html();

    var debugServers = $.map(getFields({
        ip: 2,
        name: 1
    }, Fso.read('debug_v2.pur5'), /ip\:([^:]+):(\S+)/g), function(item, index) {
        servers[item.name] = item.ip;
    });

    list.empty();

    if (!template) {
        $('div#serverList .list').css({
            'margin-right': 20
        }).html('您安装的“瑞星杀毒软件”改变了系统设置，导致开店软件不能正常运行。<br />解决办法：卸载“瑞星杀毒软件”，重启电脑。 ');
        return;
    }

    $(document).on('click', '[data-hostName]', function() {
        var $item = $(this),
            host = $item.attr('data-host'),
            hostName = $item.attr('data-hostName');

        Dwd.host = host;
        $('.splash_version').append(' - ' + hostName);
        $('#main .loginHostName').text(hostName);
        Login.layoutIn();
        window.Login && clickTimer && clearTimeout(clickTimer);
    });

    function showSpeedPicture(name, speed) {
        $('[data-hostName="' + name + '"]').attr('speed', 'on').find('.speed').addClass(speed);
    }



    var tempArr = [];
    $.each(servers, function(name, host) {
        tempArr.push(
            template
            //.replace(/src=""/g, 'src="http://$host/images/regbg2.gif?$timer"')
            .replace(/\$hostName/g, name)
            .replace(/\$host/g, host)
            .replace(/\$timer/g, new Date().getTime()));
    });

    list.html(tempArr.join(''));

    var startTime = new Date().getTime(),
        clickTimer,
        firstIp;

    for (var name in servers) {
        (function(name) {
            var ajax = $.ajax({
                url: 'http://' + servers[name] + '/ServerXXX.html',
                dataType: 'json',
                cache: false,
                timeout: 3000,
                success: function(data, status, jqXHR) {
                    var timerDiff = new Date().getTime() - startTime,
                        speed;

                    if (!firstIp) {
                        firstIp = name;
                        Fso.write('../log/server.json', jqXHR.responseText);

                        clickTimer = setTimeout(function() {
                            $('[data-hostName="' + firstIp + '"]').trigger('click');
                        }, 5000);
                    }

                    if (timerDiff <= 500) {
                        speed = 'fast';
                    } else if (timerDiff > 500 && timerDiff < 2000) {
                        speed = 'normal';
                    } else {
                        this.error();
                        return;
                    }

                    showSpeedPicture(name, speed);
                },
                error: function() {
                    showSpeedPicture(name, 'slow');
                    ajax.abort();
                }
            });
        }(name));
    }
}());
