/* These are the date objects to track the date */
var todayDate = new Date ();
var stopDate = '';  // Set in siteDetails.php
var twoLineStopDate = '';  // Set in siteDetails.php
var dailyDate = new Date ();
var weeklyChartNum = 0;
var weeklyChartCount = 12;
var yearlyChartNum = 0;
var yearlyChartCount = 5;
var monthlyChartNum = 0;
var monthlyChartCount = 5;

/* Button visible values */
var btnDayNextVisible = false;
var btnDayPreviousVisible = true;
var btnWeekNextVisible = false;
var btnWeekPreviousVisible = true;
var btnYearNextVisible = false;
var btnYearPreviousVisible = true;
var btnMonthNextVisible = false;
var btnMonthPreviousVisible = true;

/* Current chart type being updated */
var currChartUpdating = '';

/* If we are using the 2 line daily chart */
var using2Line = false;

/* Keep track of the timeouts */
var dayTimeout = null;
var weekTimeout = null;

/* The name of the current generation element */
var currGenElemName = '';

/**
 * Update the daily or weekly chart
 * This will set itself up to run again in 5 minutes for the daily chart and
 * 60 minutes for the weekly chart
*/
function updateChart (chart, prefix) {
    // Determine the url we need
    var addr = '';
    if (chart == 'DAILY') {
        var c = 'DAILY';
        if (using2Line) {
            c = 'DAILY2LINE';
        }
        
        addr = prefix +
              "/data/getChartData.php?siteIdName=" +
              siteIdName +
              "&chartType=" + c + "&chartDay=" +
              formatDate (dailyDate.getFullYear (),
                          dailyDate.getMonth (),
                          dailyDate.getDate ()) +
              "&xmlOut=1";

        dailyChart.setXMLUrl (addr);
        
        dailyTimeout = setTimeout ("updateChart('DAILY', '" + prefix + "')", 300000);
    }
    else if (chart == 'WEEKLY') {
        addr = prefix +
              "/data/getChartData.php?siteIdName=" +
              siteIdName +
              "&chartType=WEEKLY&chartNumber=" +
              weeklyChartNum +
              "&xmlOut=1";

        weeklyChart.setXMLUrl (addr);
        weeklyTimeout = setTimeout ("updateChart('WEEKLY', '" + prefix + "')", 3600000);
    }
    else if (chart == 'YEARLY') {
        addr = prefix +
               "/data/getChartData.php?siteIdName=" +
               siteIdName +
               "&chartType=YEARLY&chartNumber=" +
               yearlyChartNum +
               "&xmlOut=1";

        yearlyChart.setXMLUrl (addr);
    }
    else if (chart == 'MONTHLY') {
        addr = prefix +
               "/data/getChartData.php?siteIdName=" +
               siteIdName +
               "&chartType=COMBINED&chartNumber=" +
               monthlyChartNum +
               "&xmlOut=1";

        monthlyChart.setXMLUrl (addr);
    }
}


/*
Update the current output o-meter
This sets itself up to run again in 45 seconds
*/
function updateCurrentOutput (prefix, site, elem) {
    var addr = prefix + '/data/generateCurrentOutput.php';
    if (site != '') {
        addr += '?siteIdName=' + site;
    }

    currGenElemName = '#' + elem;
    
    $.ajax ({
                url: addr,
                method: 'post',
                success: function (data) {
                    $(currGenElemName).attr ('src', data);
                    setTimeout ("updateCurrentOutput ('" +
                                prefix +
                                "', '" +
                                site +
                                "', '" +
                                elem +
                                "')",
                                45000);  // Run again in 45 seconds
                }
    });
}


/*
Change the timeframe of a day or week chart
*/
function changeChartTimeframe (prefix, type, goForward) {
    // Cancel the automatic update and set which chart we are updating
    if (type == 'DAILY') {
        clearTimeout (dayTimeout);
        currChartUpdating = 'DAILY';
    }
    else if (type == 'WEEKLY') {
        clearTimeout (weekTimeout);
        currChartUpdating = 'WEEKLY';
    }
    else if (type == 'YEARLY') {
        currChartUpdating = 'YEARLY';
    }
    else if (type == 'MONTHLY') {
        currChartUpdating = 'MONTHLY';
    }
    
    // Change the date timeframe
    if (type == 'DAILY') {
        if (goForward) {
            dailyDate.setDate (dailyDate.getDate () + 1);
        }
        else {
            dailyDate.setDate (dailyDate.getDate () - 1);
        }
    }
    else if (type == 'WEEKLY') {
        if (goForward) {
            weeklyChartNum -= 1;
        }
        else {
            weeklyChartNum += 1;
        }
    }
    else if (type == 'YEARLY') {
        if (goForward) {
            yearlyChartNum -= 1;
        }
        else {
            yearlyChartNum += 1;
        }
    }
    else if (type == 'MONTHLY') {
        if (goForward) {
            monthlyChartNum -= 1;
        }
        else {
            monthlyChartNum += 1;
        }
    }
    
    // Check for buttons that need disabled
    if (type == 'DAILY') {
        // Are we changing the date to today?
        if (dailyDate.getMonth () == todayDate.getMonth () &&
            dailyDate.getDate () == todayDate.getDate ()) {
            if (btnDayNextVisible != false) {
                $('#btnDayNext').hide ();
                btnDayNextVisible = false;
            }
        }
        else {
            if (btnDayNextVisible != true) {
                $('#btnDayNext').show ();
                btnDayNextVisible = true;
            }
        }
        
        // Are we changing the date to the stop date?
        if (dailyDate.getMonth () == stopDate.getMonth () &&
            dailyDate.getDate () == stopDate.getDate ()) {
            if (btnDayPreviousVisible != false) {
                $('#btnDayPrevious').hide ();
                btnDayPreviousVisible = false;
            }
        }
        else {
            if (btnDayPreviousVisible != true) {
                $('#btnDayPrevious').show ();
                btnDayPreviousVisible = true;
            }
        }
        
        // Are we using 2 line and going to the 2 line stop date?
        if (dailyDate >= twoLineStopDate) {
            $('#dvUse2Line').show ();
        }
        else {
            $('#cbxUse2Line').checked = false;
            $('#dvUse2Line').hide ();
            $('#dvDailyConsumptionExplination').hide ();
            $('#dvDailyRegularExplination').show ();
            using2Line = false;
        }
    }
    else if (type == 'WEEKLY') {
        // Are we changing the date to this week?
        if (weeklyChartNum == 0) {
            if (btnWeekNextVisible != false) {
                $('#btnWeekNext').hide ();
                btnWeekNextVisible = false;
            }
        }
        else {
            if (btnWeekNextVisible != true) {
                $('#btnWeekNext').show ();
                btnWeekNextVisible = true;
            }
        }
        
        // Are we changing the date to the stop week?
        if (weeklyChartNum == (weeklyChartCount -1)) {
            if (btnWeekPreviousVisible != false) {
                $('#btnWeekPrevious').hide ();
                btnWeekPreviousVisible = false;
            }
        }
        else {
            if (btnWeekPreviousVisible != true) {
                $('#btnWeekPrevious').show ();
                btnWeekPreviousVisible = true;
            }
        }
    }
    else if (type == 'YEARLY') {
        // Are we changing the date to this year?
        if (yearlyChartNum == 0) {
            if (btnYearNextVisible != false) {
                $('#btnYearNext').hide ();
                btnYearNextVisible = false;
            }
        }
        else {
            if (btnYearNextVisible != true) {
                $('#btnYearNext').show ();
                btnYearNextVisible = true;
            }
        }

        // Are we changing the date to the stop year?
        if (yearlyChartNum == (yearlyChartCount - 1)) {
            if (btnYearPreviousVisible != false) {
                $('#btnYearPrevious').hide ();
                btnYearPreviousVisible = false;
            }
        }
        else {
            if (btnYearPreviousVisible != true) {
                $('#btnYearPrevious').show ();
                btnYearPreviousVisible = true;
            }
        }
    }
    else if (type == 'MONTHLY') {
        // Are we changing the date to this year?
        if (monthlyChartNum == 0) {
            if (btnMonthNextVisible != false) {
                $('#btnMonthNext').hide ();
                btnMonthNextVisible = false;
            }
        }
        else {
            if (btnMonthNextVisible != true) {
                $('#btnMonthNext').show ();
                btnMonthNextVisible = true;
            }
        }

        // Are we changing the date to the stop year?
        if (monthlyChartNum == (monthlyChartCount - 1)) {
            if (btnMonthPreviousVisible != false) {
                $('#btnMonthPrevious').hide ();
                btnMonthPreviousVisible = false;
            }
        }
        else {
            if (btnMonthPreviousVisible != true) {
                $('#btnMonthPrevious').show ();
                btnMonthPreviousVisible = true;
            }
        }
    }
    
    // Call update chart
    updateChart (type, prefix);
}


function resetChartChangeSetup () {
    // Reset day and button stuff
    dailyDate = new Date ();
    weeklyDate = new Date ();
    weeklyChartNum = 0;
    yearlyChartNum = 0;
    monthlyChartNum = 0;
    $('#btnDayPrevious').show ();
    $('#btnDayNext').hide ();
    $('#btnWeekPrevious').show ();
    $('#btnWeekNext').hide ();
    $('#btnYearPrevious').show ();
    $('#btnYearNext').hide ();
    $('#btnMonthPrevious').show ();
    $('#btnMonthNext').hide ();
    btnDayNextVisible = false;
    btnDayPreviousVisible = true;
    btnWeekNextVisible = false;
    btnWeekPreviousVisible = true;
    btnYearNextVisible = false;
    btnYearPreviousVisible = true;
    btnMonthNextVisible = false;
    btnMonthPreviousVisible = true;
    $('#cbxUse2Line').attr ('checked', false);
    using2Line = false;
}


function formatDate (year, month, day) {
    month = month + 1;
    return year + "-" + month + "-" + day;
}
