//===============================================================================
// General description of this file:
//	Utilities to handle files.
//===============================================================================




//===============================================================================
// Description:
//	JavaScript for the Kami-shibai by changing HTML pages.
// Classes:
//	fileActionFlip()
// Samples:
//	<script type="text/javascript">
//	  <!--
//		// Create title object
//		var fileFlip = new fileActionFlip("fileFlip", "TestWin",
//			"file1.html", 1.5,
//			...,
//			"fileN.html", 3);
//	  // -->
//	</script>
//
//	<button onClick="fileFlip.start();">TEST START</button>
//	<button onClick="fileFlip.closeWin();">Close</button>
//
//===============================================================================


//---[class]---------------------------------------------------------------------
function fileActionFlip(
	objName,	// Object instance name
	targetID	// Window ID of THML

	//... more parameters are handled in this function ...
	// file1, timeInterval1,	// time interval [sec]
	// file2, timeInterval2,
	// ...,
	// FileN, timeIntervalN
)
{
	//---[constructor]-----------------------------------------------------------------
	this.targetID = targetID;
	this.objName = objName;
	this.fileCountMax = (arguments.length - 2)/2;	// the number of files
	this.fileCount = 0;	// start from 0
	this.htmlFile = new Array();
	this.timeInterval = new Array();
	for (var i = 0; i < this.fileCountMax; i++) {
		this.htmlFile[i] = arguments[i*2 + 2];		// set file name
		this.timeInterval[i] = arguments[i*2 + 3] * 1000;	// set time interval; convert [sec] to [msec]
	}
	this.actionDoing = false;
	this.winID = null;


	//---[method]----------------------------------------------------------------------
	// Start action, and main function of action.
	//---------------------------------------------------------------------------------
	function start()
	{
		this.winID = window.open(this.htmlFile[this.fileCount], this.targetID);
		this.winID.focus();	// rise up the target window

		if (this.fileCount == 0) {	// clear if timeout action remains
			if (this.tmoutID) {
				this.reset();
			}
		}
		if (this.fileCount < this.fileCountMax-1) {
			this.actionDoing = true;
			this.tmoutID = setTimeout(this.objName + ".start()", this.timeInterval[this.fileCount]);
			this.fileCount++;	// count up file number
		} else {
			this.reset();
		}

	}
	fileActionFlip.prototype.start = start;



	//---[method]----------------------------------------------------------------------
	// Reset to initial state.
	//---------------------------------------------------------------------------------
	function reset()
	{
		if (this.actionDoing) {
			clearTimeout(this.tmoutID);
		}		
		this.actionDoing = false;	// Clear the flag
		this.fileCount = 0;
	}
	fileActionFlip.prototype.reset = reset;


	//---[method]----------------------------------------------------------------------
	// Close window. Also reset all timeout actions.
	//---------------------------------------------------------------------------------
	function closeWin()
	{
		this.reset();
		if (this.winID) {
			this.winID.close();
		}		
	}
	fileActionFlip.prototype.closeWin = closeWin;

}


