/*
 * slidefade.js
 * settings for toho co., ltd.
 *
 * Copyright(c) TOHO All rights reserved.
 *
 * Author: Kazunori Tokuda | http://5509.me/
 *
 */
 
jQuery(function($) {

	var msie = $.browser.msie && $.browser.version < 9,
		defaultImagePos = [
			15, 245, 475, 705
		],
		defaultPosLeft = $('#header').offset().left,
		promoBlock = $('#promo').css('height', 340),
		promo = $('#promo>ul'),
		promoBlockWidth = 928,
		animating = [0, 1, 2, 3, 4],
		imgLoadComp = 0,
		opacityTrim = 0.01,
		comp = 0,
		playing = true,
		rate = 90,
		// 速度はフレームレートを増加させると上昇します
		// 推奨rateは60で100を超えるとブラウザの負担が大きくなります
		clientWidth = function() {
			var width = document.body.clientWidth || document.documentElement.clientWidth;
			if ( width <= promoBlockWidth ) {
				width = promoBlockWidth + 30;
			}
			return width;
		}

	// ウィンドウ幅にあわせる
	promoBlock.wrap(
		$('<div id="promoBlockWrap"></div>')
			.css({
				width: clientWidth(),
				overflow: 'hidden'
			})
	);
	$(window)
		.resize(function() {
			$('#promoBlockWrap')
				.css('width', clientWidth());
		})
		.blur(function() {
			if ( $.browser.msie ) return false;
			playing = false;
		})
		.focus(function() {
			if ( $.browser.msie || playing ) return false;
			playing = true;
			animStart();
		});
	
	var images = promo
			.find('li')
			.css({
				display: 'block',
				position: 'absolute',
				top: 20,
				left: promoBlockWidth + 6,
				opacity: 0
			}),
		imagesLen = images.length;
		
	// 初期位置に移動
	// 全て移動したらアニメーションを開始
	function setDefaultPos(callback) {
		images.each(function(i) {
			if ( i == 4 ) return false;
			$(this)
				// 初めの4つは表示された状態から始まる
				.attr('opacity', 1)
				.delay(i*300)
				.animate({
					// 最初の位置へ移動する
					left: defaultImagePos[i],
					opacity: 1
				}, {
					//easing: 'swing',
					duration: 1000 - i*100,
					complete: function() {
						if ( comp++ == 3 ) {
							callback();
						}
					}
				});
		});
	}
	
	// アニメーション
	function animStart() {
		(function() {
			if ( !playing ) return false;
			for ( var i=0; i<5; i++ ) {
				var thisImage = images[animating[i]];
				thisImage.opacity = $(thisImage).attr('opacity') || 0;
				thisImage.left = parseInt(thisImage.style.left) - 2;
				thisImage.style.left = thisImage.left + 'px';
				// はじまり(フェードイン
				if ( thisImage.left > 0 && thisImage.opacity < 1 ) {
					thisImage.opacity = thisImage.opacity + opacityTrim;
					
					if ( !msie ) thisImage.style.opacity = thisImage.opacity;
					else thisImage.style.filter = 'alpha(opacity=' + thisImage.opacity*100 + ')';
				} else
				// おわり(フェードアウト
				if ( thisImage.left < 0 ) {
					if ( thisImage.opacity == 0 ) thisImage.opacity = 1;
					thisImage.opacity = thisImage.opacity - opacityTrim;
					
					if ( !msie ) thisImage.style.opacity = thisImage.opacity;
					else thisImage.style.filter ='alpha(opacity=' + thisImage.opacity*100 + ')';
					
					if ( thisImage.left <= -215 ) {
						// イメージの削除と次のイメージの追加
						animating.splice(0, 1);
						if ( animating[3] >= imagesLen - 1 ) {
							animating.push(0);
						} else {
							animating.push(animating[3] + 1);
						}
						images[animating[4]].style.left = parseInt(images[animating[3]].style.left) + 230 + 'px';
						thisImage.style.opacity = thisImage.opacity = 0;
						thisImage.style.left = promoBlockWidth + 6 + 'px';
					}
				}
			}

			setTimeout(arguments.callee, 1000 / rate);
		})();
	}
	
	// アニメーションのスタート
	setTimeout(function() {
		setDefaultPos(animStart);
	}, 500);
});
