var currDetailTab = 'Daily';
var dailyChartLoaded = 0;
var weeklyChartLoaded = 0;
var yearlyChartLoaded = 0;
var monthlyChartLoaded = 0;

/**
 * Set an element as visible or not visible based on if it is currently
 * not visible or visible respectively.
 *
 * @param elemID The name of the element to flip visibility on
 */
function flipElemVisible (elemID) {
    if ($(elemID).visible ()) {
        $(elemID).hide ();
    }
    else {
        $(elemID).show ();
    }
}


function goToPage () {
    if ($('siteDetails').getValue () != 'NoGo') {
        location.href = 'index.php?siteDetails=' + $('siteDetails').getValue ();
    }
}


function switchDetailTab (newTab) {
    // Switch the tabs
    $('dv' + currDetailTab).hide ();
    $('dv' + newTab).show ();
    
    // See if we need to reset the date variables and buttons for previous charts
    if (currDetailTab == 'Daily' || currDetailTab == 'Weekly') {
        resetChartChangeSetup ();
    }
    
    // Store new tab as current tab
    currDetailTab = newTab;
}


function showImage (divID, imgID, path) {
    //$(imgID).src = "images/sites/" + path;
    //$(divID).show ();
    window.open ('images/sites/' + path);
}


/**
 *
 */
function mouseOverSwap (name, fadeOut) {
    if (name == 'links') {
        if (fadeOut) {
            $('imgLinks').src = "images/links-over.jpg";
        }
        else {
            $('imgLinks').src = "images/links.jpg";
        }
    }
    else if (name == 'presentations') {
        if (fadeOut) {
            $('imgPresentations').src = "images/presentations-over.jpg";
        }
        else {
            $('imgPresentations').src = "images/presentations.jpg";
        }
    }
    else if (name == 'meterprogram') {
        if (fadeOut) {
            $('imgMeter').src = "images/meterprogram-over.jpg";
        }
        else {
            $('imgMeter').src = "images/meterprogram.jpg";
        }
    }
    
    return true;
}

/**
 *
 */
function twoLineChange (prefix, name) {
    if ($(name).checked) {
        using2Line = true;
        $('dvDailyRegularExplination').addClassName ('hidden');
        $('dvDailyConsumptionExplination').removeClassName ('hidden');
    }
    else {
        using2Line = false;
        $('dvDailyConsumptionExplination').addClassName ('hidden');
        $('dvDailyRegularExplination').removeClassName ('hidden');
    }
    
    clearTimeout (dayTimeout);
    updateChart (prefix, 'DAILY');
}


/*
Update the weather conditions
This will set itself up to run again in 4 minutes
*/
function updateWeather (prefix, picElem, tempElem) {
    // Do the AJAX request
    new Ajax.Request (prefix + '/data/generateWeather.php',
                      {
                        method: 'post',
                        onComplete: function (transport) {
                            var vals = transport.responseText.split ("\n");
                            
                            $(picElem).src = vals[1];
                            $(tempElem).update (vals[0] + " F");
                            
                            setTimeout ("updateWeather('" +
                                        prefix +
                                        "', '" +
                                        picElem +
                                        "', '" +
                                        tempElem +
                                        "')",
                                        480000);  // Run again in 8 minutes
                        }
                      });
}


/*
Update the amount of money saved
This will set itself up to run again in 20 seconds
*/
function updateSavings (prefix, site, elemName) {
    var url = prefix + '/data/generateSavings.php';
    if (site != '') {
        url += '?siteIdName=' + site;
    }
    
    // Do the AJAX request
    new Ajax.Request (url,
                      {
                        method: 'post',
                        onComplete: function (transport) {
                            $(elemName).update ("$" + transport.responseText);
                            setTimeout ("updateSavings ('" +
                                        prefix +
                                        "', '" +
                                        site +
                                        "', '" +
                                        elemName +
                                        "')",
                                        60000);  // Run again in 1 minute
                        }
                      });
}
