var objAniTimer = null;
var intAniLastFrameDelay = 2000;
var intAniIndividualFrameDelay = 300;
var intAniNextImg = 0;
var intAniCurrentImg = 0;
var intAniMinImage = 0;
var intAniMaxImage = 0;
var intAniImageArray;
var strAniFailedIndexList = ';';
var strAniDivNamePrefix = 'imageAni';
var strctlprefix = '';
var strAniDivPageLoadingName = 'wXradar-animation-wait';
var bAniIndexAsc = true;
var bAniLoop = true;
var animationState = 'play';

function animationImageLoadFailed(strIndex)
{
	strAniFailedIndexList += strIndex + ';';
}
function animationInit(intMin, intMax, bAnimateIndexAsc)
{
	// Build list of successfully loaded images for animation
	var intCurrent = 0;
	this.intAniImageArray = new Array(intMax - intMin + 1);
	for(i = intMin; i <= intMax; i++)
	{	
		if ( strAniFailedIndexList.indexOf(';' + i + ';') == -1 )
		{
			this.intAniImageArray[intCurrent] = i;
			intCurrent++;
		}
	}

	bAniIndexAsc = bAnimateIndexAsc;
	intAniMinImage = 0;
	intAniMaxImage = --intCurrent;
	intAniNextImg = ( bAniIndexAsc ? intAniMinImage : intAniMaxImage );
	intAniCurrentImg = ( bAniIndexAsc ? intAniMaxImage : intAniMinImage );
	
}

function animationPause()
{
    if(animationState == 'play'){
	    // Allows manual pause
	    animationState = 'pause'
	    if ( objAniTimer != null ) clearInterval( objAniTimer );
	    var anImg = document.getElementById("wxCtrlPlay");
        if(anImg != null)
        anImg.className = "camera-ctrl-play"; 
	}
	else{
	    animationPlay(this.strctlprefix);
	}
}
function animationPlay(strctlpref)
{

    animationState = 'play';
    var anImg = document.getElementById("wxCtrlPlay");
        if(anImg != null)
        anImg.className = "camera-ctrl-pause"; 
    this.strctlprefix = strctlpref;
    
	// Set individual frame delay
	if ( objAniTimer != null ) clearInterval( objAniTimer );

	try
	{
		document.getElementById(strctlprefix+strAniDivPageLoadingName).style.display="none";
	}
	catch(e) { /*alert(e.name + ' -- ' + e.message);return false;*/}

	var temp = 'animationTimerTask(\'' + strctlprefix + '\')';

	objAniTimer = setInterval( temp, intAniIndividualFrameDelay );
}
function animationRePlay()
{
	// Pause between loops is over so play if in looping mode
	//if ( bAniLoop ) 
}
function animationStepBackward()
{
	// Step backward one frame
	if(animationState == 'play'){
	    // Allows manual pause
	    animationState = 'pause'
	    //if ( objAniTimer != null ) clearInterval( objAniTimer );
	    var anImg = document.getElementById("wxCtrlPlay");
        if(anImg != null)
        anImg.className = "camera-ctrl-play"; 
	}
	if ( objAniTimer != null ) clearInterval( objAniTimer );
	animationStepFrame(false, strctlprefix);
}
function animationStepForward()
{
	// Step forward one frame
	if(animationState == 'play'){
	    // Allows manual pause
	    animationState = 'pause'
	    //if ( objAniTimer != null ) clearInterval( objAniTimer );
	    var anImg = document.getElementById("wxCtrlPlay");
        if(anImg != null)
        anImg.className = "camera-ctrl-play"; 
	}
	if ( objAniTimer != null ) clearInterval( objAniTimer );
	animationStepFrame(true, strctlprefix);
}
function animationStop(strctlprefix)
{
	// Stop timer and reset next image to first
	if ( objAniTimer != null ) clearInterval( objAniTimer );

	var intCurrentImage = intAniCurrentImg;
	intAniNextImg = ( bAniIndexAsc ? intAniMinImage : intAniMaxImage );
	intAniCurrentImg = ( bAniIndexAsc ? intAniMaxImage : intAniMinImage );		
	
	animationMoveNextFrame(strctlprefix);
	try
	{
		document.getElementById(strctlprefix + strAniDivNamePrefix + this.intAniImageArray[intCurrentImage]).style.display="none";
	}
	catch(e) {}
}
function animationStepFrame(bForward, strctlprefix)
{
	var bMoveFrame = true;
	
	// Check for index boundary if not looping
	if ( bForward )
	{
		bMoveFrame = !( ( (intAniCurrentImg == intAniMaxImage && bAniIndexAsc) || (intAniCurrentImg == intAniMinImage && !bAniIndexAsc) ) && !bAniLoop  );
				
	}
	else
	{
		bMoveFrame = !( ( (intAniCurrentImg == intAniMinImage && bAniIndexAsc) || (intAniCurrentImg == intAniMaxImage && !bAniIndexAsc) ) && !bAniLoop );
		if ( bMoveFrame ) 
		{
			intAniNextImg = ( bAniIndexAsc ? intAniCurrentImg - 1 : intAniCurrentImg + 1 );
		}
	}
	if (intAniNextImg > intAniMaxImage) intAniNextImg = intAniMinImage;
	if (intAniNextImg < intAniMinImage) intAniNextImg = intAniMaxImage;

	if ( bMoveFrame ) animationMoveNextFrame(strctlprefix);
}
function animationMoveNextFrame(strctlprefix)
{
	var bLastFrame = false;
	
	try
	{
		if (intAniCurrentImg != intAniNextImg)			
		{
			target = document.getElementById(strctlprefix + strAniDivNamePrefix + this.intAniImageArray[intAniNextImg]); 
			source = document.getElementById(strctlprefix + strAniDivNamePrefix + this.intAniImageArray[intAniCurrentImg]);
			target.style.display = "block";
			source.style.display = "none";
	        if(intAniCurrentImg == intAniMinImage) {  }
			intAniCurrentImg = intAniNextImg;
		}
	}
	catch(e) { }

	if ( bAniIndexAsc )
	{
		intAniNextImg++;
		if ( bLastFrame = (intAniNextImg > intAniMaxImage) ) intAniNextImg = intAniMinImage;
	}
	else
	{
		intAniNextImg--;
		if ( bLastFrame = (intAniNextImg < intAniMinImage) ) intAniNextImg = intAniMaxImage;
	}
	
	return bLastFrame;
}
function animationTimerTask(strctlprefix)
{
    
	if ( animationMoveNextFrame(strctlprefix) )
	{
		// Last image so pause intLastFrameDelay seconds. Image doesn't display until
		// function exits so set timer.  Also prevents cpu spike.
		if ( objAniTimer != null ) clearInterval( objAniTimer );
		if ( bAniLoop ) objAniTimer = setInterval( 'animationPlay(\'' + strctlprefix + '\')', intAniLastFrameDelay - intAniIndividualFrameDelay );
	}
}

