// TarrantIT  2003
// www.TarrantIT.com
// Web Cam Viewer animation client code
var replaying=false;
var currentImage=0;
var imgArray = new Array();
var dateArray = new Array();
var playBackCounter=0;
var playBackTimerID;
var faderID;
// following set from module settings admin
var imgUrl = ""; // initialy selected image url
var refreshRate=10000; // 10 seconds
var replaySpeed = 1;  //seconds
var maxImages = 60; 
// 
var countdownId;
var countdown = refreshRate / 1000;
var baseUrl="";
var noUrl = false;
var useUTCTime = true;
var showDate = true;
var Buffer = new Image();
var loading = false;
function SetCam()
{
	if (noUrl) return;
	document.images["imgDisplay"].onload="";
	Buffer.onload=Buffer_onload;
	imgArray[currentImage]=document.images["imgDisplay"].src;
	if (showDate) 
		dateArray[currentImage]= GetCurrentDate();
	faderID=setTimeout("LoadBuffer()", refreshRate);
	countdown = refreshRate / 1000;
	SetCountdown();
}
function LoadBuffer()
{
	if (noUrl) return;
	if (currentImage>=maxImages-1)
	{
		Replay_onclick(); 
		return false;	
	}
	var t =  new Date().getTime().toString();
	var nextUrl = imgUrl +"?n="+ t;
	// DEMO
	if (imgUrl.indexOf("http://localhost") > -1)
	{
		var rnd = Math.floor(Math.random()*12);
		nextUrl="http://localhost/RandomImage/wc"+ rnd +".jpg?n="+ t; 
	}
	// END DEMO

	Buffer.src = nextUrl;
    currentImage++;
    imgArray[currentImage]= nextUrl;
    if (showDate)
		dateArray[currentImage]= GetCurrentDate();
  	document.images.imgLoading.src = document.images.imgLoad.src;
  	document.getElementById("spnCountDown").innerHTML="";
  	loading = true;
}
function Buffer_onload()
{
	if (replaying)
		return;
	FadeCam(); 
	faderID = setTimeout("LoadBuffer()", refreshRate);
	document.images.imgLoading.src=baseUrl +"/Empty.gif";
	document.getElementById("spnCounter2").innerHTML=(currentImage+1);
	loading=false;
	SetCountdown();
}
function FadeCam()  
{		
	if (document.all) 
	{
		document.images.imgDisplay.style.filter="blendTrans(duration=1.0)";
		document.images.imgDisplay.filters.blendTrans.Apply();
	}
	document.images["imgDisplay"].src=Buffer.src;   
	if (document.all) 
	{											  
		document.images.imgDisplay.filters.blendTrans.Play(); // fade it
	}
}
function SetCountdown()
{
	clearInterval(countdownId);
	countdownId = setInterval("DecTimer()", 1000);
	countdown = refreshRate / 1000;
}
function DecTimer()
{
	if (countdown > 0 && ! loading)
	{
		countdown--;
		document.getElementById("spnCountDown").innerHTML=countdown;
	}
}
function Replay_onclick()
{	
	if (replaying) return;
	replaying=true;
	document.images["Button"].src=baseUrl +"ButtonRed.gif";
	clearTimeout(faderID);
	PlayBack();
	playBackTimerID=setInterval("PlayBack()", replaySpeed*1000);
	document.getElementById("spnDate").style.display="block";
	clearInterval(countdownId);
	document.getElementById("spnCountDown").innerHTML="";
	document.getElementById("spnCounter1").innerHTML="Replay ";
}
function PlayBack()
{
	if (playBackCounter < currentImage+1)
	{
		if (document.all)
		{
			document.images.imgDisplay.style.filter="blendTrans(duration=0.5)";
			document.images.imgDisplay.filters.blendTrans.Apply();
		}
		document.images["imgDisplay"].src=imgArray[playBackCounter];
		if (document.all)
		{
			document.images.imgDisplay.filters.blendTrans.Play(); // fade it
		}
		if (showDate)
			document.getElementById("spnDate").innerHTML=dateArray[playBackCounter];
		playBackCounter++;
		document.getElementById("spnCounter2").innerHTML =  playBackCounter;
	}
	else
	{
		playBackCounter=0;
		clearInterval(playBackTimerID);
		replaying=false;
		setTimeout("LoadBuffer()", refreshRate);
		SetCountdown();
		document.images["Button"].src = baseUrl +"Button.gif";
		document.getElementById("spnDate").style.display="none";
		document.getElementById("spnCounter1").innerHTML="Image ";
	} 	
}
var bHelpOn=false;
var imgTemp = new Image();
function Help()
{	
	if (! bHelpOn)
	{
		imgTemp.src=document.images["imgDisplay"].src;
		document.images["imgDisplay"].style.display = "none";
		document.getElementById("iFrameHelp").src="http://www.tarrantit.com/WebCam/";
		document.getElementById("iFrameHelp").style.display="block";
		clearTimeout(faderID);
		clearInterval(countdownId);
		document.getElementById("spnCountDown").innerHTML="";
		newCam=true;
	}	
	else
	{
		document.getElementById("iFrameHelp").style.display="none";
		document.images["imgDisplay"].style.display = "block";
		LoadBuffer();
		document.images["imgDisplay"].src=imgTemp.src;
	}
	bHelpOn =! bHelpOn;
}
function Cam_onerror()
{
	document.images["imgDisplay"].src = baseUrl +"NoReception.gif";
	currentImage--;
}
function ResetCam()
{
	if (noUrl)
	{
		alert('No Url is set for the viewer\n\r\n\rClick Show Controls - New\n\rand enter the url of the image');
		return;
	}
	if (replaying || (currentImage>=maxImages))
	{
		playBackCounter=0;
		clearInterval(playBackTimerID);
		replaying=false;
		document.images["Button"].src = baseUrl +"Button.gif";
	}
	if (currentImage >= maxImages) currentImage = 0;
	LoadBuffer();
}
function StopCam()
{
	clearInterval(playBackTimerID);
	clearInterval(countdownId);
	clearInterval(faderID);
}
function CheckUrl()
{
 	if (imgUrl.length < 9)
		Help();
}
function GetCurrentDate()
{
	var now = new Date();
	if (useUTCTime)
		return "UTC: "+ now.getUTCDate() +"/"+ (now.getUTCMonth()+1) +"/"+ now.getUTCFullYear() +"-"+ now.getUTCHours() +":"+ now.getUTCMinutes() +":"+ now.getUTCSeconds();
	else
		return now.getDate() +"/"+ (now.getMonth()+1) +"/"+ now.getFullYear() +"-"+ now.getHours() +":"+ now.getMinutes() +":"+ now.getSeconds();
}