// The Weather Network Template for Canada
// Written By: weather@massmurmur.com
//
// Version 1.0

main
{
	// debug_log;

	pointer weatherDetailStart
	{
		go_start; 
		skip_to_text("displayST", false);
		skip_to_text(">\",", false);
	};
	
	pointer weatherDetailEnd
	{
		skip_to_text("</script>", false);
	};

	pointer weatherEightDayStart
	{
		go_start; 
		skip_to_text("displayLT", false);
		skip_to_text(">\",", false);
	};
	
	pointer weatherEightDayEnd
	{
		skip_to_text("</script>", false);
	};

	map skyMap
	{
		key_value("a", "2");
		key_value("an","17");
		key_value("b", "3");
		key_value("bn","18");
		key_value("c", "3");
		key_value("cn","18");
		key_value("d", "14");
		key_value("dn","21");
		key_value("e", "3");
		key_value("en","18");
		key_value("f", "4");
		key_value("fn","19");
		key_value("g", "15");
		key_value("gn","29");
		key_value("h", "16");
		key_value("hn","30");
		key_value("i", "10");
		key_value("j", "16");
		key_value("jn","30");
		key_value("k", "5");
		key_value("l", "7");
		key_value("m", "8");
		key_value("n", "29");
		key_value("o", "13");
		key_value("p", "9");
		key_value("q", "9");
		key_value("r", "11");
		key_value("s", "11");
		key_value("t", "3");
		key_value("tn","18");
		key_value("u", "10");
		key_value("v", "6");
		key_value("w", "6");
		key_value("x", "0");
	};

	map hourMap
	{
		key_value("OVERNIGHT", "0"); // SPB defines Overnight as 0-5
		key_value("MORNING", "6");  // SPB defines Morning as 6-11
		key_value("AFTERNOON", "12"); // SPB defines Afternoon as 12-17
		key_value("EVENING", "18"); // SPB defines Evening as 18-23
	};
	
	string DELIMITER = "\", ";
	string windSpeedUnits = "kph";
	string tmprUnits = "C";
	
	string dayOne = "";
	string dayTwo = "";
	string dayThree = "";

	// Grab the page
	open_url("http://theweathernetwork.com/weather/cities/usa/Pages/" + CityCode + ".htm", "GET");
	
	// Parse the 8 day forecast
	{
		select_area(weatherEightDayStart, weatherEightDayEnd);

		string dayIndex = "";
		string sky = ".";
		string hour = "12"; // default to noon

		// Only do 8 days
		while (dayIndex $ "........")
		{
			// Get Day of Month
			skip_to_text("<BR>", false);
			skip_chars("A-Z ");
			begin_var;
			skip_chars("0-9");
			string dayOfMonth = end_var;

			// Save 1st and 2nd day digits for detailed forecast later.
			if (dayIndex ~ "")
			{
				dayOne = dayOfMonth;
			}
			if (dayIndex ~ ".")
			{
				dayTwo = dayOfMonth;
			}
			if (dayIndex ~ "..")
			{
				dayThree = dayOfMonth;
			}

			// Get sky
			skip_to_text(",", true);  // end of DayName
			skip_chars(DELIMITER);
			begin_var;
			skip_to_chars("\"");
			sky = end_var;
			if (sky)
			{
				sky = skyMap[sky];
				if (!sky)
					sky = "0";
			}

			// Get Low Temp
			skip_chars(DELIMITER);
			begin_var;
			skip_chars("\\-0-9");
			string tmprLo = end_var;

			// Get High Temp
			skip_to_text(",", true); // end of low temp C
			skip_to_text(",", true); // end of low temp F 
			skip_chars(DELIMITER);
			begin_var;
			skip_chars("\\-0-9");
			string tmprHi = end_var;

			// Get Humidity
			skip_to_text(",", true); // end of high temp C
			skip_to_text(",", true); // end of high temp F
			skip_to_text(",", true); // end of POP
			skip_to_text(",", true); // end of text weather desc
			skip_to_text(",", true); // end of min humidity %
			skip_chars(DELIMITER);
			begin_var;
			skip_chars("0-9");
			string humidity = end_var;

			// Get Wind Direction
			skip_to_text(",", true); // end of max humidity %
			skip_to_text(",", true); // end of chance of wet rain %
			skip_to_text("\"", true); // skip_chars(DELIMITER) doesn't go over the newline!
			begin_var;
			skip_chars("A-Z");
			string windDirection = end_var;

			// Get Wind Speed
			skip_to_text(",", true); // end of wind direction (E, NE, etc. or blank)
			skip_chars(DELIMITER);
			begin_var;
			skip_chars("A-Za-z0-9");
			string windSpeed = end_var;
			if (windSpeed ~ "calm")
			{
				windSpeed = "0";
			}

	//		skip_to_text(",", true); // end of wind speed km/h ("## km/h", or "calm")
	//		skip_to_text(",", true); // end of wind speed knots ("# kt" or "calm")
	//		skip_to_text(",", true); // end of "Dry Heat Index" (samples: "High (50)", "Average (35)")
	//		skip_to_text(",", true); // end of amount of rain
	//		skip_to_text(",", true); // end of amount of snow
	//		skip_to_text(",", true); // end of hours of sunshine
	//		skip_to_text(",", true); // end of "GDD" (samples: "21" "22")
	//		skip_to_text(",", true); // end of "CHU" (samples: "31" "30")


			// Add today's forecast
			if (sky)
				add_day;
			
			dayIndex = dayIndex + ".";
		}
	
	} // end 8 day forecast portion
	
	// Parse the detail forecast
	{
		select_area(weatherDetailStart, weatherDetailEnd);

		string dayIndex = "";
		string sky = ".";
		string currentDay = "";
		string currentDayIndex = "";

		// 6 items in detail list
		while (dayIndex $ "......")
		{
			// Keep Track of Number of Days.  After 4 days, there's some stuff we need to skip
			if (dayIndex ~ "....")
			{
				skip_to_text(",", true); // precip summary
				skip_to_text(",", true); // ?
				skip_to_text(",", true); // ?
				skip_to_text(",", true); // ?
			}

			// Get Day of Week
			skip_to_text("\"", true);
			begin_var;
			skip_chars("A-Z");
			string dow = end_var;
			
			// Get "hour"
			skip_chars(" ");
			begin_var;
			skip_chars("A-Z");
			string hour = end_var;
			if (hour)
			{
				hour = hourMap[hour];
			}


			// Process "dow" into an actual day of month (using data from 8 day forecast)
			if (currentDay $ dow)
			{
				currentDay = dow;
				currentDayIndex = currentDayIndex + ".";
			}
			
			string dayOfMonth = "";
			if (currentDayIndex ~ ".")
			{
				dayOfMonth = dayOne;
				if (hour ~ "0")
				{
					dayOfMonth = dayTwo
				}
			}
			if (currentDayIndex ~ "..")
			{
				dayOfMonth = dayTwo;
				if (hour ~ "0")
				{
					dayOfMonth = dayThree
				}
			}
			if (currentDayIndex ~ "...")
			{
				dayOfMonth = dayThree;
				// no overnight support on day three needed
			}



			// Get sky
			skip_to_text(",", true);  // end of DayName
			skip_chars(DELIMITER);
			begin_var;
			skip_to_chars("\"");
			sky = end_var;
			if (sky)
			{
				sky = skyMap[sky];
				if (!sky)
					sky = "0";
			}

			// Get Temp
			skip_chars(DELIMITER);
			begin_var;
			skip_chars("\\-0-9");
			string tmpr = end_var;

			// Get Humidity
			skip_to_text(",", true); // end of temp C
			skip_to_text(",", true); // end of temp F
			skip_to_text(",", true); // end of POP
			skip_to_text(",", true); // end of text weather desc
			skip_to_text(",", true); // end of "Feels Like" temp C
			skip_to_text(",", true); // end of "Feels Like" temp F
			skip_chars(DELIMITER);
			begin_var;
			skip_chars("0-9");
			string humidity = end_var;

			// Get Wind Direction
			skip_to_text(",", true); // end of humidity %
			skip_to_text("\"", true); // skip_chars(DELIMITER) doesn't go over the newline!
			begin_var;
			skip_chars("A-Z");
			string windDirection = end_var;

			// Get Wind Speed
			skip_to_text(",", true); // end of wind direction (E, NE, etc. or blank)
			skip_chars(DELIMITER);
			begin_var;
			skip_chars("A-Za-z0-9");
			string windSpeed = end_var;
			if (windSpeed ~ "calm")
			{
				windSpeed = "";
			}

			// Get Wind Speed Gusts
			skip_to_text(",", true); // end of wind speed km/h ("## km/h", or "calm")
			skip_to_text(",", true); // end of wind speed knots ("# kt" or "calm")
			skip_chars(DELIMITER);
			begin_var;
			skip_chars("A-Za-z0-9\-");
			string windGustSpeed = end_var;
			if (windGustSpeed ~ "-")
			{
				windGustSpeed = "";
			}
			
			// Get Pressure
			skip_to_text(",", true); // end of wind gust speed km/h ("## km/h", or "-")
			skip_to_text(",", true); // end of wind gust speed knots ("# kt")
			skip_to_text(",", true); // end of "chance of wet rain %"
			skip_to_text(",", true); // end of dewpoint temp C
			skip_to_text(",", true); // end of dewpoint temp F
			skip_to_text("\"", true); // skip_chars(DELIMITER) doesn't go over the newline!
			begin_var;
			skip_chars("0-9\.");
			string pressure = end_var;
			
			
			// Skip to end of this item, get ready for next item
			skip_to_text(",", true); // end of pressue
			skip_to_text(",", true); // end of rain amount
			skip_to_text(",", true); // end of snow amount

			// Add today's forecast
			add_day;
				
			dayIndex = dayIndex + ".";
		}
		
	
	} // end detail forecast portion


}