
//====================
// けさぱさテロッパー
//====================


function KesaPasaTeloper(
	vse,				// 生成したオブジェクトを格納する変数名（文字列で与えること） ... うーん、間抜けだ。
	disparea,			// 表示領域のID
	linkarea,			// リンク領域のID
	url					// 設定ファイルのURL
){

	/**************/
	/* プロパティ */
	/**************/

	this.vessel;				// 生成したオブジェクトを格納している変数名（イベント処理のため）

	this.XmlHttpObj;			// 生成したXMLHttpRequestオブジェクトを格納する変数

	this.ConfigURL;				// 設定ファイルのURL

	/* 読み込んだ表示用データを格納する配列 */

	this.DspText = new Array;	// 表示するメッセージ
	this.WinTrag = new Array;	// リンクを飛ばすときのtraget
	this.JumpURL = new Array;	// ジャンプ先のURL

	this.TextCol = new Array;	// 変化するテキストの色

	this.TDB;		// テキスト表示領域ブロックのオブジェクトを格納する変数
	this.JLA;		// リンク領域ブロックのオブジェクトを格納する変数

	this.ccCnt = -1;		// 上記の2色をなめらかに切り替えるためのカウンタ
	this.ccNum = -1;		// 2色を指定するカウンタ

	this.defaultTextColor = "000000";	// 文字色が指定されていない場合の文字色


	this.SelectedText = -1;			// 表示されているテキスト
	this.SequenceCounter = -1;

	this.ColorInterval		= 50;		// 文字色変更１シークェンス分の間隔（ミリ秒）

	this.SequenceInterval	= 80;		// テキスト表示１シークェンス分の間隔（ミリ秒）
	this.SequenceBlank		= 5;		// 文字表示切り替え時の無表示期間（↑の回数）

	this.ColorTimer;
	this.SequenceTimer;


	/******************/
	/* コンストラクタ */
	/******************/


	if (vse == '')	throw "オブジェクトを格納する変数名を指定してください。";
	if (disparea == '')	throw "表示領域を指定してください。";
	if (linkarea == '')	throw "リンク領域を指定してください。";
	if (url == '')	throw "設定ファイルを指定してください。";

	this.vessel = vse;

	this.TDB = document.getElementById( disparea );		// テキスト表示領域ブロックのオブジェクト
	this.JLA = document.getElementById( linkarea );		// リンク領域ブロックのオブジェクト

	this.ConfigURL = url;

	if (window.XMLHttpRequest) {		// 非同期通信オブジェクトを生成する

		this.XmlHttpObj = new XMLHttpRequest();		// Netscape／Firefox／Internet Explorer7以降

	} else if (window.ActiveXObject) {

		this.XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");	// Internet Explorer6以前

	} else {

		throw "非同期通信がサポートされていません。"	// NetFront3.2 .....orz

	}


	/************/
	/* メソッド */
	/************/

	//----------------------------------------------------------------
	// テキストをセットする
	//
	this.setTextMessage = function(
		texts,
		links,
		targs
	){
		var tTxt = new Array;
		var tTrg = new Array;
		var tLnk = new Array;

		if (texts.length != targs.length) return;
		if (targs.length != links.length) return;

		for(var t in texts){

			if (texts[t] == '') continue;

			tTxt.push( texts[t] );

			if (links[t] == '') {

				tTrg.push( '_self' );
				tLnk.push( 'javaScript:void(false)' );

			} else {

				tTrg.push( targs[t] );
				tLnk.push( links[t] );
			}

		}

		if (tTxt.length == 0) return;

		this.setSequenceTimer(false);

		delete this.DspText;
		delete this.WinTrag;
		delete this.JumpURL;

		this.DspText = tTxt;
		this.WinTrag = tTrg;
		this.JumpURL = tLnk;

		this.SelectedText = 0;
		this.SequenceCounter = 0;

		this.setSequenceTimer(true);
	}


	//----------------------------------------------------------------
	// 文字色をセットする
	//
	this.setTextColors = function(
		colors
	){
		var tmp = new Array;
		var regex = /^#[0-9A-F]{6}$/i;


		if (colors.length < 1) return;

		for(var t in colors){

			if ( regex.test(colors[t]) ) tmp.push( colors[t].substr(1,6) );
		}

		if (tmp.length < 1) return;

		this.setColorTimer( false );

		delete this.TextCol;

		this.TextCol = tmp;
		this.ccCnt = -1;
		this.ccNum = -1;

		this.setColorTimer( true );
	}


	//----------------------------------------------------------------
	// 表示シークェンスを進める
	//
	this.progressSequence = function (

	){
		var DispText = this.DspText[this.SelectedText];
		var TextLen = DispText.length;

		if (this.SequenceCounter < TextLen) {	// 順に表示

			this.TDB.innerHTML = DispText.substr(0,this.SequenceCounter+1);

		} else if (this.SequenceCounter < (TextLen*2)) {	// スクロール

			this.TDB.innerHTML = DispText.substring(this.SequenceCounter-TextLen, TextLen);

		} else {

			this.TDB.innerHTML = "";

		}

		if (++this.SequenceCounter >= (TextLen*2+this.SequenceBlank)) {

			this.SequenceCounter = 0;
			if (++this.SelectedText >= this.DspText.length) this.SelectedText = 0;

			this.JLA.href = this.JumpURL[this.SelectedText];
			this.JLA.target = this.WinTrag[this.SelectedText];
		}

	}


	//----------------------------------------------------------------
	// 表示文字色を切り替える
	//
	this.changeTextColor = function(

	){
		var ColorCode = '#';
		var cc;

		if ((++this.ccCnt) == 16) this.ccCnt=0;

		if (this.ccCnt == 0) this.ccNum++;

		cc = this.getTwoColors();


		if (this.ccCnt == 0) {

			ColorCode += cc.substr(0,6);

		} else {

			for(var t=0;t<3;t++){

				c1 = parseInt( cc.substr(2*t,2) ,16 );
				c2 = parseInt( cc.substr(2*t+6,2) ,16 );


				c1 = Math.round( c1+this.ccCnt*(c2-c1)/16 );

				if (c1 < 16) ColorCode += '0';

				ColorCode +=  c1.toString(16);
			}
		}

		this.TDB.style.color = ColorCode;

	}


	//----------------------------------------------------------------
	// 文字色を作る2色を文字列として取り出す
	//
	this.getTwoColors = function(

	){
		var ccnt = this.TextCol.length;
		var col;
		var cc;

		if (this.ccNum >= ccnt) this.ccNum = 0;

		if (ccnt < 1) {		// 色指定なし→デフォルトカラー

			col = this.defaultTextColor + this.defaultTextColor;
			return col;

		} else if (ccnt < 2) {

			col = this.TextCol[0] + this.TextCol[0];
			return col;

		}

		cc = this.ccNum+1;
		if (cc >= ccnt) cc = 0;

		col = this.TextCol[this.ccNum] + this.TextCol[cc];
		return col;
	}


	//----------------------------------------------------------------
	// 設定ファイルの取得要求を送信する
	//
	this.submitRequest = function(

	){
		var dt = new Date;

		this.XmlHttpObj.open( 'GET',this.ConfigURL+'&dummy='+dt.getTime(),true );

		this.XmlHttpObj.onreadystatechange = new Function( '',this.vessel+'.receiveData()' );
		this.XmlHttpObj.send('');
	}


	//----------------------------------------------------------------
	// 設定ファイルを受け取ってバッファに展開する
	//
	this.receiveData = function(

	){
		var conftext;
		var lines;

		var mode = '';

		var ele;
		var ttg;

		if ((this.XmlHttpObj.readyState != 4)||(this.XmlHttpObj.status != 200)) return;

		conftext = decodeURIComponent(this.XmlHttpObj.responseText);

		conftext = conftext.replace(/\r/g ,'');
		lines = conftext.split( /\n+/ );

		for(var lcnt in lines) {

			if (lines[lcnt].length == 0) continue;

			if (lines[lcnt] == '[text]'){
				mode = 'T';
				continue;
			}

			if (lines[lcnt] == '[color]'){
				mode = 'C';
				continue;
			}

			if (mode == 'C'){

				this.TextCol.push( lines[lcnt].match(/[0-9A-Fa-f]{6}/) );

			} else if (mode == 'T') {

				ele = lines[lcnt].split(/,/);

				this.DspText.push( decodeURI( ele.shift() ) );

				ttg = ele.shift();

				if (ele.length > 1) {	// ジャンプ先URLのリクエストパラメータにカンマが含まれていた場合の対応

					this.WinTrag.push( ttg );
					this.JumpURL.push( ele.join(',') );

				} else {

					if ( ele[0] == '') {
						this.WinTrag.push( '_self' );
						this.JumpURL.push( 'javaScript:void(false)' );
					} else {
						this.WinTrag.push( ttg );
						this.JumpURL.push( ele[0] );
					}

				}
			}

		}

		if (this.DspText.length > 0) {

			this.SelectedText = 0;
			this.SequenceCounter = 0;

			this.JLA.href = this.JumpURL[this.SelectedText];
			this.JLA.target = this.WinTrag[this.SelectedText];


			this.setColorTimer( true );
			this.setSequenceTimer( true );
		}

	}


	//----------------------------------------------------------------
	// タイマイベントを登録／停止する
	//
	this.setColorTimer = function(		// 色
		mode
	){
		if (mode) {		// true：登録

			this.ColorTimer = setInterval( this.vessel+'.changeTextColor()' , this.ColorInterval);

		} else {		// false：停止

			if (typeof(this.ColorTimer) == 'number') {

				clearInterval( this.ColorTimer );
				delete this.ColorTimer;
			}

		}

	}


	this.setSequenceTimer = function(	// テキスト
		mode
	){

		if (mode) {		// true：登録

			this.SequenceTimer = setInterval( this.vessel+'.progressSequence()' , this.SequenceInterval);

		} else {		// false：停止

			if (typeof(this.SequenceTimer) == 'number') {

				clearInterval( this.SequenceTimer );
				delete this.SequenceTimer;
			}

		}

	}



}



