var xmlHttp = createXmlHttp();

imagePath = "images/roadreports/resource/loading_camera.jpg";

loading = "<div id=\"holdContainer\" style=\"width: 338px; display: block;\"><div id=\"date\"><div style=\"float: left; padding: 5px; font-weight: bold\"></div><div style=\"float: right; width: 4px;\">&nbsp;</div>";
loading += "</div><div id=\"image\" style=\"clear: both; border: 2px solid #2D566A\"><img src="+imagePath+" width=\"334\" height=\"251\" /></div><div id=\"location\" style=\"background: #112C38; padding-top: 5px; padding-bottom: 5px; padding-left: 2px; padding-right: 2px; text-align: center; border: 2px solid #2D566A;\"><b>Loading Data...</b><br/><br/></div></div>";
loading +=	"<table id=\"dataTable\" width=\"338px\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border: 2px solid #2D566A;\">";
loading +=	"<tr style=\"background: #112C38;\"><td class=\"cellPadder\">Air Temperature:</td><td width=\"50\"></td><td></td></tr>";
loading +=	"<tr style=\"background: #112C38;\"><td class=\"cellPadder\">Pavement Temperature:</td><td width=\"50\"></td><td></td></tr>";
loading +=	"<tr style=\"background: #112C38;\"><td  class=\"cellPadder\">Wind Speed:</td><td width=\"50\"></td><td></td></tr>";
loading +=	"<tr style=\"background: #112C38;\"><td class=\"cellPadder\">Wind Direction:</td><td width=\"50\"></td><td></td></tr>";
loading +=	"<tr style=\"background: #112C38;\"><td class=\"cellPadder\">Relative Humidity:</td><td width=\"50\"></td><td ></td></tr></table>";
loading +=  "<div style=\"font-size: 9px; padding: 5px; color: #dedede;\">TheEnergyNews.com will attempt to provide you with the best service available, but situations may occur that disrupt service or cause misinformation. The information provided by camera feeds cannot be guaranteed as to accuracy or timeliness. Under ideal conditions, images and conditions reload every 30 minutes, but this information depends on a number of items, including internet availability, communications networks, and computer equipment which are beyond the control of TheEnergyNews.com<br/><br/>These reports are to be used for informational purposes only and should not be used as a basis for making financial or other commitments.</div>";


function updateMap(site, image){
	myDiv = document.getElementById("rightColResult");
	myDiv.innerHTML = loading;
	processRoadReport(site, image);
}

function processRoadReport(site, image){

	if (typeof document.body.style.maxHeight != "undefined") {
	  // IE 7, mozilla, safari, opera 9
		path = "weather/roadreports/get_data.php";
	} else {
	  // IE6, older browsers
		path = "../../weather/roadreports/get_data.php";
	}

	vars = "?site="+site+"&image="+image;
	//alert(path+vars);
	//alert("XML HTTP STATE: "+xmlHttp.readyState);
	//only continue if xmlHttp isn't void
	if(xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		//try to connect to the server
		try{
			// Initiate reading the async.txt file from the server
			xmlHttp.open("GET", path+vars, true);
			xmlHttp.send(null);
			//Send the proper header information along with the request
			xmlHttp.onreadystatechange = handleRequestStateChangeRoad;
		}catch(z){
			//Display the error in case of failure
			alert("Can't Connect to the server:\n"+ z.toString());
		}
		
	}
	
	
}// END Process Function



/*************************************
***** HANDLE REQUESTS START HERE *****
*************************************/

// Function that handles the http response
function handleRequestStateChangeRoad(){
	//alert("Handling");
	// Obtain a reference to the <div> element on the page
	myDiv = document.getElementById("rightColResult");
			//Continue only if the server response if "OK"
			if(xmlHttp.readyState == 4){
				
				if(xmlHttp.status == 200){
				try{
					//Read the message from the server
					response = xmlHttp.responseText;
					// Display the result
					myDiv.innerHTML = response;
					//alert(response);
				}catch(z){
					alert("Error reading the response: "+ z.toString());
				}
			
			}else{
				// Display error message
				alert("There was an error getting the data:\n" + xmlHttp.statusText)
			}
			}
			
}


/*** END HANDLE REQUEST METHODS ***/

/*** START SMALLER PAGE FUNCTIONS ***/	

var currentTime = new Date();	
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var hours = currentTime.getHours();


function ImageSwap(imageTab, station, imageNumber, imageTotal){
	var minutes = currentTime.getMinutes();
	if(minutes > 00 && minutes < 20){
		minutes = 20;
	}else if(minutes > 20 && minutes < 40){
		minutes = 40;
	}else{
		minutes = 00;
	}
	var lastDate = month+"M"+day+"D"+year+"Y"+hours+"H"+minutes;
	//alert(lastDate);
	for(i=1; i <= imageTotal; i++){
		imageDiv = "Image"+i;
		document.getElementById(imageDiv).className='buttonDeselect';
	}
	imageTab.className='buttonSelect';
	
	if(imageNumber == 50){
		imageHTML = '<img src="images/roadreports/resource/unavail.jpg" width="334" height="251"/>';
	}else{
		imageHTML = '<img src="images/roadreports/'+station+'_'+imageNumber+'.jpg?nocache='+lastDate+' width="334" height="251"/>';
	}
	document.getElementById("image").innerHTML=imageHTML;
}	
	
function getRoadCondition(highway){
if (typeof document.body.style.maxHeight != "undefined") {
	  // IE 7, mozilla, safari, opera 9
		path = "weather/roadreports/get_roaddata.php";
	} else {
	  // IE6, older browsers
		path = "../../weather/roadreports/get_roaddata.php";
	}

	vars = "?highway="+highway;
	if (xmlHttp) {
	  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
	  xmlHttp.open('GET',path+vars, true);  

	  xmlHttp.onreadystatechange = getRoadInfo;
      xmlHttp.send(null);
	}else
	{
	  setTimeout('setAvail('+highway+')', 250);
	}		
  
  }else
  {
	alert('There was an error creating the XML Object.');
  }
}



function getRoadInfo()
{
if (xmlHttp.readyState == 4) {
    if (xmlHttp.status == 200) {
     var xmlResponse = xmlHttp.responseXML;
     var xmlRoot = xmlResponse.documentElement;
     document.getElementById("roadcondition").innerHTML='';   
	 if(!xmlRoot.getElementsByTagName('error')[0]){
	     var highwayArr = xmlResponse.getElementsByTagName('highway');
	     var roadcondition = '';
	     for(var i=0; i<highwayArr.length; i++){
	        var highway_name = "<br/><div id=\"highwayname\" style=\"clear:both;\"><strong>"+highwayArr[i].attributes[0].value+"</strong></div><br />";		 	        													
	        var tableTitle = "<table cellspacing=\"1\" cellpadding=\"4\" width=\"665\"><tr>" +													
			             "</tr><td class=\"tableHeader\" colspan=\"2\" >Road Name</td>"+
		                 "<td class=\"tableHeader\">Road Condition</td></tr>";
			
	        var roadArr = highwayArr[i].getElementsByTagName('road');
	        var tablecells = '';
	        for(var j=0; j<roadArr.length; j++)
	        {
	          var cells = " <tr><td bgcolor=\""+roadArr[j].getElementsByTagName('condition')[0].firstChild.data+"\" width=\"2\"></td>"+												
			"<td bgcolor=\"#7A97A5\" width=\"180\">"+roadArr[j].attributes[0].value+"</td>"+
			"<td bgcolor=\"#7A97A5\">"+
			"<strong>Air Temp.</strong>"+roadArr[j].getElementsByTagName('airtemp')[0].firstChild.data+"&#176;C  "+
			"<strong>Pave Temp.</strong>"+roadArr[j].getElementsByTagName('pavetemp')[0].firstChild.data+"&#176;C "+
			"<strong>Wind Speed</strong>"+roadArr[j].getElementsByTagName('windspeed')[0].firstChild.data+" km/h "+
			"<strong>Wind Dir.</strong>  "+roadArr[j].getElementsByTagName('winddir')[0].firstChild.data+" "+
			"<strong>Hum.</strong>"+roadArr[j].getElementsByTagName('humidity')[0].firstChild.data+"%</td></tr>";
			tablecells = tablecells + cells;
	        }
	        
	     table = highway_name + tableTitle + tablecells +"</table>";
	     roadcondition  = roadcondition + table;
	     }
	    document.getElementById("roadcondition").innerHTML = roadcondition; 
	   }else
	   {
	     //Show errors here
	   }
  
  }else
  {
    
  }
}else
{
 
  document.getElementById("roadcondition").innerHTML ="<div style=\"text-align: center;\"><img src=\"images/loadingsm_light.gif\" width=\"50\" height=\"50\"/></div> ";
}
  
}
		