// JavaScript Document to handle the flash animations

var animcombobox;
/*
function getFlashMovieObject(movieName)
{
try{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
  
	} catch(e){
		
	}
  
}*/


function getAnimationMovieObject(movieName)
{
	
//alert('moviename: ' + 	movieName + ' : ' + parent.document.all);

/*if (document.frames != undefined) 
  {
     alert('window opener:' + document.frames.length);
  }	
*/
	
//alert(Ext.getCmp('mb_viewfilespanel'));	
	
if (window.document.all != undefined) 
  {
      return window.document.all[movieName];
  }		
if (document.all != undefined) 
  {
      return document.all[movieName];
  }
 
  if (document.all != undefined) 
  {
      return document.all[movieName];
  }	
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    
	if(window.frames){
		//alert(window.frames.length);	
	}
	
	if (document.embeds)
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1) && document.embeds[movieName]
  {
    return document.getElementById(movieName);
  }
//try{
 /* if (parent.document.all[movieName] != undefined) 
  {
      return parent.document.all[movieName];
  }
  
  if (document.all[movieName] != undefined) 
  {
      return document.all[movieName];
  }
  
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (parent.document.embeds && parent.document.embeds[movieName])
      return parent.document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return parent.document.getElementById(movieName);
  }*/
  
/*	} catch(e){
		
	}*/

  
}



function movieIsLoaded(theMovie) {
  if (typeof(theMovie) != "undefined") {
    try {return theMovie.PercentLoaded() == 100; } catch(e) { return false;}
  } else {
    return false;
  }
}
/*
Before we do anything lets make sure the flash movie has actually loaded.  
*/
var AnimationArray = [];

var stopAnimationInterval;
var flashplayingInterval;

var animationcurrentstate = 'paused';
var animationplaybutton = '<img src="/images/control_play_blue.png" border="0">';
var animationpausebutton = '<img src="/images/control_pause_blue.png" border="0">';




var animationMovie; // this is the loaded animation slide.



function handleAnimationSelect(combo,record,index){
	
	//alert(record.get('start'));
	playAnimation(record.get('sortorder'),record.get('start'),record.get('end'))
}


function handleAnimationClick(button,e){
	
	//alert(button.combo);
	
	animcombobox =  Ext.getCmp(button.combo);
	
	//alert(animcombobox);
	
	try{
	animationMovie.StopPlay();
	} catch(e) {}
	
	
	//alert(animationMovie.TCurrentFrame("/"));
	
	
	
	
	var currentanim = animcombobox.selectedIndex;
	var totalanim = animcombobox.store.getTotalCount() - 1;
	
	try{
		if(animationMovie.TCurrentFrame("/") != 0){
		
			var nextanim = currentanim + button.delta;
			if (nextanim > totalanim){nextanim = 0;}
			if (nextanim < 0){nextanim = totalanim;}
			animcombobox.selectedIndex = nextanim;
			//alert(nextanim);
			animcombobox.setValue(animcombobox.store.getAt(nextanim).get('sortorder'));
			handleAnimationSelect(animcombobox,animcombobox.store.getAt(nextanim),nextanim);
	
		} else{
			handleAnimationSelect(animcombobox,animcombobox.store.getAt(currentanim),0);
			
		}
	}catch(e){}
	//alert(record.get('start'));
	//playAnimation(record.get('sortorder'),record.get('start'),record.get('end'))
}


function handleAnimationPlayClick(button,e){
		animcombobox =  Ext.getCmp(button.combo);
		//loadAnimationSlide(button.flashname,true)
		//alert(button.flashname)
		animationMovie=getAnimationMovieObject(button.flashname);
		//try{
		if(animationcurrentstate == 'playing'){
			animationMovie.StopPlay();
			setPlayStatus(name,'stopped');
			
		} else {
			animationMovie.Play();
			setPlayStatus(name,'playing');			
			
		}
		/*}catch(e){
				clearAnimations();
				animcombobox.selectedIndex=0;
		   		animationSlideLoadedInterval = setInterval(function(){loadAnimationSlide(button.flashname,true);}, 1000);
			
		}*/
		
		
		
}




var animationSlideLoadedInterval;
function loadAnimationSlide(name, autoplay){
	
	
	//alert('loadAnimationSlide');
	
	//animcombobox =  Ext.getCmp('animcombobox' + name);
	//alert(animcombobox);
	animationMovie=getAnimationMovieObject(name);
	
	//alert(name);
	
	if(movieIsLoaded(animationMovie)){
		//alert('movie is loaded');
		//The movie is loaded. clear the interval. 
		//We should start the movie now if the autoplay is true. 
		
		//setPlayStatus(name,'Loaded');
		clearInterval(animationSlideLoadedInterval);
		
		if(autoplay){
			
			setPlayStatus(name,'Playing');
			animationcurrentstate = 'playing'
			//not 30 frames per second it too much for IE. set to 100 and it seems ok.
			animationMovie.Play();
			
		} else {
			setPlayStatus(name,'Stopped');
			animationcurrentstate = 'paused'	
			
			
		}//end autoplay
		
		checkPlayStatusInterval = setInterval(function(){checkPlayStatus(name);}, 100);
		
	} else {
		setPlayStatus(name,'Loading');
		//clearInterval(animationSlideLoadedInterval);
		//alert('could not load flash');
	
	}// movie is loading
	
}

// this function just sets the status bar for display

var previousStatus
function setPlayStatus(name,status){
	
	if(status != previousStatus){
		//do this to prevent writing over and over on intervals.
		//document.getElementById(name+'PlayStatus').innerHTML = status;
		previousStatus = status;
		
		
		if(status == 'Playing'){
			animplaybutton.setIconClass('anim-PAUSE');
			animationcounter.setValue('Playing');
			animationcurrentstate = 'playing';
			//document.getElementById('animation_control').innerHTML= animationpausebutton;
			//document.getElementById(name + 'PlayStatus').setAttribute("class","dps_status_animation_on");
			//document.getElementById(name + 'PlayStatus').setAttribute("className","dps_status_animation_on");
		}
		
		if(status == 'Stopped'){
			animplaybutton.setIconClass('anim-PLAY');
			animationcounter.setValue('Stopped');
			animationcurrentstate = 'stopped';
			//document.getElementById('animation_control').innerHTML= animationplaybutton;
			//document.getElementById(name + 'PlayStatus').setAttribute("class","dps_status_animation_off");
			//document.getElementById(name + 'PlayStatus').setAttribute("className","dps_status_animation_off");			
		}
		
		
	}
	
}


//this function checks the flash playing status. we can use in an interval to display the proper
//animaion number. pass the flash movie object so we do not have to check for it on each interval pass.
var checkPlayStatusInterval;
function checkPlayStatus(name){
	
	
	try{
		if(! animationMovie.IsPlaying()){
			setPlayStatus(name,'Stopped');
			
		} else {
			
			//setPlayStatus(name,'Playing: ' + animationMovie.TCurrentFrame("/"));
			setPlayStatus(name,'Playing');
			displayAnimationNumber(animationMovie.TCurrentFrame("/"));
	
		}
	
	} catch(e){
		// the user has moved away from the slide.
		clearAnimations();
	}
	
}


function displayAnimationNumber(current){
	try{
		
			var currentanim = animcombobox.selectedIndex;
			var totalanim = animcombobox.store.getTotalCount() - 1;
			
	if(current > animationJSON.getAt(animcombobox.selectedIndex).get('end')){
					

			
			var nextanim = currentanim + 1;
			if (nextanim > totalanim){nextanim = totalanim;}
			animcombobox.selectedIndex = nextanim;
			animcombobox.setValue(animcombobox.store.getAt(nextanim).get('sortorder'));												 
	
		}
		
	if(current <  animationJSON.getAt(animcombobox.selectedIndex).get('start')){
		for(var i=0;i<totalanim;i++){
			if(current > animationJSON.getAt(i).get('start') && current < animationJSON.getAt(i).get('end')){
					animcombobox.selectedIndex = i;
					animcombobox.setValue(animcombobox.store.getAt(i).get('sortorder'));		
					break;
													
			}
		}
		
	}
		
		
	/*if(current >= AnimationArray[animationposition][2]){
		
		document.getElementById(AnimationArray[animationposition][0]).setAttribute("class","dps_animation_off");
		document.getElementById(AnimationArray[animationposition][0]).setAttribute("className","dps_animation_off");
		animationposition = animationposition + 1
		if(animationposition == AnimationArray.length){animationposition = animationposition - 1}
		document.getElementById(AnimationArray[animationposition][0]).setAttribute("class","dps_animation_on");
		document.getElementById(AnimationArray[animationposition][0]).setAttribute("className","dps_animation_on");		
	}*/
	}catch(e){}
	
}



function toggleAnimation(name){

	if(animationMovie.IsPlaying()){
			animationMovie.StopPlay();

		}else{
			resetAnimationNumbers();
			animationMovie.Play();
		}// end isplaying

}//end toggle animation


function resetAnimationNumbers(){
	
	for (var i=0; i< AnimationArray.length; i++) {

			document.getElementById(AnimationArray[i][0]).setAttribute("class","dps_animation_off");
			document.getElementById(AnimationArray[i][0]).setAttribute("className","dps_animation_off");
			
			if(animationMovie.TCurrentFrame("/") >= AnimationArray[i][1] && animationMovie.TCurrentFrame("/") <= AnimationArray[i][2]){
				animationposition = i;
				document.getElementById(AnimationArray[i][0]).setAttribute("class","dps_animation_on");
				document.getElementById(AnimationArray[i][0]).setAttribute("className","dps_animation_on");
			}
			
			

	}	//end for
	
				// we need to check if we are replaying the animation
			
			if(animationMovie.TCurrentFrame("/") >=  (animationMovie.TGetProperty("/", 5) -1)){
				
				
				
				document.getElementById(AnimationArray[animationposition][0]).setAttribute("class","dps_animation_off");
				document.getElementById(AnimationArray[animationposition][0]).setAttribute("className","dps_animation_off");
				animationposition = 0;
				document.getElementById(AnimationArray[0][0]).setAttribute("class","dps_animation_on");
				document.getElementById(AnimationArray[0][0]).setAttribute("className","dps_animation_on");
				
			}
			
	
	
}





function playAnimation(name,start,end){
	//http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html
	try{clearInterval(stopAnimationInterval);}catch(e){}
	
	//alert(name);
	
	//stop the animation if it is playing.
	//animationMovie=getFlashMovieObject(name);
	animationMovie.StopPlay();
	//turn off current animations and set the clicked one.

	if(start == 0){
		animationMovie.GotoFrame(end);
	}else{
		animationMovie.GotoFrame(start);
		stopAnimationInterval = setInterval(function(){stopFrame(end);}, 30);
		animationMovie.Play();		
		
	}

}//playAnimation

function stopFrame(stop){
try{
	if(animationMovie.TCurrentFrame("/") >= stop){
		animationMovie.StopPlay();
		clearInterval(stopAnimationInterval);
	}
	}catch(e){}
}




function clearAnimations(){
	//AnimationArray = [];
	try{clearInterval(checkPlayStatusInterval);}catch(e){}
	try{clearInterval(animationSlideLoadedInterval);}catch(e){}
	try{clearInterval(stopAnimationInterval);}catch(e){}
	
	//alert('clear');
	
}
	
	
var backgroundAnimationInterval;	
function setBackground(name,frametoplay){
	var backgroundMovie=getAnimationMovieObject(name);
	backgroundAnimationInterval = setInterval(function(){checkBackgroundLoaded(backgroundMovie,frametoplay);}, 100);
	
}
	
function checkBackgroundLoaded(backgroundMovie,frametoplay){
	
	try{
		if(movieIsLoaded(backgroundMovie)){
		backgroundMovie.GotoFrame(frametoplay);
		clearInterval(backgroundAnimationInterval);
		//alert('done');
		}
	} catch (e){}
	
}

function formatMilliseconds(milliseconds){
	
	var format = '';
	
	if(milliseconds/86400000 > 1){
		format = format + 	parseInt(milliseconds/86400000) + ' : ';
		
		milliseconds = milliseconds - parseInt(milliseconds/86400000)*86400000;
		
	}
	
	if(milliseconds/3600000 > 1){
		format = format + 	parseInt(milliseconds/3600000) + ':';
		
		milliseconds = milliseconds - parseInt(milliseconds/3600000)*3600000;
		
	}	
	
	if(milliseconds/60000 > 1){
		format = format + 	parseInt(milliseconds/60000) + ':';
		
		milliseconds = milliseconds - parseInt(milliseconds/60000)*60000;
		
	}	
	
	
	if(milliseconds/1000 > 1){
		format = format + 	parseInt(milliseconds/1000)+ '.';
		
		milliseconds = milliseconds - parseInt(milliseconds/1000)*1000;
		
	}	
	
	
	if(format == ''){
		format = '0.';
	}
	format = format + parseInt(milliseconds).toString().substring(0,1) + ' sec';
		
	
	return format;
	
	
}