﻿/// <reference path="global.js" />
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                              BEGIN WEATHER
//

function RadarCB_onclick(e) {
    e = getEvent(e);
    if (document.getElementById("RadarCB").src == getBaseURL() + "Images/Radar0.gif") {
        //ShowRadar
        if (historicalPlaybackActive() == true) { AlertMessage("Weather is not available for historic playback."); return; }
        clearWeather();
        document.getElementById("RadarCB").src = getBaseURL() + "Images/Radar1.gif"
        weatherType = "lowaltradarcontours";
        setTimeout(initWeather, 1);
    } else {
        clearWeather();
        document.getElementById("RadarCB").src = getBaseURL() + "Images/Radar0.gif";
        weatherType = null;
    }
}
function CloudCB_onclick(e) {
    e = getEvent(e);
    if (document.getElementById("CloudCB").src == getBaseURL() + "Images/Cloud0.gif") {
        //ShowClouds
        if (historicalPlaybackActive() == true) { AlertMessage("Weather is not available for historic playback."); return; }
        clearWeather();
        document.getElementById("CloudCB").src = getBaseURL() + "Images/Cloud1.gif";
        weatherType = "irsatellitegrid";
        setTimeout(initWeather, 1);
    } else {
        clearWeather();
        document.getElementById("CloudCB").src = getBaseURL() + "Images/Cloud0.gif";
        weatherType = null;
    }
}
function LoopWeatherCB_onclick(e) {
    e = getEvent(e);
    cleanWeather();
    if (document.getElementById("LoopWeatherCB").src == getBaseURL() + "Images/Loop0.gif") {//if loop is NOT checked
        document.getElementById("LoopWeatherCB").src = getBaseURL() + "Images/Loop1.gif"; //check it.
        isWeatherLooping = true;
    } else {
        document.getElementById("LoopWeatherCB").src = getBaseURL() + "Images/Loop0.gif"; //uncheck it.
        isWeatherLooping = false;
    }
    setTimeout(initWeather, 1);
}
function initWeather() {
    if (weatherType == null) { return; }
    isWeather = true;
    ShowItemBlock("WeatherLegend", (weatherType == "lowaltradarcontours"));
    ShowItemBlock("weatherLegendDiv", true);
    weatherTimes = getValidTimes(true);
    if (document.getElementById("LoopWeatherCB").src == getBaseURL() + "Images/Loop0.gif") {//if loop is NOT checked
        initCurrentWeather(); //Display recent.
    } else {
        isWeatherLooping = true;
        initWeatherLoop(); //Loop
    }
    showWeatherFrame(weatherTimes.length - 1);
    currentFrame = weatherTimes.length - 1;
    weatherUpdateInterval = setInterval(updateWeatherFrames, 60 * 1000);
}
function initCurrentWeather() {
    currentTime = weatherTimes[weatherTimes.length - 1];
    weatherTimes = [currentTime];
    createWeatherFrame(weatherTimes.length - 1);
}
function initWeatherLoop() {
    for (var i = 0; i < weatherTimes.length; i++) {
        createWeatherFrame(i);
    }
    initWeatherLoopInterval();
}
function initWeatherLoopInterval() {
    if (WeatherisLooping()) {
        clearWeatherLoopInterval();
        weatherFrameInterval = setInterval(showNextWeatherFrame, .75 * 1000);
    }
}
function clearWeatherLoopInterval() {
    clearInterval(weatherFrameInterval);
}
function clearWeather() {
    cleanWeather();
    ShowItemBlock("weatherLegendDiv", false);
    document.getElementById("CloudCB").src = getBaseURL() + "Images/Cloud0.gif";
    document.getElementById("RadarCB").src = getBaseURL() + "Images/Radar0.gif";
    isWeather = false;
    isWeatherLooping = false;
}
function cleanWeather() {
    clearInterval(weatherFrameInterval);
    clearInterval(weatherUpdateInterval);
    weatherFrameInterval = null;
    weatherUpdateInterval = null;
    removeWeatherLayers();
    weatherTimes = new Array();
    currentFrame = null;
    newestFrame = null;
}
function removeWeatherLayers() {
    for (var i = 0; i < weatherTimes.length; i++) {
        removeWeatherLayer(i);
    }
    weatherTimes = null;
}
function removeWeatherLayer(index) {
    RemoveTileLayer(weatherType + getSafeName(weatherTimes[index]));
    DeleteCSSRule("weatherCSS", ".msve_" + weatherType + getSafeName(weatherTimes[index]) + "_tile"); 
}
function showNextWeatherFrame() {
    hideWeatherFrame(currentFrame);
    currentFrame = (currentFrame == (weatherTimes.length - 1)) ? 0 : currentFrame + 1;
    showWeatherFrame(currentFrame);
}
function updateWeatherFrames() {
    var newTimes = getValidTimes(false);
    if (getMostRecentTime() != newTimes[(weatherTimes.length - 1)]) {
        var oldestTime = (newestFrame == (weatherTimes.length - 1)) ? 0 : newestFrame + 1;
        removeWeatherLayer(oldestTime);
        weatherTimes[oldestTime] = newTimes[newTimes.length - 1];
        createWeatherFrame(oldestTime);
        showWeatherFrame(currentFrame);
    }
}
function createWeatherFrame(index) {
    var f = getTilePath0; 
    switch (index) {
        case 0: f = getTilePath0; break;
        case 1: f = getTilePath1; break;
        case 2: f = getTilePath2; break;
        case 3: f = getTilePath3; break;
        case 4: f = getTilePath4; break;
        case 5: f = getTilePath5; break;
        case 6: f = getTilePath6; break;
        case 7: f = getTilePath7; break;
    }
    AddTileLayer(weatherType + getSafeName(weatherTimes[index]), "", 6, Rectangle(75, -180, 1, -20), 1, 19, f, 0.00, 8, true);
    hideWeatherFrame(index);
    weatherTimesDisplay[index] = formatWeatherTime(weatherTimes[index]);
    newestFrame = index;
}
function showWeatherFrame(index) {
    ModifyCSSRule("weatherCSS", ".msve_" + weatherType + getSafeName(weatherTimes[index]) + "_tile", "filter:alpha(opacity=40) !important; opacity:0.40 !important;");
    document.getElementById("WeatherTime").innerHTML = weatherTimesDisplay[index];
}
function hideWeatherFrame(index) {
    ModifyCSSRule("weatherCSS", ".msve_" + weatherType + getSafeName(weatherTimes[index]) + "_tile", "filter:alpha(opacity=00) !important; opacity:0.00 !important;");
}
function getMostRecentTime() { return weatherTimes[newestFrame]; }

var weatherTileCount = 0;

var returnedValue = null;

function getValidTimes(init) {
    if (init) { weatherTileCount = 0; }

    returnedValue = DAL.GetValidWeatherTimes(weatherType, init, m_CurrentUser, weatherTileCount, isWeatherLooping);

    return returnedValue.value; 
}

function getTilePath0(tileContext) {
    weatherTileCount = weatherTileCount + 1;
    return "http://swarmcache" + nextWeatherServerNumber() + ".wdtinc.com/swarmweb/tile/" + weatherType + "/" + weatherTimes[0] + "/" + tileContext.ZoomLevel + "/" + tileContext.XPos + "/" + tileContext.YPos + ".png";
}
function getTilePath1(tileContext) {
    weatherTileCount = weatherTileCount + 1;
    return "http://swarmcache" + nextWeatherServerNumber() + ".wdtinc.com/swarmweb/tile/" + weatherType + "/" + weatherTimes[1] + "/" + tileContext.ZoomLevel + "/" + tileContext.XPos + "/" + tileContext.YPos + ".png";
}
function getTilePath2(tileContext) {
    weatherTileCount = weatherTileCount + 1;
    return "http://swarmcache" + nextWeatherServerNumber() + ".wdtinc.com/swarmweb/tile/" + weatherType + "/" + weatherTimes[2] + "/" + tileContext.ZoomLevel + "/" + tileContext.XPos + "/" + tileContext.YPos + ".png";
}
function getTilePath3(tileContext) {
    weatherTileCount = weatherTileCount + 1;
    return "http://swarmcache" + nextWeatherServerNumber() + ".wdtinc.com/swarmweb/tile/" + weatherType + "/" + weatherTimes[3] + "/" + tileContext.ZoomLevel + "/" + tileContext.XPos + "/" + tileContext.YPos + ".png";
}
function getTilePath4(tileContext) {
    weatherTileCount = weatherTileCount + 1;
    return "http://swarmcache" + nextWeatherServerNumber() + ".wdtinc.com/swarmweb/tile/" + weatherType + "/" + weatherTimes[4] + "/" + tileContext.ZoomLevel + "/" + tileContext.XPos + "/" + tileContext.YPos + ".png";
}
function getTilePath5(tileContext) {
    weatherTileCount = weatherTileCount + 1;
    return "http://swarmcache" + nextWeatherServerNumber() + ".wdtinc.com/swarmweb/tile/" + weatherType + "/" + weatherTimes[5] + "/" + tileContext.ZoomLevel + "/" + tileContext.XPos + "/" + tileContext.YPos + ".png";
}
function getTilePath6(tileContext) {
    weatherTileCount = weatherTileCount + 1;
    return "http://swarmcache" + nextWeatherServerNumber() + ".wdtinc.com/swarmweb/tile/" + weatherType + "/" + weatherTimes[6] + "/" + tileContext.ZoomLevel + "/" + tileContext.XPos + "/" + tileContext.YPos + ".png";
}
function getTilePath7(tileContext) {
    weatherTileCount = weatherTileCount + 1;
    return "http://swarmcache" + nextWeatherServerNumber() + ".wdtinc.com/swarmweb/tile/" + weatherType + "/" + weatherTimes[7] + "/" + tileContext.ZoomLevel + "/" + tileContext.XPos + "/" + tileContext.YPos + ".png";
}

function getSafeName(name) {
    return name.replace(/:/g, "-");
}
function nextWeatherServerNumber() {
    if (currentWeatherServerNumber > 4) {
        currentWeatherServerNumber = 0;
    } else {
        currentWeatherServerNumber = currentWeatherServerNumber + 1;
    }
    return currentWeatherServerNumber;
}
function formatWeatherTime(time) {
    time = time.replace("T", "-");
    time = Date.parseDate(time, "%y-%m-%d-%H:%M:%S");
    time.setHours(time.getHours() - GMTOffset);
    return time.print("%m/%d/%Y %I:%M:%S %p");
}
function WeatherisLooping() {
    return (isWeather && isWeatherLooping);
}
//
//                                                              END WEATHER 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
