// Functions for The Crossing webcam

var SLOW_CONNECTION = 4000;		// slow connection updates every 4 secs
var FAST_CONNECTION = 2000;		// fast connection updates every 2 secs
var TRY_AGAIN = 1000;			// guess its busy trying to load the image
								// lets wait a sec and try again
var preload = null;
var aTimer = null;
var bufferedImageAvailable = false;
var timeOut = SLOW_CONNECTION;
getNewImage()
function getNewImage() {
		if (document.images) {
			preload = null;
			preload = new Image(384,284);
			preload.onLoad = newBufferedImageAvailable();

			var aRandom = Math.round(Math.random()*1000000);
			preload.src = "http://webcam.abbeyroad.com/crossing.jpg?cacheKiller=" + aRandom;

			aTimer = setTimeout("swapImage()", timeOut); // reset the image swapping
		}
	}

function newBufferedImageAvailable() {
	bufferedImageAvailable = true;
}

function swapImage() {
thisimagetoChange = document.getElementById("picture");
	if (document.images) {
		if (bufferedImageAvailable)	{
			thisimagetoChange.setAttribute("src", preload.src);
			getNewImage();
		}
		else {
			// don't need to be able to cancel this timeout
			aTimer = setTimeout("swapImage()",TRY_AGAIN);
		}
	}
}


function setSpeed(theSpeed) {
	timeOut = theSpeed;
	if (aTimer != null)	{
		clearTimeout(aTimer);
		setTimeout("swapImage()",timeOut);
	}
	if (document.fastButton != null || document.slowButton != null)	{
		if (theSpeed == FAST_CONNECTION) {
			document.fastButton.src = "fast_over.gif";
			document.slowButton.src = "slow_off.gif";
		}
		else
		if (theSpeed == SLOW_CONNECTION) {
			document.fastButton.src = "fast_off.gif";
			document.slowButton.src = "slow_over.gif";
		}
	}
}

function slow() {
  setSpeed(SLOW_CONNECTION);
}
function fast() {
  setSpeed(FAST_CONNECTION);
}


//-->


$("showSlow").click(slow());
$("showFast").click(fast());

	
