jQuery.easing.easeOutQuart=function(x,t,b,c,d){return -c*((t=t/d-1)*t*t*t-1)+b;};
jQuery.easing.easeOutExpo=function (x,t,b,c,d){return(t==d)?b+c:c*(-Math.pow(2, -10 * t/d) + 1) + b;};

var maxHeight;

$(document).ready(function (){
	$("#mainCon").fadeOut(0);
	
	// IE9
	if(!jQuery.support.noCloneEvent && jQuery.support.opacity){
		$('div.tategaki').css({"padding":"10px 0 0 2px","height": "500px", "writing-mode": "tb-rl", "direction": "ltr", "-ms-writing-mode": "vertical-rl"})
	} else {
		var taketori=new Taketori();
		taketori.set({height:600+"px",maxHeight:500+"px"}).element('div.tategaki').toVertical();
	}
	
	
	function onMagaOver(e){
		var target=$(this).find("div");
		$(this).find("p").stop(false,true).fadeOut(0).fadeIn(500);
		$(target).stop(false,true).show();
	};
	
	function onMagaOut(e){
		var target=$(this).find("div");
		$(target).find("p").stop(false,true).fadeOut(500,function(){$(target).hide()});
	};
	
	$("#menuMaga").bind("mouseenter",onMagaOver).bind("mouseleave",onMagaOut);
	$("#menuLang a img").css("opacity",0.5).hover(function(){$(this).stop(true,false).fadeTo(250,1)},function(){$(this).stop(true,false).fadeTo(250,0.5)});
	
	$("#menuSub a").bind("mouseenter",onOver).bind("mouseleave",onOut);
	$("#menuMagaSub a").bind("mouseenter",onOver).bind("mouseleave",onOut);
	$("#menuGlobal a").bind("mouseenter",onOver).bind("mouseleave",onOut);
	
	
	function onOut(e){
		$(e.currentTarget).find("img").stop(true,false).fadeTo(300,1);
	}
	
	function onOver(e){
		$(e.currentTarget).find("img").stop(true,false).fadeTo(300,0.4);
	}
	
	
})

$(window).load(function() {
	$("#mainCon").fadeIn(1000)
	var cnt={now:0,pre:0};
	var len =$("#mainSlide li").length;
	s.init();
	
	
	
	$("#menuGlobal a").bind("mouseenter",menuGlobalOver);
	$("#mainSlideNavi li").bind("mouseenter",slideNaveOver).bind("mouseleave",slideNaveOut).bind("click",slideNaveClick);
	$("#topSide a").bind("mouseenter",topSideOver).bind("mouseleave",topSideOut);
	
	onMove(0);
	
	
	
	
	function onMove(n){
		cnt.pre=cnt.now;
		cnt.now=n;
		
		if(cnt.now==0){
			$("#btn_prev").unbind("mouseenter",slideNaveOver).unbind("mouseleave",slideNaveOut).unbind("click",slideNaveClick);
			slideNaveOver({currentTarget:$("#btn_prev")[0]});
		}else if(cnt.pre==0){
			$("#btn_prev").bind("mouseenter",slideNaveOver).bind("mouseleave",slideNaveOut).bind("click",slideNaveClick);
			slideNaveOut({currentTarget:$("#btn_prev")[0]});
		}
		
		if(cnt.now==len-1){
			$("#btn_next").unbind("mouseenter",slideNaveOver).unbind("mouseleave",slideNaveOut).unbind("click",slideNaveClick);
			slideNaveOver({currentTarget:$("#btn_next")[0]});
		}else if(cnt.pre==len-1){
			$("#btn_next").bind("mouseenter",slideNaveOver).bind("mouseleave",slideNaveOut).bind("click",slideNaveClick);
			slideNaveOut({currentTarget:$("#btn_next")[0]});
		}
		
		
		$("#mainSlide").stop(false,true).animate({"top":-maxHeight*n+"px"},600,"easeOutQuart");
		$("#menuArrow img").stop(true,false).animate({"top":260+n*32+"px"},200,"easeOutExpo");
	}
	
	function menuGlobalOver(e){
		var target=e.currentTarget;
		var n=$("#menuGlobal a").index(target);
		onMove(n)
	}
	
	function slideNaveOver(e){
		var target=$(e.currentTarget).find("img");
		$(target).stop(false,true).animate({"opacity":0.5},400,"easeOutQuart");
	}
	
	function slideNaveOut(e){
		var target=$(e.currentTarget).find("img");
		$(target).stop(false,true).animate({"opacity":1},400,"easeOutQuart");
	}
	
	function slideNaveClick(e){
		var id=e.currentTarget.id;
		var n = 0;
		
		if(id=="btn_prev"){n=cnt.now-1;} 
		else if(id=="btn_next"){n=cnt.now+1;}
		
		onMove(n);
		
	}
	function topSideOut(e){
		var target=$(e.currentTarget).find(".topSideImg");
		$(target).stop(false,true).animate({"opacity":1},400,"easeOutQuart");
	}
	function topSideOver(e){
		var target=$(e.currentTarget).find(".topSideImg");
		$(target).stop(false,true).animate({"opacity":0.5},400,"easeOutQuart");
	}
});

var s = {
	init:function(){
		$(window).resize(s.onResize);
		$(window).resize();
	},
	onResize:function(){
		s.sW=$(window).width();
		s.sH=$(window).height();
		
		$("#menuCon").css("height",null);
		maxHeight=Math.max($("#menuCon").height(),s.sH);
		$("#menuCon").css("height",maxHeight+"px");
		$("#mainCon").css("height",maxHeight+"px");
		
		
		
		
		
		var menuW = $("#menuCon").width();
		$("#mainCon").css({"width":s.sW-menuW+"px","height":maxHeight+"px"});
		$("#mainSlideNavi").css({"top":s.sH-(59*2)-1+"px"});
		
		
		$("#mainSlide").css({"width":s.sW-menuW-28+"px","height":s.sH+"px"});
		
		
		for(var i = 0; i<$("#mainSlide li").length; i++){
			$("#mainSlide li").eq(i).css({"top":maxHeight*i+"px","width":s.sW-menuW-150-28+"px", "height":maxHeight+"px"});
			
			var target=$("#mainSlide .slideImg").eq(i);
			var h=$(target)[0].clientHeight;
			var w=$(target)[0].clientWidth;
			var scale=Math.max($("#mainSlide li").eq(i).height()/h, (s.sW-menuW-150-28)/w);
			$(target).css({"top":(maxHeight-h*scale)/2+"px","left":((s.sW-menuW-150-28)-w*scale)/2+"px","width":w*scale+"px", "height":h*scale+"px"});
			
			var target=$("#mainSlide .slideTxt").eq(i);
			$(target).css({"top":(maxHeight-$(target)[0].clientHeight)/2+"px"});
			
		}
		
		$("#topNews").css({"height":maxHeight+"px"})
		
		$("#topSide li").css("height",Math.ceil(maxHeight/3)+"px");
		
		
		for(var i = 0; i<$("#topSide li").length; i++){
			var target=$("#topSide .topSideTxt").eq(i);
			var h=$(target)[0].clientHeight;
			var w=$(target)[0].clientWidth;
			$(target).css({"top":($("#topSide li").eq(i).height()-h)/2+"px","left":(150-w)/2+"px"});
			
			var target=$("#topSide .topSideImg").eq(i);
			var h=$(target)[0].clientHeight;
			var w=$(target)[0].clientWidth;
			var scale=Math.max($("#topSide li").eq(i).height()/h, 150/w);
			
			$(target).css({"top":($("#topSide li").eq(i).height()-h*scale)/2+"px","left":(150-w*scale)/2+"px","width":w*scale+"px", "height":h*scale+"px"});
			
		}
		
	}
	
}
