// 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 minTime = 3; // minimun seconds refresh
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 newCam = false;
var baseUrl="";
var noUrl = false;
var fullSize=false;
var origWidth=0;
var origHeight=0;
var showingFilm=false;
var hasMap = false;
var useUTCTime = true;
var showDate = true;
document.body.onresize=CheckFullSize;
var Buffer = new Image();
var loading = false;
function SetCam()
{		
	if (noUrl) return;
	origWidth=document.getElementById("imgDisplay").width;
	origHeight=document.getElementById("imgDisplay").height;
	imgArray[currentImage]=document.images.imgDisplay.src;
	if (showDate) 
		dateArray[currentImage]= GetCurrentDate();
	faderID=setTimeout("LoadBuffer()", refreshRate);
	countdown = refreshRate / 1000;
	SetCountdown();
	Buffer.onload=Buffer_onload;
}
function LoadBuffer()
{
	if (noUrl) return;
	if (currentImage>=maxImages-1)
	{
		Replay_onclick(); 
		return false;	
	}
	var t =  new Date().getTime().toString();
	var nextUrl = imgUrl +"?n="+ t;
	Buffer.src = nextUrl;
    currentImage++;
    imgArray[currentImage]= nextUrl;
    if (showDate)
		dateArray[currentImage]= GetCurrentDate();
    stepCounter = currentImage;
    document.images.imgLoading.src = document.images.imgLoad.src;
    document.getElementById("spnCountDown").innerHTML="";
	loading=true;
}
function Buffer_onload()
{
	if (replaying || stepping)
		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.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;
	if (stepping)
	{
		stepping=false;
		document.getElementById("spnReplay1").style.display="none";
		document.getElementById("spnReplay2").style.display="none";
		document.getElementById("spnStepperControl").innerHTML="Step";
	}
	if (loading)
		document.images.imgLoading.style.display="none";
	replaying=true;
	clearTimeout(faderID);
	document.getElementById("tdReplayAll").style.backgroundColor="Red";
	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.getElementById("tdReplayAll").style.backgroundColor="";
		document.getElementById("spnDate").style.display="none";
		document.getElementById("spnCounter1").innerHTML="Image ";
	} 	
}
function ChangeCam()
{
	if (stepping)
		ReplayStepped()
	clearTimeout(faderID);
	var oldUrl = imgUrl;
	imgUrl=prompt("Enter the URL of the new image to display", imgUrl);
	if (imgUrl != null && CheckUrl())
	{
		currentImage=-1;
		newCam=true;
		clearTimeout(faderID);
		LoadBuffer();
		document.images.imgDisplay.src = baseUrl +"NoReception.gif";
		document.images.imgLoading.src = document.images.imgLoad.src;
	}
	else
	{
		imgUrl = oldUrl;
		clearTimeout(faderID);
		LoadBuffer();
	}
}
function ChangeInterval()
{	
	clearTimeout(faderID);
	clearInterval(countdownId);
	document.getElementById("spnCountDown").innerHTML="";
	var ot=refreshRate/1000;
	var t=prompt("Enter time (in seconds) between new images. Interval must be >= "+minTime, ot);
	if (t!= null) // Cancel clicked
	{
		if (isNaN(t) || t < minTime)
		{
			alert("Refresh rate (in seconds) must\n\rbe a number larger or equal to "+ minTime +"\n\r\n\rPlease enter a valid number");
			ChangeInterval();
		}
		else
		{
			refreshRate=t*1000;
			LoadBuffer();
			document.getElementById("fntTimer").innerHTML = t +" sec";
		}
	}
	else
	{
		LoadBuffer();
	}
}
var bHelpOn=false;
var imgTemp = new Image();
function Help()
{	
	if (! bHelpOn)
	{
		clearTimeout(faderID);
		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";
		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 ShowControl()
{
	var ctl=document.getElementById("divControl");
	if (ctl.style.display=="none")
	{
		ctl.style.display="block"
		document.getElementById("fntControl").innerHTML="Hide Controls";
	}
	else
	{
		ctl.style.display="none"
		document.getElementById("fntControl").innerHTML="Show Controls";
	}
}
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;
	}
	if (stepping)
		ReplayStepped();
	if (currentImage >= maxImages) currentImage = 0;
	LoadBuffer();
}
function CheckUrl()
{
 	if (imgUrl.length < 9)
	{	
		document.images.imgDisplay.src=baseUrl +"NoReception.gif";
 		noUrl = true;
 		return false;
	}
	else
	{
		document.images.imgDisplay.style.display = "block";
		noUrl=false;
		return true;
 	}
}
var stepping = false;
var stepCounter=0;
function ReplayStepped()
{
	if (replaying)
	{		
		clearInterval(playBackTimerID);
		replaying=false;
		stepCounter = playBackCounter;
		playBackCounter=0;
	}
	if (stepping)
	{
		stepping=false;
		document.getElementById("spnReplay1").style.display="none";
		document.getElementById("spnReplay2").style.display="none";
		document.getElementById("spnDate").style.display="none";
		document.getElementById("spnStepperControl").innerHTML="Step";
		stepCounter=currentImage;
		LoadBuffer();
	}
	else
	{
		stepping=true;
		clearTimeout(faderID);
		clearInterval(countdownId);
		document.getElementById("spnCountDown").innerHTML="";
		document.getElementById("spnReplay1").style.display="block";
		document.getElementById("spnReplay2").style.display="block";
		if (showDate)
			document.getElementById("spnDate").style.display="block";
		document.getElementById("spnStepperControl").innerHTML="Play";
	}
}
function StepBack()
{
	if (stepCounter > 0)
	{
		stepCounter--;
		StepGo();
	}
}
function StepUp()
{
	if (stepCounter < currentImage)
	{
		stepCounter++;
		StepGo();
	}
}
function StepGo()
{
	if (document.all)
		{
			document.images.imgDisplay.style.filter="blendTrans(duration=0.5)";
			document.images.imgDisplay.filters.blendTrans.Apply();
		}
		document.images.imgDisplay.src=imgArray[stepCounter];
		if (document.all)
		{
			document.images.imgDisplay.filters.blendTrans.Play(); // fade it
		}
		document.getElementById("spnCounter1").innerHTML="Step ";
		document.getElementById("spnCounter2").innerHTML=(stepCounter+1);
		
		if (showDate)
			document.getElementById("spnDate").innerHTML=dateArray[stepCounter];
}
document.onkeydown = HandleDown;
function HandleDown(evt)
{	
	if(! (stepping || fullSize)) return;
	evt=(evt) ? evt : ((event) ? event : null);
	if(evt)
	{		
		switch (evt.keyCode)
		{
			case 39:
				if (stepping) StepUp(); // Right Arrow
				break;
			case 37:
				if (stepping) StepBack(); // Left Arrow
				break;
			case 188:
				if (showingFilm) ScrollFilm(1); // < key
				break;
			case 190:
				if (showingFilm) ScrollFilm(0); // > key
				break;	
			case 27:
				if (fullSize) OrigSize(); // Esc
				break;
		}	
	}
}
function ShowFilmStrip()
{
	if (! stepping)
		ReplayStepped();
	document.getElementById("divFilmStrip").style.display="block";
	var inner = document.getElementById("divShowAllInner");
	inner.parentNode.removeChild(inner);
	var divAll = document.getElementById("divFilmStrip");
	var newDiv=document.createElement("div");
	newDiv.setAttribute("id", "divShowAllInner");
	divAll.appendChild(newDiv);
	for (var i=0; i< currentImage+1; i++)
	{
		if (showDate)
		{
			var tempTitle = document.createElement("span");
			tempTitle.innerHTML=dateArray[i];
			newDiv.appendChild(tempTitle);
		}
		newDiv.appendChild(document.createElement("br"));
		var tempImg = document.createElement("img");
		tempImg.setAttribute("src", imgArray[i]);
		tempImg.setAttribute("width", "115");
		tempImg.setAttribute("height", "94");
		tempImg.setAttribute("alt", "image #" + (i+1));
		tempImg.onmouseover=FilmStrip_onmouseover;
		tempImg.onmouseout=FilmStrip_onmouseout;
		newDiv.appendChild(tempImg);
		newDiv.appendChild(document.createElement("br"));
	}
	divAll.appendChild(newDiv);
	divAll.style.display="block";
	document.getElementById("divFilmScroller").style.display="block";
	showingFilm=true;
}
function CloseFilmStrip()
{
	document.getElementById("divFilmStrip").style.display="none";
	document.getElementById("divFilmScroller").style.display="none";
	showingFilm=false;
}
function FilmStrip_onmouseover()
{
	if (document.all)
	{
		document.images.imgFilmStripLarge.style.filter="blendTrans(duration=0.5)";
		document.images.imgFilmStripLarge.filters.blendTrans.Apply();
	}
	document.images["imgFilmStripLarge"].src = event.srcElement.src;
	if (document.all)
	{
		document.images.imgFilmStripLarge.filters.blendTrans.Play(); // fade it
	}
	document.getElementById("divShowFull").style.top =  event.clientY+ document.body.scrollTop +"px";
	document.getElementById("divShowFull").style.left = 100 + event.clientX +"px";
}
function FilmStrip_onmouseout()
{
	document.getElementById("divShowFull").style.top =  "-1000px";
}
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();
}
function FullSize()
{
	// calling this function can somwhow kill the reloads
	if (noUrl || bHelpOn) return;
	var theImage = document.getElementById("imgDisplay");
	origWidth=theImage.width;
	origHeight=theImage.height;
	document.getElementById("divTITCam").style.position="static";	
	theImage.style.position="absolute";
	theImage.style.top="0px";
	theImage.style.left="0px";
	if (hasMap)
		theImage.useMap="";
	var sc=theImage.width / theImage.height;
	theImage.style.width = document.body.clientWidth;
	theImage.style.height = theImage.width / sc;
	document.getElementById("btnFullOff").style.display="block";
	document.getElementById("btnFullOff").style.left=(document.body.clientWidth/2)-50 +"px";
	fullSize=true;
}
function OrigSize()
{
	if (! fullSize) return;
	document.getElementById("divTITCam").style.position="relative";	
	var theImage = document.getElementById("imgDisplay");
	theImage.style.position="static";
	theImage.style.width = origWidth;
	theImage.style.height = origHeight;
	fullSize=false;
	if (hasMap)
		theImage.useMap="#TarrantIT_Map";
	document.getElementById("btnFullOff").style.display="none";	
}
function CheckFullSize()
{
	if (fullSize)
		FullSize();	
}
function PrintAllImages()
{
	var allDiv = document.getElementById("divPrintAll");
	allDiv.style.width=document.body.clientWidth;
	var tr, td;
	var tblBody = document.getElementById("tblPrintAll");
	tr=tblBody.insertRow(tblBody.rows.length);
	for (var i=0; i< currentImage+1; i++)
	{
		td=tr.insertCell(tr.cells.length);
		td.setAttribute("align", "center");
		if (showDate)
		{
			var tempTitle = document.createElement("span");
			tempTitle.innerHTML=dateArray[i];
			td.appendChild(tempTitle);
			td.appendChild(document.createElement("br"));
		}
		var tempImg = document.createElement("img");
		tempImg.setAttribute("src", imgArray[i]);
		tempImg.setAttribute("alt", "image " + (i+1));
		td.appendChild(tempImg);
		if (i>0 && (i+1)%2==0)
			tr=tblBody.insertRow(tblBody.rows.length);	
	}
	if (i>0)
	{
		for (var i=0; i< (i+1)%2; i++)
		{
			td=tr.insertCell(tr.cells.length);
		}
	}
	allDiv.style.display="block";
	window.print();
}
function ClosePrintAll()
{
	document.getElementById("divPrintAll").style.display="none";
}
var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var xCord,yCord;
var dobj;
function movemouse(e)
{
  if (isdrag)
  {
    dobj.style.left = nn6 ? tx + e.clientX - xCord : tx + event.clientX - xCord;
    dobj.style.top  = nn6 ? ty + e.clientY - yCord : ty + event.clientY - yCord;
    return false;
  }
}
function selectmouse(e) 
{
  var fobj = nn6 ? e.target : event.srcElement;
  var topelement = nn6 ? "HTML" : "BODY";
  while (fobj.tagName != topelement && fobj.className != "dragme")
  {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }
  if (fobj.className=="dragme")
  {
    isdrag = true;
    dobj = fobj;
    tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);
    xCord = nn6 ? e.clientX : event.clientX;
    yCord = nn6 ? e.clientY : event.clientY;
    document.onmousemove=movemouse;
    return false;
  }
}
document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");
function ResizeViewer(dir)
{
	var theImage = document.getElementById("imgDisplay");
	var w = theImage.width;
	if (dir =="d" && w < 100) return;
	if (dir =="u" && w > 600) return;
	var rat = dir == "u" ? 1.25 : 0.8;
	theImage.style.width = Math.round(parseInt(theImage.width) * rat);
	theImage.style.height = Math.round(parseInt(theImage.height) * rat);
	if (hasMap)
	{
		var arrCoords;
		var newCoords="";
		var map = document.getElementById("TarrantIT_map");
		var spots = map.getElementsByTagName("area");
		// var spots = document.getElementById("TarrantIT_map").areas; // shame Forefox!
		for (var i=0; i< spots.length; i++)
		{
			newCoords="";
			arrCoords = spots[i].coords.split(",");
			for (var y=0; y < arrCoords.length; y++)
			{
				var t = parseInt(arrCoords[y]);
				t = Math.round(t* rat);
				newCoords+= t +",";
			}
			spots[i].coords=newCoords.substr(0, newCoords.length-1);
		}
	}
}













