
var allLoaded = false ;
var entry = new Array( ) ;

function init( )
{
	if ( versionCheck( ) )
	{
		DropDownMenu.BLURSIZE = -5 ;
		DropDownMenu.ACCELERATION = 11 ;
	//	DropDownMenu.PARENT = refLayer( 'parentStage' ) ;
		DropDownMenu.CHILD    = refLayer( 'childStage' ) ;

		DropDownMenu.index = 0 ;
		allLoaded = true ;

		entry[ 0 ] = new DropDownMenu( 'layermenu01', 1 ) ;
		entry[ 1 ] = new DropDownMenu( 'layermenu02', 1 ) ;
		entry[ 2 ] = new DropDownMenu( 'layermenu03', 1 ) ;
		entry[ 3 ] = new DropDownMenu( 'layermenu04', 1 ) ;
		entry[ 4 ] = new DropDownMenu( 'layermenu05', 1 ) ;

		setInterval( 'process( )', 50 ) ;
	
		for ( var i=0; i<entry.length; i++ )
		{
			entry[ i ].child.onmouseover = new Function("entry[" +i+ "].isMouseOver=true") ;
			entry[ i ].child.onmouseout  = new Function("entry[" +i+ "].upHandler( )") ;
		}
	}
}

function versionCheck( )
{
	var flag = ( ( NN4 && getBrowserVersion( ).substr( 0, 3 ).toString( ) != '4.0' ) || IE4 || IE5 || NN6 ) ? true : false ;

	return flag ;
}

function Size( id )
{
	this.width = getLayerSize( id, 'width' ) ;
	this.height = getLayerSize( id, 'height' ) ;
}

function Position( id )
{
	this.x = getLayerPosition( id, 'left' ) ;
	this.y = getLayerPosition( id, 'top' ) ;
}

function Vector( id, x, y )
{
	this.x = x ;
	this.y = y * -1 ;
}

function DropDownMenu( layerName, step )
{
	this.name	= layerName ;
	this.child		= refLayer( layerName ) ;
//	this.id		= refLayer( layerName ) ;
//	this.child		= refLayer( layerName + 'Child' ) ;

	this.index = DropDownMenu.index++ ;

	// stop	未処理
	// down	下り
	// blur	跳ね返り
	// up		上り
	this.mode = 'stop' ;

	// マウスがロールオーバーしているかを判別するフラグ
	this.isMouseOver= false ;

	this.action = null ;

//	initLayer( this.id ) ;
	initLayer( this.child ) ;

	this.size = new Size( this.child ) ;
	this.position = new Position( this.child ) ;
	this.vector = new Vector( this.child, 0, this.size.height ) ;

	this.defaultStep = step ;
	this.step = step ;
}

DropDownMenu.prototype.motion = motion_ ;
DropDownMenu.prototype.down = down_ ;
DropDownMenu.prototype.up = up_ ;
DropDownMenu.prototype.downHandler = downHandler_ ;
DropDownMenu.prototype.upHandler = upHandler_ ;

function process( )
{
	for ( var i in entry )
	{
		if ( entry[ i ].mode != 'stop' )
		{
			entry[ i ].motion( ) ;
		}
	}
}

function motion_( )
{
	if ( this.mode == 'down' || this.mode == 'blur' )
	{
		this.down( ) ;
	}
	else if ( !this.action && !this.isMouseOver && this.mode == 'up' )
	{
		this.up( ) ;
	}
}

function down_( )
{
	if ( this.mode == 'blur' && this.vector.y == DropDownMenu.BLURSIZE )
	{
		shiftLayerTo( this.child, this.position.x, -20 ) ;
		this.vector.y = 0 ;

		this.mode = 'up' ;

		if ( !this.isMouseOver )
		{
			this.up( ) ;
		}
	}
	else if ( this.vector.y + this.step < DropDownMenu.BLURSIZE )
	{
		this.vector.y += this.step ;
		this.step += DropDownMenu.ACCELERATION ;
		shiftLayerTo( this.child, this.position.x, this.vector.y ) ;
	}
	else
	{
		shiftLayerTo( this.child, this.position.x, DropDownMenu.BLURSIZE ) ;
		this.vector.y = DropDownMenu.BLURSIZE ;

		this.mode = 'blur' ;
	}
}

function up_( )
{
	// MAC + IE4.X のバグ(※)に対処するため、メニュー部分の立て幅分のスペース(19pixel)を加えておく
	// ※overflow:hidden で消えているはずのレイヤーが、マウスのイベントを感知してしまう現象。
	// 従って、そのレイヤーの下にあるリンク等が機能しなくなってしまう
	if ( this.vector.y - this.step > -this.size.height-19 )
	{
		shiftLayerBy( this.child, 0, -this.step ) ;
		this.vector.y -= this.step ;
		this.step += DropDownMenu.ACCELERATION * 2 ;
	}
	else
	{
		shiftLayerTo( this.child, this.position.x, -this.size.height-19 ) ;
		this.vector.y = -this.size.height ;

		this.step = this.defaultStep ;

		this.mode = 'stop' ;
		hideRequest( this.layerName ) ;

		return ;
	}
}

function downHandler_( )
{
	this.isMouseOver = true ;

	if ( this.mode != 'up' )
	{
		showLayer( DropDownMenu.CHILD ) ;
		this.mode = 'down' ;
	}
	else
	{
		clearTimeout( this.action ) ;
		this.action = null ;
	}
}

function upHandler_( )
{
	this.isMouseOver= false ;
	this.action = setTimeout( "entry[ " +this.index+ " ].action=null", 100 ) ;
}

function hideRequest( src )
{
	var hideOk = true ;

	if ( ( IE4 || IE5 ) && !!src )
	{
		if ( document.all.tags("div")[ src ].contains( event.toElement ) )
		{
			return ;
		}
	}

	for ( var i in entry )
	{
		if ( entry[ i ].isMouseOver && entry[ i ].mode != 'down' )
		{
			hideOk = false ;
		}
	}

	if ( hideOk )
	{
		hideLayer( DropDownMenu.CHILD ) ;
	}
}


var lastScrollY = new Number( 0 ) ;

function controllerStaticPosition( )
{
	var diffY = new Number( getPageOffset( 'top' ) ) ;
	var percent = new Number( ) ;

	if( diffY != lastScrollY )
	{
		percent = .1 * ( diffY - lastScrollY ) ;

		if( percent > 0 )
		{
			percent = Math.ceil( percent ) ;
		}
		else
		{
			percent = Math.floor( percent ) ;
		}

		shiftLayerBy( DropDownMenu.PARENT, 0, percent ) ;
		shiftLayerBy( DropDownMenu.CHILD, 0, percent ) ;

		lastScrollY += percent ;
	}
}

/*
		function controllerStaticPosition( )
		{
			diffY = getPageOffset( 'top' )

			shiftLayerTo( refLayer( 'stage' ), 0, diffY ) ;

		}
*/

function funcBuffer( )
{
	var jsCode = funcBuffer.arguments ;

	if ( allLoaded && versionCheck( ) )
	{
		for ( var i=0; i<jsCode.length; i++ )
		{
			eval( jsCode[ i ] ) ;
		}
	}
}

function resizeHandler( )
{
	if ( NN4 && versionCheck( ) )
	{
		location.reload( ) ;
		return false ;
	}
}

function setNSEventCapture( )
{
	document.captureEvents( Event.MOUSEUP ) ;

	if ( NN4 && versionCheck( ) )
	{
		document.captureEvents( Event.RESIZE ) ;
	}
}

window.onload    = init ;
window.onresize = resizeHandler ;

