/*************************************************

Class:            animStepTime.class.js
Created On:       Aug 5, 2004
Last Modified On: Apr 7, 2005
Author:           Ricardo Cook
Copyright:        Magnifi Group, Inc. (c) 2004.

List of Modifications:

 Aug 06, 2004: * Removed argument 'nsteps' from object
 Aug 09, 2004: * Added functionality to handle multiple animations
 Aug 11, 2004: * Fixed bug in 'playStep()' function
 Aug 12, 2004: * Updated 'animationStopped()' function to handle multiple animations
               * Added 'animLength' parameter to 'addAnimation()'
               * Added 'stepIniTime' parameter to 'stepTime()'
               * Removed 'addAnimTime()', 'addStepTime()', 'setAnimations()', 'setSteps()', 'getAnimIndex()'
               * Modified 'playStep()' to consider the 'animLength' values. Added 'autoRunStatus' parameter              
 Aug 19, 2004: * Added functionality to handle sound (functions are being called through Flash)
 Oct 20, 2004: * Added command to reset camera: 'resetCamera()'      
 							 * Added functionality to scroll animation: scrollAnimation()
 Feb 08, 2005: * Removed animation length as it is not required.
 Feb 14, 2005: * Added camera animation.
 Apr 05, 2005: * Fixed camera animation to play only in Autorun and not when it got (un)Paused. 
 Apr 27, 2005: * Add interactivity for animated callout (Flash).
 May 05, 2005: * Added multiple animated callouts (predefined).
 Apr 05, 2005: * Fixed camera animation to play only in Autorun and not when it got (un)Paused.
 Jun 01, 2005: * Added step by step camera movements. 
               
*************************************************/

function animStepTime(vmpObj) {
	this.vmp = vmpObj;

	this.playingAutoRun =  false;
	this.currStep = -1;
	
	
	this.animationArray = new Array;
	this.animatedCallout = new Array(4);
	this.stepArray = new Array;
	this.stepTimeArray = new Array;	
	this.camAnim = "";
	this.camAnimArray = new Array;
	this.camAnimLengthArray = new Array;
}


/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.addAnimation = function(aName) {
	
	if (typeof Array.prototype.push=="undefined")
	{
  	Array.prototype.push=function(){
    	var i=0;
    	b = this.length,a=arguments;
    	for(i;i<a.length;i++)this[b+i]=a[i];    	
  	};
	}	
	

	this.animationArray.push(aName);
}

/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/
animStepTime.prototype.setAnimatedCallout = function (aName,index) {
	this.animatedCallout[index-1] = aName;
}

/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.camAnimation = function(caName) {
	this.camAnim = caName;
}


/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.addCamAnimation = function(aName,aLength) {
	if (typeof Array.prototype.push=="undefined")
	{
  	Array.prototype.push=function(){
    	var i=0;
    	b = this.length,a=arguments;
    	for(i;i<a.length;i++)this[b+i]=a[i];    	
  	};
	}	
	
	this.camAnimArray.push(aName);
	this.camAnimLengthArray.push(aLength);
}

/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.addStep = function(sName, sTime) {

if(typeof Array.prototype.push=="undefined"){
		Array.prototype.push=function(){
			var i=0;
			b=this.length,a=arguments;
			for(i;i<a.length;i++)this[b+i]=a[i];			
		};
	}

	this.stepArray.push(sName);
	this.stepTimeArray.push(sTime);
}


/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.isPlayingAutoRun = function() {
	return this.playingAutoRun;
}


/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.setPlayingAutoRun = function(autoRunStatus) {
	this.playingAutoRun = autoRunStatus;
}


/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.animationStopped = function(stepNum) {
	if (!this.isPlayingAutoRun() && stepNum == this.currStep) 
		for (i=0; i<this.animationArray.length; i++)
			this.vmp.StopAnim('MTSTimeElem.' + this.animationArray[i]);	
}


/*************************************************
FUNCTION:  PLAYSTEP
VERSION: 2.0
*************************************************/

animStepTime.prototype.playStep = function(argvalue, autoRunStatus) {	

	stepNum = argvalue;	
	var currTime = 0;	
	this.vmp.SetProperty('MTSScene', 'nerd', 1, 'mts_int');
	this.setPlayingAutoRun(autoRunStatus);
	this.vmp.StopAnim('MTSTimeElem.' + this.camAnim);
	this.currStep = stepNum;
	currTime = this.stepTimeArray[stepNum-1];

	for (i=0; i<this.animationArray.length; i++) {
			this.playAnimFrom(this.animationArray[i],currTime);
	}
	
	
	if (!autoRunStatus) {
		for (i=0; i<this.camAnimArray.length; i++) {
				if (this.camAnimLengthArray[i] >= currTime) 
					this.playAnimFrom(this.camAnimArray[i],currTime);		

		}
	} 
	/*else{
	this.playAnimFrom(this.camAnim,currTime);	
	}*/

}
/*************************************************
FUNCTION:  PLAYSTEPINTER
VERSION: 2.0
*************************************************/

animStepTime.prototype.playStepCam = function(argvalue, autoRunStatus) {
	
	//alert("step no interaction");

	stepNum = argvalue;
	var currTime = 0;	

	this.vmp.SetProperty('MTSScene', 'nerd', 1, 'mts_int');
	this.setPlayingAutoRun(autoRunStatus);
	this.currStep = stepNum;
	currTime = this.stepTimeArray[stepNum-1];

	for (i=0; i<this.animationArray.length; i++) {
			this.playAnimFrom(this.animationArray[i],currTime);
	}
	
	this.playAnimFrom(this.camAnim, currTime);
	
	if (!autoRunStatus) {
		for (i=0; i<this.camAnimArray.length; i++) {
				if (this.camAnimLengthArray[i] >= currTime) 
					this.playAnimFrom(this.camAnimArray[i],currTime);		

		}
	} 

}

/*************************************************
FUNCTION:  SCROLL ANIMATION
VERSION: 2.0
*************************************************/

animStepTime.prototype.scrollAnimation = function(argvalue) {
	
	//alert(argvalue);
	
	argsvalues = argvalue.split(",");
	times = argsvalues[0];
	autos = argsvalues[1];	
	freeform = argsvalues[2];
	
	//alert(freeform);
	
	for (i=0; i<this.animationArray.length; i++) {
		if (freeform == 0){
			this.playAnimFrom(this.camAnim, times); //**********************************
		}
		this.playAnimFrom(this.animationArray[i],times);
	}
}

/*************************************************
FUNCTION: PLAY ANIMATION
VERSION: 2.0
*************************************************/

animStepTime.prototype.playAnimFrom = function(aName,time) {
	this.vmp.StopAnim('MTSTimeElem.' + aName);
	this.vmp.SetProperty('MTSTimeElem.' + aName, 'rwnd', 1, 'mts_int');
	this.vmp.ResetAnim('MTSTimeElem.' + aName);
	this.vmp.SetProperty('MTSTimeElem.' + aName, 'tinc', time, 'mts_real');
	this.vmp.StartAnim('MTSTimeElem.' + aName);
}


/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.playAnimation = function(argvalue) {
	var argsvalues = argvalue.split(",");
	time = argsvalues[0];
	freeform = argsvalues[1];
	
	if(freeform == 1){
		this.playStep(1,false);
		//this.playAnimFrom(this.camAnim, time);
		//alert("free form on");
	}
	else {
		//alert("freeform off");
		this.playStep(1, freeform, true);
		this.playAnimFrom(this.camAnim, time);
	}
	
	

}

/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.resetAnimation = function() {
	var aname = '';
	
	this.playingAutoRun =  false;
	this.currStep = -1;

	for (i=0; i<this.animationArray.length; i++) {		
		aname = this.animationArray[i];
		this.vmp.StopAnim('MTSTimeElem.' + aname);
		this.vmp.ResetAnim('MTSTimeElem.' + aname);
		this.vmp.SetProperty('MTSTimeElem.' + aname, 'tinc', 0.01, 'mts_real');
	}
		this.vmp.StopAnim('MTSTimeElem.' + this.camAnim);
		this.vmp.ResetAnim('MTSTimeElem.' + this.camAnim);
		this.vmp.SetProperty('MTSTimeElem.' + this.camAnim, 'tinc', 0.01, 'mts_real');
	
}


/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.pauseAnimation = function() {
	
	for (i=0; i<this.animationArray.length; i++) {
		this.vmp.StopAnim('MTSTimeElem.' + this.animationArray[i]);
	}
	this.vmp.StopAnim('MTSTimeElem.' + this.camAnim);	
	
	for (i=0; i<this.animatedCallout.length; i++)
		this.vmp.StopAnim('MTSTimeElem.' + this.animatedCallout[i]);
	
}

/*************************************************
FUNCTION:
VERSION: 2.0
************************************************/
animStepTime.prototype.resumeAnimation = function(argvalue) {

	var argsvalues= argvalue.split(",");
	time = argsvalues[0];
	auto = argsvalues[1];	
	freeform = argsvalues[2];
	//alert (freeform);
	
	if (freeform == 0){
			this.playAnimFrom(this.camAnim, time); //**********************************
			
		} else
			this.vmp.StopAnim(this.camAnim);
	
	for (i=0; i<this.animationArray.length; i++) {
		this.playAnimFrom(this.animationArray[i],time);
	}		
		
		


	
/*
	for (i=0; i<this.animationArray.length; i++) {
		this.playAnimFrom(this.animationArray[i],time);
	}	
	
	if (freeform = 0) 
		this.playAnimFrom(this.camAnim, time);
*/	
	for (i=0; i<this.animatedCallout.length; i++)
		this.vmp.StartAnim('MTSTimeElem.' + this.animatedCallout[i]);
		
/*		
		
	for (i=0; i<this.animationArray.length; i++) {
		if (freeform == 0){
			this.playAnimFrom(this.camAnim, times); //**********************************
		}
		this.playAnimFrom(this.animationArray[i],times);
	}		
		
	*/	

}

/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.resetCamera = function() {
	this.vmp.SetProperty('MTSScene', 'rstc', '1', 'mts_int');
}

/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/
animStepTime.prototype.restarCam = function(time) {
		this.playingAutoRun	= true;
		this.playAnimFrom(this.camAnim,time);		
}

/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/
animStepTime.prototype.stopCam = function() {
	this.playingAutoRun	= false;
	this.vmp.StopAnim('MTSTimeElem.' + this.camAnim);	
}
/*************************************************
FUNCTION:
VERSION: 2.0
************************************************/
animStepTime.prototype.playCamera = function (time) {	
	this.playAnimFrom(this.camAnim, time);
}

/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.handleFSCommand = function(command, args) {
	
	if (command == "play") {
		this.playAnimation(args);
		
	}
	else if (command == "pause") {
		this.pauseAnimation();
	}
	else if (command == "resume") {
		this.resumeAnimation(args);
	}
	else if (command == "reset") {
		this.resetAnimation();
	}
	else if (command == "step") {
		this.playStep(args,this.playingAutoRun);	 
	}
	else if (command == "stepCam") {
		this.playStepCam(args,this.playingAutoRun);	 
	}
	else if (command == "resetCamera") {
		this.resetCamera();		
	}	
	else if (command == "scroll") {
		//alert(args);
		this.scrollAnimation(args);
	}
	else if (command == "restartCam") {
		this.restarCam(args);
	}
	else if (command == "stopCam") {
		this.stopCam();
	}	
	else if (command == "playCamera") {
		this.playCamera(args);
	}
	/*Options for the Database Bookmarks*/
	else if (command == "camera") {
		this.transferItemToFlash(args);
	}
	else if (command == "setCamera") {
		this.setCamera(args);
	}
	else if (command == "setCamera2") {
		this.setCamera2(args);
	}

}

/*************************************************
FUNCTION:
VERSION: 2.0
*************************************************/

animStepTime.prototype.toString = function() {
  	var str = '';

	str = 'ANIMATIONS\n';
	for (i=0; i<this.animationArray.length; i++)	{
		str += this.animationArray[i] + '\n';
	}
	
	str += '\nSTEPS\n';
	for (i=0; i<this.stepArray.length; i++) {
		str += this.stepArray[i] + ' ' + this.stepTimeArray[i] + '\n';
	}

	return str;

}



/*Functions for the Database Bookmarks*/

animStepTime.prototype.transferItemToFlash = function() {
	
	
	
	var trans = this.vmp.GetProperty('MTSScene', 'tra_', 'mts_pnt3d');
	var orbd  = this.vmp.GetProperty('MTSScene', 'orbd', 'mts_pnt3d');
	var rot   = this.vmp.GetProperty('MTSScene', 'rot_', 'mts_pnt3d');
	

	document.controls.SetVariable("_root.t",trans);
	document.controls.SetVariable("_root.o",orbd);
	document.controls.SetVariable("_root.r",rot);
}

animStepTime.prototype.setCamera = function(argValue) {
	
	var argsvalues= argValue.split(",");
	tra=argsvalues[0];
	orbd=argsvalues[1];
	rot=argsvalues[2];
	pos=argsvalues[3];	
	
	var trans = this.vmp.SetProperty('MTSScene', 'tra_', tra, 'mts_pnt3d');
	var orbd  = this.vmp.SetProperty('MTSScene', 'orbd', orbd, 'mts_pnt3d');
	var rot   = this.vmp.SetProperty('MTSScene', 'rot_', rot, 'mts_pnt3d');
	
	this.scrollAnimation(pos);
	this.pauseAnimation();
	void(0);
	
	
	
}
	
	
	
	
	
