var Prart = {};

Prart.util = function(){
	return {
		addEvent: function(elm, type, fn){
			try{
				elm.addEventListener(type, fn, false);
			}catch(e){
				elm.attachEvent('on' + type, function() {
					fn.call(elm, window.event);
				});
			}
		}
	}
}();

Prart.rollOver = function(){
	var over = "_on.", out = "_off.";
	var overImgList = document.images;
	var overIptList = document.getElementsByTagName("input");
	var main = function() {
		if(overImgList.length) {
			for(var i = 0, len = overImgList.length; i < len; i++){
				if(overImgList[i].getAttribute("src") && overImgList[i].getAttribute("src").lastIndexOf(out) != -1) {
					var preload = new Image();
					preload.src = overImgList[i].src.replace(out, over);
					Prart.util.addEvent(overImgList[i], "mouseover", function(event) {
						this.setAttribute("src", this.getAttribute("src").replace(out, over));
					});
					Prart.util.addEvent(overImgList[i], "mouseout", function(event) {
						this.setAttribute("src", this.getAttribute("src").replace(over, out));
					});
				}
			}
		}
		if(overIptList.length) {
			for(var i = 0, len = overIptList.length; i < len; i++){
				if(overIptList[i].getAttribute("src") && overIptList[i].getAttribute("src").lastIndexOf(out) != -1) {
					var preload = new Image();
					preload.src = overIptList[i].src.replace(out, over);
					Prart.util.addEvent(overIptList[i], "mouseover", function() {
						this.setAttribute("src", this.getAttribute("src").replace(out, over));
					});
					Prart.util.addEvent(overIptList[i], "mouseout", function() {
						this.setAttribute("src", this.getAttribute("src").replace(over, out));
					});
				}
			}
		}
	}();
}
Prart.util.addEvent(window, "load", Prart.rollOver);

Prart.pageScroller = function() {
	var targetX, targetY, scrollX, scrollY, timer;
	var delay = 7, ms = 10;
	var html = document.documentElement;
	var body = document.body;
	var scrollBtns = document.links;
	var run = function(){
		var X = window.pageXOffset || html.scrollLeft || body.scrollLeft || 0;
		var Y = window.pageYOffset || html.scrollTop || body.scrollTop || 0;
		scrollX += (targetX - X) / delay;
		scrollY += (targetY - Y) / delay;
		if(! ((targetX == scrollX && targetY == scrollY) || (Math.abs(targetX - X) < 1 && Math.abs(targetY - Y) < 1))) {
			window.scrollTo(scrollX, scrollY);
			timer = setTimeout(run, ms);
		} else {
			clearTimeout(timer);
		}
	}
	var main = function() {
		for(var i = 0, len = scrollBtns.length; i < len; i++){
			var btn = scrollBtns[i];
			if (btn.getAttribute("href",2).indexOf("#") != -1) {
				Prart.util.addEvent(btn, "click", function(event) {
					var targetId = this.getAttribute("href",2).substr(1);
					if (targetId == "page") {
						(event.preventDefault) ? event.preventDefault() : event.returnValue = false;
						var target = document.getElementById(targetId);
						var windowHeight = html.clientHeight || body.clientHeight;
						var windowWidth = html.clientWidth || body.clientWidth;
						var pageHeight = html.scrollHeight || body.scrollHeight;
						var pageWidth = html.scrollWidth || body.scrollWidth;
						if (document.compatMode == "BackCompat") {
							pageHeight = body.scrollHeight, pageWidth = body.scrollWidth;
						}
						try {
							var position = target.getBoundingClientRect();
							targetX = position.left + (body.scrollLeft || html.scrollLeft) - html.clientLeft;
							targetY = position.top + (body.scrollTop || html.scrollTop) - html.clientTop;
						} catch(e) {
							targetX = target.offsetLeft, targetY = target.offsetTop;
						}
						if (windowHeight + targetY > pageHeight) targetY -= (windowHeight + targetY) - pageHeight;
						if (windowWidth + targetX > pageWidth) targetX -= (windowWidth + targetX) - pageWidth;
						scrollX = window.pageXOffset || html.scrollLeft || body.scrollLeft || 0;
						scrollY = window.pageYOffset || html.scrollTop || body.scrollTop || 0;
						run();
					}
				});
			}
		}
	}();
}
Prart.util.addEvent(window, "load", Prart.pageScroller);

$(function(){
	var now = new Date();
	var hours = now.getHours(), minutes = now.getMinutes();
	if (minutes < 10) {
		minutes = "0" + minutes;
	}
	var str = "";
	var num = Number("" + hours + minutes);
	if(num < 200) {
		str = "はやく寝なくちゃ。";
	} else if(num < 400) {
		str = "頑張ってますね。徹夜ですか？";
	} else if(num < 500) {
		str = "もう少しで夜明けです。";
	} else if(num < 700) {
		str = "早起きは三文の得！";
	} else if(num < 800) {
		str = "おはようございます。";
	} else if(num < 1000) {
		str = "今日も頑張っていきましょう。";
	} else if(num < 1200) {
		str = "こんにちは。";
	} else if(num < 1300) {
		str = "お昼ご飯はもう食べた？";
	} else if(num < 1500) {
		str = "午後も頑張っていきましょう。";
	} else if(num < 1600) {
		str = "ちょっと休憩。お茶にしない？";
	} else if(num < 1800) {
		str = "今日の夕飯は何にしようかな？";
	} else if(num < 1900) {
		str = "そろそろ夕食ですか？";
	} else if(num < 2100) {
		str = "こんばんは。";
	} else if(num < 2300) {
		str = "今日も一日おつかれさま。";
	} else {
		str = "はやく寝なくちゃ。";
	}
	$("#logo").prepend("<span><b>" + str + "</b></span>");
	var timer = setTimeout(function(){
		$("#logo").find("span").hide({duration: 600, complete: function(){$(this).remove()}});
	}, 30000);
	
	var target = $("#search");
	$("#searchBtn").toggle(function(){
		var img = $(this).find("img");
		var src = img.attr("src");
		img.attr("src",src.replace(".gif","_cr.gif"));
		target.css("z-index","2");
		target.animate({top: "0px"},600,"easeOutQuad");
	},function(){
		var img = $(this).find("img");
		var src = img.attr("src");
		img.attr("src",src.replace("_cr.gif",".gif"));
		target.animate({top: "-285px"},
			{duration: 500, easing: "easeOutQuad",
				complete: function(){target.css("z-index","0")}
			}
		);
	});

	if($("#cycle").length) {
		$("#cycle .body").eq(0).css("left","0px");
		$(".cycleBtn").click(function(){
			$("#cycle .body").eq(1).animate({left: "0px"},
				{duration: 500, easing: "easeOutQuad",
					complete: function(){
						$(this).prev(".body").css("left","-549px").appendTo($("#cycle"));
						$("#cycleIcon li:last-child").prependTo($("#cycleIcon"));
					}
				}
			);
		}).hover(function(){
				$(this).addClass("hover");
			},function(){
				$(this).removeClass("hover");
			}
		);
	}

	if($("#links").length) {$("#links").tabs();}
	if($("#information").length) {$("#information").tabs();}

	if($("#menu").length) {
		$("#menu").hover(function(){
			if(!jQuery.support.opacity){
				$("#tooltip").css("display","block");
			} else {
				$("#tooltip").fadeIn("fast");
			}
		},function(){
			if(!jQuery.support.opacity){
				$("#tooltip").css("display","none");
			} else {
				$("#tooltip").fadeOut("fast");
			}
		});
	}

	if($("#menu2").length) {
		$("#menu2").hover(function(){
			if(!jQuery.support.opacity){
				$("#tooltip2").css("display","block");
			} else {
				$("#tooltip2").fadeIn("fast");
			}
		},function(){
			if(!jQuery.support.opacity){
				$("#tooltip2").css("display","none");
			} else {
				$("#tooltip2").fadeOut("fast");
			}
		});
	}

	if($(".nav").length) {
		$(".nav").hover(function(){
			if(!jQuery.support.opacity){
				$("#tooltip_new").css("display","block");
			} else {
				$("#tooltip_new").fadeIn("fast");
			}
		},function(){
			if(!jQuery.support.opacity){
				$("#tooltip_new").css("display","none");
			} else {
				$("#tooltip_new").fadeOut("fast");
			}
		});
	}

	if($("#sightseeing").length) {
		$("#sightseeing").hover(function(){
			if(!jQuery.support.opacity){
				$("#tooltip3").css("display","block");
			} else {
				$("#tooltip3").fadeIn("fast");
			}
		},function(){
			if(!jQuery.support.opacity){
				$("#tooltip3").css("display","none");
			} else {
				$("#tooltip3").fadeOut("fast");
			}
		});
	}

	if($(".gallery").length) {
		$(".gallery ul").each(function(){
			if($(this).children().length > 1){
				var len = $(this).children().length;
				var randnum = Math.floor( Math.random() * len );
				var first = $(this).children().eq(randnum).css("left","0px");
				$(this).prepend(first);
				$(this).parent().prepend("<p class='png'>Next</p>");
			} else {
				$(this).children().eq(0).css("left","0px");
			}
			if($(this).prev("p").length){
				$(this).prev().click(function(){
					var target = $(this).next();
					target.children().eq(1).animate({left: "0px"},
						{duration: 500, easing: "easeOutQuad",
							complete: function(){
								$(this).prev().css("left","-205px").appendTo(target);
							}
						}
					);
				}).hover(function(){
					$(this).addClass("hover");
				},function(){
					$(this).removeClass("hover");
				});
			}
		});
	}

	if($.cookie('TEXT_SIZE')) {
		var textSize = $.cookie('TEXT_SIZE');
		var src = $('#fontSize .' + textSize + ' img').attr("src");
		$('#container').addClass(textSize);
		$('#fontSize .' + textSize + ' img').attr("src",src.replace(".gif","_on.gif"));
	} else {
		var src = $('#fontSize .medium img').attr("src");
		$('#fontSize .medium img').attr("src",src.replace(".gif","_on.gif"));
		$('#container').addClass("medium");
	}
	$('#fontSize img').click(function() {
		var src = $(this).attr("src");
		var textSize = $(this).parent().attr('class');
		if (!$('#container').hasClass(textSize)) {
			$('#fontSize img').each(function(){
				$(this).attr("src",$(this).attr("src").replace("_on.gif",".gif"));
			});
			if(! src.match("_on.gif")){
				$(this).attr("src",src.replace(".gif","_on.gif"));
			}
			$('#container').removeClass('small medium large').addClass(textSize);
			$.cookie('TEXT_SIZE',textSize, { path: '/', expires: 10000 });
		}
		return false;
	});
});
