/*
 * クニヒロ卓球
 * (C)Shinkousoku Printing Inc. http://www.sksp.co.jp/
 */

jQuery.easing.quart =  function (x, t, b, c, d) {
	return c*((t=t/d-1)*t*t*t*t + 1) + b;
} 

$(function(){

	/**
	 * ページの先頭へボタン
	 * clickイベント
	 */	
	var target = $.support.boxModel ? navigator.appName.match(/Opera/) ? 'html' : 'html,body' : 'body';
	$('#top a').click(function(e) {
		var event = e || window.event;
		(event.preventDefault) ? event.preventDefault() : event.returnValue = false;
		(event.stopPropagation) ? event.stopPropagation() : event.cancelBubble = true;
		$(target).animate({scrollTop: 0}, 800, 'quart');
		return false;
	});
	$('#top').fadeOut();
	
	/**
	 * ページの先頭へボタン
	 * スクロールフォロー
	 */
	var topInterval = null;
	var topFllow = function(){
		clearInterval(topInterval);
		var topHeight        = parseInt($('#top').css('height'));
		var windowHeight     = parseInt($(window).height());		
		var documentHeight   = parseInt($(document).height());
		var scrollableHeight = (documentHeight - windowHeight);
		var scrollTop        = parseInt($(document).scrollTop());
		$('#top:not(:animated)').animate({bottom:(scrollableHeight - scrollTop)}, 1000, 'quart', function(){
			(scrollTop == 0) ? $('#top').fadeOut('fast') : $('#top').fadeIn('fast');
		});
	};
	var scrollHandler = function(){
		clearInterval(topInterval);
		topInterval = setInterval(topFllow, 500);
	};
	$(window).scroll(scrollHandler).resize(scrollHandler).trigger('resize');

	/**
	 * プロモーション
	 * ※キャンペーンの時は、キャンペーンパネルを [0] に設置して、isCampaign フラグを true にします。
	 */
	var Promotion = {
		isCampaign : false,
		interval   : null,
		prevTab    : null,
		prevPanel  : null,
		showTab    : null,
		showPanel  : null,
		showIndex  : -1,
		length     : $('#promotion section').length,
 		stop : function(){
			clearInterval(Promotion.interval);
		},
		play : function(){
			Promotion.interval = setInterval(function(){
				if (Promotion.isCampaign) {
					Promotion.showIndex = 0;
				}
				else {
					Promotion.showIndex = (Promotion.showIndex < Promotion.length - 1) ? Promotion.showIndex + 1 : 0;
				}
				Promotion.change();
			}, (Promotion.isCampaign ? 1000 : 6000));
		},
		change : function(){
			$('#promotion li a').removeClass('current');
			if (!Promotion.isCampaign) $('#promotion li a').eq(Promotion.showIndex).addClass('current');
			$('#promotion section').stop().each(function(){
				var isShown = (Promotion.showIndex == $('#promotion section').index($(this)));
				$(this)	.css('zIndex', (isShown ? 2 : 1)).fadeTo('fast', (isShown ? 1 : 0));
			});
		}
 	};

	$('#promotion section').fadeTo(0, 0).css('display', 'block').hover(Promotion.stop, Promotion.play);
	$('#promotion li a').hover(function(){
		Promotion.stop();
		Promotion.showIndex = $('#promotion li a').index($(this)) + (Promotion.isCampaign ? 1 : 0);
		Promotion.change();
	}, Promotion.play);

	if (Promotion.isCampaign) {
		Promotion.showIndex = 0;
		Promotion.change();
	}
	else {
		$('#promotion li a:first').trigger('mouseover');
		Promotion.play();
	}

});
