MixiMusic = Class.create();
MixiMusic.prototype = {
	initialize:		function() {
						this.update();
					},
	
	update:			function() {
						this._initDisplay();
						this._startRequest();
					},
	
	_initDisplay:	function() {
						var obj = $('mixi_music');
						if(obj) {
							// 掃除
							while(obj.hasChildNodes()) {
								obj.removeChild(obj.lastChild);
							}
							
							// 事実上のルート
							div_elem1 = obj.appendChild(document.createElement('div'));
							div_elem1.setAttribute('id', 'mixi_music_');
							
							// 見出し
							h_elem = div_elem1.appendChild(document.createElement('h2'));
							h_elem.appendChild(document.createTextNode('mixi music'));
							
							// 見出し以外
							div_elem2 = div_elem1.appendChild(document.createElement('div'));
							div_elem2.setAttribute('id', 'mixi_music_body');
							
							// 読み込み中
							p_elem = div_elem2.appendChild(document.createElement('p'));
							p_elem.setAttribute('class', 'echo_loading');
							img_elem = p_elem.appendChild(document.createElement('img'));
							img_elem.setAttribute('src', '/web20/loading.gif');
							img_elem.setAttribute('width', '16');
							img_elem.setAttribute('height', '16');
							img_elem.setAttribute('alt', 'loading...');
							img_elem.setAttribute('title', '');
						}
					},
	
	_startRequest:	function() {
						var now = new Date();
						var uri = '/document/webmaster/mixi/music/music.xml';
						uri += '?';
						uri += now.getTime();
						var ajax = new Ajax.Request(uri,
													{
														method:		'get',
														onComplete:	this._onComplete,
														onFailure:	this._onFailure
													}
												   );
					},
	
	_onFailure:		function(req) {
						// 失敗したら echo の中身をすべて消す
						var obj = $('mixi_music');
						if(obj) {
							while(obj.hasChildNodes()) {
								obj.removeChild(obj.lastChild);
							}
						}
					},
	
	_onComplete:	function(httpobj) {
						var domdoc = httpobj.responseXML;
						if(!domdoc) {
							this._onFailure();
							return;
						}
						var songs = domdoc.getElementsByTagName('song');
						var count = 0;
						var ul = document.createElement('ul');
						ul.setAttribute('class', 'music_data');
						var now = new Date();
						for(var i = 0;
							i < songs.length && count < 10;
							++i)
						{
							var song = songs.item(i);
							var data_date   = undefined;
							var data_artist = undefined;
							var data_title  = undefined;
							for(node = song.firstChild; node; node = node.nextSibling) {
								if(node.nodeType == 1 &&
								   node.firstChild &&
								   node.firstChild.nodeType == 3)
								{
									var textContent = node.firstChild.nodeValue;
									if(node.nodeName == 'date') {
										data_date = textContent;
									} else if(node.nodeName == 'artist') {
										data_artist = textContent;
									} else if(node.nodeName == 'title') {
										data_title = textContent;
									}
								}
							}
							
							if(data_date && data_artist && data_title) {
								++count;
								
								var li = ul.appendChild(document.createElement('li'));
								var elem = li.appendChild(document.createElement('p'));
								elem.setAttribute('class', 'music_date');
								elem.setAttribute('className', 'music_date');
								elem.appendChild(document.createTextNode(data_date));
								elem = li.appendChild(document.createElement('p'));
								elem.setAttribute('class', 'music_title');
								elem.setAttribute('className', 'music_title');
								elem.appendChild(document.createTextNode(data_title));

								if(data_artist.length > 12) {
									data_artist = data_artist.substr(0, 11) + '…';
								}
								elem = li.appendChild(document.createElement('p'));
								elem.setAttribute('class', 'music_artist');
								elem.setAttribute('className', 'music_artist');
								elem.appendChild(document.createTextNode(data_artist));
							}
						}
						var body = $('mixi_music_body');
						while(body.hasChildNodes()) {
							body.removeChild(body.lastChild);
						}
						if(count > 0) {
							body.appendChild(ul);
						} else {
							var p = body.appendChild(document.createElement('p'));
							p.appendChild(document.createTextNode('最近再生した音楽が見つかりません'));
						}
					}
};

var myMixiMusic = undefined;

function initMixiMusic() {
	if(!myMixiMusic) {
		myMixiMusic = new MixiMusic();
	}
}

Event.observe(window, 'load', initMixiMusic, false);
