function pageOnReady() {
}

function pageOnLoad() {
	scrollYMD();
	rss();
}

function scrollYMD() {
	// setting
	var yTime = 2000;
	var yWidth = 86;
	var mTop = [0, 28, 58, 85, 115, 144, 172, 201, 229, 258, 288, 317];
	var mHeight = 27;

	$("#year").css({backgroundPosition: "1535px 0px", width: "0px", opacity: 0});
	var d = new Date();
	var y = d.getFullYear();
	var left = 1535 - ((y - 2004) * yWidth);
	$("#year").animate(
		{backgroundPosition: left + "px 0px", width: yWidth + "px", opacity: 1},
		2000,
		"easeOut"
	);
	$("#month").css({backgroundPosition: "51px -1002px", width: "51px", height: "0px", opacity: 0});
	$("#month").animate(
		{backgroundPosition: "51px " + (347 - mTop[d.getMonth()]) + "px", height: "27px", opacity: 1},
		1000,
		"easeOut"
	);
	var dd = d.getDate();
	$("#date").css({backgroundPosition: "71px " + (2680 - (19 + (dd - 2) * 86)) + "px", width: "71px", height: "0px", opacity: 0});
	$("#date").animate(
		{backgroundPosition: "71px " + (2680 - (17 + ((dd - 1) * 86.3))) + "px", height: "86px", width: "71px", opacity: 1},
		1000,
		"easeOut"
	);
	$("#day").css({backgroundPosition: "54px 808px", width: "54px", height: "0px", opacity: 0});
	setTimeout(function() {
		var dy = d.getDay();
		$("#day").animate(
			{backgroundPosition: "54px " + (196 - ((dy) * 28)) + "px", height: "22px", width: "54px", opacity: 1},
			500,
			"easeOut"
		);
	}, 1200);
}
var on = false;
// Feeds↓
function toggle() {
	var f = document.getElementById("selector_checkboxes");
	var c = f.childNodes;
	for (var i = 0; i < c.length; i++) {
		if(c[i].type == "checkbox") {
			c[i].checked = on;
		}
	}
	on = !on;
}
function loadRSS(rss, filter, l, ex) {
	var block = "div";
	if (ua.ie && ua.ie <= 7) {
		block = "span";
	}
	var feed = new google.feeds.Feed(rss);
	feed.setNumEntries(document.forms["selector"].num.value);
	feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
	feed.load(function(result) {
		if (!result.error) {
			var container = document.getElementById("feed");
			var count = 0;
			for (var i = 0; i < result.feed.entries.length; i++) {
				var entry = result.feed.entries[i];
				
				if (-1 < entry.link.indexOf(filter)
					&& entry.title.indexOf("PR:") < 0
					&& entry.title.indexOf("AD:") < 0
					&& entry.title.indexOf("PR：") < 0
					&& entry.title.indexOf("AD：") < 0
				) {
					count++;
					var li = document.createElement("li");

					// div date
					var sd = document.createElement(block);
					sd.setAttribute("class", "date");
					if (entry.publishedDate != "") {
						var d = new Date(entry.publishedDate);
						sd.appendChild(document.createTextNode(DateFormatter.format(d, "Y.m.d")));
					}
					else {
						var alert_span = document.createElement("span");
						alert_span.setAttribute("style", "color: #ccc; font-size: 70%;");
						alert_span.appendChild(document.createTextNode("日付解析不能"));
						sd.appendChild(alert_span);
					}

					// div label
					var sl = document.createElement(block);
					sl.setAttribute("class", "label");
					if (l == "JVN") {
						sl.setAttribute("style", "color: red;");
					}
					sl.appendChild(document.createTextNode("≪" + l + "≫"));

					// link
					var a = document.createElement("a");
					a.setAttribute("href", entry.link);
					a.setAttribute("class", "feed_link");
					if (l == "JVN") {
						a.setAttribute("style", "color: #cc0000;");
					}
					if (ex) {
						var img = document.createElement("img");
						img.setAttribute("src", "css/external.png");
						img.setAttribute("alt", "外部リンク");
						img.setAttribute("border", "0");
						img.setAttribute("style", "margin-right: 4px;");
						a.appendChild(img);
						a.setAttribute("target", l);
					}
					var title = entry.title;
					if (50 < title.length) {
						title = title.substring(0, 45) + "...";
					}
					a.appendChild(document.createTextNode(title));


					li.appendChild(sd);
					li.appendChild(sl);
					li.appendChild(a);
					container.appendChild(li);
				}
			}
			setTimeout(function () {
				var stat = document.getElementById("stat");
				var sm = document.createElement("small");
				sm.appendChild(document.createTextNode(l + ":" + count + "件取得 "));
				stat.appendChild(sm);
			}, 70 * (60 - result.feed.entries.length));
		}
	});
}

function rss() {
	var container = document.getElementById("feed");
	while (container.firstChild) {
		container.removeChild(container.firstChild);
	}
	var stat = document.getElementById("stat");
	while (stat.firstChild) {
		stat.removeChild(stat.firstChild);
	}
	var disp = document.getElementById("feed_display");
	while (disp.firstChild) {
		disp.removeChild(disp.firstChild);
	}
	if(document.forms["selector"]["CodeZine"].checked)
		loadRSS("http://codezine.jp/rss/new/20/index.xml", "codezine.jp", "CodeZine", true);
//	loadRSS("http://thinkit.jp/rss/rss20.xml", "thinkit.jp", "ThinkIT", true);
	if(document.forms["selector"]["devWorks"].checked)
		loadRSS("http://www.ibm.com/developerworks/jp/views/rss/customrssatom.jsp?zone_by=Architecture&zone_by=DB2&zone_by=Java+technology&zone_by=Linux&zone_by=Web+architecture&zone_by=XML&type_by=Articles&search_by=&day=1&month=01&year=2004&max_entries=20&feed_by=rss&encoding=UTF-8&isGUI=true&Submit.x=43&Submit.y=11", "ibm.com", "devWorks", true);
	if(document.forms["selector"]["IT"].checked)
		loadRSS("http://rss.rssad.jp/rss/itmatmarkit/rss.xml", "http", "@IT", true);
	if(document.forms["selector"]["JVN"].checked)
		loadRSS("http://jvn.jp/rss/jvn.rdf", "jvn", "JVN", true);
	if(document.forms["selector"]["gihyo"].checked)
		loadRSS("http://rss.rssad.jp/rss/gihyo/feed/rss2", "http", "gihyo", true);
	if(document.forms["selector"]["sforge"].checked)
		loadRSS("http://rss.rssad.jp/rss/sourceforge/magazine/rss", "sourceforge", "sforge", true);
	if(document.forms["selector"]["fowler"].checked)
		loadRSS("http://capsctrl.que.jp/kdmsnr/wiki/bliki/?c=rss", "bliki", "fowler", true);
	if(document.forms["selector"]["tor"].checked)
		loadRSS("http://feeds2.feedburner.com/jp/webblog", "webcreativepark", "to-R", true);
	if(document.forms["selector"]["ws"].checked)
		loadRSS("http://standards.mitsue.co.jp/index.rdf", "mitsue", "Web標準", true);
	nowLoading(0);
	setTimeout(function(){nowLoading(1);}, 1000 * 1);
	setTimeout(function(){nowLoading(2);}, 1000 * 2);
	setTimeout(function(){nowLoading(3);}, 1000 * 3);
	setTimeout(function(){sortFeeds();},   1000 * 4);
}

function sortFeeds() {
	var feeds = document.getElementById("feed");
	var fa = feeds.childNodes;
	var a = new Array();
	for (var i = 0; i < fa.length; i++) {
		if (fa[i].nodeType != 1) continue;
		var d = fa[i].firstChild.innerHTML.replace(/\./g, '');
		a[a.length] = {"date":d, "li":fa[i]};
	}
	a.sort(function(a, b){return b.date - a.date});
	var container = document.getElementById("feed_display");
	if (container.hasChildNodes()) {
		container.removeChild(container.firstChild);
	}
	var temp = a[0].date;
	var top = false;
	var bStyle = "border-top: dashed 1px #333333; padding-top: 0.8em;";
	for (var i = 0; i < 60 && i < a.length; i++) {
		if (temp != a[i].date) {
			a[i].li.setAttribute("style", bStyle);
			top = true;
		}
		container.appendChild(a[i].li);
		temp = a[i].date;
		if (i + 1 < a.length && i + 1 < 60 && temp != a[i + 1].date) {
			a[i].li.setAttribute("style", "padding-bottom: 0.8em;" + (top ? bStyle : ""));
		}
		top = false;
	}
}

function nowLoading(time) {
	var container = document.getElementById("feed_display");
	if (container.hasChildNodes()) {
		container.removeChild(container.firstChild);
	}
	var li = document.createElement("li");
	var t = "now loading";
	for (var i = 0; i < time; i++) {
		t += ".";
	}
	li.appendChild(document.createTextNode(t));
	container.appendChild(li);
}
// Feeds↑

