$(document).ready(function(){
	$.ajax({
		type: "GET",
		url: "news.php",
		cache: false,
		success: xmlParser
	});


	$("#info").hover(
		function(){
			$("#info .newWindow img").stop(false, true).fadeTo("normal",0);
			$("#info-box").stop(false, true).fadeTo("normal",1, function(){ $("#info-box").css('display','block'); } );
		},
		function(){
			$("#info .newWindow img").stop(false, true).fadeTo("normal",1);
			$("#info-box").stop(false, true).fadeTo("fast",0, function(){ $("#info-box").css('display','none'); } );
		}
	);

	$("#header .nav img").each( function() {
		$(this).parent().css('background', 'url('+this.src.replace('off.','on.')+') left top no-repeat' );
	});
	$("#header .nav img").hover(
		function(){
			$(this).stop(false, true).fadeTo("fast",0);
		},
		function(){
			$(this).stop(false, true).fadeTo("fast",1);
		}
	);

	$("#faci #view dt img").each( function() {
		$(this).parent().css('background', 'url('+this.src.replace('off.','on.')+') center top no-repeat' );
	});
	$("#faci #view dt").hover(
		function(){
			$(this).parent().find('dt').each( function() {
				$(this).find('img').stop(false, true).fadeTo("normal",0);
			});
			$(this).parent().find('dd').stop(false, true).fadeTo("normal",1);
		},
		function(){
			$(this).parent().find('dt').each( function() {
				$(this).find('img').stop(false, true).fadeTo("normal",1);
			});
			$(this).parent().find('dd').stop(false, true).fadeTo("normal",0);
		}
	);

/*
	$("#dat .nav a").click(
		function(){
			$("html,body").animate({ scrollTop: $( $(this).attr('href') ).offset().top }, 1000); return false;
		}
	);

*/
	$("#doc-sch #next").mouseup(
		function schNext(){
			$("#doc-sch #next").attr('src' , $("#doc-sch #next").attr('src').replace('off.','on.') );
			$("#doc-sch #back").attr('src' , $("#doc-sch #back").attr('src').replace('on.','off.') );
			$('#t1').css('display','none');
			$('#t2').css('display','block');
		}
	);
	$("#doc-sch #back").mouseup(
		function(){
			$("#doc-sch #back").attr('src' , $("#doc-sch #back").attr('src').replace('off.','on.') );
			$("#doc-sch #next").attr('src' , $("#doc-sch #next").attr('src').replace('on.','off.') );
			$('#t2').css('display','none');
			$('#t1').css('display','block');
		}
	);

	$("#aces #map img ").mousedown(
		function(){
			$(this).stop(false, true).fadeTo("normal",0);
		}
	);
	$("#aces #map img ").mouseup(
		function(){
			$(this).stop(false, true).fadeTo("normal",1);
		}
	);


	$(".outImg").hover(
		function(){
			$(this).stop(false, true).fadeTo("normal",0);
		},
		function(){
			$(this).stop(false, true).fadeTo("normal",1);
		}
	);

	$(".hovImg").hover(
		function(){
			this.src = this.src.replace('off.','on.');
		},
		function(){
			this.src = this.src.replace('on.','off.');
		}
	);

	$(".newWindow").click(
		function(){
			window.open(this.href); return false;
		}
	);

	$(".backBtn").click(
		function(){
			history.back(); return false;
		}
	);

	$(".topBtn").click(
		function(){
			$("html,body").animate({ scrollTop: 0 }, 500); return false;
		}
	);


});

function xmlParser(xml) {

	var now = new Date();
	var startDate = now.getTime() - 365 * 86400000;// 過去1年分
	var lastDate = 0;

	$("#info-box").html('');

	$(xml).find("item").each(function () {
		var title = $(this).find("title").text();
		var url = $(this).find("link").text();

		var date;
		$(this).children().each(function() {
			if ($(this)[0].tagName == "dc:date") {
				date = ''+$(this).text().match(/^[0-9]{2,4}-[0-9]{2}-[0-9]{2}/i);
				date = new Date( date.replace(/-/g,'/') );
			}
		});

		if( lastDate < date.getTime() )  lastDate = date.getTime();
		
		if( date.getTime() > startDate ) {
			$("#info-box").append('<dt>● <a href="'+ url + '">' + title + '</a></dt><dd>' + chgDate( date ) + '</dd>');
		}
	});

	$("#info-box dt a").click(
		function(){
			window.open(this.href); return false;
		}
	);

	$("#lastupdate").html( '更新日 ' + chgDate( new Date(lastDate) ) );
}


function chgDate( d ){
	yy = d.getYear();
	mm = d.getMonth() + 1;
	dd = d.getDate();
	if (yy < 2000) { yy += 1900; }
	if (mm < 10) { mm = "0" + mm; }
	if (dd < 10) { dd = "0" + dd; }

	return yy + "/" + mm + "/" + dd;
}


