// #require mootools.js

var headline;

if (window.addEvent) {

var headlineDataSample = ([
						   
{
"text": "全国展開！一部上場の大手ブライダル企業での＜ブライダルアドバイザー＞のお仕事です!♪♪",
"link": "http://www.hc-recruiting.com/2010/07/post_110.html",
},

{
"text": "税理士補助業務＜週３日ＯＫ！人気の扶養内勤務！＞環境抜群！キレイな広々オフィスで活気ある事務所です。",
"link": "http://www.hc-recruiting.com/2010/07/post_112.html",
},

{
"text": "大人気のデータ入力・ファイリング（2名増員！）長期安定！未経験OK！アットホームで職場環境良好です♪",
"link": "http://www.hc-recruiting.com/2010/07/2ok.html",
},

{
"text": "【幼児中心の英会話教室でのスクールマネージャー！！入学の受付や学校行事の企画もお任せします！】",
"link": "http://www.hc-recruiting.com/2010/08/post_59.html",
},

{
"text": "Ｗｅｂ制作・デザイナー【業界は幅広く多数募集！】まずは説明会参加から！じっくりと今後のキャリアプランを考えてから応募大歓迎！",
"link": "http://www.hc-recruiting.com/2010/05/post_79.html",
},
						   
{
"text": "【ＩＴ企業・紹介予定派遣】上場企業でコールセンターＳＶ業務！今までの経験を活かして、新しい環境でお仕事しませんか？",
"link": "http://www.hc-recruiting.com/2010/08/post_116.html",
},


]).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': 1,
					'left': 0,
					'width': 570,
					'height': 20,
					'background-color': '#DAE3F5',
					'visibility': 'hidden'
				}
			}).injectInside(headlineArea);
			var tickArea = new Element('div', {
				'styles': {
					'position': 'absolute',
					'top': 3,
					'left': 5,
					'width': 560,
					'height': 16,
					'overflow': 'hidden'
				}
			}).injectInside(headlineArea);
			var elm = new Element('div', {
				'styles': {
					'position': 'absolute',
					'width': 552,
					'height': 16,
					'left': 575,
					'font-size': 11,
					'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 = [];
				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'/_data/headline.php,
				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(700, 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();
}
