{% extends "layout.html" %}

{% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/ts3_viewer.css') }}"> {% endblock %}

{% block title %}Dota Noobs{% endblock %}

{% block content %}
{% set teamspeak_data = get_teamspeak_window() %}
{% set latest_news = get_latest_news() %}
<div class="uk-grid">

    <div id="events_large" class="uk-width-1-2 uk-visible-large uk-panel uk-panel-space">
        {% include "events_widget.html" %}
    </div>

    <div id="teamspeak" class="uk-width-large-1-2 uk-width-medium-1-1 uk-panel uk-panel-box uk-text-center">
        <a href="#" data-uk-modal="{target: '#tsviewer'}"><div class="uk-badge uk-panel-badge uk-badge-success"><i class="uk-icon-user"></i> Users</div></a>
        <h1 class="uk-panel-title">Teamspeak</h1>
        <div class="uk-grid uk-margin-bottom">
            <div class="uk-width-1-3 uk-panel">
                <h3>Users<br/><span class="ts_num">{{ ts3_current_clients() }}</span></h3>
            </div>
            <div class="uk-width-1-3 uk-panel">
                <h3 class="uk-text-success">Online</h3>
            </div>
            <div class="uk-width-1-3 uk-panel">
                <h3>Countries<br/><span class="ts_num">{{ ts3_countries_active(teamspeak_data) }}</span></h3>
            </div>
        </div>

        <a class="uk-button uk-button-success uk-width-1-1" href="ts3server://voice.dotanoobs.com"><i class="uk-icon-microphone"></i> Connect</a>
        <a class="uk-button uk-width-1-1 uk-margin" href="{{ url_for('teamspeak') }}"><i class="uk-icon-globe"></i> Stats</a>
        <a class="uk-button uk-width-1-1" href="http://www.teamspeak.com/?page=downloads"><i class="uk-icon-download"></i> Download</a>

        <div id="tsviewer" class="uk-modal uk-text-left">
            <div class="uk-modal-dialog">
                <a class="uk-modal-close uk-close"></a>
                {{ ts3_viewer() | safe }}
            </div>
        </div>
    </div>

    <div id="events_small" class="uk-width-1-1 uk-hidden-large uk-panel uk-panel-space">
        {% include "events_widget.html" %}
    </div>
    
    <div id="streams" class="uk-width-1-1 uk-text-center uk-panel">
    </div>

    <div class="uk-width-1-1 uk-panel uk-panel-header uk-panel-space">
        <h1 class="uk-panel-title">News and Announcements</h1>
        {% for news in latest_news %}
            <article class="uk-article dn-news-article">
            <h4 class="uk-article-title" title="{{ news['title'] }}"><a href="{{ news['url'] }}">{{ news['title'] }}</a></h4>
                <p class="uk-article-meta">{{ news['date'] }}</p>
                <p>{{ news['text'] | shorten }}</p>
            </article>
        {% endfor %}
    </div>

</div>
{# ERRORS WHEN CACHING WTF? DISABLE TEMPORARILY #}
{% cache 60*5 %}
{% endcache %}
{% endblock %}

{% block pagescripts %}
<script>
$(document).ready(function() {
        if ($('#events_large > dl').length < 1) {
            var msg = "<h2>Events</h2><em>No events planned for the near future. Suggest one on the forum!</em>"
            $('#events_small').append(msg);
            $('#events_large').append(msg);
        }

	// Add the twitch streams
	var stream_url = "https://api.twitch.tv/kraken/streams/";
        // var channels = ["dotanoobs", "bearhugdota", "kreejaffakree", "prettypenguins", "shaneomad"];
        var channels = {{ streamers['twitch']|tojson|safe }};
	for (var idx in channels) {
		$.getJSON(stream_url+channels[idx]+"?callback=?", function(data) {
                    if (data.stream) {
                        var $a = $("<a href='"+data.stream.channel.url+"'></a>");
                        var $strm = $("<div class='dn-streamer uk-text-success uk-panel uk-panel-box' id='"+data.stream.channel.name+"'></div>");

                        $strm.append("<p class='uk-text-bold'>" + data.stream.channel.display_name + "</p>");
                        $strm.append("<img src='" + data.stream.preview.small + "' />");
                        $strm.append("<p><i class='uk-icon-male'></i> "+data.stream.viewers+"</p>");

                        $a.append($strm);
                        $("#streams").prepend($a);

                    } else {
                        $.getJSON(data._links.channel+"?callback=?", function(data) {
                            var $a = $("<a href='"+data.url+"'></a>");
                            var $strm = $("<div class='dn-streamer-offline uk-text-success uk-panel uk-panel-box' id='"+data.name+"'></div>");

                            $strm.append("<p class='uk-text-bold'>" + data.display_name + "</p>");
                            $strm.append("<img src='" + data.logo + "' />");
                            $strm.append("<p class='dn-offline'>Offline</p>");

                            $a.append($strm);
                            $("#streams").append($a);
                        });
                    }
		});
        }
        
	// Add the hitbox streams
	var stream_url = "http://api.hitbox.tv/media/live/";
        //var channels = ["Bandita", "Gibb3d"];
        var channels = {{ streamers['hitbox']|tojson|safe }};
	for (var idx in channels) {
		$.getJSON(stream_url+channels[idx], function(data) {
                    var livestream = data.livestream[0];
                    var $a = $("<a href='"+livestream.channel.channel_link+"'></a>");
                    if (livestream.media_is_live == "1") {
                        var $strm = $("<div class='dn-streamer uk-text-success uk-panel uk-panel-box' id='"+livestream.media_name+"'></div>");
                        $strm.append("<p class='uk-text-bold'>" + livestream.media_user_name + "</p>");
                        $strm.append("<img src='http://edge.sf.hitbox.tv" + livestream.media_thumbnail + "' />");
                        $strm.append("<p><i class='uk-icon-male'></i> " + livestream.media_views + "</p>");

                        $a.append($strm);
                        $("#streams").prepend($a);

                    } else {
                        var $strm = $("<div class='dn-streamer-offline uk-text-success uk-panel uk-panel-box' id='"+livestream.media_name+"'></div>");
                        $strm.append("<p class='uk-text-bold'>" + livestream.media_user_name + "</p>");
                        $strm.append("<img src='http://edge.sf.hitbox.tv" + livestream.channel.user_logo + "' />");
                        $strm.append("<p class='dn-offline'>Offline</p>");

                        $a.append($strm);
                        $("#streams").append($a);
                    }
		});
        }

        $("#streams").on({
            mouseover: function() {
                $(this).addClass('dn-streamer-hover');
            },
            mouseleave: function() {
                $(this).removeClass('dn-streamer-hover');
            }
        }, "div");
});
</script>
{% endblock %}
