(function($){

	$.fn.weather = function() 
	{
		// Functions
		return this.each(function(i, e) 
		{
			var $e = $(e);
			
			$.ajax({
				type: 'GET',
				url: '/weather/getweather.php',
				dataType: 'json',
				success: function(data) {
					if (data) {
						_callback(e, data.results.channel);
					} else {
						$e.html('<p>Weather information unavailable</p>');
					}
				},
				
				error: function(data) {
					$e.html('<p>Weather request failed</p>');
				}
			});

		});
	};

	// Function to each feed item
	var _callback = function(e, feed) 
	{
		var highlow = 'High: '+ feed.item.forecast[0].high + '&deg; - Low: ' + feed.item.forecast[0].low + '&deg;';
		var html = '';
		
		html += '<div class="weatherFeed" style="background-image: url(/weather/weatherpics/'+ feed.item.condition.code +'s.png); background-repeat: no-repeat;">';
			html += '<div class="weatherTemp">'+ feed.item.condition.temp +'&deg;</div>';
			html += '<div class="weatherDesc">'+ feed.item.condition.text + '</div>';
			html += '<div class="highlow">' + highlow + '</div>';
		html += '</div>';
		
		$(e).append(html);
	};
	
})(jQuery);

