﻿/// <reference path="~/CMSTemplates/DMRASPX/Scripts/jquery-1.3.2.min-vsdoc.js"/>
//Webcam Image fetch
var rssPath = 'DMR.Modules/TTIEvents/RSS/RSS.aspx';
var redAlertInterval = 0;
var eventTimer = 0;

$(document).ready(function () {
    redAlertInterval = setInterval(fetchRedAlert, 60000);
    fetchRedAlert();
});

/* Get any red alerts. */
function fetchRedAlert() {
    $.getJSON(webPrefix + '/cache/RedAlert.js?' + new Date().getTime(), showRedAlert);
}

/* Display the red alert details. */
function showRedAlert(dto) {
    if (dto.HasRedAlert) {
        $('#redalertContent').show();
        $('#redAlertTime').text(dto.RedAlertTime + (dto.RedAlertTime ? ' ' : ''));
        $('#redAlertLocation').text(dto.RedAlertLocation).attr('href', dto.RedAlertUrl);
        $('#redAlertDescription').text(dto.RedAlertDescription);
    } else {
        $('#redalertContent').hide();
    }
}

/* Wait before updating events. */
function delayLoadEvents() {
    if (eventTimer) {
        clearTimeout(eventTimer);
    }
    eventTimer = setTimeout(loadEvents, 5);
}

/*Update the events widget*/
function loadEvents() {
    var bounds = decartamap.getBoundingBoxViewable();
    var service = new DMR.TTIEventService();
    service.GetBoundedEvents(bounds.minPosition.lat, bounds.minPosition.lon, bounds.maxPosition.lat, bounds.maxPosition.lon, 'All', createEvents, function (error, userContext, methodName) { $.log(error + '|' + userContext + '|' + methodName); });
    clearTimeout(redirectTimer);

}

/* Process event details from server into list. */
function createEvents(dto) {
    var eventLists = { Incident: [], LoadLimits: [], RoadWorks: [], SpecialEvent: [] };
    var eventText = { Incident: 'incidents', LoadLimits: 'load limits or closures',
        RoadWorks: 'road works', SpecialEvent: 'special events'
    };
    for (var i = 0; i < dto.length; i++) {
        eventLists[dto[i].Cause].push('<li><span class="time">' + dto[i].StartTime + '</span>' +
            (dto[i].StartTime ? ' ' : '') +
            '<a title="' + dto[i].Description + '" href="Regions/' + dto[i].RegionURLName +
            '/Road-Conditions.aspx?regionId=' + dto[i].RegionId + '&tab=' +
            dto[i].Cause.toLowerCase() + '">' + dto[i].RegionDisplayName + '</a><br />' +
            dto[i].Description + '</li>');
    }
    var pageSize = 4;
    for (var n in eventLists) {
        var list = eventLists[n];
        var cause = n.toLowerCase();
        var count = Math.min(list.length, 2 * pageSize);
        if (count == 0) {
            list.push('<li>No ' + eventText[n] + ' within your map selection</li>');
        }
        var html = '<div><ul class="alerts">' + list.slice(0, pageSize).join('') + '</ul>';
        if (count > pageSize) {
            html += '<div class="prevNext"><img src="' + imagePath + 'b-listpage-next.gif" ' +
                'alt="Next page" title="Next page" ' +
                'onclick="$(\'#' + cause + ' > div\').toggle()"/></div>'
        }
        if (count > 0) {
            html += '<p class="alerts-status">Displaying 1 - ' +
                Math.min(count, pageSize) + ' of ' + count +
                '&nbsp;&nbsp;<a title="' + n + ' - View All" href="Road-Conditions.aspx?tab=' +
                cause + '">View all</a>&nbsp;&nbsp;<a title="' + n + '- RSS" href="' +
                rssPath + '?regionid=0&amp;eventcause=' + n + '">' +
                '<img alt="RSS" title="RSS" src="' + imagePath + 'feed.gif"/></a></p>';
        }
        html += '</div>';
        if (count > pageSize) {
            html += '<div style="display: none;">' +
                '<ul class="alerts">' + list.slice(pageSize, 2 * pageSize).join('') + '</ul>' +
                '<div class="prevNext"><img src="' + imagePath + 'b-listpage-previous.gif" ' +
                'alt="Previous page" title="Previous page" ' +
                'onclick="$(\'#' + cause + ' > div\').toggle()"/></div>' +
                '<p class="alerts-status">Displaying ' + (pageSize + 1) + ' - ' + count + ' of ' + count +
                '&nbsp;&nbsp;<a title="' + n + ' - View All" href="Road-Conditions.aspx?tab=' +
                cause + '">View all</a>&nbsp;&nbsp;<a title="' + n + '- RSS" href="' +
                rssPath + '?regionid=0&amp;eventcause=' + n + '">' +
                '<img alt="RSS" title="RSS" src="' + imagePath + 'feed.gif"/></a></p></div>';
        }
        $('#' + cause).html(html);
    }
}
