$(function() {
	btn_initialize('ul#menu span');
	btn_initialize('div.help span');
	btn_initialize('div#topLeft p.contact span');
	
	$('#news_area').hover(function() {
		$(this).stop().animate({
			left: '337px'
		}, 400);
	}, function() {
		$(this).stop().animate({
			left: '602px'
		}, 400);
	});
	
	setTimeout(first_initialize, 2000);
	setTimeout(image_rotate, 6000);
	setTimeout(string_rotate, 4000);
});

function first_initialize() {
	$('ul#menu a').addClass('pngfix');
	$('div.help a').addClass('pngfix');
	$('div#topLeft p.contact a').addClass('pngfix');
	$('div#news_area').addClass('pngfix');
	
	$('#black').fadeOut(2000);
}

function btn_initialize(element) {
	$(element).css('opacity', '0');
	$(element).addClass('pngfix');
	$(element).hover(function() {
		$(this).stop().animate({
			opacity: 1
		}, 'normal');
	}, function() {
		$(this).stop().animate({
			opacity: 0
		}, 'normal');
	});
}

function initialize(time) {
	rotate_initialize(time);
	
	setTimeout(image_rotate, 5000);
	setTimeout(string_rotate, 2000);
}

function rotate_initialize(time) {
	image_initialize(time);
	string_initialize(time);
}

function image_initialize(time) {
	$('img.rotate').fadeOut(time);
}

function string_initialize(time) {
	$('#string01 img').fadeOut(time);
	$('#string02 img').fadeOut(time);
	$('#string01').fadeOut(time);
	$('#string02').fadeOut(time);
}

function image_rotate() {
	var num = 8;
	var pos = -1;
	var next_time = 6000;
	var fade_time = 2500;
	for(var i = 1; i <= num; i++) {
		if ($('#rotate0' + i).hasClass('now')) {
			pos = i;
			if (pos >= 3) {
				next_time = 6000;
			}
			$('#rotate0' + i).removeClass('now');
			if (i == num) {
				return;
			} else {
				$('#rotate0' + i).fadeOut(fade_time);
				setTimeout(function() {
					$('#rotate0' + (i+1)).addClass('now');
					$('#rotate0' + (i+1)).fadeIn(fade_time);
					setTimeout(image_rotate, next_time);
				}, fade_time + 100);
			}
			return;
		}
	}
	
	if (pos == -1) {
		$('#rotate01').fadeIn(fade_time);
		$('#rotate01').addClass('now');
		setTimeout(image_rotate, next_time);
	}
}

function string_rotate() {
	if ($('#string02').hasClass('now')) {
		$('#string02').removeClass('now');
		initialize(2000);
	} else if ($('#string01').hasClass('now')) {
		$('#string01').removeClass('now');
		string02_rotate();
	} else {
		setTimeout(string01_rotate, 1000);
	}
}

function string01_rotate() {
	var time = 2000;
	var next_time = 4000;
	$('#string01').show(0);
	$('#string01').addClass('now');
	$('#string01_01').fadeIn(time);
	$('#string01_02').fadeIn(time);
	setTimeout(function() {
		$('#string01_03').fadeIn(time);
	}, 800);
	setTimeout(string_rotate, 30000);
}

function string02_rotate() {
	string_initialize(2000);
	var time = 2000;
	var num = 8;
	var next_time = 4000;
	var offset_time = 4500;
	$('#string02').show(0);
	$('#string02').addClass('now');
	
	setTimeout(function() {
		$('#string02_01').fadeIn(time);
	}, next_time + offset_time);
	setTimeout(function() {
		$('#string02_02').fadeIn(time);
	}, next_time * 2 + offset_time);
	setTimeout(function() {
		$('#string02_03').fadeIn(time);
	}, next_time * 3 + offset_time);
	setTimeout(function() {
		$('#string02_04').fadeIn(time);
	}, next_time * 4 + offset_time);
	setTimeout(function() {
		$('#string02_05').fadeIn(time);
	}, next_time * 5 + offset_time);
	setTimeout(function() {
		$('#string02_06').fadeIn(time);
	}, next_time * 6 + offset_time);
	setTimeout(function() {
		$('#string02_07').fadeIn(time);
	}, next_time * 7 + offset_time);
	setTimeout(function() {
		$('#string02_08').fadeIn(time);
	}, next_time * 8 + offset_time);
	setTimeout(string_rotate, next_time * 10 + offset_time);
}
