////////////////////////////////////////////////
//
// util.js[ver1.1 080618]
//
////////////////////////////////////////////////

var w = 0;
var h = 0;
var pageScrollTimer;

var util = {
	preloader: {
		loadedImages: [],
		load: function (url){
			var img = this.loadedImages;
			var l = img.length;
			img[l] = new Image();
			img[l].src = url;
		}
		},

     URI: function(s){
		this.originalPath = s;
		this.getAbsolutePath = function(path){
			if (!path.match(/^(mailto:)|(javascript:)/)) {
				var img = new Image();
				img.src = path;
				path = img.src;
				img.src = '#';
			}
			return path;
		};
		this.absolutePath = this.getAbsolutePath(s);
		this.isSelfLink = (this.absolutePath == location.href);
		var fields = {'schema' : 2, 'username' : 5, 'password' : 6, 'host' : 7, 'path' : 9, 'query' : 10, 'fragment' : 11};
		var r = /^((\w+):)?(\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(this.absolutePath);
		for (var field in fields) {
			this[field] = r[fields[field]]; 
		}
	}
};

$(function(){
$('.pageTop a').click(function(){pageScroll(0,7);return false;});

$('.hover').each(function(){
	this.originalSrc = $(this).attr('src');
		if ((/\d\.(jpg|gif|png)/).test(this.originalSrc)){
			this.rolloverSrc = this.originalSrc.replace(/(\.)(jpg|gif|png)/,"hover.$2");
		}else{
			this.rolloverSrc = this.originalSrc.replace(/(\.)(jpg|gif|png)/,"Hover.$2");
		}	
	util.preloader.load(this.rolloverSrc);
});

$(".hover").hover(function(){
	$(this).attr('src',this.rolloverSrc);
}, function(){
	$(this).attr('src',this.originalSrc);
});

$(".fade").hover(function(){
	$(this).fadeTo(100, 0.5);
},
function(){
	$(this).fadeTo(100, 1.0);
});

$(".navc").css('opacity',0.5);

$("#mainCont,#sideCont").autoHeight();

$("#foot ul li.info,#index #main table.mainImage td,#index #main .bg").click(function(){
popup("top");
return false;
});

$(".detail .sectionH1 .info,.detail .sectionH1 h1 img").click(function(){
popup();
return false;
});

$("#head #enter a").click(function(){
	var h = $(this).attr("href");
	$("body").fadeOut("slow",function(){location.href=h;});
	return false;
});

$("#home #main .sectionH2 .detail p").exResize(function(){
	$("#home #main .sectionH2 .detail").autoHeight({reset : 'reset'});
	$("#home #main .sectionH2 .detail").autoHeight();
});

$(document).ready(function(){
	$(window).bind("resize",rsize);
	$("#index #foot").delay(1500).fadeIn(1000);
	$("#head #enter").delay(1500).fadeIn(1000);
	$("#home #main .sectionH2 .detail").autoHeight();
	rsize();
});

/*$("#index #main div.va img").load(function(){
	var num = $(this).height();
	if(num < 600){
		num = (600 - num) / 2;
		$("#index #main div.va").css("paddingTop",num);
		$("#index #main div.va").css("paddingBottom",num);
	}
});*/

$("#index .wrapBg").width($("#wrapCont").width()-3).height($(window).height()-25);

$(window).bind("resize",function(){$("#index .wrapBg").width($("#wrapCont").width()-2).height($("#index #main div.va").height());});

$('#index #main table.mainImage td img,#index #main .bg img,.detail .sectionH1 h1 img,.detail .sectionH1 .info img').easyToolTip();
$('#index .wrapBg img,#head #enter img').easyToolTip2();
$('#foot ul li.info img').easyToolTip3();

$("#home #main .h203 .detail ul").each(function(){
	var uSize = $(this).find("li").size();
	$(this).css("marginTop",(-64*uSize)/2);
});

/*$(".detail h1").each(function(){
	var num = $(window).height();
	if(num > 740){
		$(".detail .sectionH1").css("height",num-200);
	}
	
	$(window).bind("resize",function(){
		var num = $(window).height();
		if(num > 740){
			$(".detail .sectionH1").css("height",num-200);
		}
	});
	var imw = $(this).find("img").attr("width");
	var imh = $(this).find("img").attr("height");
	imw = imw /2;
	imh = imh /2;

	$(this).css("marginTop",-imh);
	$(this).css("marginLeft",-imw);
});*/

$(".detail #main .pictNav ul").each(function(){
	var uSize = $(this).find("li").size();
	var para = location.search;
	$(this).width(61*uSize);
	para = para.slice(1,para.length);
	para = "li"+para;
	$(".detail #main .pictNav ul li").each(function(){
		var cName = $(this).attr("class");
		if(para == cName){
			para = $(".detail #main .pictNav ul li").index(this);
		}
	});
	if(para == "li"){
		para = 0;
		move(para,0);
	}else{
		move(para-4,0);
		move(para,400);
	}

});

$(".detail #main .pictNavLeft").click(function(){
	var mNum = $(".detail #main .pictNav ul").css("marginLeft");
	mNum = -Number(mNum.replace("px","")) / 61;
	move(Math.floor(mNum)-1,400);
	return false;
});

$(".detail #main .pictNavRight").click(function(){
	var mNum = $(".detail #main .pictNav ul").css("marginLeft");
	mNum = -Number(mNum.replace("px","")) / 61;
	move(Math.ceil(mNum)+1,400);
	return false;
});

$("#news #main .h202 dl dd,#ex #main .sectionH2 .txt h2,#exDetail #main .date").each(function(){
	var date = new Date();
	var dayStart = $(this).find("span.start").text();
	var dayEnd = $(this).find("span.end").text();
	var today = getdate(date);
	var eng = $("body").attr("class");

	if(today < dayStart){
		if(eng.indexOf("eng") == -1){
			$(this).html('<img src="/img/blog/status01.jpg" alt="開催予告" />');
		}else{
			$(this).html('<img src="/img/blog/status01e.jpg" alt="NOTICE" />');
		}
	}else 	if(dayStart < today && today < dayEnd){
		if(eng.indexOf("eng") == -1){
			$(this).html('<img src="/img/blog/status02.jpg" alt="開催中" />');
		}else{
			$(this).html('<img src="/img/blog/status02e.jpg" alt="ON VIEW" />');
		}
	}else{
		if(eng.indexOf("eng") == -1){
			$(this).html('<img src="/img/blog/status03.jpg" alt="終了しました" />');
		}else{
			$(this).html('<img src="/img/blog/status03e.jpg" alt="PAST" />');
		}
	}
});
});

function getdate(date){
	var today = "";
	var temp;

	today += date.getFullYear();

	temp = "0" + (date.getMonth() + 1);
	temp = temp.substr(temp.length - 2, temp.length);
	today += temp;

	temp = "0" + date.getDate();
	temp = temp.substr(temp.length - 2, temp.length);
	today += temp;
	
	return today;
}

function move(mNum,sp){
	var num = $(".detail #main .pictNav ul li").size() - 4;

	if(mNum < 1){
		$(".detail #main .pictNav ul").stop(true,false).animate({marginLeft:0},sp);
		$(".detail #main .pictNavRight").stop(true,false).fadeIn(300);
	}else if(mNum < num){
		$(".detail #main .pictNav ul").stop(true,false).animate({marginLeft:-mNum * 61},sp);
		$(".detail #main .pictNavRight").stop(true,false).fadeIn(300);
	}else{
		$(".detail #main .pictNav ul").stop(true,false).animate({marginLeft:-num * 61},sp);
		$(".detail #main .pictNavRight").stop(true,false).fadeOut(300);
	}
	if(mNum < 1){
		$(".detail #main .pictNavLeft").stop(false,true).fadeOut(300);
	}else{
		$(".detail #main .pictNavLeft").stop(true,false).fadeIn(300);
	}
}

function popup(curl){
	var flag = 0;
	$("body").append('<div id="white"></div>');
	$("#white").fadeIn("slow");

	$(".close").insertAfter("#white").fadeIn("slow");
	$("#wrapper").append('<div id="whiteWrap"></div>');
	$("#whiteWrap").insertAfter("#white");
	if(curl){
		$("#whiteCont").appendTo("#whiteWrap");
		h = $("#whiteCont").height();
		w = $("#whiteCont").width();
		rsize();
		$("#whiteCont").fadeIn("slow");
	}else{
		$("#whiteWrap").wrapInner('<div class="whiteWrapInner"></div>');
		h = $(".sectionH1").height();
		$("#whiteWrap").css("height",h);
		$(".whiteWrapInner").css("height",h);
		h = h-122;
		$(".close").appendTo(".whiteWrapInner");
		$(".show").css("display","none").appendTo(".whiteWrapInner").css("display",function(none){$(".show").css("marginTop",-$(".show").height()/2);w = 430;rsize();}).fadeIn("slow");
	}
	
	$("#white,.close").click(function(){
		wclose();
		return false;
	});
}

function rsize(){
	var num = $(window).width();
	if(num > 1060){
		num = num / 2;
		num = num - w / 2;
		$("#whiteWrap").css("right",num);
		$("#whiteWrap").css("left","auto");
	}else{
		$("#whiteWrap").css("right","auto");
		$("#whiteWrap").css("left",315);
	}
	$("#white").height($("#wrapper").height());
	if($(".sectionH1").height()){
		h = $(".sectionH1").height();
		$("#whiteWrap").css("height",h);
		$(".whiteWrapInner").css("height",h);
		h = h-122;
	}
	if($(window).height() > 740){
		var num2 = ($(window).height() > h ) ? $(window).scrollTop() + ($(window).height() / 2) - ( h /2) : $(window).scrollTop();
		$("#index #whiteWrap").css("top",num2);
	}else{
		$("#index #whiteWrap").css("top",320);
	}

	if($("#wrapper").height() > $(window).height()){
		$("#index .close").css("top",645);
		$("#index .close").css("bottom","auto");
	}else{
		$("#index .close").css("top","auto");
		$("#index .close").css("bottom",27);
	}

	
}

function wclose(){
	$("#whiteWrap").fadeOut("slow");
	$("#white").fadeOut("slow");
	$(".close").fadeOut("slow",function(){
		$(".close").insertAfter("#main");
		$("#whiteCont").insertAfter("#main").css("display","none");
		$("#whiteWrap .show").appendTo(".sectionH1");
		$("#whiteWrap").remove();
		$("#white").remove();
	});
	return false;
}

$.fn.easyToolTip = function(options){
		var toolTip = $('<div id="toolTip"></div>').hide();
		$('body').append(toolTip);
		
		$(this).filter(function(){
			return this.title || this.alt;
		}).each(function(){
		
			var self = $(this);
			var target = this.title ? 'title' : 'alt';
			var tipText = self.attr(target);
			
			self.hover(function(e){
				self.attr(target,'');
				toolTip
					.stop(true,true)
					.fadeIn(200)
					.text(tipText)
					.css({
						position: 'absolute',
						top: e.pageY + 20,
						left: e.pageX + 20
					})
			},function(){
				self.attr(target,tipText);
				toolTip.fadeOut(200);
			}).mousemove(function(e){
				toolTip.css({
					top: e.pageY + 20,
					left: e.pageX + 20
				});
			});
			
		});
	}

$.fn.easyToolTip2 = function(options){
		var toolTip = $('<div id="toolTip2"></div>').hide();
		$('body').append(toolTip);
		
		$(this).filter(function(){
			return this.title || this.alt;
		}).each(function(){
		
			var self = $(this);
			var target = this.title ? 'title' : 'alt';
			var tipText = self.attr(target);
			
			self.hover(function(e){
				self.attr(target,'');
				toolTip
					.stop(true,true)
					.fadeIn(200)
					.text(tipText)
					.css({
						position: 'absolute',
						top: e.pageY + 20,
						left: e.pageX + 20
					})
			},function(){
				self.attr(target,tipText);
				toolTip.fadeOut(200);
			}).mousemove(function(e){
				toolTip.css({
					top: e.pageY + 20,
					left: e.pageX + 20
				});
			});
			
		});
	}

$.fn.easyToolTip3 = function(options){
		var toolTip = $('<div id="toolTip3"></div>').hide();
		$('body').append(toolTip);
		
		$(this).filter(function(){
			return this.title || this.alt;
		}).each(function(){
		
			var self = $(this);
			var target = this.title ? 'title' : 'alt';
			var tipText = self.attr(target);
			
			self.hover(function(e){
				self.attr(target,'');
				toolTip
					.stop(true,true)
					.fadeIn(200)
					.text(tipText)
					.css({
						position: 'absolute',
						top: e.pageY - 10,
						left: e.pageX + 20
					})
			},function(){
				self.attr(target,tipText);
				toolTip.fadeOut(200);
			}).mousemove(function(e){
				toolTip.css({
					top: e.pageY - 10,
					left: e.pageX + 20
				});
			});
			
		});
	}


function pageScroll(toY,frms,cuY){
	if (pageScrollTimer) clearTimeout(pageScrollTimer);
	if (!toY || toY < 0) toY = 0;
	if (!cuY) cuY = 0 + $(window).scrollTop();
	if (!frms) frms = 6;
	if ((toY > cuY) && (toY > getContentsHeight() - $(window).height())){ toY = getContentsHeight() - $(window).height() +1 };
	cuY += (toY - $(window).scrollTop()) / frms;
	if (cuY < 0) cuY = 0;
	var posY = Math.floor(cuY);

	window.scrollTo(0, posY);
	if (posY != toY) {pageScrollTimer = setTimeout("pageScroll("+toY+","+frms+","+cuY+")",16);}
}

function getContentsHeight(){return ($.support.boxModel) ? $('body').height() : document.body.scrollHeight ;}
