/*****************************************************************************
 * Author:          Nik Estep
 * Created:         August 31, 2009
 * Last Modified:   April 10, 2010
 * Purpose:         Contains the javascript variables and functions that are
 *                  shared among different pages within the website.
 *****************************************************************************/

var locations = [];
var timeoutHandle = '';
var loadImageSite = '';


/* Class used to store installation locaions */
function locationObj 
(
	idName, 
	dispName, 
	startDate, 
	numberOfWeeks, 
	numberOfYears, 
	yearStartDate, 
	image1, 
	image2
) {
    this.idName = idName;
    this.displayName = dispName;
    this.startDate = startDate;
    this.numberOfWeeks = numberOfWeeks;
    this.numberOfYears = numberOfYears;
    this.yearStartDate = yearStartDate;
    this.image1 = image1;
    this.image2 = image2;
}


/**
 * Load the installation location list into the array. It will also update a
 * selection tag with the ID 'selSite' upon successful retrieval of the data.
 * 
 * @param prefix {string}   The path prefix to the 'meter_monitor' folder from
 *                          the caling page
 * @param imgSite {string}  The site ID name to load the images for if you want
 *                          images loaded
 */
function loadLocations (prefix, imgSite) {
    loadImageSite = imgSite;
    
    /* Make AJAX request for location */
    jQuery.getJSON (
    			prefix + "/meter_monitor/data/getLocations2.php",
    			function (json) {
    				if (json.valid == 1) {
                        var html = "<option value='select'>-- " +
                                   "Select Location</option>\n";
                        var index = 0;
                        locations = [];
                        
                        while (index < json.locationCount) {
                            var idName = json.locations[index].idName;
                            var dispName =
                                     json.locations[index].displayName;
                            var startDate =
                                       json.locations[index].startDate;
                            var numberOfWeeks =
                                   json.locations[index].numberOfWeeks;
                            var numberOfYears =
                                   json.locations[index].numberOfYears;
                            var yearStartDate = 
                            	   json.locations[index].yearStartDate;
                            var image1 = json.locations[index].image1;
                            var image2 = json.locations[index].image2;
                            
                            html += "<option value='" +
                                    idName +
                                    "'>" +
                                    dispName +
                                    "</option>\n";
                            
                            locations[index] = new locationObj (
                                                 idName,
                                                 dispName,
                                                 new Date (startDate),
                                                 numberOfWeeks,
                                                 numberOfYears,
                                                 yearStartDate,
                                                 image1,
                                                 image2);
                            
                            index = index + 1;
                        }
                        
                        /* Update the drop down */
                        jQuery('#selSite').empty ();
                        jQuery('#selSite').append (html);
                        
                        try {
                        	/* If we are in a chart page, try this */
                        	jQuery('#selSite').val (currChartSiteId);
                        	jQuery('#selSite').change ();
                        	
                        	/* Load images if needed */
                            if (loadImageSite != '') {
                                loadThumbnails (loadImageSite);
                            }
                        }
                        catch (e) { }
                    }
                }
    		);
}


/**
 * Get the index within the 'locations' array that a site is located at
 * 
 * @param idName {string}   The ID name of the site to get index for
 * @returns {int}           Index of the site or -1 if the site is not found
 */
function getSiteIndex (idName) {
    var locIndex = 0;
    while (locIndex < locations.length) {
        if (locations[locIndex].idName == jQuery('#selSite').val ()) {
            break;
        }
        else {
            locIndex++;
        }
    }
    
    if (locIndex == locations.length) {
        locIndex = -1;
    }
    
    return locIndex;
}


/**
 * Convert a date object into a string representing the date formated for the
 * Tigra Calendar.
 * 
 * @param date {Date}	Date to format
 * @return {string}		Date formated for Tigra Calendar as a string
 */
function formatDateForTigra (date) {
	return (date.getMonth () + 1) +
		   "/" +
		   date.getDate () +
		   "/" +
		   date.getFullYear ();
}


/**
 * Match the month, day, and year of two date objects.
 *
 * @param date1 {Date}  First date to compare
 * @param date2 {Date}  Second date to compare
 * @returns {boolean}   True if dates share month, day, and year
 */
function doDatesMatch (date1, date2) {
    if (date1.getMonth () != date2.getMonth () ||
        date1.getDate () != date2.getDate () ||
        date1.getFullYear () != date2.getFullYear ()) {
        return false;
    }
    
    return true;
}