﻿$(document).ready(function() {	
	//ロールオーバー
	var postfix = '_on';
	$('[class=rollover]').not('[src*="'+ postfix +'."]').each(function() {
		var img = $(this);
		var src = img.attr('src');
		var src_on = src.substr(0, src.lastIndexOf('.'))
		           + postfix
		           + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_on);
		img.hover(
			function() {
				img.attr('src', src_on);
			},
			function() {
				img.attr('src', src);
			}
		);
	});
	
	//スクロール
	jQuery.easing.quart = function (x, t, b, c, d) {
    	return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};
	$('a[href^=#]').click(function(){
		var targetOffset = $($(this).attr('href')).offset().top;
		$('html,body').animate({scrollTop:targetOffset}, 1000, 'quart');
		return false;
	});
	
	//TOPイメージ
	jQuery.preloadImages = function(){
    	for(var i = 0; i < arguments.length; i++){
        	jQuery("<img>").attr("src", arguments[i]);
    	}
	};
	$.preloadImages("img/pht_main1.jpg", "img/pht_main2.jpg", "img/pht_main3.jpg", "img/pht_main4.jpg", "img/pht_main5.jpg", "img/pht_main6.jpg", "img/pht_main7.jpg", "img/pht_main8.jpg", "img/pht_main9.jpg", "img/pht_main10.jpg");
	n = 1;
	$("#mainImg img").hide();
	$("#mainImg img").fadeIn(1500);
	timerID = setInterval("anime()", 2000);

});

function anime() {
	n++;
	str = "img/pht_main" + n;
	$("#mainImg img").before("<img src='" + str + ".jpg' alt='' />");
	$("#mainImg img:last").fadeOut(1500,function(){
		$(this).remove();
	});
	if(n == 10){
		clearInterval(timerID);
	} else if (n % 2 == 0){
		clearInterval(timerID);
		timerID = setInterval("anime()", 5000);
	} else if(n % 2 == 1){
		clearInterval(timerID);
		timerID = setInterval("anime()", 2000);
	}
}
