﻿////////////////////////////////////////////////////////
//
// 「YKK CP 2011」のフッター動作コントロール用jQuery
// ※対象エレメントが[position: fixed]になってもレイアウトがくずれない様になっているのが前提です
// 
//
////////////////////////////////////////////////////////
jQuery.fn.extend({

	// a_visHeight : close時に表示されているheight
	// a_mouseOverDuration : マウスオーバー時間
	// a_openCloseDuration : 開閉時間
	// a_easing : イージング
	// a_callBackFunc : 開閉アニメーション終了時のコールバック関数 ※2011/06/13 現在 未使用
    footerCtrl: function(a_visHeight, a_mouseOverDuration, a_openCloseDuration, a_easing, a_callBackFunc)
	{
		if( a_mouseOverDuration == undefined || a_mouseOverDuration == null )
			a_mouseOverDuration = 1000;
		if( a_openCloseDuration == undefined || a_openCloseDuration == null )
			a_openCloseDuration = 1000;
		
		if( a_easing == undefined || a_easing == null )
			a_easing = 'linear';
			
    	var tgtElement = this;
		var mouseOver = false;
		
		var timeOutId = null;	// タイマーID
				
		var isPlay = false;	// アニメーション判定用
		var openBottom = parseInt($(tgtElement).css("bottom").replace("px","")); // open時の位置
		var closeBottom = openBottom - ($(tgtElement).height()-a_visHeight);	// close時の位置
		
		// mode判定用
		var MODE_OPEN = "open";
		var MODE_CLOSE = "close";
		
		// 起動初期時は閉じておく
		var mode = MODE_CLOSE;
		$(tgtElement).css( {'bottom': closeBottom/*, 'position': 'fixed'*/});
		
		$(tgtElement).mouseover(function()
		{
			mouseOver = true;
			
			// タイマーをクリア
			if( timeOutId )
			{
				clearTimeout(timeOutId);
				timeOutId = null;
			}
			
			// open中は無視
			if( mode == MODE_OPEN )
				return;
			
			// 一定時間マウスオーバーされていたらopenアニメーションを動作させる
			timeOutId = setTimeout( startOpenAnim, ((isPlay) ? 0 : a_mouseOverDuration));
		})
		.mouseout(function()
		{
			mouseOver = false;
			
			// タイマーをクリア
			if( timeOutId )
			{
				clearTimeout(timeOutId);
				timeOutId = null;
			}
			
			// openアニメーション中、又はclose時は無視
			if( (isPlay && mode == MODE_OPEN) || mode == MODE_CLOSE)
				return;
			
			// マウスアウトから一定時間たってからcloseアニメーションを動作させる
			timeOutId = setTimeout( startCloseAnim, a_mouseOverDuration );
		});
		
		// openアニメーション開始
		function startOpenAnim()
		{
			var currBottom = parseInt($(tgtElement).css("bottom").replace("px",""));
			var rate = (closeBottom-currBottom)/(closeBottom-openBottom);
			var duration = a_openCloseDuration * (1.0-rate);
			
			isPlay = true;
			mode = MODE_OPEN;
			
			$(tgtElement).stop()
			.animate( { 'bottom': openBottom }, duration, a_easing, 
			function()
			{
			  isPlay = false; 
			  
			  // 終了時にmouseOutだった場合
			  if( !mouseOver )
				timeOutId = setTimeout( startCloseAnim, a_mouseOverDuration );
			});
		}
		
		// closeアニメーション開始
		function startCloseAnim()
		{
			isPlay = true;
			mode = MODE_CLOSE;
			
			$(tgtElement).stop()
			.animate( { 'bottom': closeBottom }, a_openCloseDuration, a_easing,
			function(){  isPlay = false; });
		}
		
        return jQuery;
    }
});
