// stage.js

var ID_Scr640x480	= 1;
var ID_Scr800x600	= 2;
var ID_Scr1024x768	= 3;

//----------------------------
var _Scr640x480 = 	{
				contentHeight: 290,
				contentWidth: 599
				}
//----------------------------
var _Scr800x600 = 	{
				contentHeight: 410,
				contentWidth: 759
				}
//----------------------------
var _Scr1024x768 = 	{
				contentHeight: 578,
				contentWidth: 983
				}

var mCurrentStage;

//-----------------------------------------------------------------------------  
//     STAGE HELPER CLASSES  (StageObject, FrameObject, FrameObjectsMgr)
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
function StageObject(elem)
	{
	this.type = elem.type;
	if (elem.src != undefined) this.src = elem.src;
	}
//-----------------------------------------------------------------------------
function StageObjects()
	{
	this.objects = new Array();
	}
//-----------------------------------------------------------------------------
StageObjects.prototype.Add = function(elem)
	{
	this.objects[this.objects.length] = new StageObject(elem);
	}
//-----------------------------------------------------------------------------
StageObjects.prototype.GetTotal = function()
	{
	return this.objects.length;
	}
//-----------------------------------------------------------------------------
StageObjects.prototype.Get = function(idx)
	{
	return this.objects[idx];
	}
//-----------------------------------------------------------------------------
StageObjects.prototype.Find = function(elem)
	{
	for (idx = 0; idx < this.objects.length; idx++)
		{
		if (this.type == elem.type)
			{
			if (this.src == elem.src) return idx;	
			}
		}
	return -1;
	}

//-----------------------------------------------------------------------------
function FrameObject(o)
	{
	if (o.elemID != undefined) this.elemID = o.elemID;
	if (o.objIdx != undefined) this.objIdx = o.objIdx;
	if (o.top != undefined) this.top = o.top;
	if (o.left != undefined) this.left = o.left;
	if (o.width != undefined) this.width = o.width;
	if (o.height != undefined) this.height = o.height;
	}

//-----------------------------------------------------------------------------
function FrameObjects(){
	this.objects = new Array();		// of FrameObject objects
}
//-----------------------------------------------------------------------------
FrameObjects.prototype.Add = function(elem)
	{
	var s = elem.style;
	
	this.objects[this.objects.length] = new FrameObject({
		elemID: elem.id,
		objIdx: mCurrentStage.frameMgr.stageObjects.Find(elem),
		top: s.top,
		left: s.left,
		height: s.height,
		width: s.width
	});
	}
//-----------------------------------------------------------------------------
FrameObjects.prototype.Clear = function(){
	for (var idx = 0; idx < this.objects.length; idx++)
		{
		delete this.objects[idx];
		}
	delete this.objects;
	this.objects = new Array();
}
//-----------------------------------------------------------------------------
function FrameObjectsMgr(){
	this.stageObjects = new StageObjects();
	this.frameObjsList = new Array();	// of FrameObjects objects
	this.lastUsedElemID = 0;
	this.frameIdx = 0;
}
//-----------------------------------------------------------------------------
FrameObjectsMgr.prototype.AddStageObject = function(elem){
	if (this.stageObjects.Find(elem) == -1)
		{
		this.stageObjects.Add(elem);
		}
}

//-----------------------------------------------------------------------------
FrameObjectsMgr.prototype.SetFrame = function(frameIdx){
	this.frameIdx = frameIdx;
	if (frameIdx >= this.frameObjsList.length)
		{
		for (var idx = this.frameObjsList.length; idx <= frameIdx; idx++)
			{
			this.frameObjsList[idx] = new FrameObjects();	
			}
		}
}

//-----------------------------------------------------------------------------
FrameObjectsMgr.prototype.ClearFrame = function(frameIdx){
	this.frameIdx = frameIdx;
	this.frameObjsList[this.frameIdx].Clear();
}

//-----------------------------------------------------------------------------
FrameObjectsMgr.prototype.AddElem = function(elem){
	this.frameObjsList[this.frameIdx].Add(elem);
}
//-----------------------------------------------------------------------------
FrameObjectsMgr.prototype.GetTotalObjsInFrame = function(){
	return this.frameObjsList[this.frameIdx].objects.length;
}
//-----------------------------------------------------------------------------
FrameObjectsMgr.prototype.GetFrameObj = function(idx){
	return this.frameObjsList[this.frameIdx].objects[idx];
}
//-----------------------------------------------------------------------------
FrameObjectsMgr.prototype.FindFrameObj = function(elemID){
	var fObjs = this.frameObjsList[this.frameIdx].objects;
	for (idx = 0; idx < fObjs.length; idx++)
		{
		if (fObjs[idx].elemID == elemID) return fObjs[idx];
		}
	return null;
}
//-----------------------------------------------------------------------------
FrameObjectsMgr.prototype.IsDifferentFromPrevFrame = function(elem)
	{
	if (this.frameIdx > 0) 
		{
		--this.frameIdx;
		var fObj = this.FindFrameObj(elem.id);
		++this.frameIdx;
		if (fObj)
			{
			var s = elem.style;
			if (fObj.top == s.top)
				{
				if (fObj.left == s.left)
					{
					if (fObj.height == undefined) fHeight = 0; else fHeight = fObj.height;
					if (s.height == undefined) sHeight = 0; else sHeight = s.height;
					if (fObj.width == undefined) fWidth = 0; else fWidth = fObj.width;
					if (s.width == undefined) sWidth = 0; else sWidth = s.width;
					if (sHeight == fHeight)
						{
						if (sWidth == fWidth)
							{
							return false;
							}
						}
					}
				}
			}
		}
	return true;
	}
	
//-----------------------------------------------------------------------------  
//     BEGINS STAGE CLASS
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
function Stage(o){
	o.elemType = "div";
	if (o.top == undefined) o.top = 0;
	if (o.left == undefined) o.left = 0;
	if (o.scrnSize == undefined) o.scrnSize = _Scr800x600;
	else o.scrnSize = o.scrnSize;
	if (o.height == undefined ) o.height = o.scrnSize.contentHeight;
	if (o.width == undefined ) o.width = o.scrnSize.contentWidth;
	if (o.overflow == undefined) o.overflow = "hidden";
	this.div = NewElem(o);
	this.div.obj = this;
	this.framesPerSec = 20;
	this.frameInterval = 1000/this.framesPerSec;
	this.currentTime = 0;
	this.endTime = 0;
	this.frameDescripts = new Array();

	this.frameMgr = new FrameObjectsMgr();
	this.ContinueRecording_InProgress = false;
	mCurrentStage = this;
}
//-----------------------------------------------------------------------------
Stage.prototype.GetHeight = function(){
	return this.div.style.height;
}
//-----------------------------------------------------------------------------
Stage.prototype.GetWidth = function(){
	return this.div.style.width;
}

//-----------------------------------------------------------------------------
Stage.prototype.SetSize = function(size)
	{
	var s = this.div.style;

	switch(size)
		{
		case ID_Scr640x480:
			s.height = _Scr640x480.contentHeight;
			s.width = _Scr640x480.contentWidth;
			break;
			
		case ID_Scr800x600:
			s.height = _Scr800x600.contentHeight;
			s.width = _Scr800x600.contentWidth;
			break;
			
		case ID_Scr1024x768:
			s.height = _Scr1024x768.contentHeight;
			s.width = _Scr1024x768.contentWidth;
			break;
		}
	}


//-----------------------------------------------------------------------------
Stage.prototype.StartRecording = function(ctlPanel)
	{
	this.ctlPanel = ctlPanel;
	this.beganRecording = GetTick();
	this.recordFuncID = setInterval("ContinueRecording()", this.frameInterval);
	}

//-----------------------------------------------------------------------------
function ContinueRecording()
	{
	if (mCurrentStage.ContinueRecording_InProgress) return;
	mCurrentStage.ContinueRecording_InProgress = true;
//	mCurrentStage.currentTime = GetTick() - mCurrentStage.beganRecording;
	
//	mCurrentStage.ctlPanel.SetReadout(mCurrentStage.currentTime);
	mCurrentStage.ctlPanel.panel.container.SetReadout(mCurrentStage.currentTime);
	
	if (mCurrentStage.currentTime > mCurrentStage.endTime)
		{
		mCurrentStage.endTime = mCurrentStage.currentTime;
		}
	mCurrentStage.ShowFrame();
	mCurrentStage.RecordFrame();
	mCurrentStage.currentTime += mCurrentStage.frameInterval;
	mCurrentStage.ContinueRecording_InProgress = false;
	}
	
//-----------------------------------------------------------------------------
function FrameElemDescript()
	{
	this.frameElems = new Array();
	}
//-----------------------------------------------------------------------------
Stage.prototype.RecordFrame = function()
	{
	var frameNum = parseInt(this.currentTime / this.frameInterval);
	var fMgr = this.frameMgr;
	
	fMgr.SetFrame(frameNum);
	fMgr.ClearFrame(frameNum);

	var node = this.div.firstChild;
	while(node)
		{
//		if (fMgr.IsDifferentFromPrevFrame(node)) fMgr.AddElem(node);	zzzzzzzzzz
		fMgr.AddElem(node);
		node = node.nextSibling;
		}
//mGridDebug.AddText("frame #"+frameNum+" has "+fMgr.GetTotalObjsInFrame()+" objs");
	}
//-----------------------------------------------------------------------------
Stage.prototype.ShowFrame = function()
	{
	var frameNum = parseInt(this.currentTime / this.frameInterval);
	var fm = this.frameMgr;
	fm.SetFrame(frameNum);
	var total = fm.GetTotalObjsInFrame();
	for (idx = 0; idx < total; idx++)
		{
		var fObj = fm.GetFrameObj(idx);
		elem = document.getElementById(fObj.elemID);
		if (elem)
			{
			if (!elem.beingDragged)
				{
				var s = elem.style;
				if (fObj.top != undefined) s.top = fObj.top;
				if (fObj.left != undefined) s.left = fObj.left;
				if (fObj.height != undefined) s.height = fObj.height;
				if (fObj.width != undefined) s.width = fObj.width;
				}
			}
		}
	}

//-----------------------------------------------------------------------------
Stage.prototype.StopRecording = function(ctlPanel)
	{
	clearInterval(this.recordFuncID);
	}
//-----------------------------------------------------------------------------
Stage.prototype.StartPlaying = function(ctlPanel)
	{
	this.ctlPanel = ctlPanel;
	if (mCurrentStage.currentTime == mCurrentStage.endTime) mCurrentStage.currentTime = 0;
	this.beganPlaying = GetTick();
	this.playFuncID = setInterval("ContinuePlaying()", this.frameInterval);
	}
//-----------------------------------------------------------------------------
function ContinuePlaying()
	{
	if (mCurrentStage.ContinuePlaying_InProgress) return;
//	mCurrentStage.ContinuePlaying_InProgress = true;
	
	if ((mCurrentStage.currentTime + mCurrentStage.frameInterval) < mCurrentStage.endTime)
		{
		mCurrentStage.currentTime += mCurrentStage.frameInterval;	
//		mCurrentStage.ctlPanel.SetReadout(mCurrentStage.currentTime);
		mCurrentStage.ctlPanel.panel.container.SetReadout(mCurrentStage.currentTime);
		mCurrentStage.ShowFrame();
		}
	else mCurrentStage.StopPlaying(null);
//	mCurrentStage.ContinuePlaying_InProgress = false;
	}

//-----------------------------------------------------------------------------
Stage.prototype.StopPlaying = function(ctlPanel)
	{
	clearInterval(this.playFuncID);
	mCurrentStage.ctlPanel.panel.container.playBut.elem.value = "Play";
	}

//-----------------------------------------------------------------------------
Stage.prototype.GoToFirstFrame = function(ctlPanel)
	{
	ctlPanel.panel.container.SetReadout(this.currentTime = 0);
	this.ShowFrame();
	}
//-----------------------------------------------------------------------------
Stage.prototype.GoToPrevFrame = function(ctlPanel)
	{
	this.currentTime -= this.frameInterval;
	if (this.currentTime < 0) this.currentTime = 0;
	ctlPanel.panel.container.SetReadout(this.currentTime);
	this.ShowFrame();
	}
//-----------------------------------------------------------------------------
Stage.prototype.GoToNextFrame = function(ctlPanel)
	{
	this.currentTime += this.frameInterval;
	ctlPanel.panel.container.SetReadout(this.currentTime);
	if (this.currentTime > this.endTime) this.endTime = this.currentTime;
	this.ShowFrame();
	}
//-----------------------------------------------------------------------------
Stage.prototype.GoToLastFrame = function(ctlPanel)
	{
	ctlPanel.panel.container.SetReadout(this.currentTime = this.endTime);
	this.ShowFrame();
	}
//-----------------------------------------------------------------------------
Stage.prototype.AddPictFile = function(path){
	var img = new Image({
		parent: this.div,
		id: ++this.frameMgr.lastUsedElemID,
		className: "drag",
		top: 0,
		left: 0,
		src: path,
		onmousedown: DragObject_Begin
	});

	this.frameMgr.AddStageObject(img.elem);
}

//-----------------------------------------------------------------------------
Stage.prototype.SetBackgroundImage = function(path){
	this.div.style.backgroundImage = "url("+path+")";
}

//-----------------------------------------------------------------------------
Stage.prototype.DeleteAllStageElements = function(){
	var div = this.div;
	while (div.firstChild) {
		div.removeChild(box.firstChild);
	}
}
