/* -----------------------------------------------------------------------------------------------------
 * Layer操作ライブラリ 
 * 
 * @author：Media Gate
 * @version：1.03
 * --------------------------------------------------------------------------------------------------- */


/* -----------------------------------------------------------------------------------------------------
 * public function segment.
 * --------------------------------------------------------------------------------------------------- */
/* -----------------------------------------------------------------------------------------------------
 * レイヤ左端位置の設定を行う。
 * @param：レイヤ名称文字列
 * @param：レイヤ左端位置(pixel)
 * @return：無し
 * --------------------------------------------------------------------------------------------------- */
function setLayerLeft(name, left)
{
	if( NN6 || IE5 || MOZ ) document.getElementById( name ).style.left = left;
	else if( NN4 ) document.layers[ name ].left = left;
	else if( IE4 ) document.all( name ).style.pixelLeft = left;
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ左端位置の取得を行う。
 * @param：レイヤ名称文字列
 * @return：null以外：レイヤ左端位置(pixel)
 *			null	：取得失敗
 * --------------------------------------------------------------------------------------------------- */
function getLayerLeft( name )
{
	return ( NN6 || IE5 || MOZ )? document.getElementById( name ).style.left : 
				(NN4? document.layers[ name ].left : (IE4? document.all( name ).style.pixelLeft : null));
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ上端位置の設定を行う。
 * @param：レイヤ名称文字列
 * @param：レイヤ上端位置(pixel)
 * @return：無し
 * --------------------------------------------------------------------------------------------------- */
function setLayerTop( name, top )
{
	if( NN6 || IE5 || MOZ ) document.getElementById( name ).style.top = top; //  
	else if( NN4 ) document.layers[ name ].left = left;
	else if( IE4 ) document.all( name ).style.pixelTop = left;
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ上端位置の取得を行う。
 * @param：レイヤ名称文字列
 * @return：null以外：レイヤ上端位置(pixel)
 *			null	：取得失敗
 * --------------------------------------------------------------------------------------------------- */
function getLayerTop( name )
{
	return ( NN6 || IE5 || MOZ )? document.getElementById( name ).style.top : 
				(NN4? document.layers[ name ].top : (IE4? document.all( name ).style.pixelTop : null));
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ幅の設定を行う。
 * @param：レイヤ名称文字列
 * @param：レイヤ幅(pixel)
 * @return：無し
 * --------------------------------------------------------------------------------------------------- */
function setLayerWidth( name, width )
{
	if( NN6 || IE5 || MOZ ) document.getElementById( name ).style.width = width;
	else if( NN4 ) document.layers[ name ].clip.width = width;
	else if( IE4 ) document.all( name ).style.pixelWidth = width;
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ幅の取得を行う。
 * @param：レイヤ名称文字列
 * @return：null以外：レイヤ幅(pixel)
 *			null	：取得失敗
 * --------------------------------------------------------------------------------------------------- */
function getLayerWidth( name )
{
	return ( NN6 || IE5 || MOZ )? document.getElementById( name ).style.width : 
				(NN4? document.layers[ name ].clip.width : (IE4? document.all( name ).style.pixelWidth : null));
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ高の設定を行う。
 * @param：レイヤ名称文字列
 * @param：レイヤ高(pixel)
 * @return：無し
 * --------------------------------------------------------------------------------------------------- */
function setLayerHeight( name, height )
{
	if( NN6 || IE5 || MOZ ) document.getElementById( name ).style.height = height;
	else if( NN4 ) document.layers[ name ].clip.height = height;
	else if( IE4 ) document.all( name ).style.pixelHeight = height;
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ高の取得を行う。
 * @param：レイヤ名称文字列
 * @return：null以外：レイヤ高(pixel)
 *			null	：取得失敗
 * --------------------------------------------------------------------------------------------------- */
function getLayerHeight( name )
{
	return ( NN6 || IE5 || MOZ )? document.getElementById( name ).style.height : 
				(NN4? document.layers[ name ].clip.height : (IE4? document.all( name ).style.pixelHeight : null));
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ優先順位の設定を行う。
 * @param：レイヤ名称文字列
 * @param：レイヤー優先順位(pixel)	0	：最背面
 * @return：無し
 * --------------------------------------------------------------------------------------------------- */
function setLayerZindex(name, zIndex)
{
	if( NN6 || IE5 || MOZ ) document.getElementById( name ).style.zIndex = zIndex;
	else if( NN4 ) document.layers[ name ].zIndex = zIndex;
	else if( IE4 ) document.all( name ).style.zIndex = zIndex;
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ優先順位の取得を行う。
 * @param：レイヤ名称文字列
 * @return：null	：取得失敗
 * 			null以外：レイヤー優先順位
 * --------------------------------------------------------------------------------------------------- */
function getLayerZIndex( name )
{
	return ( NN6 || IE5 || MOZ )? document.getElementById( name ).style.zIndex : 
				(NN4? document.layers[ name ].zIndex : (IE4? document.all( name ).style.zIndex : null));
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ可視状態の設定を行う。
 * @param：レイヤ名称文字列
 * @param： true	：可視状態
 *			false	：不可視状態
 * @return：なし
 * --------------------------------------------------------------------------------------------------- */
function setLayerVisibility(name, visible)
{
	visible = visible? (NN4? 'show' : 'visible') : 'hidden';

	if( NN6 || IE5 || MOZ ) document.getElementById( name ).style.visibility = visible;
	else if( NN4 ) document.layers[ name ].visibility = visible;
	else if( IE4 ) document.all( name ).style.visibility = visible;
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ可視状態の取得を行う。
 * @param：レイヤ名称文字列
 * @return：visible	：可視状態
 *			hidden	：不可視状態
 * 			null	：取得失敗
 * --------------------------------------------------------------------------------------------------- */
function getLayerVisibility(name, visible)
{
	return ( NN6 || IE5 || MOZ )? document.getElementById( name ).style.visibility : 
				(NN4? document.layers[ name ].visibility : (IE4? document.all( name ).style.visibility : null));
}

/* -----------------------------------------------------------------------------------------------------
 * レイヤ表示開始位置の設定を行う。
 * @param：レイヤ名称文字列
 * @param：レイヤ左端位置(pixel)
 * @param：レイヤ上端位置(pixel)
 * @return：無し
 * --------------------------------------------------------------------------------------------------- */
function setLayerPosition(name, left, top)
{
	setLayerLeft(name, left);
	setLayerTop(name, top);
}

/* -----------------------------------------------------------------------------------------------------
 * レイアースクロールの開始を行う。
 * @param：レイヤ名称文字列
 * @param：スクロール種別					0:Wipper
 * @param：レイヤ到達予定左端位置(pixel)
 * @param：レイヤ到達予定上端位置(pixel)
 * @return：無し
 * --------------------------------------------------------------------------------------------------- */
function startLayerScroll(name, kind, x, y)
{
	var active = 0;
	for(var i = 0; i < maxLayer; i++){
		if(layerName[i] == ""){
			layerName[i] = name;
			kindScroll[i] = kind;
			forX[i]	= x;
			forY[i]	= y;
		}else{
			active++;
		}
	}

	if(active == 0){
		timerID	= setInterval("executionLayerScroll()", 2);
	}
}


// --------------------------------------------------------------------------------------------------
// private method and private variable segment.
// --------------------------------------------------------------------------------------------------
var maxLayer	= 3;
forX			= new Array( maxLayer );
forY			= new Array( maxLayer );
layerName		= new Array( maxLayer );
kindScroll		= new Array( maxLayer );

// --------------------------------------------------------------------------------------------------
// 機能		：レイヤースクロール移動距離算出
// 引数		：kind			：スクロール種別		0:ワイパー
//			：p				：現在位置
// 			：f				：目標位置
// 戻り値	：0				：目標座標へ到達
// 			：0以外			：移動量
// --------------------------------------------------------------------------------------------------
function amountCalculationOfMovements(kind, p, f)
{
	if(! (f - p)){
		return 0;
	}

	var move;

	switch( kind ){
		case 0:// Wiper
			move = Math.ceil((f - p) >> (kind == 0)? 1 : 4);
			break;
	}

	return (Math.abs(move)? move : f - p);
}

// --------------------------------------------------------------------------------------------------
// 機能		：スクロールの実行（指定間隔でコールバックされる）
// 引数		：無し	
// 戻り値	：無し
// --------------------------------------------------------------------------------------------------
function executionLayerScroll()
{
	var active = 0;
	for(var i = 0; i < maxLayer; i++){
		if(layerName[i] != ""){
			var posX = getLayerLeft( layerName[i] );
			var	posY = getLayerTop( layerName[i] );

			posX += amountCalculationOfMovements(kindScroll[i], posX, forX[i]);
			posY += amountCalculationOfMovements(kindScroll[i], posY, forY[i]);

			setLayerPosition(layerName[i], posX, posY);

			if(posX == forX[i] && posY == forY[i]){
				layerName[i] = "";
			}else{
				active++;
			}
		}
	}

	if(active == 0){
		clearInterval( timerID );
	}
}


	