// JavaScript


/**
 * トップ・エリア情報
 */
var AreaInfo = function(){};

AreaInfo.tabs = {};
AreaInfo.containers = {};
AreaInfo.districts = ['latest', 'hokkaido', 'tohoku', 'kanto', 'chubu', 'kinki', 'chugoku', 'shikoku', 'kyushu'];

/**
 * 初期化
 */
AreaInfo.init = function() {
	
	// 地域ごとのタブを格納
	for (var i=0; i<AreaInfo.districts.length; i++) {
		// タブ
		if (document.getElementById('tab-'+AreaInfo.districts[i])) {
			AreaInfo.tabs[AreaInfo.districts[i]] = document.getElementById('tab-'+AreaInfo.districts[i]);
			document.getElementById('tab-'+AreaInfo.districts[i]).onclick = function() {
				var did = this.id.substring(4);
				AreaInfo.selectTab(did);
				return false;
			};
		}
		if (document.getElementById('areainfolist-'+AreaInfo.districts[i]))
			AreaInfo.containers[AreaInfo.districts[i]] = document.getElementById('areainfolist-'+AreaInfo.districts[i]);
	}
	
	document.getElementById('latestAreaInfoAnchor').onclick = function() {
		AreaInfo.selectTab('latest');
		return false;
	};
	
	var initDid = null;
	
	// 選択されているタブ
	if (initDid == null)
		AreaInfo.selectTab(AreaInfo.districts[0]);
	else
		AreaInfo.selectTab(AreaInfo.districts[0]);
		
	// overflow
	document.getElementById('areainfolist').style.overflow = 'visible';
	
	// areatop
	var areatops = YAHOO.util.Dom.getElementsByClassName('areatop');
	for (var i=0; i<areatops.length; i++) {
		areatops[i].style.height = '100%';
	}
	
};


/**
 * タブを選択
 */
AreaInfo.selectTab = function(did) {
	
	if (!did)
		did = this.selected;
		
	for (var i in this.tabs) {
		this.setSelected(this.tabs[i], (i == did));
	}
	
	for (var i in this.containers) {
		this.containers[i].style.display =  (i==did) ? 'block':'none';
	}
		
};


/**
 * タブを選択状態にする
 */
AreaInfo.setSelected = function(element, bool) {
	if (bool) {
		YAHOO.util.Dom.addClass(element, 'active');
	} else {
		YAHOO.util.Dom.removeClass(element, 'active');
	}
};


/**
 * ホットニュースのアニメーションを設定
 */
var HotNews = function(){};
HotNews.iteration = 4;
HotNews.nodeNumber = 0;
HotNews.setHotnewsAnimation = function() {
	this.nodeNumber = document.getElementById('hotnewslist').childNodes.length;
	setTimeout(HotNews.animate, 4000);
};
HotNews.animate = function() {
	HotNews.iteration -= 1;
	document.getElementById('hotnews').style.top = HotNews.iteration+'px';
	// document.getElementById('iterator').innerHTML = HotNews.iteration+'px';
	if (HotNews.iteration > 4) {
		setTimeout(HotNews.animate, 50);
	} else if (HotNews.iteration < (HotNews.nodeNumber+2) * -20 +1 +4) {
		HotNews.iteration = 24;
		setTimeout(HotNews.animate, 50);
	} else if ((HotNews.iteration-4)%20 == 0) {
		setTimeout(HotNews.animate, 4000);
	} else {
		setTimeout(HotNews.animate, 50);
	}
};


addLoadEvent(AreaInfo.init);
addLoadEvent(HotNews.setHotnewsAnimation);
