var top_data;
var last_date;
var dangerous_threshold;	// 危険値（-999も同様）
var attention_threshold;	// 注意値

var obj = {};

// 定期的に最新データを取得表示する
function getData(data)
{
	if (!data) {
		alert("データが取得できませんでした。しばらくしてからもう一度表示してください。");
		return;
	}
	top_data = data.data;
	last_date = data.last_date;
	dangerous_threshold = data.dangerous_threshold;
	attention_threshold = data.attention_threshold;
//alert("取得しました:" + last_date);
	// 表示処理
	var i;
	for (i in top_data) {
		if (!obj[i]) {
			obj[i] = new newMark();
			obj[i].create(top_data[i]);
		} else {
			obj[i].create(top_data[i]);
		}
	}
	// 日時設定
	str = '最終更新時刻:' + last_date.substr(0, 4) + '年' +
		last_date.substr(4, 2) + '月' + 
		last_date.substr(6, 2) + '日' + 
		last_date.substr(8, 2) + '時' + 
		last_date.substr(10, 2) + '分';
	document.getElementById('updatedate').innerHTML = str;
}

function getNewData()
{
	var url = 'bin/top.php?func=getData&eo=UTF-8&' + new Date().getTime();
	var script = document.createElement('script');

	script.type = 'text/javascript';
	script.charset = 'UTF-8';
	script.src = url;
//alert(url);
	document.body.appendChild(script);
}

// 道府県番号と地図上の位置の対応
var pref_point = {
	1: [ 240, 90],
	2: [ 260, 135],
	4: [ 270, 194],
	7: [ 270, 220],
	8: [ 270, 250],
	14: [ 250, 270],
	15: [ 230, 224],
	17: [ 200, 240],
	//18: [ 178, 273], // mod @20090520 関連道府県をくっつける。
	18: [ 178, 275],
	26: [ 185, 287],
	22: [ 225, 285],
	27: [ 175, 300],
	31: [ 145, 285],
	//33: [ 145, 303],// mod @20090520 関連道府県をくっつける。
	33: [ 147, 297],
	32: [ 120, 298],
	38: [ 130, 330],
	//41: [ 80, 342], // mod @20090520 関連道府県をくっつける。
	41: [ 81, 343],
	42: [ 82, 355],
	46: [ 98, 380]
};

// エリア別ページへ飛ぶ
function go_map(area)
{
//	alert("実際はエリア地図「"+ area +"」へ飛びます");
	window.location = area;
}

// マークと表を1つ設定
var newMark = function()
{
	this.trTag = null;
	this.tdTag1 = null;
	this.tdTag2 = null;
}

function mark_on(i)
{
	document.getElementById("map_comment1").style.display = "block";
	document.getElementById("map_comment2").style.display = "block";
	document.getElementById("map_comment3").style.display = "block";
}
function mark_off()
{
	document.getElementById("map_comment1").style.display = "none";
	document.getElementById("map_comment2").style.display = "none";
	document.getElementById("map_comment3").style.display = "none";
}
// 
function comment_on(e, station, cx, cy)
{
	var item = document.getElementById('pref_comment');
	item.style.left = '0px';
	item.innerHTML = station;
	item.style.display = 'block';	// 次の行で表示の高さを求めるため、ここで表示する。（順序注意）
	var oh = item.offsetHeight;
	var arrow = document.getElementById('pref_arrow');
	arrow.style.left = (cx - 50) + "px";
	arrow.style.top = (cy - 1) + "px";
	cy = cy - oh;
	arrow.style.display = 'block';
	cx = cx - (station.length * (oh / 2));	// DIVの右を越えると改行が発生するのを防止
//	item.innerHTML = station;
	item.style.left = cx + 'px';
	item.style.top = cy + 'px';
}
// 
function comment_off()
{
	document.getElementById('pref_arrow').style.display = 'none';
	document.getElementById('pref_comment').style.display = 'none';
}

newMark.prototype.create = function(data)
{
	var i;
	var el = document.getElementById("pref_list");	// 表の本体
	var ary = data.plant.split(' ');
	var cx = 540;
	this.data = data;
	this.area = data.area;

	this.plant = '';
	var j;
	var ary2;
	for (i in ary) {
		//this.plant += '・' + ary[i] + '<br>';
		ary2 = ary[i].split(',');
		for (j in ary2) {
			if (j==0) {
				this.plant += '・' + ary2[j] + '<br>';
			} else {
				ary2[j] = ary2[j].replace('_', '&nbsp;&nbsp;&nbsp;');
				if (j < ary2.length - 1) {
					this.plant += '&nbsp;&nbsp;&nbsp;&nbsp;' + ary2[j] + ',<br>';
				} else {
					this.plant += '&nbsp;&nbsp;&nbsp;&nbsp;' + ary2[j] + '<br>';
				}
			}
		}
	}
	// MOD:END by s.miyake @20090321
	// 表を作成
	if (!this.trTag) {
		this.trTag = document.createElement('tr');
		el.appendChild(this.trTag);
	}
	var self = this;
	this.trTag.onmouseover = function(e) {
		var top_point = document.getElementById('pref_top').offsetTop;
		document.getElementById("map_comment1").innerHTML = self.area;
		document.getElementById("map_comment3").innerHTML = self.plant
		mark_on();
		cy = top_point - 10 + this.offsetTop - document.getElementById('tables').scrollTop;
		comment_on(e, self.data.station, cx, cy);
		this.style.backgroundColor = "#aaccff";
	};
	this.trTag.onmouseout = function(e) {
		mark_off();
		comment_off();
		this.style.backgroundColor = "#ffffff";
	};
	this.trTag.onclick = function() {
		go_map(data.url);
	};
	if (!this.tdTag1) {
		this.tdTag1 = document.createElement('td');
	}
	this.tdTag1.setAttribute("align", "center");
	this.tdTag1.innerHTML = this.area;
	this.trTag.appendChild(this.tdTag1);
	//
	if (!this.tdTag2) {
		this.tdTag2 = document.createElement('td');
	}
	this.tdTag2.setAttribute("align", "center");
	if ((parseInt(data.value) >= attention_threshold)) {
		if (attention_threshold / 1 == 9999) {
			this.tdTag2.innerHTML = '調整中'; // 条件追加20091222
		} else {
			this.tdTag2.innerHTML = '注意'; // 黄色マーク(attention_threshold以上の場合 ※-888は線量率では無いとのこと)
		}
	} else if (parseInt(data.value) >= dangerous_threshold || (data.value < 0)) {
		this.tdTag2.innerHTML = '調整中'; // ピンクマーク(dangerous_threshold以上、または-999の場合)
	} else {
		this.tdTag2.innerHTML = data.value + 'nGy/h';
	}
	this.trTag.appendChild(this.tdTag2);
	//

	var i;
	for (i in data.pref_data) {
		var pref = data.pref_data[i];
		var x =  pref_point[parseInt(pref.pref_id)][0];
		var y =  pref_point[parseInt(pref.pref_id)][1];
		var map = document.getElementById("map");
		var mark = document.createElement('img');

		if ((parseInt(pref.value) >= attention_threshold)) {
			if (attention_threshold / 1 == 9999) {
				mark.src = "img/marker2.gif"; // 条件追加20091222
			} else {
				mark.src = "img/marker3.gif";
			}
		} else if (parseInt(pref.value) >= dangerous_threshold || (pref.value < 0)) {
			mark.src = "img/marker2.gif";
		} else {
			mark.src = "img/marker1.gif";
		}
		mark.style.position = "absolute";
/*
		mark.style.top = "0px";
		mark.style.left = "0px";
		mark.style.marginLeft = x + "px";
		mark.style.marginTop = y + "px";
*/
		mark.style.marginTop = "0px";
		mark.style.marginLeft = "0px";
		mark.style.left = x + "px";
		mark.style.top = y + "px";

		mark.onmouseover = function(e) {
			document.getElementById("map_comment1").innerHTML = self.area;
			document.getElementById("map_comment3").innerHTML = self.plant;
			mark_on();
			var top_point = document.getElementById('pref_top').offsetTop;
			cy = top_point - 10 + self.trTag.offsetTop - document.getElementById('tables').scrollTop;
			comment_on(e, data.station, cx, cy);
			self.trTag.style.backgroundColor = "#aaccff";
		};
		mark.onmouseout = function() {
			mark_off();
			comment_off();
			self.trTag.style.backgroundColor = "#ffffff";
		};
		mark.onclick = function() {
			go_map(data.url);
		};
		map.appendChild(mark);
	}
}
