<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>FreeGo</title>
    <style>
        * {
            font-family: 'Avenir Next', Avenir, 'Helvetica Neue', Helvetica, 'Franklin Gothic Medium', 'Franklin Gothic', 'ITC Franklin Gothic', Arial, sans-serif;
            margin: 0;
            padding: 0;
            color: #7d7d7d;
            -webkit-user-select: none;
        }
        a {
            color: #d55;
        }
        body {
            background: #ebebeb;
        }
        header {
            background: #fff;
            padding: 60px 0 50px 0;
        }
        header .title {
            text-align: center;
            color: #d55;
            font-size: 40px;
            margin-bottom: 20px;
        }
        header p {
            text-align: center;
            font-size: 16px;
            line-height: 25px;
        }
        .container {
            padding: 20px 10px;
        }
        .container .content {
            background: #fff;
            border-radius: 4px;
            padding: 20px;
            font-size: 14px;
            line-height: 20px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
        }
        table {
            border-collapse: collapse;
            width: 100%;
            border: 1px solid #7d7d7d;
        }
        table td, table th {
            padding: 10px;
        }
        thead {
            background: #7d7d7d;
            text-align: left;
        }
        thead th {
            color: #fff;
        }
        tbody tr {
            transition: background .3s;
            cursor: pointer;
        }
        tbody tr:active, tbody tr.active {
            background: #02B4A9;
        }
        tbody td {
            transition: color .3s;
        }
        tbody tr:active td, tbody tr.active td {
            color: #fff;
        }
        .clear-btn {
            height: 30px;
            margin: 10px 0;
            padding: 4px;
            background-color: #d55;
        }
        .clear-btn a {
            display: block;
            text-decoration: none;
            text-align: center;
            color: #fff;
            line-height: 30px;
        }
    </style>
</head>
<body>
<header>
    <h1 class="title">FreeGo</h1>
    <p>
        Proxy for Redirecting Requests<br/>
        View it on <a href="https://github.com/surunzi/freego">GitHub</a>
    </p>
</header>
<div class="container">
    <div class="content">
        {{#if target}}
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                    </tr>
                </thead>
                <tbody>
                    {{#each target.root.target}}
                        <tr id="target_{{value}}" ontouchstart>
                            <td>{{name}}</td>
                        </tr>
                    {{/each}}
                </tbody>
            </table>
            <div class="clear-btn"><a href="javascript:;" id="clear-cookie">Clear</a></div>
            <script>
                var targets = JSON.parse('{{{targets}}}')
                var len = {{target.root.target.length}};

                for (var i = 0; i < len; i++) init(i);

                document.getElementById('clear-cookie').onclick = function () {
                    var hostArray = location.host.split('.')
                    var cookie = 'free_go_proxy=;path={{target.path}};expires=' + getExpires(-1);

                    document.cookie = cookie

                    while (hostArray.length > 1) {
                        var currentDomain = hostArray.join('.');
                        document.cookie = cookie + ';domain=' + currentDomain;
                        hostArray.shift();
                    }

                    location.reload();
                }

                function init(num)
                {
                    var isEnable = false;
                    var value = targets[num].value;

                    if (document.cookie.indexOf('free_go_proxy=' + value) > -1) isEnable = true;

                    var target = document.getElementById('target_' + value);
                    if (isEnable) target.className += ' active';

                    var cookie = 'free_go_proxy=' + value +';{{#if target.domain}}domain={{target.domain}};{{/if}}path={{target.path}};expires=';

                    target.onclick = function ()
                    {
                        cookie += getExpires(isEnable ? -1 : 30);
                        document.cookie = cookie;
                        {{#if target.autoJump}}
                            location.replace('{{target.autoJumpUrl}}');
                        {{else}}
                            location.reload();
                        {{/if}}
                    };
                }

                function getExpires(date)
                {
                    var expires = new Date();
                    expires.setMilliseconds(expires.getMilliseconds() + date * 864e+5);

                    return expires.toUTCString();
                }
            </script>
        {{else}}
            Sorry, this url doesn't have a configuration yet:(
        {{/if}}
    </div>
</div>
</body>
</html>
