/*
 * requires:jQuery v1.6.4 later
 * name:opening.js
 * author:Manabu Kushimoto(web-park.org)
 */
 
jQuery(function($){

	var $win=$(window);
	var $body=$("body");
	var $head=$("div#head_wrap")
	var $header=$("div#header")
	var $foot=$("div#foot_wrap");
	var $opening=$("div#opening");
	var $frame=$opening.find("div.frame");
	var $loading=$("p#loading");
	var $half=$("div.half");
	var $multi=$("div.multi");
	var win_w=$win.width();
	var win_h=$win.height();
	var len=$frame.length;
	var imgObj=new Image();

	$header.hide();
	$foot.css({top:win_h+38+"px"});

	$("div#scene").eq(len).hide();
	
	function first_set(){
		$("div#scene1").append('<img src="./img/top/1.jpg" style="display: none;" />');
		body_size();
		$("div#scene1").css({top:0,left:0,width:0,height:win_h+"px",zIndex:len});

		for(var i=1;i<len;i++){
			$frame.eq(i).css({top:0,left:0,zIndex:len-i});
			imgObj.src="./img/top/"+i+".jpg";
		}

		$frame.eq(len).css({zIndex:len});
	};
	first_set();
	
	//アニメーション設定
	window.onload=function(){
		setTimeout(function(){
			$loading.fadeOut(300,function(){
				$head.animate({ top: 0 },1000);
				$foot.animate({
					top:win_h-38+"px"
				},1000,function(){
					$header.fadeIn(1000,function(){
						loop_animation();
						auto_loop();
					});
				});
			});
		},1000);
	};
	
	function loop_animation(){
		
		setTimeout(function(){

			$("div#scene1").find("img").show();
	
			$("div#scene1").animate({
				width:win_w+"px"
			},3000,"easeOutExpo",function(){
	
				$frame.eq(len).find("img").remove();
				
				$("div#scene2").append('<img src="./img/top/2.jpg" />');
				body_size();
				$("div#scene2").css({top:0,left:0,width:win_w+"px",height:0,zIndex:len}).animate({
					height:win_h+"px"
				},3000,"easeOutExpo",function(){
	
					$("div#scene1").find("img").remove();
	
					$("div#scene3").append('<img src="./img/top/3.jpg" />');
					body_size();
					$("div#scene3").css({top:0,left:win_w+"px",width:0,height:win_h+"px",zIndex:len});
					$("div#scene3").find("img").css({marginLeft:-win_w+"px"});
			
					$("div#scene3").animate({
						left:0,
						width:win_w+"px"
					},3000,"easeOutExpo");
					$("div#scene3").find("img").animate({
						marginLeft:0
					},3000,"easeOutExpo",function(){
	
						$("div#scene2").find("img").remove();
		
						$("div#scene4").append('<img src="./img/top/4.jpg" />');
						body_size();
						$("div#scene4").css({top:win_h+"px",left:0,width:win_w+"px",height:0,zIndex:len});
						$("div#scene4").find("img").css({top:-win_h+"px"});
	
						$("div#scene4").animate({
							top:0,
							height:win_h+"px"
						},3000,"easeOutExpo");
						$("div#scene4").find("img").animate({
							top:0
						},3000,"easeOutExpo",function(){
	
							$("div#scene3").find("img").remove();
			
							$("div#scene5").append('<img src="./img/top/5.jpg" />');
							body_size();
	
							$("div#scene5").css({top:0,left:0,width:0,height:win_h+"px",zIndex:len}).animate({
								width:win_w+"px"
							},3000,"easeOutExpo",function(){
	
								$("div#scene4").find("img").remove();
				
								$("div#scene6").append('<img src="./img/top/6.jpg" />');
								body_size();
	
								$("div#scene6").css({top:0,left:0,width:win_w+"px",height:0,zIndex:len}).animate({
									height:win_h+"px"
								},3000,"easeOutExpo",function(){
	
									$("div#scene5").find("img").remove();
	
									$("div#scene7").append('<img src="./img/top/7.jpg" />');
									body_size();
	
									$("div#scene7").css({top:0,left:win_w+"px",width:0,height:win_h+"px",zIndex:len});
									$("div#scene7").find("img").css({marginLeft:-win_w+"px"});
							
									$("div#scene7").animate({
										left:0,
										width:win_w+"px"
									},3000,"easeOutExpo");
									$("div#scene7").find("img").animate({
										marginLeft:0
									},3000,"easeOutExpo",function(){
	
										$("div#scene6").find("img").remove();
					
										setTimeout(function(){
											first_set();
										},3000);
	
									});
	
	
								});
	
							});
	
						});
	
					});
	
				});
	
			});

		},500);

	};
	
	function auto_loop(){
		animation=setInterval(function(){
			loop_animation();
		},24500);
	};

	//リサイズ設定
	function body_size(){
		win_w=$win.width();
		win_h=$win.height();
		
		$body.css({height:win_h+"px"});
		$opening.css({width:win_w+"px",height:win_h+"px"});
		
		$frame.css({width:win_w+"px",height:win_h+"px"});

		if(win_w>600&&win_h>400){
			if(win_w<500&&win_h>300){
				//$("h2").text("a")
				$frame.find("img").css({width:"auto",height:win_h+"px"});
			}else{
				if(win_w<1200&&win_h>500){
					//$("h2").text("a+")
					$frame.find("img").css({width:"auto",height:win_h+"px"});
				}else{
					//$("h2").text("a++")
					$frame.find("img").css({width:win_w+"px",height:"auto"});
				}
			}
		}else if(win_w<600&&win_h<400){
			if(win_h>300){
				//$("h2").text("b")
				$frame.find("img").css({width:"auto",height:win_h+"px"});
			}else{
				//$("h2").text("b+")
				$frame.find("img").css({width:win_w+"px",height:"auto"});
			}
		}else if(win_w>600&&win_h<400){
			//$("h2").text("c")
			$frame.find("img").css({width:win_w+"px",height:"auto"});
		}else if(win_w<600&&win_h>400){
			if(win_h>300){
				//$("h2").text("d")
				$frame.find("img").css({width:"auto",height:win_h+"px"});
			}else{
				//$("h2").text("d+")
				$frame.find("img").css({width:win_w+"px",height:"auto"});
			}
		}else{
			//$("h2").text("other")
		}

		//$("#logo").text(win_w+","+win_h);

	};

	$(window).resize(function(){
		body_size();
		$foot.css({top:win_h-38+"px"});			
	});

	
	//ニュース自動切換え
	$.fn.news_sticker=function(){
		var active="active";
		var $t=$(this);
		var $child=$t.children();
		$child.each(function(){
			$child.not($child.eq(0)).hide();
			$child.eq(0).addClass(active);
		});
		
		function auto_event(){
			auto=setInterval(function(){
				var active_index=$child.index($("."+active,$t));
				var len=$child.length;
				if(active_index==len-1){
					$child.eq(active_index).fadeOut(100,function(){
						$child.eq(0).fadeIn(300).addClass(active);
						$(this).removeClass(active);
					});
				}else{
					$child.eq(active_index).fadeOut(100,function(){
						$child.eq(active_index).next().fadeIn(300).addClass(active);
						$(this).removeClass(active);
					});
				}
			},5000);
		}
		auto_event();
		
	};
	
	$("div#news_sticker > div").news_sticker();
	
	
});
