$(document).ready(function() {
   xmlpath = "xmlfetch.php?id=Latest&cachetime=0"
   $('.nojs').css({"display": "none"})
   $("#data").append('<img src="images/ajax-loader.gif" class="loader" />')
   $('.loader').css({'position':'relative','left':'240px','top':'150px'});
	$('.locations').live('click',function(e){popwindow(e)});
function popwindow(e){
e.preventDefault();
var href=$(e.target).attr('href');
window.open(href,'','menubar=1,location=1,toolbar=1,width=1024,height=768');
};   
   function refreshData () {
	$.ajax({
		type: "GET",
		url: xmlpath,
		dataType: "xml",
		cache: false,
		success: function(xml) {
			statusQ = [];
			$(xml).find('location').each(function() {
				var name = $(this).attr('name');
				var long = $(this).attr('longitude');
				var lat = $(this).attr('latitude');
				if ($(this).find('datetime').size() >= 1) {
					$(this).find('datetime').each(function() {
						var date = $(this).attr('date');
						var time = $(this).attr('time');
						$(".loader").hide();
						$('<table cellpadding="0" cellspacing="0" class="table_' + name + '"></table>').html('<tr><th colspan="2">' + name + ' <span>(' + date + ' - ' + time + ')</span></th>').appendTo('#data');
						$(this).find('parameter').each(function() {
							attrname = $(this).attr('name');
							attrvalue = $(this).attr('value');
							attrunit = $(this).attr('unit');
							$('<tr class="'+attrname+'"></tr>').html('<td><strong>'+attrname+':</strong>&nbsp;&nbsp;</td><td>'+attrvalue+'&nbsp;'+attrunit+'</td>').appendTo('.table_'+name);
						});
					});
				} else {statusQ.push("<br />** Sensor "+name+" is not reporting")};
			});
			var listItem = $('table')
			var indexedtables = $("#data").index(listItem);
			if (indexedtables <= 12) {
				$(".table_M71").after('<div class="dataside"><div class="refreshdata">Refresh Data</div><a class="locations" href="/data/map.php" target="_blank">View Locations/Data On A Map</a> <p class="status">'+$.each(statusQ, function(i, val){$(val).html();})+'</p><br /><p>The Current River Data Chart displayed here is to make it easier to get current river information in near real time from one central location. Current river data displayed for pH, conductivity and temperature is collected from various sites along the Allegheny, Monongahela, Ohio, Youghiogheny, Chenango, and Beaver rivers.</p></div>');
			} else {
				$(".table_M4").after('<div class="dataside">hi'+$.each(statusQ, function(i, val){$(val).html();})+'</div>');
			}
		},
		error: function(){$(".loader").hide(); $('<div class="nojs"><h4>No data is currently available.  Please check back shortly.</h4></div>').appendTo("#data")}
	});
   }
   refreshData();
   $(".refreshdata").live("click", function() {$("#data").empty(); $("#data").append('<img src="images/ajax-loader.gif" class="loader" />');$('.loader').css({'position':'relative','left':'240px','top':'150px'}); refreshData();})
	
});
