var totopDiv;
var sideDiv;
var viewPort;
var FOOTER_Y;
var CONT_Y;

function initMain()
{
	replaceAnchor();
	
	locateTotop2Bottom();
}

function locateTotop2Bottom()
{
	totopDiv	= document.getElementById( 'ttp' );
	sideDiv		= document.getElementById( 'side' );
	
	FOOTER_Y	= getAbsolutePos( document.getElementById( 'footer' ) ).y;			// 8px埋まってるし
	CONT_Y		= getAbsolutePos( document.getElementById( 'side' ) ).y;
	
	//
	// for IE6
	//
	if( IE && document.compatMode == "BackCompat" )
	{
	//	document.body.onscroll = onScrollEvent;
	}
	//
	// for Other Browser
	//
	else
	{
		document.onscroll = onScrollEvent;
		document.documentElement.onscroll = onScrollEvent;
	}
	
	onScrollEvent();
}

function onScrollEvent()
{
	viewPort = getViewportArea();
	
	//
	// toTopの固定
	//
	if ( totopDiv )
	{
		if ( ( viewPort.h + viewPort.y ) > FOOTER_Y )
		{
			totopDiv.style.position = "absolute";
			totopDiv.style.top = "0px";
			totopDiv.style.bottom = null;
		}
		else
		{
			totopDiv.style.position = "fixed";
			totopDiv.style.top = "100%";
			totopDiv.style.bottom = "0px";
		}
	}
	
	//
	// サイドメニューの固定
	//
	if ( sideDiv )
	{
		if ( viewPort.y < ( CONT_Y - 30 ))
		{
			sideDiv.style.position = "relative";
			sideDiv.style.top = null;
		}
		else if ( ( viewPort.y + sideDiv.offsetHeight + 30 ) > FOOTER_Y - 40 )
		{
			sideDiv.style.position = "relative";
			sideDiv.style.top = ( FOOTER_Y - sideDiv.offsetHeight - 40 - CONT_Y ) + "px";
		}
		else
		{
			sideDiv.style.position = "fixed";
			sideDiv.style.top = "44px";			// 30px + 14px (marginTop)
		}
	}
}

addOnloadEvent( initMain );

