﻿var alertUrl = null;


function getAlertData()
{
    if (location.isUs)
    {
        alertUrl = WebRoot + "/DataService/GetAlert.ashx?zip=" + locat.zipCode;
        var dataReq = new httpReq(alertUrl, parseAlert);
        dataReq.load();
    }
    else
        plugin.content.findName("WeatherAlertIcon").Opacity = 0;
}


var alertData = null;
var alertTextElements = ["WeatherAlertDesc", "WeatherAlertText"];
var alertLeftTop = {};
var alertFirstTime = true;
function parseAlert(adata)
{
    alertData = adata
    
    if (null == alertData && typeof(this.getJsonObj) == "function")
        alertData = this.getJsonObj();
    
    try
    {
        if (null != alertData)
        {
            
            if (alertFirstTime)
            {
                alertLeftTop = captureLeftTop(alertTextElements);
                alertFirstTime = false;
            }
            if (alertData.alertCount > 0)
            {
                numWeatherAlerts = alertData.alertCount;
                plugin.content.findName("WeatherAlertIcon").Opacity = 100;
                if (alertData.alertList.length > 0)
                {
                    plugin.content.findName("WeatherAlertDesc").Text = alertData.alertList[0].description;
                    plugin.content.findName("WeatherAlertText").Text = alertData.alertList[0].message;
                    if (alertData.alertCount > 1)
                    {
                        plugin.content.findName("WeatherAlertNavigation").Visibility = "Visible";
                        plugin.content.findName("WeatherAlertCounter").Text = "1 of " + alertData.alertCount.toString();
                    }
                    else
                        plugin.content.findName("WeatherAlertNavigation").Visibility = "Collapsed";
                } 
                leftAlignText(alertTextElements, alertLeftTop);
                if (hasNewAlerts(alertData))
                    ShowWeatherAlertPopup(null, null);
            }
            else
            {
                clearAlerts();                
            }
        }
        else
        {
            clearAlerts();                
        }
    }
    catch(error)
    {
        writeLine(error.name + ": " + error.description);
    
    }
}

function clearAlerts()
{
    numWeatherAlerts = 0;
    plugin.content.findName("WeatherAlertIcon").Opacity = 0;
    acknowledgedAlertIds = null;
}

function navUpdateAlert(alertIndex)
{
    plugin.content.findName("WeatherAlertDesc").Text = alertData.alertList[alertIndex].description;
    plugin.content.findName("WeatherAlertText").Text = alertData.alertList[alertIndex].message;
    //centerText(alertTextElements, alertLeftTop);
}


var acknowledgedAlertIds = null;
function acknowledgeAlert()
{
    acknowledgedAlertIds = null;
    if (null!= alertData && alertData.alertCount > 0)
    {
        acknowledgedAlertIds = new Array();
        for (var i = 0; i < alertData.alertList.length; i++)
        {
            acknowledgedAlertIds[i] = alertData.alertList[i].id;
        }
    }
}

function hasNewAlerts(ad)
{
    var has = true;
    if (null!= ad && ad.alertCount > 0 && null != acknowledgedAlertIds && acknowledgedAlertIds.length > 0)
    {
        for (var i = 0; i < ad.alertList.length; i++)
        {
            var isIn = false;
            for (var j = 0; j < acknowledgedAlertIds.length; j++)
            {
                if (ad.alertList[i].id == acknowledgedAlertIds[j])
                {
                    isIn = true;
                    break;
                }
            }
            
            if (!isIn) 
            {
                has = true;
                break; 
            }
            else 
                has = false;           
        }
    }
    
    return has;
}