var img = new Array( "top_images/14.jpg","top_images/15.jpg","top_images/13.jpg","top_images/12.jpg" );
var timer;

$(document).ready(function(){ 
	$('#background_image').animate({'opacity':'0.00'},{duration:0})
	setTimeout( "initBackground()", 400 );
	/*バナー設定*/
	$('#banner1').fadeOut(0);
	$('#banner2').fadeOut(0);
	$('#index_menu').fadeOut(0);
	setTimeout( "setBanner()", 500 );
	setTimeout( "setIndex()", 800 );
});

function initBackground() {
	setBackground();
	$(window).resize();
}

function setBackground() {
	$('#background_image').animate({'opacity':'0.00'},{duration:500})
	var rand = Math.floor(Math.random()*img.length);
	$('#background_image').attr('src',img[rand]);
	$('#background_image').animate({'opacity':'1.00'},{duration:600})
	timer = setTimeout("randomImage()",20000);
}

$(window).resize(function() {
	if ( document.getElementById('background_image') ) {
		var imgWidth = 2;
		var imgHeight = 1;
		var winWidth = $('#main_top').attr('offsetWidth');
		var winHeight = $('#main_top').attr('offsetHeight');
		widthRate = imgWidth / winWidth; // 画像サイズと画像表示領域のサイズの比率 (幅)
		heightRate = imgHeight / winHeight; // 画像サイズと画像表示領域のサイズの比率 (高さ)
		if (widthRate >= 1 && heightRate >= 1){
			// 画像のwidth、height共に画面に収まらない場合
			if (widthRate > heightRate){
				// 画像のwidthの比率の方が大きい場合
				// widthに合わせてリサイズ
				$('#background_image').width(Math.floor(imgWidth / heightRate));
				$('#background_image').height(winHeight);
			} else {
				// 画像のheightの比率の方が大きい場合
				// heightに合わせてリサイズ
				$('#background_image').width(winWidth);
				$('#background_image').height(Math.floor(imgHeight / widthRate));
			}
		} else if (widthRate >= 1 && heightRate < 1){
			// 画像のwidthのみ画面に収まらない場合
			$('#background_image').width(Math.floor(imgWidth / heightRate));
			$('#background_image').height(winHeight);
		} else if (widthRate < 1 && heightRate >= 1){
			// 画像のheightのみ画面に収まらない場合
			$('#background_image').width(winWidth);
			$('#background_image').height(Math.floor(imgHeight / widthRate));
		} else {
			// 画像のwidht、heightともに画面に収まる場合
			if (widthRate > heightRate){
				// 画像のwidthの比率の方が大きい場合
				// widthに合わせてリサイズ
				$('#background_image').width(Math.floor(imgWidth / heightRate));
				$('#background_image').height(winHeight);
			} else {
				// 画像のheightの比率の方が大きい場合
				// heightに合わせてリサイズ
				$('#background_image').width(winWidth);
				$('#background_image').height(Math.floor(imgHeight / widthRate));
			}
		}
		$('#background_image').css('visibility','visible');
		document.getElementById("background_image2").style.width = document.getElementById("background_image").style.width;
		document.getElementById("background_image2").style.height = document.getElementById("background_image").style.height;
	}
	positionIndex();
});

function changeImageMode( ) {
	if ( $('#index_ctrl_area').css('display') != 'none' ) {
		$('#index_area').fadeIn(200);
		$('#index_menu').fadeIn(200);
		$('#background_image2').css('display','block');
		$('#main_area2')
			.css('filter','Alpha(opacity=50)')
			.css('opacity',0.5);
		$('#index_ctrl_area')
			.fadeOut(600);
		var pos = $('#main_area2').position();
		if ( pos.top > 100 ) changeMoji();
	} else {
		$('#index_area').fadeOut(600);
		$('#index_menu').fadeOut(600);
		$('#background_image2').css('display','none');
		$('#main_area2')
			.css('filter','Alpha(opacity=100)')
			.css('opacity',1);
		$('#index_ctrl_area')
			.fadeIn(200);
	}
}

function changeMoji() {
	var pos = $('#main_area2').position();
	if ( pos.top <= 100 ) {
		$('#main_area2')
			.animate({'top':"600px"},{duration:500,queue:false});
	} else {
		$('#main_area2')
			.animate({'top':"0px"},{duration:500,queue:false});
	}
}


function changeBackground( num ) {
	$('#background_image').fadeOut(500,function() { 
		$('#background_image').attr('src','images/spacer.gif');
		$('#background_image').attr('src',num).fadeIn(200);
	});
	clearTimeout( timer );
	timer = setTimeout("randomImage()",20000);
}

function randomImage() {
	var rand = Math.floor(Math.random()*img.length);
	changeBackground( img[rand] );
}

function setIndex() {
	positionIndex();
	$('#index_menu').fadeIn(300);
}

function positionIndex() {
	if ( document.getElementById( 'index_area' ) ) {
		var pos = $('#index_area').offset();
		var x = pos.left+695;
		$('#index_menu').animate({'left':x+'px'},{duration:0});
	}
}

/* バナー用 */
function changeBanner( num ) {
	$('#banner1').delay(100).fadeIn(200);
}

function setBanner() {
	changeBanner(1);
}


