/**
 *	@filename		home.js
 *	@charset		utf-8
 *	@modified		
 *	@description	
 */
$(function(){
	
	//
	//	メインビジュアル
	//
	var SPD = 1500;
	var INTERVAL_MS = 6000;
	var flash = (function(){
		var imgs = [];
		$("#flash li").each(function(){
			imgs.push($(this).css({ opacity : 0 }));
		});
		imgs[0].animate({ opacity : 1 }, SPD);
		var LENGTH = imgs.length;
		var cnt = 0;
		return {
			get : function(){
				return imgs[cnt];
			},
			next : function(){
				cnt++;
				if(cnt > LENGTH - 1){
					cnt = 0;
				}
				return imgs[cnt];
			}
		};
	})();
	
	setInterval(function(){
		flash.get().animate({ opacity : 0 }, SPD);
		flash.next().animate({ opacity : 1 }, SPD);
	}, INTERVAL_MS);
	
	/*
	$("table tr").each(function(){
		$("td:first-child").addClass("firstChild");
		$("td:last-child").addClass("lastChild");
	});
	$("font").removeAttr("size");
	*/
	
	
	//
	//	トピックス読み込み
	//
	var loadXml = function(url, id){
		$.ajax({
			dataType : "xml",
			url : url,
			success : function(xml){
				var html = [],
					items = xml.getElementsByTagName("item"),
					regexExtractOneDigitNumber = /^(\d)$/,
					item,
					date,
					href,
					title,
					tgt;
				
				var i = items.length;
				while(i--){
					item = items[i];
					date = new Date(item.getElementsByTagName("pubDate")[0].firstChild.nodeValue);
					date = 
						date.getFullYear() + "." + 
						(date.getMonth() + 1 + "").replace(regexExtractOneDigitNumber, "0$1") + "." + 
						(date.getDate() + "").replace(regexExtractOneDigitNumber, "0$1");
					
					href =  item.getElementsByTagName("guid")[0].firstChild.nodeValue;
					title = item.getElementsByTagName("title")[0].firstChild.nodeValue;
					
					html[i] = [
						'<tr>',
							'<td class="date">',
								date,
							'</td>',
							'<td class="title">',
								'<a href="', href, '">',
									title,
								'</a>',
							'</td>',
						'</tr>'
					].join("");
				}
				items = null;
				
				tgt = document.getElementById(id);
				tgt.innerHTML = '<table cellspacing="0" cellpadding="0">' + html.join("") + '</table>';
				tgt = null;
			}
		});
	};
	
	//トピックス読み込み
	loadXml("http://www.mokkedano.net/rss/topics.xml", "topicsContent");
	
	//ニュース読み込み
	loadXml("http://www.mokkedano.net/rss/oshirase.xml", "newsContent");

});
