/*
DelSys.utilities.js
*/

// Create the DelSys namespace if it doesn't already exist.
var DelSys = DelSys ? DelSys : new Object();

/*
IE doesn't format the hanging indents as I want, maybe because of a float clear issue.
It works properly when the height of the hanging paragraph is set equal to the content height of the hanger paragraph.
The clientHeight property includes content and padding height.
*/
DelSys.hangingTabsInit = function(selector) {
	$(selector).each( function(n) {
		var clientHeight = $(this).next().get(0).clientHeight;
		var paddingBottom = parseInt($(this).next().css("padding-bottom"));
		var paddingTop = parseInt($(this).next().css("padding-top"));
		// content height = clientHeight - padding height.
		this.style.height = (clientHeight - paddingBottom - paddingTop) + "px";
	});
}

// Lookup zip code given city and state.
// <City>Greenbelt</City><State>MD</State>
DelSys.uspsZipLookup = function(ID, addr2, city, state) {
	var url = "proxyUSPSZipLookup.php?ID=" + encodeURIComponent(ID) +
						"&addr2=" + encodeURIComponent(addr2) +
						"&city=" + encodeURIComponent(city) +
						"&state=" + encodeURIComponent(state);
						
	$.get(url, function(xmlDoc, status) {
		var zip5 = $(xmlDoc).find("Zip5");
		return zip5;
	});
}

// Get PST from Yahoo's time service.
DelSys.yahooGetTime = function(selector) {
	var url = "proxyYahooGet?path=" + encodeURIComponent("TimeService/V1/getTime?appid=YahooDemo");
	$.get(url, function(xmlDoc, status) {
		var ts = $(xmlDoc).find("Timestamp").text();
		$(selector).text(ts);
	});
}

// Get weather from Yahoo's weather service.
// Reformat it to allow coherent CSS styling.
// Replace the content of selector with the formatted weather content.
DelSys.yahooGetWeather = function(location, selector) {
	var url = "proxyYahooGetWeather?path=" + encodeURIComponent("forecastrss?p=" + location);
	$.get(url, function(xmlDoc, status) {
		var $item = $(xmlDoc).find("item");
		var title = $item.find("title").text();
		
		// Check for an invalid zipcode.
		var err = title.indexOf("City not found");
		if (err != -1) {
			$(selector).html("<h2>" + location + " Isn't A Valid Zipcode</h2>");

			} else {
			// Delete "Conditions for "
			title = title.replace(/Conditions for /, "");
			
			
			var description = $item.find("description").text();

			//  Delete 1st <br />
			description = description.replace(/<br \/>/, "");
			// Insert <p> in front of <img
			description = description.replace(/<img/, "<p class=\"pConditions\"><img");
			// Replace <b>Current Conditions:</b> with <span class="spanConditions">
			description = description.replace(/<b>Current Conditions\:<\/b><br \/>/, "<span class=\"spanConditions\">");
			// Replace <BR />\n<BR /> with </p>\n
			description = description.replace(/<BR \/>\n<BR \/>/, "</span></p>\n");
			// Replace Forecast: with Forecast
			description = description.replace(/<b>Forecast\:<\/b>/, "");
			// Replace <BR />\n with \n<p>
			description = description.replace(/<BR \/>\n/, "\n<p class=\"pForecast\">");
			// Replace <br />\n<br />\n with </p>\n<p>
			description = description.replace(/<br \/>\n<br \/>\n/, "</p>\n<p class=\"pYahoo\">");
			// Replace )<br/> with )</p>
			description = description.replace(/\)<br\/>/, ")</p>");
			// Chop off everything after and including <a
			var a = description.search(/<a/);
			description = description.substring(0, a);

			$(selector).html("<h2>" + title + "</h2>" + description);
		}
	});
}
