var FlashNews = Class.create();
FlashNews.prototype = {
	initialize: function() {
		this.article = [];
		this.articleChar = 0;
		this.articleIndex = 0;
		this.displayTime = 5000;
	},
	// フラッシュニュース追加
	displayArticle: function() {
		var objClass = this;
		$$("div.FlashNews").each(function(div) {
			var news = {};
			var tdObj = div.getElementsByTagName("td");
			$A(tdObj).each(function(td) {
				var imgObj = td.getElementsByTagName("img");
				if (imgObj.length == 0) {
					var aObj = td.getElementsByTagName("a");
					if (aObj.length > 0) {
						$A(aObj).each(function(a) {
							news["text"] = a.innerHTML;
							news["href"] = a.href;
						});
					} else {
						news["time"] = td.innerHTML;
					}
				}
				if (news["time"] && news["text"] && news["href"]) {
					// フラッシュニュース追加
					objClass.addArticle(news);
					news = {};
				}
			});
		});
		// フラッシュニュース初期設定
		this.initArticle();
	},
	// フラッシュニュース追加
	addArticle: function(news) {
		this.article[this.articleIndex] = news;
		this.articleIndex++;
	},
	initArticle: function() {
		if (this.article.length > 0) {
			var element = document.createElement("div");
			element.id = "FlashNewsTicker";
//			element.innerHTML = this.article[0].time + " <a href=\"" + this.article[0].href + "\">" + this.article[0].text + "</a>";
			$$("div.FlashNews").each(function(div) {
				div.appendChild(element);
			});
			if (this.article.length > 1) {
				this.articleIndex = 0;
//				setTimeout(this.showArticleTime.bindAsEventListener(this), this.displayTime);
				this.showArticleTime();
			}
		}
	},
	showArticleTime: function() {
		var article = this.article[this.articleIndex];
		// 記事がある場合
		if (article) {
			if (article.time.length > this.articleChar) {
				this.articleChar++;
				$("FlashNewsTicker").innerHTML = article.time.substring(0, this.articleChar);
				setTimeout(this.showArticleTime.bindAsEventListener(this), 75);
			} else {
				this.articleChar = 0;
				setTimeout(this.showArticleTitle.bindAsEventListener(this), 200);
			}
		// 記事がない場合
		} else {
			this.articleChar = 0;
			this.articleIndex = 0;
			setTimeout(this.showArticleTime.bindAsEventListener(this), this.displayTime);
		}
	},
	showArticleTitle: function() {
		var article = this.article[this.articleIndex];
		// 記事がある場合
		if (article) {
			if (article.text.length > this.articleChar) {
				this.articleChar++;
				$("FlashNewsTicker").innerHTML = article.time + " <a href=\"" + article.href + "\">" + article.text.substring(0, this.articleChar) + "</a>";
				setTimeout(this.showArticleTitle.bindAsEventListener(this), 50);
			} else {
				this.articleChar = 0;
				this.articleIndex++;
				if (this.articleIndex >= this.article.length) {
					this.articleIndex = 0;
				}
				setTimeout(this.showArticleTime.bindAsEventListener(this), this.displayTime);
			}
		// 記事がない場合
		} else {
			this.articleChar = 0;
			this.articleIndex = 0;
			setTimeout(this.showArticleTime.bindAsEventListener(this), this.displayTime);
		}
	}
}

// ロード完了時
Event.observe(window, "load", function() {
	var flashNews = new FlashNews();
	flashNews.displayArticle();
});

