// Start the first query.
window.setTimeout('getData()', 0);

// When all queries have finished, retrieve the data
$(document).ajaxComplete(gotData);

function getData() {
 $.ajax({
   type: "GET",
   url: "weather.asp",
   dataType: "xml",
   error: setAJAXTimeout
 });
}

function setAJAXTimeout() {
	window.setTimeout('getData()', 1000);
}

function gotData(e, xmlhttp, opt) {
	var icon_url1;
	var icon_url2;
	if (xmlhttp.responseXML == null || xmlhttp.responseXML.documentElement == null)
	       return;
       x=xmlhttp.responseXML.documentElement.getElementsByTagName("WeatherInfo");
       xx = x[0].getElementsByTagName("*");

       for (var i=0; i < xx.length; i++)
       {
		   nName = xx[i].nodeName;
		   if (nName == "weathericon1")
			icon_url1 = xx[i].firstChild.nodeValue;
		   if (nName == "weathericon2")
			icon_url2 = xx[i].firstChild.nodeValue;
		   if ((nName == "temperature") || (nName == "hightemperature"))
			   if (document.getElementById)
			   {
					   elmnt = document.getElementById(nName);
					   if (elmnt != null && typeof(elmnt) == 'object')
							   if (xx[i].firstChild != null)
								{
									if (nName == "temperature")
										elmnt.innerHTML = "Currently: " + xx[i].firstChild.nodeValue + "&deg;";
									else // (nName == "hightemperature")
										elmnt.innerHTML = "Tomorrow: " + xx[i].firstChild.nodeValue + "&deg;";
								}
							   else
								   elmnt.innerHTML = "";
			   }
       }

	   $(function () {
		  var img = new Image();
		  
		  // wrap our new image in jQuery, then:
		  $(img)
		    // once the image has loaded, execute this code
		    .load(function () {
		      // set the image hidden by default    
		      $(this).hide();
		    
		      // with the holding div #loader, apply:
		      $('#weathericon1')
		        // then insert our image
		        .append(this);
		    
		      // fade our image in to create a nice effect
		      $(this).fadeIn();
		    })
		    
		    // if there was an error loading the image, react accordingly
		    .error(function () {
		      // notify the user that the image could not be loaded
		    })
		    
		    // *finally*, set the src attribute of the new image to our image
		    .attr('src', 'http://www.tcbankutah.com/images/' + icon_url1 + '.gif');
		});
	   
	   $(function () {
		  var img = new Image();
		  
		  // wrap our new image in jQuery, then:
		  $(img)
		    // once the image has loaded, execute this code
		    .load(function () {
		      // set the image hidden by default    
		      $(this).hide();
		    
		      // with the holding div #loader, apply:
		      $('#weathericon2')
		        // then insert our image
		        .append(this);
		    
		      // fade our image in to create a nice effect
		      $(this).fadeIn();
		    })
		    
		    // if there was an error loading the image, react accordingly
		    .error(function () {
		      // notify the user that the image could not be loaded
		    })
		    
		    // *finally*, set the src attribute of the new image to our image
		    .attr('src', 'http://www.tcbankutah.com/images/' + icon_url2  + '.gif');
		});
}
