//-------------------------------------------------------------------+
//
// anim_utils.js:
//
// Animation utilities, common for all animations
//
//	LoadFirst	load first frame (no.0)
//	LoadOne		load a single frame with selected time step
//	LoadImages	load all frames (0 .. nplots-1)
//	Loaderror	decrease nplots by one
//	Step(increm)	previous/next frame
//	Skip(dir)	first/last frame
//	Stop		stop the animation
//	Animate(dir)	single animation
//	Loop(dir,ofz)	repeated animation
//	Tempo(factor)	speed up/slow down the animation
//
// 2005.08.25  jw
// 2006.08.01  pnn  Functions Stop and Tempo modified
// 2006.08.23  jw   document.debug commented out
// 2009.06.11  jw   added function Pause
//-------------------------------------------------------------------+

// Variables

var delay	= 400;
var playing	= 0;
var iter	= 0;
var max_iter	= 1000;
var picture     = new Array();

// End

//------------------------------
//	image loading functions
//------------------------------

function LoadFirst()
{
 j=0;
 SetPrefix();
 SetNumber(j);
 document.MainImage.src = prefix + figno + ".png";
 // document.out.dates.value = document.images[1].src;
 //document.input.filename.value = document.MainImage.src;
}

function LoadOne()
{
 if (playing>0)
 {
  clearTimeout(timer);
  iter = 0;
  playing=0;
 }
 SetPrefix();
 SetNumber(j);
 document.MainImage.src = prefix + figno + ".png";
 // document.debug.info.value = document.MainImage.src;
 //document.input.filename.value = document.MainImage.src;
}

function LoadImages()
{
 SetPrefix();
 for (i=0 ; i<nplots ; i++)
 {
  SetNumber(i);
  picture[i] = new Image();
  picture[i].src = prefix + figno + ".png";
  picture[i].onerror;
 }
}

function Loaderror()
{
 nplots--;
 Skip(1);
}

function LoadMap(domain,map)
{
 document.input.domain.value = domain + ".";
 document.MapImage.src = "maps/" + map + ".png";
 document.MapImage.useMap = '#' + map;
 LoadOne();
}

//------------------------------
//	image stepping functions
//------------------------------

function Step(i)
{
 j += i;
 if (j >= nplots) {j=nplots-1;}
 if (j < 0) {j=0;}
 LoadOne();
}

function Skip(rand)
{
 if (rand == -1) {j=0;}
 else {j=nplots-1;}
 LoadOne();
}

function Stop()
{
 clearTimeout(timer);
 if (playing > 0) {
  if (i == 1) {j--; if (j<0) {j=0;} }
  else if (i == -1) {j++; if (j>nplots-1) {j=nplots-1;} }
 }
 document.MainImage.src = picture[j].src;
 iter=0;
 playing=0;
}

//------------------------------
//	animation functions
//------------------------------

function Animate(dir)
//	dir=1 forw, dir=-1 backw
{
 // save a copy of args to make recursive call work
 i = dir;
 if (iter == 1) 
 {
  Stop();
  if (i == 1) {j=0;}
  else if (i == -1) {j=nplots-1;} 
  return;
 }
 if (dir == 1 && j >= nplots-1) {iter++;}
 else if (dir == -1 && j < 0) {iter++;}
 document.MainImage.src = picture[j].src;
 j += i;
 playing=1;
 timer=window.setTimeout("Animate(i)",delay);
}

function Loop(dir,ofz)
//	dir=1 forw, dir=-1 backw
{
 // save a copy of args to make recursive call work
 i = dir;
 o = ofz;
 if (iter >= max_iter) {Stop(); return;}
 if (j >= nplots-o) {iter++;Pause(1000);j=0;}
 else if (j < 0) {iter++;Pause(1000);j=nplots-o;}
 document.MainImage.src = picture[j].src;
// j++;
 j += i;
 playing=1;
 timer=window.setTimeout("Loop(i,o)",delay);
}

function Tempo(faktor)
{
 delay = delay+faktor*(delay/2.5);
 delay = Math.floor(delay);
 if (delay < 10) delay = 10;
 else if (delay > 2400) delay = 2400;
}

function Pause(millis)
// www.sean.co.uk
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
}

