﻿function getIconName( str ){
	switch( str )
	{
		case '鎧':
			return "ico_armor.gif";	
		case '盾':
			return "ico_shield.gif";	
		case '剣':
			return "ico_saber.gif";	
		case '銃':
			return "ico_gun.gif";	
		case '杖':
			return "ico_cane.gif";	
		case 'ユニット':
			return "ico_unit.gif";	
		case 'アイテム':
			return "ico_item.gif";	
		default:
			return "";
	}
}
function changeTime( str ) {
	return( Date.parse( str.replace( /年/, '/' ).replace( /月/, '/' ).replace( /日/, '' ).replace( /時/, ':' ).replace( /分/, ':' ).replace( /秒/, '' ) ) );
}
function getNum( num ){
	if( num < 10 ) return '0' + num;
	return num;
}

var slist = new Array();
var itemlist = new Array();
function CheckDate(index,date) {
	this.index = index;
	this.date = date;
}
function cmp_date(a, b) {
	return b.date - a.date;
}


$(document).ready( function (){
	$.ajax({
		url: 'akabako100.xml',
		type: 'GET',
		dataType: 'xml',
		timeout: 5000,
		//ロード失敗
		error: function(){
			$('h4#tableOutput').after( '<div id="errorBox">アイテムリストの読み込みに失敗しました。</div>' );
			$('div#tableLoadBox').hide();
		},
		//ロード完了
		success: function(xml){
			var count = 0;
			var tree = '<table id="TannounceTable"><tr><th class="Ico">種類</th><th class="item">アイテム名</th>'
					+ '<th class="Star">レアリティ</th><th class="date">ドロップ日時</th><th class="party">パーティー名</th></tr>';
			var newclass = '';
			
			$(xml).find('rare-item').each( function(){
				if( $(this).attr('dropped') == "true" ){
					slist.push( new CheckDate( count,changeTime( $(this).attr('date') ) ) );
					itemlist.push( [ $(this).attr('kind'), $(this).attr('name'), $(this).attr('rarity'), $(this).attr('date'), $(this).attr('partyname'), $(this).attr('new') ] );
					count++;
				}
			} );
			
			slist.sort( cmp_date );
			
			for( var i = 0; i < slist.length && i < 14; i ++ ){
				if( itemlist[slist[i].index][5] == "true" ) {
					newclass = ' newclass="new"';
				}
				else {
					newclass = '';
				}
				tree += '<tr' + newclass + '><td class="Ico"><img src="dropanouncement/drop_list/' + getIconName( itemlist[slist[i].index][0] ) + '" alt="" width="25" height="25" /></td>'
					+ '<td>' + itemlist[slist[i].index][1] + '</td>'
					+ '<td class="Star">' + itemlist[slist[i].index][2] + '</td>'
					+ '<td class="date">' + itemlist[slist[i].index][3].replace(' ','<br />') + '</td>'
					+ '<td>' + itemlist[slist[i].index][4].replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;') + '</td></tr>';
			}
			tree += '</table>';
			
			
			//$(xml).find('summary').attr('total-count')
			var itemtree = '<ul id="rewardsList" class="clearfix">';
			var openNum = Math.floor( $(xml).find('summary').attr('total-count') / 10 );
			for( var i = 0; i < 12; i++ ){
				if( i == 9 ){
					if( openNum > i ) {
						itemtree += '<li id="targetPts"><a href="javascript:tb_show(\'ドロップアイテムクリア数目標：レアアイテムドロップアナウンスコンプリート！\',\'secret/present_' + getNum(i+1) + 'l.jpg\');"><img src="secret/present_' + getNum(i+1) + '.jpg" alt="レアアイテムドロップアナウンスコンプリート！" width="311" height="102"></a></li>';
					}
					else {
						itemtree += '<li id="targetPts"><img src="image/secret/present_' + getNum(i+1) + 'close.jpg" alt="目標：' + (i+1) + '0アイテムドロップ" width="311" height="102" /></li>';
					}
				}
				else if( i >= 10 ) {
					if( ( openNum - 1 ) > i ) {
						itemtree += '<li class="addPts"><a href="javascript:tb_show(\'ドロップアイテムクリア数目標：' + (i+2) + '0アイテム達成報酬\',\'secret/present_' + getNum(i+1) + 'l.jpg\');"><img src="secret/present_' + getNum(i+1) + '.jpg" alt="ドロップアイテムクリア数目標：' + (i+2) + '0アイテム達成報酬" width="311" height="102"></a></li>';
					}
					else {
						itemtree += '<li class="addPts"><img src="image/secret/present_' + getNum(i+1) + 'close.jpg" alt="目標：' + (i+2) + '0アイテムドロップ" width="311" height="102" /></li>';
					}
				}
				else {
					if( openNum > i ) {
						itemtree += '<li><a href="javascript:tb_show(\'ドロップアイテムクリア数目標：' + (i+1) + '0アイテム達成報酬\',\'secret/present_' + getNum(i+1) + 'l.jpg\');"><img src="secret/present_' + getNum(i+1) + '.jpg" alt="ドロップアイテムクリア数目標：' + (i+1) + '0アイテム達成報酬" width="103" height="102"></a></li>';
					}
					else {
						itemtree += '<li><img src="image/secret/present_' + getNum(i+1) + 'close.jpg" alt="目標：' + (i+1) + '0アイテムドロップ" width="103" height="102" /></li>';
					}
				}
			}
			itemtree += '</ul>';
			
			//反映
			$('dd#dropNum').html( $(xml).find('summary').attr('total-count') );
			$('div#lastupdate').html( /*$(xml).find('last-updated').attr('year') + '年' + */$(xml).find('last-updated').attr('month') + '月' + $(xml).find('last-updated').attr('mday') + '日 '
						+ $(xml).find('last-updated').attr('hour') + '時' + $(xml).find('last-updated').attr('min') + '分' + $(xml).find('last-updated').attr('sec') + '秒更新' );
			//パネル
			$('h4#panelOutput').after( itemtree );
			$('div#panelLoadBox').hide();
			//リスト
			$('h4#tableOutput').after( tree );
			$('div#tableLoadBox').hide();
		}
	});
} );
