// onDomReady
$(function () {
	// Weather parser
	var parseWeather = function (forecast) {
		// Hide weather if there are no conditions available
		if(!forecast || forecast.conditions == "" || forecast.max_temp  == "" || forecast.min_temp == "") {
			$("#localweather, .weather").hide();
			return false;
		}
		$("#localweather, .weather").show();
		
		$("#weather-conditions-description").append(forecast.conditions);
		$(".weather-high").append(forecast.max_temp);
		$(".weather-low").append(forecast.min_temp);
		
		if(forecast.conditions.match(/\bstorm*/i) || forecast.conditions.match(/\bthunder*/i)) {
			//storm icon
			$("#weather-conditions").addClass("storm");
		} else if(forecast.conditions.match(/\brain\b/i) || forecast.conditions.match(/\bshowers\b/i)) {
			//rain icon
			$("#weather-conditions").addClass("rain");
		} else if(forecast.conditions.match(/\bshower\b/i) || forecast.conditions.match(/\bdrizzle\b/i)) {
			//showers icon
			$("#weather-conditions").addClass("showers");
		} else if(forecast.conditions.match(/\bfine\b/i)) {
			if(forecast.conditions.match(/\bcloud*/i) || forecast.conditions.match(/\bdry\b/i)) {
				// mainly fine
				$("#weather-conditions").addClass("mainly-fine");
			} else {
				// fine
				$("#weather-conditions").addClass("fine");
			}
		} else {
			// fine 
			$("#weather-conditions").addClass("fine");
		}
	}
	
	// don't want to escape / as this is used for e.g. /vic/melbourne
	function custom_escape (url) {
		url = url.replace(/\?/, "%3F")
		url = url.replace(/\=/, "%3D")
		url = url.replace(/\&/, "%26")
		return url;
	}
	
	$.getJSON("http://ws.blocksglobal.com/forecasts/show/"+ custom_escape(weather_station) +"?callback=?", function(response) {
		parseWeather(response);
	});
});
