////////////////////////////////////////////////////
// Traffic Camera Widget
// Displays RIDOT traffic cam on a web page
// View is automatically updated every 30 seconds
///////////////////////////////////////////////////
// Written by Timothy C. Barmann
// The Providence Journal
// 6-19-2009
// tbarmann at projo.com
// ////////////////////////////////////////////////

// Usage:
// <script> showTrafficCam(camera_number,width); </script>
// camera_number (optional) is the RIDOT camera number
// if no camera_number is given, the default is 1
// width (optional) is how wide to show the camera image, in pixels. Height
// is automatically scaled to the correct proportion
// if no width is specific, the default width is 352 pixels wide.
///////////////////////////////////////////////////////////////////

function getCurrentTimeString() {
// returns string like 1:03:09 PM
// or 12:29:49 AM

var ampm = "";
var d = new Date();
var curr_hour = d.getHours();
if (curr_hour < 12)
   ampm = "AM";
else
   ampm = "PM";
if (curr_hour == 0)
   curr_hour = 12;
if (curr_hour > 12)
   curr_hour = curr_hour - 12;
var curr_min = d.getMinutes();
curr_min = curr_min + "";
if (curr_min.length == 1)
   {
   curr_min = "0" + curr_min;
   }
var curr_sec = d.getSeconds();
curr_sec = curr_sec + "";
if (curr_sec.length == 1)
   {
   curr_sec = "0" + curr_sec;
   }

return(curr_hour + ":" + curr_min + ":" + curr_sec + " " + ampm);

}

function refreshCam(camID) {

 camIDstr = 'cam' + camID;
 camLabelIDStr = 'camLabel' + camID;
 var ts=parseInt(new Date().getTime().toString().substring(0, 10));
 document.getElementById(camIDstr).src = 'http://www.tmc.dot.ri.gov/CamCenter/camImage.aspx?camID=' + camID + '&ts=' +ts;
 document.getElementById(camLabelIDStr).innerHTML = 'Current as of ' + getCurrentTimeString();
 jsCommand = 'refreshCam(' + camID + ');';
 timeOut = setTimeout(jsCommand, 30000);

 }


function showTrafficCam(cam,width) {
	if (cam === undefined)
		cam = "1";
	if (width === undefined)
		width="352";

	height = parseInt(width*0.81);
	var src='';
	var imgStyle='border:1px solid #000000;';
	var camID = 'cam' + cam;
	var labelID = 'camLabel' + cam;
	var camLabelStyle = 'font-size:10px;font-family:arial, Helvetica,sans-serif; width:' + width + 'px; ';
	var updateStyle = 'float:left;';
	var creditStyle = 'float:right;';
	var creditText = 'RIDOT';
	var creditLink = 'http://www.tmc.state.ri.us/';
	document.write('<img ');
	document.write('style="' + imgStyle + '" ');
	document.write('src="" ');
	document.write('width="'+width+'" height="' + height +'" ');
	document.write('id="' + camID + '" ');
	document.write('alt="Image temporarily unavailable. Refresh the Web page to try again." />');
	document.write('<div style="' + camLabelStyle + '" >');
		document.write('<div style="' + updateStyle + '" ');
		document.write('id="' + labelID + '" ');
		document.write('></div>');
		document.write('<div style="' + creditStyle + '" ');
		document.write('<a href="' + creditLink + '">' + creditText + '</a>');
		document.write('</div>');
	document.write('</div>');
	refreshCam(cam);
}

