var nap_album = {
	execute: function() {
		window.onload = function() { nap_album.view() };
//		window.onload = function() { nap_album.msg('<span style="color:#F00;">メンテナンス中のため、現在ご利用いただけません</span>'); };
	},
	jacket: function( object_id, album_id ) {
		var url = '/music/albumHTML/'+album_id;
		new Ajax.Request( '/music/albumHTML/'+album_id, { method: 'get', onComplete: function( r ) {
			var html = r.responseText;
			if ( ! html.match( /<img src="(http:\/\/images\.[^"]+)"/i ) ) return;
			$(object_id).innerHTML = '<img src="'+RegExp.$1+'" border="0" />';
			$(object_id).style.display = 'block';
		} } );
	},
	sanitize: function( str ) {
		return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
	},
	notfound: function() {
		var box = '';
		box += '<div class="sectionDataB"><div class="innerBlockC"><div class="innerBlockD"><div class="noresults">';
		box += '<p class="message">お客様の検索された音楽が見つかりませんでした。</p>';
		box += '<p>Napsterでは、新譜・旧譜／ジャンル／アーティストの国籍を問わず、毎日たくさんの楽曲を追加しています。<br />最新の追加楽曲は、新着アルバム情報でご確認いただけます。</p>';
		box += '<ul class="link"><li><a href="/music/update">新着アルバム情報</a></li></ul><!--//link -->';
		box += '</div></div></div></div>';
		$('results').innerHTML = box;
	},
	msg: function( str ) {
		var box = '';
		box += '<div class="sectionDataB"><div class="innerBlockC"><div class="innerBlockD"><div class="noresults">';
		box += '<p class="message">'+str+'</p>';
		box += '</div></div></div></div>';
		$('results').innerHTML = box;
	},
	init: function() {
		var temp = location.href.split('/');
		if ( ! temp[temp.length-1].match(/^(\d+)/) ) {
			this.notfound();
			return false;
		}
		this.id = Number( RegExp.$1 );
		if ( isNaN( this.id ) ) {
			this.notfound();
			return false;
		}
		return true;
	},
	view: function() {
		if ( ! this.init() ) return;
		this.msg('検索中...');
		new Ajax.Request( '/music/lookupXML/album/'+this.id, { method: 'get', onComplete: function( r ) {
			var xml = r.responseXML.getElementsByTagName('result');
			var temp   = new Array;
			var exists = new Array;
			for ( var i = 0; i < xml.length; i ++ ) {
				if ( exists[xml[i].getAttribute('track_id')] ) continue;
				exists[xml[i].getAttribute('track_id')] = 1;
				temp.push( xml[i] );
			}
			if ( temp.length <= 0 ) return;
			nap_album.result = temp;
			nap_album.update();
		} } );
	},
	update: function() {
		this.result.sort( function( a, b ) {
			var at = Number( a.getAttribute('trackNumber') );
			var bt = Number( b.getAttribute('trackNumber') );
			if ( at == bt ) return 0;
			return at > bt ? 1 : -1;
		} );
		var artists_list = new Object;
		var artists = 0;
		var albums = new Array;
		for ( var i = 0; i < this.result.length; i ++ ) {
			var disc = Number( this.result[i].getAttribute('disc_number') ) - 1;
			if ( ! artists_list[this.result[i].getAttribute('artist_id')] ) {
				artists_list[this.result[i].getAttribute('artist_id')] = true;
				artists ++;
			}
			if ( ! albums[disc] ) albums[disc] = new Array;
			albums[disc].push( this.result[i] );
		}

		if ( artists == 1 ) {
			$('path_title1').innerHTML = 'アーティスト: '+this.sanitize(this.result[0].getAttribute('artist_name'));
			$('path_title1').href      = '/music/artist/'+this.result[0].getAttribute('artist_id');
			document.title = 'アルバム: '+this.sanitize(this.result[0].getAttribute('album'))+' / '+this.result[0].getAttribute('artist_name')+' | '+document.title;
		} else {
			$('path_title1').innerHTML = 'アーティスト: Various Artist';
			document.title = 'アルバム: '+this.sanitize(this.result[0].getAttribute('album'))+'/ Various Artist | '+document.title;
		}
		$('path_title2').innerHTML = 'アルバム: '+this.sanitize(this.result[0].getAttribute('album'));

		var box = '';
		box += '<div class="sectionB"><div class="sectionDataB"><div class="innerBlockC"><div class="titleBlock"><div class="titleBlockInner"><div class="information">';
		box += '<div id="jacket" style="margin:0 13px 0 0; border: #888 1px solid; float:left; display:none;"></div>';
		box += '<h1>'+this.sanitize(this.result[0].getAttribute('album'))+'</h1>';

		// 申し込みバナー
		var banner_href = appeal_href ? appeal_href : '/start/';
		var banner_src = appeal_src_searchalbum ? appeal_src_searchalbum : '/shared/images/bnr_download_135_191.jpg';
		var banner_alt = appeal_alt ? appeal_alt : '申し込み | 聴き放題サービス入会特典初めの7日間無料!!'; 
		box += '<p class="appealingBlockA"><a href="' + banner_href + '" target="_blank">';
		box += '<img src="' + banner_src + '" alt="' + banner_alt + '" width="136" height="191" class="btn searchtop" /></a></p>';

		if ( artists == 1 ) {
			box += '<p class="artist"><a href="/music/artist/'+this.result[0].getAttribute('artist_id')+'">'+this.sanitize(this.result[0].getAttribute('artist_name'))+'</a></p>';
		} else {
			box += '<p class="artist">Various Artist</p>';
		}
		if ( this.result[0].getAttribute('album_release_year') != '1970' ) {
			box += '<p class="release">リリース: '+this.sanitize(this.result[0].getAttribute('album_release_year').replace(/-/g,'/'))+'</p>';
		}
		box += '</div><!--//information -->';
		box += '<p class="example"><span class="napsterlink-usage"><a target="_blank" href="http://sms.jp.napster.com/duet/general/handle_napster_link.html?opcode=switch&tab=browseartists&artistid=' + this.sanitize( this.result[0].getAttribute('artist_id') ) + '&albumid=' + this.sanitize( this.result[0].getAttribute('album_id') ) + '&op=play_album&ids=' + this.sanitize( this.result[0].getAttribute('album_id') ) + ( this.result[0].getAttribute('streamable') == 1 ? '&skip_tsc=0' : '' ) + '&class=album&artist_id=' + this.sanitize( this.result[0].getAttribute('artist_id') ) + '&album_id=' + this.sanitize( this.result[0].getAttribute('album_id') ) + '&p=Deeplink&plc=JPTool&c=NapsterLink&t=TextLink"><img src="/music/images/btn_napster02.gif" alt="[Napsterで聴く]" height="39" width="39" />Napsterで聴く</a><span id="napsterlink-usage" style="visibility: hidden;"><img src="/music/images/baloon02.png" alt="ナップスターアプリをお持ちの方は、ボタンをクリックするとアルバム／トラックを聴くことができます。" height="48" width="272" /></span></span>';
		box += '<a target="_blank" href="http://tower.jp/item/jan/?a='+escape(this.result[0].getAttribute('artist_name'))+'&amp;t='+escape(this.result[0].getAttribute('album'))+'&amp;kid=pnap"><img src="/music/images/btn_tower01.gif" alt="[タワレコで商品を探す]" height="39" width="39" />タワレコで商品を探す</a>';
		box += '</p>';

		// ソーシャルブックマーク
		box += '<p class="example" style="padding-top:20px; text-align:center; font-weight:bold;">ブックマークする</p>';
		box += '<p class="example" style="padding-top:10px; text-align:center;">';
		box += '<a href="http://b.hatena.ne.jp/add?mode=confirm&amp;title='+encodeURIComponent( document.title ) + '&amp;url='+location.href+'" target="_blank"><img src="/music/images/bookmark_hatena.gif" alt="このページをはてなブックマークに登録する" /></a>';
		box += '<a href="http://bookmarks.yahoo.co.jp/action/bookmark?t='+encodeURIComponent( document.title ) + '&amp;u='+location.href+'" target="_blank"><img src="/music/images/bookmark_yahoo.gif" alt="このページをYahoo!ブックマークに登録する" /></a>';
		box += '<a href="http://del.icio.us/post?v=4&amp;title='+encodeURIComponent( document.title ) + '&amp;url='+location.href+'" target="_blank"><img src="/music/images/bookmark_delicious.gif" alt="このページをdel.icio.usに登録する" /></a>';
		box += '<a href="http://newsing.jp/nbutton?title='+encodeURIComponent( document.title ) + '&amp;url='+location.href+'" target="_blank"><img src="/music/images/bookmark_newsing.gif" alt="このページをnewsingに登録する" /></a>';
		box += '<a href="http://clip.livedoor.com/clip/add?title='+encodeURIComponent( document.title )+'&amp;link='+location.href+'" target="_blank"><img src="/music/images/bookmark_livedoor.gif" alt="このページをlivedoorクリップに登録する" /></a>';
		box += '</p>';

		box += '</div></div></div></div></div>';
		box += '<div class="sectionA results"><h2 class="headingA">トラック</h2>';
		box += '<div class="sectionDataB"><div class="innerBlockC"><div class="innerBlockE"><div class="generalBlockC lastChild">';

		for ( var disc = 0; disc < albums.length; disc ++ ) {
			if ( albums.length > 1 ) box += '<h3 class="discNumber'+(disc==0?' firstDisc':'')+'">Disc '+(disc+1)+'</h3>';
			box += '<table class="rankingTable"><thead><tr>';
			box += '<th scope="col" class="category"><img src="/music/images/text_chart01.gif" width="66" height="13" alt="配信カテゴリ" /></th>';
			box += '<th scope="col" class="number"><img src="/music/images/text_chart11.gif" width="19" height="13" alt="No." /></th>';
			box += '<th scope="col" class="artist"><img src="/music/images/text_chart03.gif" width="62" height="13" alt="アーティスト" /></th>';
			box += '<th scope="col" class="track"><img src="/music/images/text_chart07.gif" width="42" height="13" alt="トラック" /></th>';
			box += '<th scope="col" class="duration"><img src="/music/images/text_chart10.gif" width="24" height="13" alt="時間" /></th>';
			box += '<th scope="col" class="napster"></th>';
			box += '</tr></thead><tbody>';
			for ( var i = 0; i < albums[disc].length; i ++ ) {
				box += '<tr class="'+(i%2?'even':'odd')+(i==albums[disc].length-1?' last':'')+'"><td class="category">';
				box += albums[disc][i].getAttribute('streamable')   == 1 ? '<img src="/music/images/icon_infinity.gif" width="82" height="25" class="streamable" alt="聴き放題" />'  : '<img src="/music/images/icon_infinity_d.gif" width="82" height="25" class="streamable" alt="" />';
				box += albums[disc][i].getAttribute('can_purchase') == 1 ? '<img src="/music/images/icon_payment.gif" width="82" height="19" class="purchase" alt="有料楽曲購入" />' : '<img src="/music/images/icon_payment_d.gif" width="82" height="19" class="purchase" alt="" />';
				box += '</td>';
				box += '<td class="number">'+albums[disc][i].getAttribute('trackNumber')+'</td>';
				box += '<td class="artist"><a href="/music/artist/'+albums[disc][i].getAttribute('artist_id')+'">'+this.sanitize(albums[disc][i].getAttribute('artist_name'))+'</a></td>';
				box += '<td class="track">'+this.sanitize(albums[disc][i].getAttribute('track'))+'</td>';
				box += '<td class="duration">'+albums[disc][i].getAttribute('duration')+'</td>';
				box += '<td class="napster"><a target="_blank" href="http://sms.jp.napster.com/duet/general/handle_napster_link.html?opcode=switch&tab=browseartists&artistid='+albums[disc][i].getAttribute('artist_id')+'&albumid='+albums[disc][i].getAttribute('album_id')+'&select_id='+albums[disc][i].getAttribute('track_id')+'&op=play_tracks&ids='+albums[disc][i].getAttribute('track_id')+'&class=track&artist_id='+albums[disc][i].getAttribute('artist_id')+'&track_id='+albums[disc][i].getAttribute('track_id')+'&p=Deeplink&plc=JPTool&c=NapsterLink&t=TextLink"><img src="/music/images/btn_napster02.gif" width="39" height="39" alt="[Napsterで聴く]" /></a></td>';
				box += '</tr>';
			}
			box += '</tbody></table>';
		}

		box += '</div></div></div></div></div>';

		s.pageName = 'アルバム: ' + this.result[0].getAttribute('album');
		var s_code = s.t();
		if ( s_code ) box += s_code;

		$('results').innerHTML = box;
		this.jacket( 'jacket', this.id );

		RN_preloadImages();
	}
};

nap_album.execute();

