﻿	$(window).ready(function(){
		$("#main_wrp").css("display","none");
		$("#bgimg").css("display","none");
		$.fn.preload = function(){
			var i = 4;
			this.each(function(){
				$("#bgimg").append('<li id="bg_image_' + i + '"><img /></li>');
				$("#bgimg").children("#bg_image_" + i).children("img").attr("src", this);
				i--;
			});
		}
		$([
			'./img/index/bg_02.jpg',
			'./img/index/bg_04.jpg',
			'./img/index/bg_05.jpg',
			'./img/index/bg_01.jpg'
		]).preload();

		var ucnt = 0;
		var dcnt = 0;
		var limit = ($(".diary_entry").size())-4;

		if(4 > $(".diary_entry").size()){
			$("#diary_rollup").children().css("display","none");
			$("#diary_rolldown").children().css("display","none");
		}

		$("#diary_rollup").click(function(){
			if(ucnt < limit){
				ucnt++;
				$("#diary_set").animate({"top": "-=206px"},300,"easeInOutExpo");
			}
		});
		$("#diary_rolldown").click(function(){
			var dSetTop = ($("#diary_set").stop().css("top").match(/(.+)px/)||[])[1];
			if(dSetTop < 0){
				$("#diary_set").animate({"top": "+=206px"},300,"easeInOutExpo");
				ucnt--;
			}
		});

		$("#info_feed").children("dt").each(function(){
			if(newerInfo($(this))){
				$(this).append('&nbsp;<img src="./img/index/info_new.gif" alt="new!" />');
			}
		});
		$("#column_feed").children("dt").each(function(){
			if(newerInfo($(this))){
				$(this).append('&nbsp;<img src="./img/index/info_new.gif" alt="new!" />');
			}
		});
		$("#student_feed").children("dt").each(function(){
			if(newerInfo($(this))){
				$(this).after('<dd class="student_new"><img src="./img/index/info_new.gif" alt="new!" /></dd>');
			}
		});

		var postfix = '_r';
		$('img.ir').not('[src*="'+ postfix +'."]').each(function() {
			var img = $(this);
			var src = img.attr('src');
			var src_on = src.substr(0, src.lastIndexOf('.'))
			           + postfix
			           + src.substring(src.lastIndexOf('.'));
			$('<img>').attr('src', src_on);
			img.hover(
				function() {
					img.attr('src', src_on);
				},
				function() {
					img.attr('src', src);
				}
			);
		});
		$('img.ia').hover(function(){
			$(this).stop().fadeTo(100,0.6);
		}, function(){  
			$(this).stop().fadeTo(300,1);
		});

		$("a[rel='blank']").click(function(){
			window.open($(this).get(0).href);
			return false;
		});

		$("a[rel='in']").click(function(){
			var URI = $(this).get(0).href;
			var targetID = (URI.match(/#(.+)/)||[])[1];
			var targetOffset = $("#" + targetID).offset().top;
			$('html,body').animate({scrollTop: targetOffset}, 600);
			return false;
		});

		if (typeof document.documentElement.style.maxHeight == "undefined"){
			$(document).pngFix();
		}
	});

	$(window).load(function(){
			$("#bgimg").fadeTo(500,1, function(){
				if(navigator.userAgent.indexOf("MSIE") != -1) {
					$("#bgimg").children().children("img").fadeTo(400,1);
				}
				$("#main_wrp").fadeTo(500,1);
			});
			var i = 0;
			var imgFade = setInterval(function(){
				i++;
				$("#bgimg").children("#bg_image_" + i).children("img").fadeTo(400,0);
				if(i >= 4){
					i = 0;
					$("#bgimg").children().children("img").fadeTo(400,1);
				}
			},6000);
			
	});

	function newerInfo(info)
	{
		var pubTime = $(info).html();
		var date = new Date();
		var month = null;
		var day = null;
		if((date.getMonth() + 1) < 9){
			month = String("0") + String(date.getMonth() + 1);
		}else{
			month = String(date.getMonth() + 1);
		}
		if(date.getDate() < 9){
			day = String("0") + String(date.getDate());
		}else{
			day = String(date.getDate());
		}
		var chkTime = String(date.getFullYear()) + String(month) + String(day);
		pubTime = pubTime.replace(/\./g, '');
		if((chkTime - pubTime) < 14){
			return true;
		}else{
			return false;
		}
	}

	function initialize()
	{
		var feed = new google.feeds.Feed("http://lilyvale-info.blogspot.com/atom.xml?0");
		feed.setNumEntries(1);	
		feed.load(function(result)
		{
			if(!result.error)
			{
				for (var i = 0; i < result.feed.entries.length; i++)
				{
					var entry = result.feed.entries[i];
					var date = new Date(entry.publishedDate);
					var title = entry.title;
					$("#blog_feed").append("<dt>" + date.getFullYear() + "." + (date.getMonth() + 1) + "." + date.getDate() + "</dt><dd><a href=\"" + entry.link + "\" target='_blank'>" + title + "</a></dd>");
				}
			}
		});
	}
