/*

Shows weather forecast in element.
Requires gwproxy.php to be present on webserver
container is a jQuery element or selector
locale and location are strings.
location could be whatever google accepts like 
'New York' or ',,,22500000,31000000' to get weather for lat=22.5 and long=31

*/

function showWeather(container,location,locale){

	$(container).text('Loading...');

	$.get( '/weather/gwproxy.php?weather=' + encodeURI(location) + '&hl='+encodeURI(locale), function(xml) {

		function addForecastDiv( day, condition, temp, icon ){
			daydiv = $("<div class='day'></div>");			
			daydiv.append( "<img src='http://www.google.com/" + icon + "' width=\"32\" height=\"32\"/>" );
			tempArr = condition.split('<br/>')
			daydiv.append( "<p class='condition'><span class='wtype'>" + tempArr[0] + "</span><span class='wdens'>" + tempArr[1] + "</span><span class='wwind'>" + tempArr[2] + "</span></p>" );
			//tempArr = temp.split(', ')
			daydiv.append( "<p class='wtemp'>" + temp + "</p>");
			$(container).append(daydiv);
			
		}

		$(container).text('');
		
		var t_unit = '&deg;C';

		if( $(xml).find('forecast_information').find('unit_system').attr('data') == 'SI' ){
			t_unit = '&deg;C';
			tempVar = $(xml).find('current_conditions').find('temp_c').attr('data') + '&deg;C'
		} else {
			tempVar = $(xml).find('current_conditions').find('temp_f').attr('data') + '&deg;F';
		}
	
		addForecastDiv(
			'',
			$(xml).find('current_conditions').find('condition').attr('data') + '<br/>' + 
			$(xml).find('current_conditions').find('humidity').attr('data') + '<br/>' + 
			$(xml).find('current_conditions').find('wind_condition').attr('data'),
			tempVar,
			$(xml).find('current_conditions').find('icon').attr('data')
		);
/*
		$(xml).find('forecast_conditions').each(function(){
			addForecastDiv(
				$(this).find('day_of_week').attr('data'),
				$(this).find('condition').attr('data'),
				$(this).find('low').attr('data') + t_unit + ' - ' + 
				$(this).find('high').attr('data') + t_unit,
				$(this).find('icon').attr('data')
			);
		});
		*/

	},'xml');

}


function showWeather2(container,location,locale){

	$(container).text('Loading...');

	$.get( '/weather/gwproxy.php?weather=' + encodeURI(location) + '&hl='+encodeURI(locale), function(xml) {

		function addForecastDiv( day, condition, temp, icon ){
			
			
			if(day != ''){
				daydiv = $("<div class='day'></div>");	
				daydiv.append( "<div class='name'>" + day + "</div>" );
				daydiv.append( "<img src='http://www.google.com/" + icon + "' />" );
				daydiv.append( "<p class='temp'>" + temp + "</p>");
			} else {
				daydiv = $("<div class='today'></div>");	
				daydiv.append( "<p class='temp'>" + temp + "</p>");
				daydiv.append( "<p class='condition'>" + condition + "</p>" );
			}
			$(container).append(daydiv);
			
		}

		$(container).text('');
		
		var t_unit = '&deg;F';

		if( $(xml).find('forecast_information').find('unit_system').attr('data') == 'SI' ){
			t_unit = '&deg;C';
			tempVar = $(xml).find('current_conditions').find('temp_c').attr('data') + '&deg;C'
		} else {
			tempVar = $(xml).find('current_conditions').find('temp_f').attr('data') + '&deg;F';
		}
	
		addForecastDiv(
			'',
			$(xml).find('current_conditions').find('condition').attr('data') + '<br/>' + 
			$(xml).find('current_conditions').find('humidity').attr('data') + '<br/>' + 
			$(xml).find('current_conditions').find('wind_condition').attr('data'),
			tempVar,
			$(xml).find('current_conditions').find('icon').attr('data')
		);

		$(xml).find('forecast_conditions').each(function(){
			addForecastDiv(
				$(this).find('day_of_week').attr('data'),
				$(this).find('condition').attr('data'),
				$(this).find('low').attr('data') + t_unit + ' - ' + 
				$(this).find('high').attr('data') + t_unit,
				$(this).find('icon').attr('data')
			);
		});

	},'xml');

}


