function loadWeatherData(weather) {
	var updateValues = function(name, value) {
		var els = $$('.weather_'+name);
		$each(els, function(el) {
			el.innerHTML = value;
		});
	};

	if (weather.query_failed) $$(".weather_query_failed").setStyle('display', 'block');
	if (weather.city_name) updateValues('city_name', weather.city_name);
	if (weather.state) updateValues('state', weather.state);
	if (weather.zip) updateValues('zip', weather.zip);
	if (weather.noaa_summary) updateValues('noaa_summary', weather.noaa_summary);
	if (weather.city_code != "MSP" && weather.city_code != "STP") $$('.weather_indices').setStyle('display','none');
	
	if (gup('save_location')) Cookie.set("weather_location", weather.city_name.toUpperCase()+","+weather.state.toUpperCase(), {path:'/', duration:999});
	
	$each(weather.current, function(v,k) {
		
		var els = $$(".current_"+k);

		$each(els,function(el) {
			if (v) {
				if (el.getTag() == 'img') {
					el.setAttribute('src', 'http://stmedia.startribune.com/images/weather_' + v + '.png');
				}
				else {
					el.innerHTML = v;
				}
			} else {
				var c_els = $$(".current_"+k+"_container");
				$each(c_els, function(c_el) {
					c_el.setStyle('display', 'none');
				});
			}
		});
	});

	$each(weather.seven_day, function(day, num) {
		$each(day, function(v,k) {
			var els = $$(".forecast_"+k+"_"+(num+1).toString());
			$each(els, function(el) {
				if (el.getTag() == 'img') {
					el.setAttribute('src', 'http://stmedia.startribune.com/images/weather_'+v+'_s.png');
				} else {
					if (k == 'name') {
						el.innerHTML = v.substr(0, 3) + '.';
					}
					else {
						el.innerHTML = v;
					}
				}
			});
		});
	});
	
	$$('.weatherDataLoading').setStyle('display','none');
	$$('.dynamicWeatherData').setStyle('display','block');	
}

function gup(name){
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) 
        return false;
    else 
        return results[1];
}

var weatherLocation = '';

if(Cookie.get('weather_location')) {
	weatherLocation = Cookie.get('weather_location');
}

if(gup('location')) {
	weatherLocation = gup('location');
	
}

if(!weatherLocation || weatherLocation.toUpperCase() == "MINNEAPOLIS,MN") {
	loadWeatherData(minneapolisWeatherData);
} else { 
	$$('.weatherDataLoading').setStyle('display','block');
	
	var weatherFeedURL = "http://"+window.location.hostname+"/templates/getWeatherData?output=json";
	if(weatherLocation) {
		weatherFeedURL = weatherFeedURL+"&location="+weatherLocation; 
	}
	var jSonRequest = new Json.Remote(weatherFeedURL, {async:true,method:"get",onComplete: function(weather_data){ loadWeatherData(weather_data); }});

	jSonRequest.send();
	
}

