/* Javascript class for the weather_regular module. */
var weather_regular = $.inherit(Module, {
    __constructor: function(div) {
        this.__base(div);
        this.type = "weather_regular";
    },

    // Function to be called only on published pages. Provides access to ajax functionality on published pages.
    initialize: function() {
        // Get the current weather conditions for this location from 
        // the database and then load them into the addon
        this.loadWeatherValues();
    },

    // Called after the module has loaded, this retrieves and loads the current
    // weather data for the location associated with this module
    loadModuleCallback: function(data, textStatus) {

        // We need to use innerHTML here because of the (highly probable)
        // chance that the html we get back has <script> in it.  jQuery 1.4
        // tries to be smart and not insert non-standard code.
        this.container[0].innerHTML = data.html;

        // Load the current weather conditions for this location into the addon
        this.loadWeatherValues();

        this.addDragHandle(data);
        this.appropriatelyResize();
        this.manageModuleMargins();
    },

    loadWeatherValues: function() {
        
        // Get the weather data
        var _this = this;
        var options = { 'a': 'getWeatherValues', 'm': this.id };

        this.ajaxPost('getWeatherValues', {}, function(data) {
            var weatherData = eval('(' + data.response + ')');
            var thisLocation = weatherData.location.city + ', ' + ((weatherData.location.country == 'United States') ? weatherData.location.region : weatherData.location.country);

            if (weatherData.error) {
                $('*', _this.container).hide();
                $('<div class="errordiv">div>').html("Can't find " + thisLocation).prependTo(_this.container);
            }
            else {

                var current = weatherData.current;
                var sevendays = weatherData.sevendays;
                var units = weatherData.units;

                // Weather location
                $('#weatherLocation', _this.container).html(thisLocation);

                // Measurement values
                $('.temperatureUnits', _this.container).html(units.temperature);
                $('.speedUnits', _this.container).html(units.speed);
                $('.distanceUnits', _this.container).html(units.distance);

                // Basic weather data
                $('.icon', _this.container).html('<img src="/adm/images/modules/weather_regular/icons/' + weatherData.current.icon.replace('.jpg', '.png') + '" />');
                $('#temperatureValue', _this.container).html(current.temperature);
                $('#sunriseValue', _this.container).html(weatherData.sunrise);
                $('#sunsetValue', _this.container).html(weatherData.sunset);
                $('#currentConditionsValue', _this.container).html(_this.ucwords(current.icon.replace('_', ' ').replace('.jpg', '')));
                $('#whighValue', _this.container).html(sevendays[0].high);
                $('#wlowValue', _this.container).html(sevendays[0].low);

                // Extended weather data
                $('#humidityValue', _this.container).html(current.relative_humidity + '%');
                $('#cloudCoverValue', _this.container).html(current.cloud_cover + '%');
                $('#visibilityValue', _this.container).html(current.visibility);
                $('#UVIndexValue', _this.container).html(current.uv + ' - ' + current.uv_words);
                $('#windValue', _this.container).html(current.wind_speed);
                $('#dewPointValue', _this.container).html(current.dew_point);
                $('#lastUpdatedValue', _this.container).html(weatherData.last_modified);
            }
        });
    },

    // Make the first letter of every word in a string uppercase
    // (Adapted from http://phpjs.org/functions/ucwords:569)
    ucwords: function(str) {
        return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
            return $1.toUpperCase();
        });
    }
});

