// I contain the code for the RSS news feed and the XML weather feed

// Load the RSS news feed
function loadNewsFeed() { 
	
	<!-- // News feed basic formatting:
		 // <h4><a href="entry.link" onclick="window.open(this.href)">entry.title</a></h4>
		 // <p>entry.contentSnippe (description)t</p>	
	// -->
	
	var feed = new google.feeds.Feed("http://lmtonline.com/?rss=news"); // URL of the RSS news feed
	
	feed.load(function(result) { 
	
		if (!result.error) { 
			
			var newsUL = $('<ul/>');
			var newsLI = $('<li/>');
			var spacing = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			
			$(newsUL).attr("id","newsTicker");
			$(newsLI).append('<strong>Laredo Morning Times News:</strong>' + spacing);
			
			for (var i = 0; i < result.feed.entries.length; i++) { 
				
				var entry = result.feed.entries[i]; 

				var newsTitleLink = $('<a/>'); // News item link
				$(newsTitleLink).attr('href', entry.link.toString()); 	// Set the link URL
				$(newsTitleLink).html(entry.title.toString());   //  Set the link text
				$(newsTitleLink).click(function(){  // Open it in a new window
					window.open(this.href,'newWindow','location=1,menubar=1,toolbar=1,status=1,scrollbar=1,resizable=1,width=600,height=600');
					return false;
				});
				$(newsLI).append(newsTitleLink);  // Add the link to the LI
				$(newsLI).append(' &mdash; ');  // Add the dash to the LI
				$('<span/>').html(entry.contentSnippet.toString() + spacing).appendTo(newsLI);  // Add short description to the LI
			}
			
			$(newsLI).append($(newsLI).html()); // Make a copy of the news LI contents so it's doubled 2x (so the ticker appears to loop)
			$(newsLI).append($(newsLI).html()); // Make a another copy of the news LI contents so it's 4x
			$(newsLI).appendTo(newsUL);
			$(newsUL).appendTo('#newsContainer');
				
			$(newsUL).liScroll({travelocity: 0.06}); // Make the list a scrolling ticker
			
		} else {
			alert("Error fetching news feed data. The news feed is currently unavailable."); 
		}
	});  // END feed.load()
}   // END loadNewsFeed




function loadWeather() {
	// Load the Zapata County Airport weather data, we append the weather data into an ordered list item and then apply the liScroll plug-in.
		
	$.ajax({
		type: "GET", 
		// If you change the URL you will also need to change the URL in the proxy.aspx.cs file or the proxy will ignore the request.
		// http://www.weather.gov/xml/current_obs/KAPY.xml
		// http://www.weather.gov/xml/current_obs/seek.php?state=tx&Find=Find 
		// http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=KAPY 
		// url: "proxy.aspx?u=http://www.weather.gov/xml/current_obs/KAPY.xml",  This is the old script
		url: "getWeather.aspx?type=current",
		dataType: "xml",
		success: function(xml, status, error) {

			var weather = $(xml).find('weather').text(); // Get the 'weather' node
			var temp_f = $(xml).find('temp_f').text(); // Get the 'weather' node
			var relative_humidity = $(xml).find('relative_humidity').text();
			var wind_string = $(xml).find('wind_string').text();
			var pressure_in = $(xml).find('pressure_in').text();
			var dewpoint_f = $(xml).find('dewpoint_f').text();
			var heat_index_f = $(xml).find('heat_index_f').text();
			var windchill_f = $(xml).find('windchill_f').text();
			var local_time = $(xml).find('observation_time').text();
			var weatherIcon = $(xml).find('icon_url_base').text(); // Get the image icon path
			weatherIcon = weatherIcon + $(xml).find('icon_url_name').text(); // Get the image icon name
			var creditIcon = $(xml).find('url').text(); // Get the NOAA logo 
			
			// Output the XML data into some basic containers
			
			// $('<img/>').attr('src',weatherIcon).appendTo('#currentWeather');
			//$('#weatherContainer').css('background','url(' + weatherIcon + ') top right no-repeat').attr('alt',weather);
			
			var weatherUL = $('<ul/>'); 
			var weatherLI = $('<li/>');
			var spacing = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			
			$(weatherUL).attr("id","weatherTicker");
			
			$(weatherLI).append('<strong>Zapata, TX Weather (KAPY):</strong>' + spacing);
			$(weatherLI).append('<em>' + local_time + '</em>' + spacing);
			$(weatherLI).append('<em>Currently: </em>' + weather + ', ' + temp_f + 'F' + spacing);
			if(heat_index_f && heat_index_f != 'NA') {
				$(weatherLI).append('<em>Heat Index: </em>' + heat_index_f + 'F' + spacing);
			} else if(windchill_f && windchill_f != 'NA') {
				$(weatherLI).append('<em>Wind Chill :</em>' + windchill_f + 'F' + spacing);
			}
			$(weatherLI).append('<em>Winds: </em>' + wind_string + '' + spacing);
			$(weatherLI).append('<em>Humidiy: </em>' + relative_humidity + '%' + spacing);
			$(weatherLI).append('<em>Pressure: </em>' + pressure_in + 'in Hg' + spacing);
			$(weatherLI).append('<em>Dewpoint: </em>' + dewpoint_f + 'F' + spacing);
			
			//$(weatherLI).append('<a href="" target="_blank" title="NOAA\'s National Weather Service" class="newWindow"></a>' + spacing);
			var NOAALink = $('<a/>'); // NOAA Link
			$(NOAALink).attr('href', 'http://weather.gov'); 	// Set the link URL
			$(NOAALink).html('NOAA Weather');   //  Set the link text
			$(NOAALink).click(function(){  // Open it in a new window
				window.open(this.href,'newWindow','location=1,menubar=1,toolbar=1,status=1,scrollbar=1,resizable=1,width=600,height=600');
				return false;
			});
			$(weatherLI).append(NOAALink);
			$(weatherLI).append(spacing);
			
			$(weatherLI).append($(weatherLI).html()); // Make a copy of the weather LI contents so it's doubled 2x (so the ticker appears to loop)
			$(weatherLI).append($(weatherLI).html()); // Make a another copy of the weather LI contents so it's 4x
			$(weatherLI).append($(weatherLI).html()); // Make a another copy of the weather LI contents so it's 8x
			
			$(weatherUL).append(weatherLI);
			$(weatherUL).appendTo('#weatherContainer');
			
			$(weatherUL).liScroll({travelocity: 0.06}); // Make the list a scrolling ticker

		}, 
		error: function(request, status, error) {
			alert('We were unable to retrieve the current weather conditions.');
			//alert("XMLHttpRequest="+request.responseText+"\ntextStatus="+status+"\nerrorThrown="+error);
		}
	});	
	
}
