
//==========================
// けさぱさテロッパー ver2
//==========================


function KesaPasaTeloper(
	disparea,		// テロップ表示領域のID
	linkarea		// リンク領域のID
){

	/**************/
	/* プロパティ */
	/**************/

	this.DspText = new Array(	// メッセージ
<!--V0[mess]-->
	);


	this.JumpURL = new Array(	// ジャンプ先URL
<!--V0[link]-->
	);


	this.WinTarg = new Array(	// ターゲットパラメータ
<!--V0[target]-->
	);



	this.TextCol = new Array(	// テキストカラー

<!--V0[color]-->
	);


	this.DefaultColor = "#000000";		// テキスト色のデフォルト値


	this.TDB = null;	// テロップ表示領域のオブジェクトを格納する変数
	this.JLA = null;	// リンク領域のオブジェクトを格納する変数


	this.SelectedText = 0;			// 表示中のテキスト
	this.SequenceCounter = -1;		// 文字表示のシークエンスをすすめるカウンタ

	this.CcCnt = -1;	// 文字色変更カウンタ
	this.CcNum = -1;	// 表示中の文字色

	this.ColorInterval = 50;		// 色変化の間隔（ミリ秒）

	this.SequenceInterval = 80;		// テキスト表示の間隔（ミリ秒）
	this.SequenceBlank = 5;			// 文字列切替時の非表示時間（↑の回数）

	this.SequenceTimer;
	this.ColorTimer;


	/************/
	/* メソッド */
	/************/




	//----------------------------------------------------------------
	// 表示項目を変更する（設定画面用）
	//
	this.resetParams = function (
		ms,
		co
	){
		this.switchTelop(false);

		this.DspText = new Array;
		this.JumpURL = new Array;
		this.WinTarg = new Array;

		for(var t=0;t<ms.length;t++){

			this.DspText.push( ms[t][0] );
			this.JumpURL.push( ms[t][1] );
			this.WinTarg.push( ms[t][2] );
		}


		this.TextCol = new Array;

		for(var t=0;t<co.length;t++){
			this.TextCol.push( co[t] );
		}

		this.SelectedText = 0;
		this.SequenceCounter = -1;

		this.CcCnt = -1;
		this.CcNum = -1;

		this.switchTelop(true);
	}


	//----------------------------------------------------------------
	// 表示シークェンスを進める
	//
	this.progressSequence = function (

	){
		var DispText = this.DspText[this.SelectedText];
		var TextLen = DispText.length;

		if (this.SequenceCounter == 0) {	// メッセージが切り替わった場合、リンク先を変更する

			if (this.JumpURL[this.SelectedText] == ''){

				this.JLA.href = '#';
				this.JLA.target = '';
			} else {

				this.JLA.href = this.JumpURL[this.SelectedText];
				this.JLA.target = this.WinTarg[this.SelectedText];
			}
		}


		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;
		}

	}



	//----------------------------------------------------------------
	// 文字色を作る2色を文字列として取り出す
	//
	this.getTwoColors = function(

	){
		var ccnt = this.TextCol.length;
		var col;
		var cc;

		if (this.CcNum >= ccnt) this.CcNum = 0;

		if (ccnt < 1) {		// 色指定なし→デフォルトカラー

			return (this.DefaultColor + this.DefaultColor);

		} else if (ccnt < 2) {

			return (this.TextCol[0] + this.TextCol[0]);

		}

		cc = this.CcNum+1;
		if (cc >= ccnt) cc = 0;

		return (this.TextCol[this.CcNum] + this.TextCol[cc]);
	}


	//----------------------------------------------------------------
	// 表示文字色を切り替える
	//
	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(1,6);

		} else {

			for(var t=0;t<3;t++){

				c1 = parseInt( cc.substr(2*t+1,2) ,16 );
				c2 = parseInt( cc.substr(2*t+8,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;
	}



	//----------------------------------------------------------------
	// 停止する／再開する
	//
	this.switchTelop = function(
		mode		// true…スタート／false…ストップ
	){
		var self = this;

		if (mode) {

			if (this.SequenceTimer != null) return;	// すでに動作中

			this.SequenceTimer = setInterval( function(){ self.progressSequence() },this.SequenceInterval );
			this.ColorTimer = setInterval( function(){ self.changeTextColor() },this.ColorInterval );

		} else {

			if (this.SequenceTimer == null) return;	// すでに停止中

			clearInterval( this.SequenceTimer );
			clearInterval( this.ColorTimer );

			this.SequenceTimer = null;
			this.ColorTimer = null;
		}

	}


	/******************/
	/* コンストラクタ */
	/******************/


	this.TDB = document.getElementById( disparea );

	if (this.TDB == null) {
		alert("テロップ表示領域のIDを確認してください。");
		return;
	}



	this.JLA = document.getElementById( linkarea );

	if (this.JLA == null) {
		alert("リンク領域のIDを確認してください。");
		return;
	}


	this.switchTelop( true );
}






