//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	JAVA アニメーション・ライブラリ
//
//		Author: Hiroshi Shibata
//
//
//------------------------------------------------------------
//	このスクリプトは、『芦屋人』のオリジナルです。
//	著作権は全て、『芦屋人』に帰属します。
//	全部または一部をコピー、転載、その他の無断使用を禁じます。
//				(Mail: hiroshi_shibata@ashiya-people.com)
//
//	Copyright 2004 Ashiya-People.Com All rights reserved.
//------------------------------------------------------------
//	● グローバル ●
var gAniRoot;
//	● 定数 ●
var animeIntervalTime = 50;
//____________________________________________________________



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	初期化
//____________________________________________________________

function animeInit()
{
	//< 初期化 >----------
	gAniRoot = this;
	this.animeObjs = new Object();
	this.animeInterval = setInterval("animeIntervalCall()", animeIntervalTime);
	//< /初期化 >---------
}
//____________________________________________________________



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	インターバル・コール
//____________________________________________________________

function animeIntervalCall()
{
	for (var id in this.animeObjs) {
		var obj = this.animeObjs[id];
		if (obj) {
			obj.loop();
			switch (obj.cmd) {
				case 'delete':
					this.animeObjs[id] = null;
					delete obj;
					break;
				default:
					break;
			}
		}
	}
}
//____________________________________________________________



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	インターバル関数 登録
//____________________________________________________________

function animeNew(
	id, data
){
	var obj;
	switch (data.type) {
		case 'fadeIn':
			obj = new animeObjInit(id, data, fadeIn_init);
			break;
		case 'fadeOut':
			obj = new animeObjInit(id, data, fadeOut_init);
			break;
		case 'simpleSlide':
			obj = new animeObjInit2(id, data, simpleSlide_init);
			break;
		case 'curtainSlide':
			obj = new animeObjInit2(id, data, curtainSlide_init);
			break;
		case 'crossFade':
			obj = new animeObjInit2(id, data, crossFade_init);
			break;

		default:
			obj = new animeObjInit(id, data, msFilter_init);
			break;
	}
	switch (obj.err) {
		case 'delete':
			delete obj;
			break;
		default:
			this.animeObjs[id] = obj;
			break;
	}
}
//____________________________________________________________



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	インターバル関数 削除
//____________________________________________________________

function animeDel(
	id
){
	var obj = this.animeObjs[id];
	if (obj) {
		this.animeObjs[id] = null;
		delete obj;
	}
}
//____________________________________________________________










//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	共通処理
//____________________________________________________________

//-----< 共通初期化 >-----
function animeObjInit(
	id, data, next
){
	this.id = id;
	this.data = data;

	var img = window[id];
	if (!img) {
		this.err = 'delete';
		return;
	}
	this.img = img;

	//データ内ファイルリストがない場合、グローバルデータを参照
	if (data.files) this.files = data.files;
	else if (window[id+'Files']) this.files = window[id+'Files'];

	//初期化
	this.no = (this.data.no) ? this.data.no : 0;
	this.num = this.files.length;

	this.loop = next;
}

//-----< 共通初期化 (画像切り替え用) >-----
function animeObjInit2(
	id, data, next
){
	this.id = id;
	this.data = data;

	var img = window[id];
	if (!img) {
		this.err = 'delete';
		return;
	}

	//データ内ファイルリストがない場合、グローバルデータを参照
	if (data.files) this.files = data.files;
	else if (window[id+'Files']) this.files = window[id+'Files'];

	//初期化
	this.no = (this.data.no) ? this.data.no : 0;
	this.num = this.files.length;
	this.side = (this.data.side) ? this.data.side : 0;

	if (!img.lenght) {
		//アニメ用にもう一枚コピーする
		var clone = img.cloneNode(true);
		img.parentElement.appendChild(clone);
		img = window[id];
	}
	this.img = img;

	//zIndex取得
	this.zIndex = img[0].style.zIndex;
	if (!this.zIndex) this.zIndex = 0;

	this.loop = next;
}

//-----< 待ち処理 >-----
function animeTimerWait()
{
	if (!this.timer) {
		this.loop = this.next;
	} else this.timer--;
}

//-----< NOP >-----
function animeNop()
{
}
//____________________________________________________________










//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	msFilter
//____________________________________________________________

function msFilter_init()
{
	//dispFrm: 表示時間
	this.dispFrm = (this.data.dispFrm) ? this.data.dispFrm : 200;
	//

	//イメージ生成
	this.preload = new Image();

	//フィルターセット
	var filters = this.data.msFilter;
	if (filters) {
		var filter="";
		for (var i=0; i<filters.length; i++) {
			if (i) filter += ' ';
			filter += 'progid:'
			if (!filters[i].match(/\./)) filter += 'DXImageTransform.Microsoft.';
			filter += filters[i];
		}
		this.img.style.filter = filter;
	}

	this.loop = msFilter_loadNext;
}

function msFilter_loadNext()
{
	//次の画像の準備
	if (this.num <= ++this.no) this.no = 0;
	this.preload.src = ((this.data.path)? this.data.path:"")+this.files[this.no];

	//タイマーセット
	this.next = msFilter_loadCK;
	this.timer = this.dispFrm;
	this.loop = animeTimerWait;
}

function msFilter_loadCK()
{
	if (this.preload.readyState=='complete') {
		this.loop = msFilter_swap;
	}
}

function msFilter_swap()
{
	//次の画像の準備
	this.img.filters[0].Apply();
	this.img.src = this.preload.src;
	this.img.filters[0].Play();

	if ((this.data.loop!=undefined) && (this.data.loop==false)) {
		this.cmd = 'delete';
	} else {
		//タイマーセット
		this.next = msFilter_loadNext;
		this.timer = this.dispFrm;
		this.loop = animeTimerWait;
	}
}
//____________________________________________________________










//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	fadeIn
//____________________________________________________________

function fadeIn_init()
{
	//dispFrm: 表示時間
	if (this.data.fadeInWait) this.fadeInWait = this.data.fadeInWait;
	else this.fadeInWait = (this.data.dispFrm) ? this.data.dispFrm : 80;
	this.fadeOutWait = (this.data.fadeOutWait) ? this.data.fadeOutWait : this.fadeInWait;
	//maxOpacity: 目標透明度
	this.maxOpacity = (this.data.maxOpacity) ? this.data.maxOpacity : 100;
	//minOpacity: 現在値
	this.minOpacity = this.opacity = this.img.filters[0].opacity;
	//fadeFrm: フェード時間
	if (this.data.opacityInc) this.opacityInc = this.data.opacityInc;
	else if (this.data.fadeFrm) {
		this.opacityInc = (this.maxOpacity-this.minOpacity) / this.data.fadeFrm;
	} else this.opacityInc = (this.maxOpacity-this.minOpacity) / 30;
	//loopCount: ループ回数
	this.loopCount = (this.data.loopCount) ? this.data.loopCount : 0;

	//タイマーセット
	this.next = fadeIn_fade;
	this.timer = this.fadeInWait;
	this.loop = animeTimerWait;
}

function fadeIn_fade()
{
	this.opacity += this.opacityInc;
	if (this.maxOpacity <= this.opacity) {
		this.opacity = this.maxOpacity;
		if (this.loopCount) {
			//タイマーセット
			this.next = fadeOut_fade;
			this.timer = this.fadeOutWait;
			this.loop = animeTimerWait;
			this.loopCount--;
		} else this.cmd = 'delete';
	}
	this.img.filters[0].opacity = this.opacity;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	fadeOut
//____________________________________________________________

function fadeOut_init()
{
	//dispFrm: 表示時間
	if (this.data.fadeOutWait) this.fadeOutWait = this.data.fadeOutWait;
	else this.fadeOutWait = (this.data.dispFrm) ? this.data.dispFrm : 80;
	this.fadeInWait = (this.data.fadeInWait) ? this.data.fadeInWait : this.fadeOutWait;
	//minOpacity: 目標透明度
	this.minOpacity = (this.data.minOpacity) ? this.data.minOpacity : 0;
	//maxOpacity: 現在値
	this.maxOpacity = this.opacity = this.img.filters[0].opacity;
	//fadeFrm: フェード時間
	if (this.data.opacityInc) this.opacityInc = this.data.opacityInc;
	else if (this.data.fadeFrm) {
		this.opacityInc = (this.maxOpacity-this.minOpacity) / this.data.fadeFrm;
	} else this.opacityInc = (this.maxOpacity-this.minOpacity) / 30;
	//loopCount: ループ回数
	this.loopCount = (this.data.loopCount) ? this.data.loopCount : 0;

	//タイマーセット
	this.next = fadeOut_fade;
	this.timer = this.fadeOutWait;
	this.loop = animeTimerWait;
}

function fadeOut_fade()
{
	this.opacity -= this.opacityInc;
	if (this.opacity <= this.minOpacity) {
		this.opacity = this.minOpacity;
		if (this.loopCount) {
			//タイマーセット
			this.next = fadeIn_fade;
			this.timer = this.fadeInWait;
			this.loop = animeTimerWait;
			this.loopCount--;
		} else this.cmd = 'delete';
	}
	this.img.filters[0].opacity = this.opacity;
}
//____________________________________________________________



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	simpleSlide
//____________________________________________________________

function simpleSlide_init()
{
	//dispFrm: 表示時間
	this.dispFrm = (this.data.dispFrm) ? this.data.dispFrm : 200;

	this.loop = simpleSlide_loadNext
}

function simpleSlide_loadNext()
{
	//次の画像の準備
	if (this.num <= ++this.no) this.no = 0;
	this.side ^= 1;
	this.img[this.side].style.zIndex = this.zIndex-1;
	this.img[this.side].src = ((this.data.path)? this.data.path:"")+this.files[this.no];

	//タイマーセット
	this.next = simpleSlide_loadCK;
	this.timer = this.dispFrm;
	this.loop = animeTimerWait;
}

function simpleSlide_loadCK()
{
	if (this.img[this.side].readyState=='complete') {
		this.loop = simpleSlide_swap;
	}
}

function simpleSlide_swap()
{
	this.img[this.side].style.zIndex = this.zIndex;
	this.loop = simpleSlide_loadNext;
}
//____________________________________________________________





//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	crossFade
//____________________________________________________________

function crossFade_init()
{
	//dispFrm: 表示時間
	this.dispFrm = (this.data.dispFrm) ? this.data.dispFrm : 200;
	//maxOpacity: 最大透明度
	this.maxOpacity = (this.data.maxOpacity) ? this.data.maxOpacity : 100;
	//fadeFrm: フェード時間
	if (this.data.opacityInc) this.opacityInc = this.data.opacityInc;
	else if (this.data.fadeFrm) this.opacityInc = this.maxOpacity / this.data.fadeFrm;
	else this.opacityInc = this.maxOpacity / 30;

	this.loop = crossFade_loadNext
}

function crossFade_loadNext()
{
	//次の画像の準備
	if (this.num <= ++this.no) this.no = 0;
	this.side ^= 1;
	this.opacity = 0;
	this.img[this.side].filters[0].opacity = this.opacity;
	this.img[this.side].style.zIndex = this.zIndex+1;
	this.img[this.side].src = ((this.data.path)? this.data.path:"")+this.files[this.no];
	this.img[this.side^1].style.zIndex = this.zIndex;

	//タイマーセット
	this.next = crossFade_loadCK;
	this.timer = this.dispFrm;
	this.loop = animeTimerWait;
}

function crossFade_loadCK()
{
	if (this.img[this.side].readyState=='complete') {
		this.loop = crossFade_swap;
	}
}

function crossFade_swap()
{
	this.opacity += this.opacityInc;
	if (this.maxOpacity <= this.opacity) {
		this.opacity = this.maxOpacity;
		this.loop = crossFade_loadNext;
	}
	this.img[this.side].filters[0].opacity = this.opacity;
	this.img[this.side^1].filters[0].opacity = 100-this.opacity;
}
//____________________________________________________________





//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	curtainSlide
//____________________________________________________________

function curtainSlide_init()
{
	//dispFrm: 表示時間
	this.dispFrm = (this.data.dispFrm) ? this.data.dispFrm : 200;
	//fadeFrm: フェード時間
	if (this.data.moveRateInc) this.moveRateInc = this.data.moveRateInc;
	else if (this.data.fadeFrm) this.moveRateInc = 100 / this.data.fadeFrm;
	else this.moveRateInc = 100 / 30;
	//scrlType: スクロールタイプ
	this.scrlType = (this.data.scrlType) ? this.data.scrlType : 'horizon';
	//scrlDir: スクロール方向 ( 0:左(上), 1:右(下) )
	this.scrlDir = (this.data.scrlDir) ? this.data.scrlDir : 0;
	//カスタム処理
	if (this.data.funcSwap) this.funcSwap = this.data.funcSwap;
	if (this.data.funcBlockLoad) this.funcBlockLoad = this.data.funcBlockLoad;
	if (this.data.funcBlockSwap) this.funcBlockSwap = this.data.funcBlockSwap;

	//画像サイズ
	this.width = this.img[0].width;
	this.height = this.img[0].height;

	this.loop = curtainSlide_loadNext;
}

function curtainSlide_loadNext()
{
	//次の画像の準備
	if (this.num <= ++this.no) this.no = 0;
	this.side ^= 1;
	this.scrlDir ^= 1;
	this.moveRate = 0;
	switch (this.scrlType) {
		case 'vertical':
			this.img[this.side].style.top = (this.scrlDir) ? this.height : -this.height;
			break;
		case 'horizon':
		default:
			this.img[this.side].style.left = (this.scrlDir) ? -this.width : this.width;
			break;
	}
	this.img[this.side].style.zIndex = this.zIndex+1;
	this.img[this.side].src = ((this.data.path)? this.data.path:"")+this.files[this.no];
	this.img[this.side^1].style.zIndex = this.zIndex;

	//タイマーセット
	this.next = curtainSlide_loadCK;
	this.timer = this.dispFrm;
	this.loop = animeTimerWait;
}

function curtainSlide_loadCK()
{
	if (this.img[this.side].readyState=='complete') {
		if (this.funcBlockSwap) {
			//ブロック処理
			this.block = true;
			this.blockFunc = this.funcBlockSwap;
			this.next = curtainSlide_swap;
			this.loop = curtainSlide_block;
		} else this.loop = curtainSlide_swap;
	}
}

function curtainSlide_swap()
{
	this.moveRate += this.moveRateInc;
	if (100 <= this.moveRate) {
		this.moveRate = 100;
		if (this.funcBlockLoad) {
			//ブロック処理
			this.block = true;
			this.blockFunc = this.funcBlockLoad;
			this.next = curtainSlide_loadNext;
			this.loop = curtainSlide_block;
		} else this.loop = curtainSlide_loadNext;
	}

	var scrl = 0;
	switch (this.scrlType) {
		case 'vertical':
			this.img[this.side].style.top;
			break;
		case 'horizon':
		default:
			if (this.scrlDir) scrl = this.width * (this.moveRate-100)*0.01;
			else scrl = this.width * (100-this.moveRate)*0.01;
			this.img[this.side].style.left = scrl;
			break;
	}

	//カスタム処理
	if (this.funcSwap) this.funcSwap();
}

function curtainSlide_block()
{
	//ブロック処理
	this.blockFunc();
	if (!this.block) this.loop = this.next;
}
//____________________________________________________________
