// weather_xml.js

// Fetch the weather XML feed from the webtools site.
// Display the weather summary box or detailed forecast.


// Insert the iframe into the main page.
function initWeather() {
	// Make sure browser supports getElementById method, and weather_summary or weather_detail element exists
	if (document.getElementById && (document.getElementById('weather_summary') || document.getElementById('weather_detail'))) {

		try
		{
			document.domain="ca.gov"; // Allows ca.gov sites to access this xml feed.
			var URLParams = location.search.replace(/<|>|%3c|%3e/gi,''); // Strip out < >
		}
		catch (e)
		{
			return(0); // Site is not a ca.gov site.
		}
	
	
		if (document.getElementById('weather_detail')) {
			document.getElementById('weather_detail').innerHTML = '<iframe src="http://webtools.ca.gov/javascript/shared/weather/weather_xml.html' + URLParams + '" width="1" height="1" frameborder="0"></iframe>';
		} else {
			if (document.getElementById('weather_summary'))
				document.getElementById('weather_summary').innerHTML = '<iframe src="http://webtools.ca.gov/javascript/shared/weather/weather_xml.html" width="1" height="1" frameborder="0"></iframe>';
		}
	}
}


// show the XML content in the browser
function showXML(objWeather)
{
	if (objWeather.condition) {
		if (document.getElementById('weather_detail')) {
			//// Display the weather details ////
			var htmlContent = '';

			htmlContent += '<h3>' + objWeather.city + '</h3>';
			htmlContent += '<div class="weather_current">';
			htmlContent +=  '<img src="http://www.google.com' + objWeather.icon + '" alt="' + objWeather.condition + '" title="' + objWeather.condition + '" class="weather_icon" />';
			htmlContent +=  '<div class="weather_current_temp">' + objWeather.temp_f + '&deg;F</div>';
			htmlContent +=  '<div class="weather_current_conditions">';
			htmlContent +=   'Current: ' + objWeather.condition + '<br />';
			htmlContent +=   objWeather.wind_condition + '<br />';
			htmlContent +=   objWeather.humidity;
			htmlContent +=  '</div>';
			htmlContent += '</div>';

			htmlContent += '<div class="cleaner">&nbsp;</div>';

			for (var i=0; i<objWeather.forecasts.length; i++)
			{
				if (objWeather.forecasts[i].day_of_week) {
					htmlContent += '<div class="weather_forecast_day">';
					htmlContent += objWeather.forecasts[i].day_of_week + '<br />';
					htmlContent += '<img src="http://www.google.com' + objWeather.forecasts[i].icon + '" alt="' + objWeather.forecasts[i].condition + '" title="' + objWeather.forecasts[i].condition + '" class="weather_icon" /><br />';
					htmlContent += objWeather.forecasts[i].high + '&deg; / ';
					htmlContent += objWeather.forecasts[i].low + '&deg; ';
					htmlContent += '</div>';
				}
			}

			htmlContent += '<div class="cleaner">&nbsp;</div>';

			htmlContent += '<form name="weather" action="weather.html"><input type="text" name="q" size="20" value="' + objWeather.postal_code + '" onfocus="if(this.value==\'' + objWeather.postal_code + '\')this.value=\'\'"><input type="image" class="weather_submit" alt="Go" src="http://webtools.ca.gov/javascript/shared/weather/weather_go_transparent.gif" /></form>';

			htmlContent += '<div class="weather_credit">Weather data provided by Google.</div>';

			document.getElementById('weather_detail').innerHTML = htmlContent;

			if (document.getElementById('weather_summary')) { // If there's also a summary box, hide it
				document.getElementById('weather_summary').innerHTML = '';
				document.getElementById('weather_summary').style.display = 'none';
			}
		} else {
			//// Display the weather summary ////
			var htmlContent;
			htmlContent = '<img src="http://www.google.com' + objWeather.icon + '" alt="' + objWeather.condition + '" title="' + objWeather.condition + '" class="weather_icon" />';

			htmlContent += '<div class="weather_title">Weather: ' + objWeather.temp_f + '&deg;F ' + '</div>';

			htmlContent += '<form name="weather" action="weather.html"><input type="text" name="q" size="20" class="weather_textfield" value="Sacramento, CA" onfocus="if(this.value==\'Sacramento, CA\')this.value=\'\'"><input type="image" class="weather_submit" alt="Go" src="http://webtools.ca.gov/javascript/shared/weather/weather_go_transparent.gif" /></form>';

			htmlContent += '<div class="cleaner">&nbsp;</div>';

			document.getElementById('weather_summary').innerHTML = htmlContent;
		}

	} else {
		// Could not retrieve XML data, so just remove the iframe.
		if (document.getElementById('weather_summary')) {
			document.getElementById('weather_summary').innerHTML = '';
		}
		if (document.getElementById('weather_detail')) {
			var htmlContent = '<h3>Not found</h3> <p>Please enter another city or zip code:</p>';
			htmlContent += '<form name="weather" action="weather.html"><input type="text" name="q" size="20" value=""><input type="image" class="weather_submit" alt="Go" src="http://webtools.ca.gov/javascript/shared/weather/weather_go_transparent.gif" /></form>';
			document.getElementById('weather_detail').innerHTML = htmlContent;
		}
	}
}

addLoadEvent(initWeather); // Add initWeather to the page onload event handler
