/**
 * @author 		Jorge Colon
 * @email		info@2upmedia.com
 * @version		0.7
 * @descrition	Weather widget that retrieves XML converted JSON from Google's weather API.
 */
String.prototype.sanitize = function () {
	return this.toLowerCase().replace(/\s/g, '_');
}
String.prototype.convertToF = function () {
	return Math.round(((this - 32)*5)/9);
}
$(function () {
	$.ajax({
		url: '/3rdparty/weather/response.php',
		dataType: 'json',
		success: function(data) {
			for_conds = data.weather.forecast_conditions;
			today_cond = for_conds[0].condition.attributes.data;
			today_temp = for_conds[0].high.attributes.data.convertToF();
			today_image = for_conds[0].icon.attributes.data.split("/")[3];;
			
			buffer = 	'<div id="current">'+
							'<img src="/img/weather/large/'+today_image+'" alt="'+today_cond+'" title="'+today_cond+'" />'+
							'<strong>'+today_temp+'<sup>&deg;</sup></strong>'+
							'<em>'+today_cond+'</em>'+
						'</div>';
			buffer +=	'<ul id="forecast">';
			
			l = for_conds.length;
			
			for(i = 1; i < l; i++) {
			    forecast_cond = for_conds[i].condition.attributes.data;
			    forecast_highTemp = for_conds[i].high.attributes.data.convertToF();
			    forecast_lowTemp = for_conds[i].low.attributes.data.convertToF();
    			forecast_image = for_conds[i].icon.attributes.data.split('/')[3];
    			forecast_day = for_conds[i].day_of_week.attributes.data;
            			    
				buffer += '<li><img src="/img/weather/small/'+forecast_image+'" title="'+forecast_cond+'" /><span>'+forecast_day+'</span><em>'+forecast_lowTemp+'&deg;/'+forecast_highTemp+'&deg;</em></li>';
			}
			
			buffer += '</ul>';
			
			$('#weatherApp').html(buffer);
			$('#weatherApp').removeClass('loading');
			$('#weatherApp').click(function() { window.location='/weather'; });
		},
		error: function (x, status, error) {
			$('#weatherApp').removeClass('loading');		
			$('#weatherApp').html('An error occurred while retrieving weather data.');
		}
	}
	);
});
