// poll for new image every interval seconds
var poll_interval = 10;
var online_interval = 30;
var offline_interval = 300;
var next_update = 0;
var timer;
var pic;
var running = 0;
var online = 0;
var refreshed;
var last_time = 0;
var rts_mo = 3;
var rts_day = 10;
var rts_yr = 2009;
var image = 'images/webcam.jpg';

function start()
{
	pic = document.getElementById('pic');
    refreshed = document.getElementById('refreshed');
	refreshed.innerHTML = 'Image is automatically refreshed';
	document.control.pause.value = 'Pause';
	timer = setInterval('update()', poll_interval * 1000);
	running = 1;
	online = 1;
	next_update = 0;
	update();
}

function stop()
{
	clearInterval(timer);
	refreshed.innerHTML = 'Paused';
	document.control.pause.value = 'Resume';
	running = 0;
}

function toggle()
{
	if (!running)
		start();
	else
		stop();
}

function update()
{
	var now = new Date();
	t = now.getTime();
	if (t >= next_update)
	{
		next_update = t + online_interval * 1000 - 500;
		getPicInfo();
	}
}

var IFrameObj;
function getPicInfo(theFormName)
{
	if (!document.createElement)
		return true;
	var IFrameDoc;
	var URL = 'getPicInfo.php?image=' + image;
	
	if (   !IFrameObj
		&& document.createElement)
	{
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time 
		// getPicInfo() is called
		try
		{
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','RSIFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			IFrameObj = document.body.appendChild(tempIFrame);
			
			if (document.frames)
			{
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames['RSIFrame'];
			}
		}
		catch(exception)
		{
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			iframeHTML='<iframe id="RSIFrame" style="border:0px;width:0px;height:0px;"><\/iframe>';
			document.body.innerHTML += iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
			IFrameObj.document.location.replace = function(location) { this.iframe.src = location; }
		}
		setTimeout('getPicInfo("'+theFormName+'")', 10);
		return false;
	}
	
	if (   navigator.userAgent.indexOf('Gecko') != -1
	    && !IFrameObj.contentDocument)
	{
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout('getPicInfo("'+theFormName+'")', 10);
		return false;
	}
	
	if (IFrameObj.contentDocument)
	{
		// For NS6
		IFrameDoc = IFrameObj.contentDocument; 
	}
	else
	if (IFrameObj.contentWindow)
	{
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	}
	else
	if (IFrameObj.document)
	{
		// For IE5
		IFrameDoc = IFrameObj.document;
	}
	else
		return true;
	
	IFrameDoc.location.replace(URL);
	return false;
}

// handlePicInfo is passed the latest image time when called from the onload
// event of the pages loaded in the hidden IFRAME
function handlePicInfo(time, width, height, age)
{
	var now = new Date();
	t = now.getTime();
	if (age > offline_interval)
	{
		next_update = t + offline_interval * 1000 - 500;
		if (online)
		{
			online = 0;
			refreshed.innerHTML = 'Image is automatically refreshed (camera offline)';
            if (   now.getFullYear() < rts_yr
                || (   now.getMonth() < rts_mo-1
                    || (   now.getMonth() == rts_mo-1
                        && now.getDate() <= rts_day)))
            {
                var month=new Array(12);
                month[0]="January";
                month[1]="February";
                month[2]="March";
                month[3]="April";
                month[4]="May";
                month[5]="June";
                month[6]="July";
                month[7]="August";
                month[8]="September";
                month[9]="October";
                month[10]="November";
                month[11]="December";
                refreshed.innerHTML += '<br>Camera is scheduled to return to service on ' + month[rts_mo-1] + ' ' + rts_day + ', ' + rts_yr;
            }
		}
	}
	else
	{
		next_update = t + online_interval * 1000 - 500;
		if (!online)
		{
			online = 1;
			refreshed.innerHTML = 'Image is automatically refreshed';
		}
	}
	if (time > last_time)
	{
		pic.src = image + '?' + time;
		pic.width = width;
		pic.height = height;
		last_time = time;
	}
}
