// infoagent.js
var GrampusInfoAgent = new Class ({
	initialize: function (dest) {
		this.dest = $(dest);
		if (!this.dest) {
			if (window.console)	console.error('$("' + dest + '") is Not Found.');
			return;
		}
		//
		this.target = (this.dest.hasClass('infobox') ? this.dest : this.dest.getElement('.infobox'))
		 || new Element('div', {
			'class': 'infobox'
		}).inject(this.dest, 'top');
		if (this.dest.id || this.target.id) {
			GrampusInfoAgent['target:' + (this.dest.id || this.target.id)] = this;
		}
		this.target.empty();
	},
	set: function (data, options) {
		options = options || {};
		if ($type(options) == 'number') {
			if (options > 1) {
				options = {
					omitCat: false,
					limit: options
				};
			} else {
				options = {
					omitCat: !!options
				};
			}
		} else if ($type(options) == 'boolean') {
			options = {
				omitCat: !!options
			};
		}
		if (!options.limit) {
			options.limit = 20;	// As Default.
		}
		var target = this.target;
		if (!data || !data.entry || !data.entry.length) {
			if (window.console) {
				if (data && data.entry)	console.warn('Data is Empty.');
				else console.error('Data is Not Valid.');
			}
			new Element('p', {
				'class': 'note',
				'styles': {
					'margin': '0px 0px 0px 0px',
					'padding': '20px 0px 20px 0px',
					'background': 'none',
					'text-align': 'center'
				},
				'html': '\u6700\u65b0\u306e\u60c5\u5831\u306f\u3042\u308a\u307e\u305b\u3093'	// 「最新の情報はありません」
			}).inject(target.empty());
			return;
		}
		var entries = data.entry;
		if (options.limit && options.limit > 0) {
			entries = Array.slice(entries, 0, options.limit);
		}
		//
		var table = '<table class="information">';
		$each(entries, function (entry, idx) {
			var tr = '<tr class="' + ((idx % 2) ? 'even' : 'odd') + '">';
			tr += '<th class="date">' + entry.published4view + '</th>';
			if (!options.omitCat)	tr += '<td class="category">[' + entry.category + ']</td>';
			tr += '<td><p><a href="' + entry.link + '" title="' + entry.summary + '">' + entry.title + '</a></p></td>';
			tr += '</tr>';
			table += tr;
		});
		table += '</table>';
		target/*.empty()*/.set('html', table);
	}
});
GrampusInfoAgent.JSONMAP = new Hash({
	'__all__': '/information/infolist.json'
});
GrampusInfoAgent.deploy = function (key, target, omitCat) {
	var url = GrampusInfoAgent.JSONMAP.get(key);
	var retryCount = 10;
	window.addEvent('domready', function () {
		url = url || GrampusInfoAgent.JSONMAP.get(key);	// for Webkit
		if (!url) {
			if (retryCount--)	arguments.callee.delay(13);
//			if (window.console)	console.log(10 - retryCount)
			return;
		}
		target = $(target) || $(key + '-infobox') || $('infobox') || document.getElement('.infobox');
		if (!target)	return;
		var agent = new GrampusInfoAgent(target);
		agent.key = key;
		new Request.JSON({
			url: url,
			onSuccess: function (data, text) {
				agent.set(data, omitCat);
			},
			onFailure: function (xhr) {
				agent.set({ entry: [] }, omitCat);
			}
		}).get();
	});
};
GrampusInfoAgent.deployHere = function (key, omitCat) {
	if (!$defined(key))	key = '__all__';
	document.write('<div id="' + key + '-infobox" class="infobox"></' + 'div>');
	GrampusInfoAgent.deploy(key, (key + '-infobox'), omitCat);
}
GrampusInfoAgent.update = function (target, omitCat) {
	target = $(target);
	if (!target || !target.id)	return;
	var agent = GrampusInfoAgent['target:' + target.id];
	if (!agent || !agent.key)	return;
	var url = GrampusInfoAgent.JSONMAP.get(agent.key);
	new Request.JSON({
		url: url,
		onSuccess: function (data, text) {
			agent.set(data, omitCat);
		},
		onFailure: function (xhr) {
			agent.set({ entry: [] }, omitCat);
		}
	}).get();
};

// Asset CSS
Asset.css('/common/css/infobox.css', {'media':'all'});
if (Browser.Engine.trident && !document.documentMode) {	// When IE 6or7 (<8)
	Asset.css('/common/css/infobox-ie.css', {'media':'all'});
}
// Override JSONMAP
Asset.javascript('/information/js/infojsonlist.js');
