﻿var timestepDefault = { selectedForecastDayIndex: 0, timestepAllDayData: {} };

$.timestep =
 {
     //function to view timesteps for selected forecastday
     showDetail: function(options) {
         if (lists.jsonData == null) return;

         $.extend(timestepDefault, { selectedForecastDayIndex: options.selectedForecastDayIndex }, { timestepAllDayData: lists.jsonData[options.selectedForecastDayIndex] });

         var timestepString = '';
         var timestepSingleRecord = '';
         var dayTimesteps = timestepDefault.timestepAllDayData.length;

         for (var i = 0; i < dayTimesteps; i++) {
             timestepSingleRecord = timestepDefault.timestepAllDayData[i];
             timestepString += (i == (dayTimesteps - 1)) ? "<div class='timestepRowNoBorder'>" : "<div class='timestepRow'>";
             timestepString += "<div class='timestepDate'><span class='timestepDateLabel'>" + getTimeLabel() + "</span><span class='timestepDateInterval'>" + timestepSingleRecord.ForecastHour.findHourInterval($.timestep.timeInterval()) + "</span></div>";
             timestepString += "<div class='timestepSkySymbol'><img src=" + getSkySymbolPath($.timestep.symbolDetail(timestepSingleRecord)) + " ></img></div>";
             timestepString += "<div class='" + $.timestep.temperatureClass(timestepSingleRecord) + "'><span class='timestepTemperatureValue'>" + Math.round(timestepSingleRecord.Temperature) + "</span><span>°</span><span class='timestepTemperatureUnit'></span></div>";
             timestepString += "<div class='timestepWindSymbol'><img src=" + getWindSymbolPath(timestepSingleRecord.Winddirection, timestepSingleRecord.Windspeed) + "></img></div>";
             timestepString += "<div class='timestepWindSpeed'><span class='timestepWindSpeedValue'>" + timestepSingleRecord.Windspeed + "</span><span class='timestepWindSpeedUnit'>" + 'm/s' + "</span></div>";
             timestepString += "<div class='timestepPrecipitation'><span class='timestepPrecipitationText'>" + variables.precLabel + "</span><span class='timestepPrecipitationValue'>" + $.timestep.precipitationDetail(timestepSingleRecord) + "</span><span class='timestepPrecipitationUnit'>" + 'mm' + "</span></div>";
             timestepString += "<span class='timestepText'>" + getSkySymbolText($.timestep.symbolDetail(timestepSingleRecord)) + " / " + getWindSymbolText(timestepSingleRecord.Winddirection, timestepSingleRecord.Windspeed) + "</span>";
             timestepString += "<div class='clear'></div></div><div class='clear'></div>";
         }
         $("#timesteps").html("<div class='timestepsCollection'>" + timestepString + "</div>");
     },

     symbolDetail: function(timestep) {
         switch (variables.controlName) {
             case forecasttype.OverviewPage:
             case forecasttype.Overview:
             case forecasttype.Weekend:
                 return timestep.Symbol;
                 break;
             case forecasttype.Hourly:
             case forecasttype.Chart:
             case forecasttype.Table:
                 if (timestepDefault.selectedForecastDayIndex > 2) return timestep.Symbol03;
                 else return timestep.Symbol01;
                 break;
         }
     },

     precipitationDetail: function(timestep) {
         switch (variables.controlName) {
             case forecasttype.OverviewPage:
             case forecasttype.Overview:
             case forecasttype.Weekend:
                 return timestep.Precipitation.toFixed(1);
                 break;
             case forecasttype.Hourly:
             case forecasttype.Chart:
             case forecasttype.Table:
                 if (timestepDefault.selectedForecastDayIndex > 2) return timestep.Prec03;
                 else return timestep.Prec01.toFixed(1);
                 break;
         }
     },

     timeInterval: function() {
         switch (variables.controlName) {
             case forecasttype.OverviewPage:
             case forecasttype.Overview:
             case forecasttype.Weekend:
                 return 6;
                 break;
             case forecasttype.Hourly:
             case forecasttype.Chart:
             case forecasttype.Table:
                 if (timestepDefault.selectedForecastDayIndex <= 2) return 1;
                 else return 3;
                 break;
         }
     },

     temperatureClass: function(timestep) {
         return Math.round(timestep.Temperature) <= 0 ? "timestepMinusTemperature" : "timestepTemperature";
     }
 }

 String.prototype.findHourInterval = function(interval) {
     var curHour = parseInt(this);
     var curTime = '';
     var nextTime = '';
     switch (variables.currentCulture) {
         case "en":
             curTime = ((curHour % 12) == 0 ? 12 : (curHour % 12)) + ((curHour < 12) ? "AM" : "PM");
             nextTime = ((curHour + interval) >= 24) ? (curHour + interval) % 24 : (curHour + interval);
             nextTime = ((nextTime % 12) == 0 ? 12 : (nextTime % 12)) + ((nextTime < 12) ? "AM" : "PM");
             return curTime + " - " + nextTime;
             break;
         default:
             curTime = ((curHour < 10) ? "0" : "") + curHour;
             nextTime = (((curHour + interval) < 10) ? "0" : "") + (curHour + interval);
             nextTime = (nextTime >= 24) ? "0" + (nextTime % 24) : nextTime;
             return curTime + " - " + nextTime;
             break;
     }
 }

function getTimeLabel() {
    switch (variables.currentCulture) {
        case "en":
            return '';
            break;
        default:
            return variables.timeLabel;
            break;
    }
}