var currDetailTab = 'Daily';
var dailyChartLoaded = 0;
var weeklyChartLoaded = 0;
var yearlyChartLoaded = 0;
var monthlyChartLoaded = 0;

$('document').ready (function () {
	try {
		$("a.imgGroup").fancybox ({
			'transitionIn': 'elastic',
			'transitionOut': 'elastic',
			'titleShow': 'true',
			'titlePosition': 'over',
			'showCloseButton': 'true',
			'showNavArrows': 'true',
			'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">(' +  
					   (currentIndex + 1) + 
					   ' of ' + 
					   currentArray.length + 
					   ')&nbsp;&nbsp;&nbsp;' + 
					   title + 
					   '</span>';
			}
	
		});
    }
    catch (err) { }
});

/**
 * 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) {
    $('#' + elemID).toggle ();
}


function goToPage () {
    if ($('#siteDetails').val () != 'NoGo') {
        location.href = '/installations/' + $('#siteDetails').val ();
    }
}


function switchDetailTab (newTab, prefix) {
    // Switch the tabs
    $('#dv' + currDetailTab).hide ();
    $('#dv' + newTab).show ();
    
    // Reset the date and chart number variables
    resetChartChangeSetup ();

    // Cancel timers if necessary
    if (currDetailTab == 'Daily') {
        clearTimeout (dayTimeout);
    }
    else if (currDetailTab == 'Weekly') {
        clearTimeout (weekTimeout);
    }
    
    // Store new tab as current tab
    currDetailTab = newTab;
    
    // Update the chart
    if (currDetailTab == 'Daily') {
    	updateChart ('DAILY', prefix);
    }
    else if (currDetailTab == 'Weekly') {
    	updateChart ('WEEKLY', prefix);
    }
    else if (currDetailTab == 'Yearly') {
    	updateChart ('YEARLY', prefix);
    }
    else if (currDetailTab == 'Monthly') {
    	updateChart ('MONTHLY', prefix);
    }
}


function switchVideo (newVid) {
	if (currVideo == newVid) {
		return;
	}
	
	$('#li' + currVideo).removeClass ('vidActive');
	$('#li' + newVid).addClass ('vidActive');
	$('#vid' + currVideo).addClass ('hidden');
	$('#vid' + newVid).removeClass ('hidden');
	currVideo = newVid;
}


function showImage (divID, imgID, path) {
    //$(imgID).src = "images/sites/" + path;
    //$(divID).show ();
    window.open ('images/sites/' + path);
}

/**
 *
 */
function twoLineChange (prefix, name) {
    if ($('#' + name).attr ('checked')) {
        using2Line = true;
        $('#dvDailyRegularExplination').hide ();
        $('#dvDailyConsumptionExplination').show ();
    }
    else {
        using2Line = false;
        $('#dvDailyConsumptionExplination').hide ();
        $('#dvDailyRegularExplination').show ();
    }
    
    clearTimeout (dayTimeout);
    currChartUpdating = 'DAILY';
    updateChart ('DAILY', prefix);
}


/*
Update the weather conditions
This will set itself up to run again in 4 minutes
*/
function updateWeather (prefix, picElem, tempElem) {
    // Do the AJAX request
    $.getJSON (prefix + '/data/generateWeather.php',
               function (data) {
                    $('#' + picElem).attr ('src', data.imgURL);
                    $('#' + tempElem).html (data.currTemp + " F");
                            
                    setTimeout ("updateWeather('" +
                                prefix +
                                "', '" +
                                picElem +
                                "', '" +
                                tempElem +
                                "')",
                                600000);  // Run again in 10 minutes
               }
              );
}


/*
Update the amount of money saved
This will set itself up to run again in 20 seconds
*/
function updateSavings (prefix, site, elemName) {
    var addr = prefix + '/data/generateSavings.php';
    if (site != '') {
        addr += '?siteIdName=' + site;
    }
    
    // Do the AJAX request
    $.ajax ({
                url: addr,
                method: 'post',
                success: function (data) {
                    $('#' + elemName).html ("$" + data);
                    setTimeout ("updateSavings ('" +
                                prefix +
                                "', '" +
                                site +
                                "', '" +
                                elemName +
                                "')",
                                60000);  // Run again in 1 minute
                }
          });
}

