﻿var delay = 200;
var refresh = 30000;

function LoadOfficialTweets() {
    StartLoading($("#officialNotices .content"), $("#officialNotices .loading"));

    $.getJSON("/OfficialTweets",
        function(data) {
            setTimeout(function() {
                $("#officialNotices .content").processTemplate(data);
                $("#officialNotices .lastupdaetd").text((new Date()).toLocaleTimeString());

                for (var i = 0; i < data.results.length; i++) {
                    AddPoint(data.results[i].id, data.results[i].text, true);
                }
                StopLoading($("#officialNotices .content"), $("#officialNotices .loading"));

                setTimeout(function() { LoadCommunityTweets(true); }, delay);
            }, delay);
        });
    }

function SelectTweet(sender, id) {
    for (var i = 0; i < allMarkers.length; i++) {
        if (allMarkers[i].id == id) {
            allMarkers[i].openInfoWindow($(sender).outerHTML());
            break;
        }
    }
}

function LoadCommunityTweets(poll) {
    
    StartLoading($("#communityNotices .content"), $("#communityNotices .loading"));

    $.getJSON("/CommunityTweets",
        function(data) {
            setTimeout(function() {
                $("#communityNotices .content").processTemplate(data);
                $("#communityNotices .lastupdaetd").text((new Date()).toLocaleTimeString());

                for (var i = 0; i < data.results.length; i++) {
                    AddPoint(data.results[i].id, data.results[i].text, false);
                }

                StopLoading($("#communityNotices .content"), $("#communityNotices .loading"));
                if (poll) setTimeout(LoadOfficialTweets, refresh);
            }, delay);
        });
}

function StartLoading(element, loader) {
    //$(element).fadeOut(delay).hide();
    $(element).slideUp(delay).hide();
    $(loader).fadeIn(delay);
}

function StopLoading(element, loader) {
    //$(element).fadeIn(delay);
    $(element).slideDown(delay);
    $(loader).fadeOut(delay);
}


$(function() {
    $("#officialNotices .content, #communityNotices .content").setTemplateURL("/Content/Tweet.htm");
});