// imp_gather1.js   ver.2004/02/13
var gather_layers = new Array();
var gather_step = 0;

function GatherLayer (s) {
	this.id = s;
	this.show = gather_show;
	this.startpos = gather_layer_startpos;
	this.setsteps = gather_layer_setsteps;
	this.move = gather_layer_move;
	this.angle = Math.random() * 360;
	this.xend = parseInt(document.getElementById(this.id).style.left);
	this.yend = parseInt(document.getElementById(this.id).style.top);
	this.x = 0;
	this.y = 0;
	this.max_step = 20;
	this.step = 0;
	this.swing = 0;
	this.xsteps = new Array(this.max_step);
	this.ysteps = new Array(this.max_step);
}

function gather_show() {
	document.getElementById(this.id).style.visibility = "visible";
}

function gather_layer_startpos(x, y) {
	var i;
	var style = document.getElementById(this.id).style;
	this.x = Math.floor(x); this.y = Math.floor(y);
	var w = 0; if(style.width) w = parseInt(style.width);
	var h = 0; if(style.height) h = parseInt(style.height);
	if(w && this.x + w > gather_winwidth())
		this.x = gather_winwidth() - w;
	if(h && this.y + h > gather_winheight())
		this.y = gather_winheight() - h;
	this.x += gather_scrollleft();
	this.y += gather_scrolltop();
	style.left = this.x + "px";
	style.top = this.y + "px";
	
	this.setsteps(this.max_step);
}

function gather_layer_setsteps(num) {
	this.max_step = num;
	
	for(i = 0; i < this.max_step - 1; i++) {
		this.xsteps[i] = Math.floor(this.x +
			((this.xend - this.x) / this.max_step) * (i+1));
		this.ysteps[i] = Math.floor(this.y +
			((this.yend - this.y) / this.max_step) * (i+1));
	}
	this.xsteps[this.max_step - 1] = this.xend;
	this.ysteps[this.max_step - 1] = this.yend;
}

function gather_layer_move() {
	if(this.step >= this.max_step) return;
	var style = document.getElementById(this.id).style;
	
	var x = this.xsteps[this.step];
	var y = this.ysteps[this.step];
	if(this.swing != 0 && this.step < this.max_step - 1) {
		var swing = Math.floor(this.swing * 
			Math.sin((this.step+1) * (Math.PI*2 / this.max_step)));
		y += swing;
		if(this.x < this.xend && this.y < this.yend) x -= swing;
		else if(this.x > this.xend && this.y > this.yend) x -= swing;
		else x += swing;
	}
	
	style.left = x + "px";
	style.top  = y + "px";
	this.step++;
}

function gather_init() {
	var i;
	
	if(!document.getElementById) return;
	
	for(i = 0; ; i++) {
		if(!document.getElementById(gather_prefix + (i+1))) break;
		gather_layers[i] = new GatherLayer(gather_prefix + (i+1));
	}
	
	// 文字の出現位置を自動計算
	for(i = 0; i < gather_layers.length; i++) {
		var p = 0;
		if(gather_layers.length > 2) {
			p = (i * (gather_winwidth() + gather_winheight()))
			/ (gather_layers.length - 1);
		}
		var x, y;
		if(p < gather_winheight()/2) {
			x = 0; y = gather_winheight()/2;
			if(i % 2) y += p; else y -= p;
		}
		else if(p < gather_winheight()/2 + gather_winwidth()) {
			x = p - gather_winheight()/2;
			if(i % 2) y = gather_winheight(); else y = 0;
		}
		else {
			x = gather_winwidth();
			y = p - (gather_winheight()/2 + gather_winwidth());
			if(i % 2) y = gather_winheight() - y;
		}
		
		gather_layers[i].startpos(x, y);
	}
}

function gather_setstartpos(id, x, y) {
	for(i = 0; i < gather_layers.length; i++) {
		if(gather_layers[i].id == id) {
			gather_layers[i].startpos(x, y);
			break;
		}
	}
}

function gather_setsteps(id, num) {
	for(i = 0; i < gather_layers.length; i++) {
		if(gather_layers[i].id == id) {
			gather_layers[i].setsteps(num);
			break;
		}
	}
}

function gather_setswing(id, num) {
	for(i = 0; i < gather_layers.length; i++) {
		if(gather_layers[i].id == id) {
			gather_layers[i].swing = num; break;
		}
	}
}

function gather_start() {
	var i;
	
	if(!document.getElementById) return;
	
	for(i = 0; i < gather_layers.length; i++) {
		gather_layers[i].show();
	}
	gather_step = 0;
	setTimeout("gather_anim();", gather_speed);
}

function gather_anim() {
	var i;
	
	for(i = 0; i < gather_layers.length; i++) {
		gather_layers[i].move();
	}
	gather_step++;
	for(i = 0; i < gather_layers.length; i++) {
		if(gather_step < gather_layers[i].max_step) {
			setTimeout("gather_anim();", gather_speed); break;
		}
	}
}

/* ------------------------------------
　ウィンドウの幅取得
------------------------------------ */
function gather_winwidth () {
	if(window.innerWidth)
		return window.innerWidth;
	if(document.compatMode == "CSS1Compat")
		return document.documentElement.clientWidth;
	if(document.body && document.body.clientWidth)
		return document.body.clientWidth;
	return 600;
}

/* ------------------------------------
　ウィンドウの高さ取得
------------------------------------ */
function gather_winheight () {
	if(window.innerHeight)
		return window.innerHeight;
	if(document.compatMode == "CSS1Compat")
		return document.documentElement.clientHeight;
	if(document.body.clientHeight)
		return document.body.clientHeight;
	return 400;
}

/* ------------------------------------
　スクロール領域の上端
------------------------------------ */
function gather_scrolltop () {
	if(window.pageYOffset)
		return window.pageYOffset;
	if(document.compatMode == "CSS1Compat")
		return document.documentElement.scrollTop;
	if(document.body.scrollTop)
		return document.body.scrollTop;
	return 0;
}

/* ------------------------------------
　スクロール領域の左端
------------------------------------ */
function gather_scrollleft () {
	if(window.pageXOffset)
		return window.pageXOffset;
	if(document.compatMode == "CSS1Compat")
		return document.documentElement.scrollLeft;
	if(document.body.scrollLeft)
		return document.body.scrollLeft;
	return 0;
}
