/**
 * Get Weather Information
 *
 * @author: Rilwis
 * @url: http://hontap.blogspot.com
 * @email: rilwis@gmail.com
 * @license: MIT License
 */

/**
 * Callback function to show weather information
 */
function showWeatherCallback(data) {
	var channel = data.query.results.weather.rss.channel;
	img = channel.item.description.match(/http:\/\/[^"']*/)+'';
	var img2 = '';
	var check = false;
	for(i=img.length-1;i>=0;i--)
	{
		if (img.charAt(i) != "/")
		{
			if (check==true)
				img2 = img.charAt(i)+img2;
			if (img.charAt(i) == '.')
				check = true;
		}
		else
			break;
	}
	var cities = new Array();
	cities['Hanoi'] = 'Hà Nội';
	cities['Da Nang'] = 'Đà Nẵng';
	cities['Ho Chi Minh City'] = 'Hồ Chí Minh';
	img2 = "http://l.yimg.com/a/i/us/nws/weather/gr/"+img2+"d.png";
	html = '<div class="hinh_thoi_tiet"><img style="vertical-align:bottom" width="120" src="' + img2 + '" /></div> <span class="nhiet_do">' + channel.item.condition.temp + '&deg;C</span><br />' + 
				'<div class="mo_ta_thoi_tiet">' +
				'Thời tiết dành cho  ' + cities[channel.location.city] + '<br />' +
				'Độ ẩm : ' + channel.atmosphere.humidity + '%<br />' + 
				'Áp suất : ' + channel.atmosphere.pressure + '</div>';
	$("#load_weather_1").html(html);
}

/**
 * Show weather information. Use YQL to get information
 * location: in format "city, country"
 *
 * Ex: showWeather('hanoi,vietnam');
 */
function showWeather(location) {
	var baseUrl = 'http://query.yahooapis.com/v1/public/yql?q=',
		q = 'use "http://github.com/yql/yql-tables/raw/master/weather/weather.bylocation.xml" as we;' + 
			'select * from we where location="' + location + '" and unit="c"',
		url = baseUrl + encodeURIComponent(q) + '&format=json&diagnostics=false&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=showWeatherCallback';

	$("#script_load_weather").html('<script type="text/javascript" src="' + url + '"></' + 'script>');
}

