// #require mootools.js

var headline;

if (window.addEvent) {

	var headlineDataSample = ([
		{
			"text": "Dragons公式ブログ　日々の最新レポートを掲載！「合同自主トレスタート！！！」",
			"date": "2008年1月15日"
		},
		{
			"text": "リンク先: Google, 日付: あり(スラッシュ区切り)",
			"link": "http://www.google.co.jp",
			"target": "_blank",
			"date": "2008/3/17"
		},
		{
			"text": "テキストのみテキストのみテキストのみテキストのみテキストのみテキストのみテキストのみ"
		},
	]).filter($chk);

	var HeadlineCtrl = new Class({
		initialize: function () {
			this.timer = null;
			this.initialized = false;
			this.dataready = false;
			this.dataloading = false;
			var headlineArea = $('headline');
			if (headlineArea.getStyle('position') == 'static')	headlineArea.setStyle('position', 'relative');
//スライダーが入る全体のエリア
			var backArea = new Element('div', {
				'styles': {
					'position': 'absolute',
					'top': 2,
					'left': 125,
					'width': 365,
					'height': 23,
					'background-color': '#ffffff',
					'visibility': 'hidden'
				}
//テキストのエリア
			}).injectInside(headlineArea);
			var tickArea = new Element('div', {
				'styles': {
					'position': 'absolute',
					'top': 7,
					'left': 128,
					'width': 365,
					'height': 16,
					'overflow': 'hidden'
				}
			}).injectInside(headlineArea);
			var elm = new Element('div', {
				'styles': {
					'position': 'absolute',
					'width': 575,
					'height': 18,
					'left': 1575,
					'font-size': 10,
					'line-height': 16,
					'display': 'none'
				}
			}).injectInside(tickArea);
			elm.addEvent('mouseenter', (function () {
				this.stop();
				if (this.dataready) {
					backArea.setStyle('visibility', 'visible');
				}
			}).bind(this)).addEvent('mouseleave', (function () {
				if (this.dataready) {
					backArea.setStyle('visibility', 'hidden');
					this.start();
				}
			}).bind(this));
			this.elm = elm;
			this.fx = elm.effect('left', {'duration': 800});
			this.tickerIdx = 0;
			// data
			this.headlineData = '';
			this._onDataReady = [];
			if (!window.location.protocol.test(/^https?:/)) {
				this.headlineData = headlineDataSample;
				this.dataready = true;
			}
		},
		loadData: function () {
			if (this.dataready)	return;
			this.reloadData();
		},
		reloadData: function () {
			if (this.dataloading)	return;
			this.dataloading = true;
			if (!this.dataready) {
				this.elm.setStyles({
					'top': 0,
					'left': 0,
					'width': 575,
					'height': 18,
					'display': 'block'
				}).empty().setHTML('\u203b\u30c7\u30fc\u30bf\u8aad\u8fbc\u4e2d...');
			}
			var ctrlObj = this;
			var jsonRequest = new Json.Remote("/_data/headline.php", {
				method: 'get',
				encoding: 'utf-8',
				onComplete: function (data) {
					ctrlObj.dataloading = false;
					if (!ctrlObj.dataready) {
						ctrlObj.elm.empty().setStyle('left', 575);
					}
					ctrlObj.headlineData = data;
					ctrlObj.dataready = true;
					// fire DataReady EventHandlers.
					if (window.gecko) {
						(function () {
							ctrlObj._onDataReady.each(function (fn) { fn(); });
							ctrlObj._onDataReady = [];
						}).delay(1);
					} else {
						ctrlObj._onDataReady.each(function (fn) { fn(); });
						ctrlObj._onDataReady = [];
					}
				},
				onFailure: function () {
					ctrlObj.dataloading = false;
					ctrlObj.elm.setStyles({
						'top': 0,
						'left': 0,
						'width': 575,
						'height': 18,
						'display': 'block'
					}).empty().setHTML('\u203b\u305f\u3060\u3044\u307e\u30b5\u30fc\u30d0\u304c\u6df7\u96d1\u3057\u3066\u3044\u307e\u3059\u3002');
				}
			});
			jsonRequest.send();
		},
		start: function () {
			if (this.timer)	return;
			if (!this.dataready) {
				this.elm.setStyle('display', 'block');
				this._onDataReady.push(this.start.bind(this));
				if (!this.dataloading)	this.loadData();
				return;
			}
			var fnFormatData = function (item) {
				var text = '';
				if ($type(item) == 'string') {
					text = item;
				} else {
					if ($defined(item.text)) {
						text = item.text;
						if ($defined(item.link)) {
							var link = '<a href="' + item.link + '"';
							if ($defined(item.target)) {
								link += ' target="' + item.target + '"';
							}
							link += '>';
							text = link + text + '</a>';
						}
						if ($defined(item.date)) {
							text += ' (' + item.date + ')';
						}
					} else {
						text += item;
					}
				}
				return text;
			};
			var fnFx = function() {
				this.fx.start(-575).chain((function () {
					var idx = this.tickerIdx = (this.tickerIdx + 1) % this.headlineData.length;
					this.elm.setStyles({
						'top': 0,
						'left': 575,
						'width': 575,
						'height': 18,
						'display': 'block'
					}).empty().setHTML(fnFormatData(this.headlineData[idx]));
					(function () { this.fx.start(0) }).delay(100, this);
				}).bind(this));
			};
			if (!this.initialized) {
				this.initialized = true;
				this.elm.setStyle('display', 'block')
				.setHTML(fnFormatData(this.headlineData[this.tickerIdx]));
				this.fx.start(0);
				this.timer = fnFx.periodical(7000, this);
			} else {
				this.timer = (function () {
					this.timer = $clear(this.timer);
					fnFx.call(this);
					this.timer = fnFx.periodical(7000, this);
				}).delay(3500, this);
			}
		},
		stop: function () {
			this.timer = $clear(this.timer);
		}
	});

	window.addEvent('domready', function() {
		if (!headline)	headline = new HeadlineCtrl();
	}).addEvent('load', function() {
		if (!headline)	headline = new HeadlineCtrl();
		headline.loadData();
		headline.start();
	}).addEvent('unload', function () {if (headline) headline.stop();});

}

// for cooperation with Flash.
function start_headline() {
	if (headline)	headline.start();
}
