var setup = {
	xmlPath: "/update/donation.xml",
	targetYear: ".year",
	targetMonth: ".month",
	targetJpnMonthly: "#jpnMonthly",
	targetJpnTotal: "#jpnTotal",
	targetNpoMonthly: ".npoMonthly",
	targetNpoTotal: ".npoTotal",
	targetUsaYear: ".usaYear",
	targetUsaDollar: ".usaDollar",
	targetUsaYen: ".usaYen",
	targetSurfMonthly: "#surfMonthly",
	targetSurfTotal: "#surfTotal",
	npoList: ".amountList",
	usaList: ".usaAmountList"
}

$.donationSet = {
	init: function(){
		for(module in $.donationSet){
			if($.donationSet[module].init){
				$.donationSet[module].init();
			}
		}
	}
}

$.donationSet.startAjax = {
	init: function(){
		$('.secGS01, .pagetopGS').css('display', 'block');
		$(window).bind('load', this.loadXmlFn);
	},
	loadXmlFn: function(){
		$.ajax({
			url: setup.xmlPath,
			type: 'get',
			cache: false,
			dataType: 'xml',
			success: $.donationSet.startAjax.parseXmlFn
		});
	},
	parseXmlFn: function(xml, status){
		
		jpnDate = new Array();
		jpnResult = new Array();
		npoResult = new Array();
		usaResult = new Array();
		surfResult = new Array();
		
		if(status!='success') return;
		$(xml).find('Year').each(function(){
			var year = $(this).text();
			jpnDate.push(year);
		});
		$(xml).find('Month').each(function(){
			var month = $(this).text();
			jpnDate.push(month);
		});
		$(xml).find('JpnResult').each(function(){
			var monthly = $(this).attr('monthly');
			var total = $(this).attr('total');
			jpnResult.push(monthly);
			jpnResult.push(total);
		});
		$(xml).find('NpoResult').each(function(){
			var monthly = $(this).attr('monthly');
			var total = $(this).attr('total');
			var data = new Array(monthly, total);
			npoResult.push(data);
		});
		$(xml).find('UsaResult').each(function(){
			var year = $(this).attr('year');
			var dollar = $(this).attr('dollar');
			var yen = $(this).attr('yen');
			var data = new Array(year, dollar, yen);
			usaResult.push(data);
		});
		$(xml).find('SurfResult').each(function(){
			var monthly = $(this).attr('monthly');
			var total = $(this).attr('total');
			surfResult.push(monthly);
			surfResult.push(total);
		});
		$.donationSet.startAjax.attachDataFn();
	},
	attachDataFn: function(){
		$(setup.targetYear+' img').hide().parent().text(jpnDate[0]);
		$(setup.targetMonth+' img').hide().parent().text(jpnDate[1]);
		$(setup.targetJpnMonthly+' img').hide().parent().text(jpnResult[0]);
		$(setup.targetJpnTotal+' img').hide().parent().text(jpnResult[1]);
		$(setup.npoList).each(function(index){
			$(setup.targetNpoMonthly+':eq('+index+') img').hide().parent().text(npoResult[index][0]);
			$(setup.targetNpoTotal+':eq('+index+') img').hide().parent().text(npoResult[index][1]);
		});
		$(setup.usaList).each(function(index){
			$(setup.targetUsaYear+':eq('+index+') img').hide().parent().text(usaResult[index][0]);
			$(setup.targetUsaDollar+':eq('+index+') img').hide().parent().text(usaResult[index][1]);
			$(setup.targetUsaYen+':eq('+index+') img').hide().parent().text(usaResult[index][2]);
		});
		$(setup.targetSurfMonthly+' img').hide().parent().text(surfResult[0]);
		$(setup.targetSurfTotal+' img').hide().parent().text(surfResult[1]);
	}
}


$(function(){
	$.donationSet.init();
});
