var todayDT = new Date();

YAHOO.util.Event.onDOMReady(init);

function init()
{
	doCurrents();
	doForecast();
}

function doCurrents()
{
	var request = YAHOO.util.Connect.asyncRequest('GET', "/proxy.php?url=" + currentsFeed + "&mimeType=text/xml", { success:successHandler, failure:failureHandler });

	function failureHandler(o)
	{
		//alert("Error!");
	}
	
	function successHandler(o)
	{
		var root = o.responseXML.documentElement;
		var currents = 
		{
			date:			new Date(getParsableDT(root.getElementsByTagName("observation_time_rfc822")[0].firstChild.nodeValue)),
			condition:		root.getElementsByTagName("weather")[0].firstChild.nodeValue,
			temperature:	Math.round(root.getElementsByTagName("temp_f")[0].firstChild.nodeValue),
			windDir:		root.getElementsByTagName("wind_dir")[0].firstChild.nodeValue,
			windSpeed:		root.getElementsByTagName("wind_mph")[0].firstChild.nodeValue
		};
		todayDT = currents.date;
		
		drawCurrents(currents);
	}
}

function doForecast()
{
	var request = YAHOO.util.Connect.asyncRequest('GET', "/proxy.php?url=" + forecastFeed + "&mimeType=text/xml", { success:successHandler, failure:failureHandler });

	function failureHandler(o)
	{
		//alert("Error!");
	}
	
	function successHandler(o)
	{
		var root = o.responseXML.documentElement;
		
		var forecastD0 = 
		{
			hiTemp:			root.getElementsByTagName("day")[0].getElementsByTagName("hi")[0].firstChild.nodeValue,
			loTemp:			root.getElementsByTagName("day")[0].getElementsByTagName("lo")[0].firstChild.nodeValue,
			sunrise:		root.getElementsByTagName("day")[0].getElementsByTagName("sunrise")[0].firstChild.nodeValue,
			sunset:			root.getElementsByTagName("day")[0].getElementsByTagName("sunset")[0].firstChild.nodeValue
		};
		
		var forecastD1 = 
		{
			date:			new Date(todayDT.setDate(todayDT.getDate() + 1)),
			condition:		root.getElementsByTagName("day")[1].getElementsByTagName("condition")[0].firstChild.nodeValue,
			icon:			root.getElementsByTagName("day")[1].getElementsByTagName("weather-icon")[0].firstChild.nodeValue,
			hiTemp:			root.getElementsByTagName("day")[1].getElementsByTagName("hi")[0].firstChild.nodeValue,
			loTemp:			root.getElementsByTagName("day")[1].getElementsByTagName("lo")[0].firstChild.nodeValue
		};
		
		var forecastD2 = 
		{
			date:			new Date(todayDT.setDate(todayDT.getDate() + 1)),
			condition:		root.getElementsByTagName("day")[2].getElementsByTagName("condition")[0].firstChild.nodeValue,
			icon:			root.getElementsByTagName("day")[2].getElementsByTagName("weather-icon")[0].firstChild.nodeValue,
			hiTemp:			root.getElementsByTagName("day")[2].getElementsByTagName("hi")[0].firstChild.nodeValue,
			loTemp:			root.getElementsByTagName("day")[2].getElementsByTagName("lo")[0].firstChild.nodeValue
		};
		
		var forecastD3 = 
		{
			date:			new Date(todayDT.setDate(todayDT.getDate() + 1)),
			condition:		root.getElementsByTagName("day")[3].getElementsByTagName("condition")[0].firstChild.nodeValue,
			icon:			root.getElementsByTagName("day")[3].getElementsByTagName("weather-icon")[0].firstChild.nodeValue,
			hiTemp:			root.getElementsByTagName("day")[3].getElementsByTagName("hi")[0].firstChild.nodeValue,
			loTemp:			root.getElementsByTagName("day")[3].getElementsByTagName("lo")[0].firstChild.nodeValue
		};
		
		var forecastD4 = 
		{
			date:			new Date(todayDT.setDate(todayDT.getDate() + 1)),
			condition:		root.getElementsByTagName("day")[4].getElementsByTagName("condition")[0].firstChild.nodeValue,
			icon:			root.getElementsByTagName("day")[4].getElementsByTagName("weather-icon")[0].firstChild.nodeValue,
			hiTemp:			root.getElementsByTagName("day")[4].getElementsByTagName("hi")[0].firstChild.nodeValue,
			loTemp:			root.getElementsByTagName("day")[4].getElementsByTagName("lo")[0].firstChild.nodeValue
		};
		
		drawForecast(forecastD0, forecastD1, forecastD2, forecastD3, forecastD4);
	}
}


function drawCurrents(currents)
{
	document.getElementById("todaysDate").innerHTML += formatDT(currents.date);
	document.getElementById("currentIcon").src = getIcon(currents.condition);
	document.getElementById("currentTemp").innerHTML = formatTemp(currents.temperature);
	document.getElementById("currentCondition").innerHTML = currents.condition;
	document.getElementById("currentWind").innerHTML += formatDirection(currents.windDir) + " " + formatSpeed(currents.windSpeed);
}

function drawForecast(forecastD0, forecastD1, forecastD2, forecastD3, forecastD4)
{
	// D0
	document.getElementById("currentHi").innerHTML = formatTemp(forecastD0.hiTemp);
	document.getElementById("currentLo").innerHTML = formatTemp(forecastD0.loTemp);
	document.getElementById("currentSunrise").innerHTML += forecastD0.sunrise + "AM";
	document.getElementById("currentSunset").innerHTML += forecastD0.sunset + "PM";
	
	// D1
	document.getElementById("forecastD1Date").innerHTML = formatDT(forecastD1.date);
	document.getElementById("forecastD1Condition").innerHTML = forecastD1.condition;
	document.getElementById("forecastD1Icon").src = formatIcon(forecastD1.icon);
	document.getElementById("forecastD1Hi").innerHTML += formatTemp(forecastD1.hiTemp);
	document.getElementById("forecastD1Lo").innerHTML += formatTemp(forecastD1.loTemp);
	
	// D2
	document.getElementById("forecastD2Date").innerHTML = formatDT(forecastD2.date);
	document.getElementById("forecastD2Condition").innerHTML = forecastD2.condition;
	document.getElementById("forecastD2Icon").src = formatIcon(forecastD2.icon);
	document.getElementById("forecastD2Hi").innerHTML += formatTemp(forecastD2.hiTemp);
	document.getElementById("forecastD2Lo").innerHTML += formatTemp(forecastD2.loTemp);
	
	// D3
	document.getElementById("forecastD3Date").innerHTML = formatDT(forecastD3.date);
	document.getElementById("forecastD3Condition").innerHTML = forecastD3.condition;
	document.getElementById("forecastD3Icon").src = formatIcon(forecastD3.icon);
	document.getElementById("forecastD3Hi").innerHTML += formatTemp(forecastD3.hiTemp);
	document.getElementById("forecastD3Lo").innerHTML += formatTemp(forecastD3.loTemp);
	
	// D4
	document.getElementById("forecastD4Date").innerHTML = formatDT(forecastD4.date);
	document.getElementById("forecastD4Condition").innerHTML = forecastD4.condition;
	document.getElementById("forecastD4Icon").src = formatIcon(forecastD4.icon);
	document.getElementById("forecastD4Hi").innerHTML += formatTemp(forecastD4.hiTemp);
	document.getElementById("forecastD4Lo").innerHTML += formatTemp(forecastD4.loTemp);
}

function formatDT(oDT)
{
	var dayBox = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	var monthBox = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	return dayBox[oDT.getDay()] + ", " + monthBox[oDT.getMonth()] + " " + oDT.getDate() + ", " + oDT.getFullYear();
}

function formatTemp(temp)
{
	return temp + "&deg;F";
}

function formatSpeed(speed)
{
	return speed + "mph";
}

function formatIcon(icon)
{
	return condImagePath + icon + ".gif";
}

function formatDirection(direction)
{
	var directionDictionary = new Array();
	directionDictionary["north"] = "N";
	directionDictionary["south"] = "S";
	directionDictionary["east"] = "E";
	directionDictionary["west"] = "W";
	directionDictionary["northwest"] = "NW";
	directionDictionary["northeast"] = "NE";
	directionDictionary["southwest"] = "SW";
	directionDictionary["southeast"] = "SE";
	
	return directionDictionary[direction.toLowerCase()];
}

function getIcon(cond)
{
	var mostlycloudy =		"Mostly Cloudy | Mostly Cloudy with Haze | Mostly Cloudy and Breezy | Mostly Cloudy and Windy | Overcast and Windy".split(" | ");
	var sunny =				"Fair | Clear | Fair with Haze | Clear with Haze | Fair and Breezy | Clear and Breezy | Windy | Breezy | Fair and Windy".split(" | ");
	var partlycloudy = 		"A Few Clouds | A Few Clouds with Haze | A Few Clouds and Breezy | Partly Cloudy | Partly Cloudy with Haze | Partly Cloudy and Breezy | A Few Clouds and Windy | Partly Cloudy and Windy".split(" | ");
	var cloudy = 			"Overcast | Overcast with Haze | Overcast and Breezy".split(" | ");
	var fog =	 			"Fog/Mist | Fog | Freezing Fog | Shallow Fog | Partial Fog | Patches of Fog | Fog in Vicinity | Freezing Fog in Vicinity | Shallow Fog in Vicinity | Partial Fog in Vicinity | Patches of Fog in Vicinity | Showers in Vicinity Fog | Light Freezing Fog | Heavy Freezing Fog | Smoke | Dust | Low Drifting Dust | Blowing Dust | Sand | Blowing Sand | Low Drifting Sand | Dust/Sand Whirls | Dust/Sand Whirls in Vicinity | Dust Storm | Heavy Dust Storm | Dust Storm in Vicinity | Sand Storm | Heavy Sand Storm | Sand Storm in Vicinity".split(" | ");
	var rainsnow =			"Freezing Rain | Freezing Drizzle | Light Freezing Rain | Light Freezing Drizzle | Heavy Freezing Rain | Heavy Freezing Drizzle | Freezing Rain in Vicinity | Freezing Drizzle in Vicinity | Ice Pellets | Light Ice Pellets | Heavy Ice Pellets | Ice Pellets in Vicinity | Showers Ice Pellets | Thunderstorm Ice Pellets | Ice Crystals | Hail | Small Hail/Snow Pellets | Light Small Hail/Snow Pellets | Heavy small Hail/Snow Pellets | Showers Hail | Hail Showers | Freezing Rain Snow | Light Freezing Rain Snow | Heavy Freezing Rain Snow | Freezing Drizzle Snow | Light Freezing Drizzle Snow | Heavy Freezing Drizzle Snow | Snow Freezing Rain | Light Snow Freezing Rain | Heavy Snow Freezing Rain | Snow Freezing Drizzle | Light Snow Freezing Drizzle | Heavy Snow Freezing Drizzle | Rain Ice Pellets | Light Rain Ice Pellets | Heavy Rain Ice Pellets | Drizzle Ice Pellets | Light Drizzle Ice Pellets | Heavy Drizzle Ice Pellets | Ice Pellets Rain | Light Ice Pellets Rain | Heavy Ice Pellets Rain | Ice Pellets Drizzle | Light Ice Pellets Drizzle | Heavy Ice Pellets Drizzle | Rain Snow | Light Rain Snow | Heavy Rain Snow | Snow Rain | Light Snow Rain | Heavy Snow Rain | Drizzle Snow | Light Drizzle Snow | Heavy Drizzle Snow | Snow Drizzle | Light Snow Drizzle | Heavy Drizzle Snow | Freezing Rain Rain | Light Freezing Rain Rain | Heavy Freezing Rain Rain | Rain Freezing Rain | Light Rain Freezing Rain | Heavy Rain Freezing Rain | Freezing Drizzle Rain | Light Freezing Drizzle Rain | Heavy Freezing Drizzle Rain | Rain Freezing Drizzle | Light Rain Freezing Drizzle | Heavy Rain Freezing Drizzle".split(" | ");
	var heavyrain = 		"Rain Showers | Light Rain Showers | Light Rain and Breezy | Heavy Rain Showers | Rain Showers in Vicinity | Light Showers Rain | Heavy Showers Rain | Showers Rain | Showers Rain in Vicinity | Rain Showers Fog/Mist | Light Rain Showers Fog/Mist | Heavy Rain Showers Fog/Mist | Rain Showers in Vicinity Fog/Mist | Light Showers Rain Fog/Mist | Heavy Showers Rain Fog/Mist | Showers Rain Fog/Mist | Showers Rain in Vicinity Fog/Mist | Showers in Vicinity | Showers in Vicinity Fog/Mist | Showers in Vicinity Fog | Showers in Vicinity Haze".split(" | ");
	var thunderstorms =		"Thunderstorm | Thunderstorm Rain | Light Thunderstorm Rain | Heavy Thunderstorm Rain | Thunderstorm Rain Fog/Mist | Light Thunderstorm Rain Fog/Mist | Heavy Thunderstorm Rain Fog and Windy | Heavy Thunderstorm Rain Fog/Mist | Thunderstorm Showers in Vicinity | Light Thunderstorm Rain Haze | Heavy Thunderstorm Rain Haze | Thunderstorm Fog | Light Thunderstorm Rain Fog | Heavy Thunderstorm Rain Fog | Thunderstorm Light Rain | Thunderstorm Heavy Rain | Thunderstorm Rain Fog/Mist | Thunderstorm Light Rain Fog/Mist | Thunderstorm Heavy Rain Fog/Mist | Thunderstorm in Vicinity Fog/Mist | Thunderstorm Showers in Vicinity | Thunderstorm in Vicinity Haze | Thunderstorm Haze in Vicinity | Thunderstorm Light Rain Haze | Thunderstorm Heavy Rain Haze | Thunderstorm Fog | Thunderstorm Light Rain Fog | Thunderstorm Heavy Rain Fog | Thunderstorm Hail | Light Thunderstorm Rain Hail | Heavy Thunderstorm Rain Hail | Thunderstorm Rain Hail Fog/Mist | Light Thunderstorm Rain Hail Fog/Mist | Heavy Thunderstorm Rain Hail Fog/Hail | Thunderstorm Showers in Vicinity Hail | Light Thunderstorm Rain Hail Haze | Heavy Thunderstorm Rain Hail Haze | Thunderstorm Hail Fog | Light Thunderstorm Rain Hail Fog | Heavy Thunderstorm Rain Hail Fog | Thunderstorm Light Rain Hail | Thunderstorm Heavy Rain Hail | Thunderstorm Rain Hail Fog/Mist | Thunderstorm Light Rain Hail Fog/Mist | Thunderstorm Heavy Rain Hail Fog/Mist | Thunderstorm in Vicinity Hail | Thunderstorm in Vicinity Hail Haze | Thunderstorm Haze in Vicinity Hail | Thunderstorm Light Rain Hail Haze | Thunderstorm Heavy Rain Hail Haze | Thunderstorm Hail Fog | Thunderstorm Light Rain Hail Fog | Thunderstorm Heavy Rain Hail Fog | Thunderstorm Small Hail/Snow Pellets | Thunderstorm Rain Small Hail/Snow Pellets | Light Thunderstorm Rain Small Hail/Snow Pellets | Heavy Thunderstorm Rain Small Hail/Snow Pellets | Thunderstorm in Vicinity | Thunderstorm in Vicinity Fog | Thunderstorm in Vicinity Haze".split(" | ");
	var snow = 				"Snow | Light Snow | Heavy Snow | Snow Showers | Light Snow Showers | Heavy Snow Showers | Showers Snow | Light Showers Snow | Heavy Showers Snow | Snow Fog/Mist | Light Snow Fog/Mist | Heavy Snow Fog/Mist | Snow Showers Fog/Mist | Light Snow Showers Fog/Mist | Heavy Snow Showers Fog/Mist | Showers Snow Fog/Mist | Light Showers Snow Fog/Mist | Heavy Showers Snow Fog/Mist | Snow Fog | Light Snow Fog | Heavy Snow Fog | Snow Showers Fog | Light Snow Showers Fog | Heavy Snow Showers Fog | Showers Snow Fog | Light Showers Snow Fog | Heavy Showers Snow Fog | Showers in Vicinity Snow | Snow Showers in Vicinity | Snow Showers in Vicinity Fog/Mist | Snow Showers in Vicinity Fog | Low Drifting Snow | Blowing Snow | Snow Low Drifting Snow | Snow Blowing Snow | Light Snow Low Drifting Snow | Light Snow Blowing Snow | Light Snow Blowing Snow Fog/Mist | Heavy Snow Low Drifting Snow | Heavy Snow Blowing Snow | Thunderstorm Snow | Light Thunderstorm Snow | Heavy Thunderstorm Snow | Snow Grains | Light Snow Grains | Heavy Snow Grains | Heavy Blowing Snow | Blowing Snow in Vicinity".split(" | ");
	var drizzle = 			"Light Rain | Drizzle | Light Drizzle | Heavy Drizzle | Light Rain Fog/Mist | Drizzle Fog/Mist | Light Drizzle Fog/Mist | Heavy Drizzle Fog/Mist | Light Rain Fog | Drizzle Fog | Light Drizzle Fog | Heavy Drizzle Fog".split(" | ");
	var rain = 				"Rain | Heavy Rain | Rain Fog/Mist | Heavy Rain Fog/Mist | Rain Fog | Heavy Rain Fog".split(" | ");
	var tornado = 			"Funnel Cloud | Funnel Cloud in Vicinity | Tornado/Water Spout".split(" | ");
	var haze = 				"Haze".split(" | ");
	
	var conditions = new Array(mostlycloudy, sunny, partlycloudy, cloudy, fog, rainsnow, heavyrain, thunderstorms, snow, drizzle, rain, tornado, haze);
	var icons = new Array("mostlycloudy", "sunny", "partlycloudy", "cloudy", "fog", "rain-snow", "heavyrain", "thunderstorms", "snow", "drizzle", "rain", "tornado", "haze");
	
	for (var i = 0; i < conditions.length; i++)
		for (var j = 0; j < conditions[i].length; j++)
			if (conditions[i][j] == cond) return formatIcon(icons[i]);
}

function getParsableDT(sDT)
{
	return sDT.substr(0, sDT.length - 10);
}
