if (typeof(OHUB) == 'undefined') { OHUB = {}; }
OHUB.weather = {
config: {
debug: false,
core: 's3.ohub.us/ohub_core.min.js',
geoip: 'app.ohub.us/geoip.js',
stats: {
action_ids: {
impression: 9,
click: 1,
mouseover: 3,
mouseout: 4
}
}
},
data: {},
prepared_data: {},
thresholds: {
wind_mph: 20,
fog_vis: 1,
temp_cold: 40,
temp_hot: 90,
temp_ice: 32
},
conditions: {},
flags: {
fetched_weather: false
},
/**
* Initialize the object
* @param int override_postal_code Postal code to override lookup for
* @return void
*/
init: function(override_postal_code) {
OHUB.weather.debug('init(postal_code=' + override_postal_code + ')', 'info');
OHUB.jquery(function() {
OHUB.load('http://' + OHUB.weather.config.geoip, 'js', function() {
if (typeof(window.weather_thresholds) != 'undefined') {
OHUB.weather.thresholds = window.weather_thresholds;
}
var postal_code = parseInt(geoip_postal_code(), 10);
var city = geoip_city();
var state = geoip_region();
var country = geoip_country_code();
var alt = city +', ' + state + ' ' + country;
if (typeof(OHUB.weather.postal_code) != 'undefined') {
postal_code = parseInt(OHUB.weather.postal_code, 10);
} else {
postal_code = parseInt(postal_code, 10);
}
if (typeof(override_postal_code) != 'undefined') {
postal_code = parseInt(override_postal_code, 10);
}
OHUB.weather.get(postal_code, alt);
});
});
},
/**
* Gets weather for postal code
* @param int postal_code
* @param string alt location (City, ST CC) e.g. Flower Mound, TX US
* @return data
*/
get: function(postal_code, alt) {
OHUB.weather.debug('get(postal_code=' + postal_code + ', alt=' + alt + ')');
OHUB.weather.fetch(postal_code, alt);
},
/**
* Fetch weather from Yahoo
* @param int postal_code to fetch weather for
* @param string alt location (City, ST CC) e.g. Flower Mound, TX US
* @return object of weather data
*/
fetch: function(postal_code, alt) {
OHUB.weather.debug('fetch(postal_code=' + postal_code + ')');
if (isNaN(postal_code)) {
var query = 'select * from weather.forecast where location in (select id from weather.search where query="' + alt + '")';
} else {
var query = 'select * from weather.forecast where location=' + postal_code;
}
try {
$_.getJSON(
'http://query.yahooapis.com/v1/public/yql?callback=?',
{
q: query,
format: 'json',
env: 'store://datatables.org/alltableswithkeys'
},
function(data, textStatus) {
if (typeof(data.query.results.channel[0]) != 'undefined') {
OHUB.weather.data = weather_data = data = data.query.results.channel[0];
} else {
OHUB.weather.data = weather_data = data = data.query.results.channel;
}
//locale
d = data.location;
OHUB.weather.prepared_data.location = d.city + ', ' + d.region + ' ' + d.country;
//condition
d = data.item.condition;
OHUB.weather.prepared_data.condition = {
text: d.text,
as_of: d.date
};
//description
d = data.item.description;
OHUB.weather.prepared_data.description = d;
//forecast
d = data.item.forecast;
var rows = [];
$_.each(d, function(i, day) {
rows.push(
'
' + day.day + ' ' + day.date + ' | '
+ ''
+ day.text + ' ('
+ 'High: ' + day.high + '° ' + data.units.temperature
+ ' - Low: ' + day.low + '° ' + data.units.temperature
+ ') |
'
);
});
var html = '';
html += 'Forecast ' + data.item.title;
html += rows.join();
OHUB.weather.prepared_data.forecast = html;
//astronomy
d = data.astronomy;
OHUB.weather.prepared_data.sunrise = d.sunrise;
OHUB.weather.prepared_data.sunset = d.sunset;
//atmosphere
d = data.atmosphere;
OHUB.weather.prepared_data.humidity = d.humidity + '° ' + data.units.temperature;
OHUB.weather.prepared_data.pressure = d.pressure + ' ' + data.units.pressure + '.';
OHUB.weather.prepared_data.rising = d.rising + ' ° ' + data.units.temperature;
OHUB.weather.prepared_data.visibility = d.visibility + ' ' + data.units.distance + '.';
//wind
d = data.wind;
OHUB.weather.prepared_data.wind_chill = d.chill + '° ' + data.units.temperature;
OHUB.weather.prepared_data.wind_direction = d.direction + ' °';
OHUB.weather.prepared_data.wind_speed = d.speed + ' ' + data.units.speed;
//conditions
OHUB.weather.conditions = OHUB.weather.get_condition(
{ //weather_data
condition: weather_data.item.condition.text,
temperature: {
current: parseInt(weather_data.item.condition.temp, 10),
daily_high: parseInt(weather_data.item.forecast[0].high, 10),
daily_low: parseInt(weather_data.item.forecast[0].low, 10)
},
atmosphere: {
visibility: parseInt(weather_data.atmosphere.visibility, 10),
wind_speed: parseInt(weather_data.wind.speed, 10)
},
temp_type: (typeof(window.weather_temp_type) == 'undefined') ? 'current' : window.weather_temp_type
},
OHUB.weather.thresholds
);
OHUB.weather.flags.fetched_weather = true;
if (typeof(window.weather_ready) == 'function') {
weather_ready.apply(OHUB.weather, [ OHUB.weather.data ]);
}
}
);
} catch(e) {
if (typeof(window.weather_ready) == 'function') {
weather_ready.apply(OHUB.weather, [ false ]);
}
}
},
/**
* Gets the keywords for the current weather conditions
* @param string condition of weather
* @return array of strings (clear|rain|snow|sun|cloud)
*/
get_keywords: function(condition) {
OHUB.weather.debug('get_keywords(condition=' + condition + ')');
condition = condition.toLowerCase();
var keywords = [];
if (condition.indexOf('fair') !== -1) {
keywords.push('clear');
}
if (condition.indexOf('clear') !== -1) {
keywords.push('clear');
}
if (condition.indexOf('rain') !== -1) {
keywords.push('rain');
}
if (condition.indexOf('shower') !== -1) {
keywords.push('rain');
}
if (condition.indexOf('thunder') !== -1) {
keywords.push('rain');
}
if (condition.indexOf('flur') !== -1) {
}
if (condition.indexOf('snow') !== -1) {
keywords.push('snow');
}
if (condition.indexOf('sun') !== -1) {
keywords.push('sun');
}
if (condition.indexOf('cloud') !== -1) {
keywords.push('cloud');
}
return keywords;
},
/**
* Determines if variable passed is in range of match
* @param int current_val value of variable in it's current state
* @param int min_val minimum value in range
* @param int max_val maximum value in range
* @return boolean TRUE if within range FALSE otherwise
*/
in_range: function(current_val, min_val, max_val) {
OHUB.weather.debug('in_range(current_val=' + current_val + ', min_val=' + min_val + ', max_val=' + max_val + ')');
var min = (typeof(min_val) != 'undefined') ? min_val : null;
var max = (typeof(max_val) != 'undefined') ? max_val : null;
var in_range = true;
if (min != null) {
in_range = (current_val >= min);
}
if (max != null) {
in_range = (current_val <= max);
}
return in_range;
},
/**
* Returns an array of simplified weather conditions
* @param object weather_data in format {
* condition: (string),
* temperature: {
* current: (int),
* daily_high: (int),
* daily_low: (int),
* },
* atmosphere: {
* visibility: (int),
* wind_speed: (int)
* },
* temp_type: (string) (current|daily)
* }
* @param object condition_thresholds in format {
* wind_mph: (int) [default: 20], //wind miles per hour
* fog_vis: (int) [default: 1], //miles of visibility
* temp_cold: (int) [default: 40], //temperature for "cold"
* temp_hot: (int) [default: 90], //temperature for "hot"
* temp_ice: (int) [default: 32] //temperature for "ice"
* }
* @return object of conditions set to bools
*/
get_condition: function(weather_data, condition_thresholds) {
OHUB.weather.debug('get_condition(weather_data=' + weather_data + ', condition_threholds=' + condition_thresholds + ')');
var type = weather_data.temp_type;
var temp = weather_data.temperature.current;
var high = weather_data.temperature.daily_high;
var low = weather_data.temperature.daily_low;
var vis = weather_data.atmosphere.visibility;
var mph = weather_data.atmosphere.wind_speed;
var keys = OHUB.weather.get_keywords(weather_data.condition).join(',');
var default_thresholds = {
wind_mph: 20,
fog_vis: 1,
temp_cold: 40,
temp_hot: 90,
temp_ice: 32
};
if (typeof(condition_thresholds) == 'undefined') {
condition_thresholds = default_thresholds;
}
var thresholds = {
wind_mph: (typeof(condition_thresholds.wind_mph) != 'undefined') ? condition_thresholds.wind_mph : default_thresholds.wind_mph,
fog_vis: (typeof(condition_thresholds.fog_vis) != 'undefined') ? condition_thresholds.fog_vis : default_thresholds.fog_vis,
temp_cold: (typeof(condition_thresholds.temp_cold) != 'undefined') ? condition_thresholds.temp_cold : default_thresholds.temp_cold,
temp_hot: (typeof(condition_thresholds.temp_hot) != 'undefined') ? condition_thresholds.temp_hot : default_thresholds.temp_hot,
temp_ice: (typeof(condition_thresholds.temp_ice) != 'undefined') ? condition_thresholds.temp_ice: default_thresholds.temp_ice
};
var conditions = {
raining: (keys.indexOf('rain') !== -1),
snowing: (keys.indexOf('snow') !== -1),
cloudy: (keys.indexOf('cloud') !== -1),
sunny: (keys.indexOf('sun') !== -1),
wet: (keys.indexOf('rain') !== -1 || keys.indexOf('snow') !== -1),
windy: (parseInt(mph, 10) >= thresholds.wind_mph),
foggy: (parseInt(vis, 10) <= thresholds.fog_vis),
icy: false,
hot: false,
cold: false
};
switch(type) {
case 'current':
conditions.icy = (conditions.wet && temp <= thresholds.temp_ice);
conditions.hot = (conditions.sunny && temp >= thresholds.temp_hot);
conditions.cold = (temp <= thresholds.temp_cold);
break;
case 'daily':
break;
default:
conditions.icy = (conditions.wet && low <= thresholds.temp_ice);
conditions.hot = (conditions.sunny && high >= thresholds.temp_hot);
conditions.cold = (low <= thresholds.temp_cold);
break;
}
return conditions;
},
/**
* Determines if weather condition is cloudy
* @return boolean TRUE if it is cloudy | FALSE otherwise
*/
is_cloudy: function() {
OHUB.weather.debug('is_cloudy()');
return OHUB.weather.conditions.cloudy;
},
/**
* Determines if weather condition is cold
* @return boolean TRUE if it is cold | FALSE otherwise
*/
is_cold: function() {
OHUB.weather.debug('is_cold()');
return OHUB.weather.conditions.cold;
},
/**
* Determines if weather condition is foggy
* @return boolean TRUE if it is foggy | FALSE otherwise
*/
is_foggy: function() {
OHUB.weather.debug('is_foggy()');
return OHUB.weather.conditions.foggy;
},
/**
* Determines if weather condition is hot
* @return boolean TRUE if it is hot | FALSE otherwise
*/
is_hot: function() {
OHUB.weather.debug('is_hot()');
return OHUB.weather.conditions.hot;
},
/**
* Determines if weather condition is icey
* @return boolean TRUE if it is icey | FALSE otherwise
*/
is_icy: function() {
OHUB.weather.debug('is_icey()');
return OHUB.weather.conditions.icey;
},
/**
* Determines if weather condition is icey (helper for bad spellers)
* @return boolean TRUE if it is icey | FALSE otherwise
*/
is_icey: function() {
return is_icy(conditions);
},
/**
* Determines if weather condition is raining
* @return boolean TRUE if it is raining | FALSE otherwise
*/
is_raining: function() {
OHUB.weather.debug('is_raining()');
return OHUB.weather.conditions.raining;
},
/**
* Determines if weather condition is snowing
* @return boolean TRUE if it is snowing | FALSE otherwise
*/
is_snowing: function() {
OHUB.weather.debug('is_snowing()');
return OHUB.weather.conditions.snowing;
},
/**
* Determines if weather condition is sunny
* @return boolean TRUE if it is sunny | FALSE otherwise
*/
is_sunny: function() {
OHUB.weather.debug('is_sunny()');
return OHUB.weather.conditions.sunny;
},
/**
* Determines if weather condition is wet
* @return boolean TRUE if it is wet | FALSE otherwise
*/
is_wet: function() {
OHUB.weather.debug('is_wet');
return OHUB.weather.conditions.wet;
},
/**
* Determines if weather condition is windy
* @return boolean TRUE if it is windy | FALSE otherwise
*/
is_windy: function() {
OHUB.weather.debug('is_windy');
return OHUB.weather.conditions.windy;
},
/**
* Debug message handler
* @param mixed out string, func, or object
* @param string type of message { warn | info | error | [log] }
* @return void
*/
debug: function(out, type) {
if (!OHUB.weather.config.debug) {
return;
}
try {
if (typeof(console) == 'object') {
if (typeof(console[type]) != 'function') {
type = 'log';
}
switch(typeof(out)) {
case 'string':
case 'function':
switch(type) {
case 'warn':
console.warn('OHUB.weather: ' + out);
break;
case 'info':
console.info('OHUB.weather: ' + out);
break;
case 'error':
console.error('OHUB.weather: ' + out);
break;
default:
console.log('OHUB.weather: ' + out);
break;
}
break;
case 'object':
console.dir({ 'OHUB.weather': out } );
break;
}
}
} catch(e) { }
}
};
(function() {
d = document;
s = d.createElement('script');
s.type = 'text/javascript';
if (s.readyState) {
s.onreadystatechange = function() {
if (s.readyState == 'loaded' || s.readyState == 'complete') {
s.onreadystatechange = null;
OHUB.DEBUG = OHUB.weather.config.debug;
OHUB.ready(function() {
try {
OHUB.weather.init(OHUB.overrides.weather.postal_code);
} catch(e) {
OHUB.weather.init();
}
});
}
};
} else {
s.onload = function() {
OHUB.DEBUG = OHUB.weather.config.debug;
OHUB.ready(function() {
try {
OHUB.weather.init(OHUB.overrides.weather.postal_code);
} catch(e) {
OHUB.weather.init();
}
});
};
}
s.src = 'http://' + OHUB.weather.config.core;
d.getElementsByTagName('head')[0].appendChild(s);
})();