//----------------------------//
// Layer Positioning Script   //
// Version 1.0.02             //
// (c) 2002, Nicolas Stohler, //
// insign gmbh                //
//----------------------------//
var g_bDebugMsgs	= true;
var g_bDocLoaded 	= false;
var g_bReloadNS4	= false;
var g_bIsOpera		= typeof window.opera != "undefined"
var g_nOperaTimer	= 500;
var aLayerPosition 	= new Array();

// include the following lines into the html-file header:
/*
	<script src="laypos.js" name="layer positioning script"></script>
	<script name="layer positioning configuration"><!--
		window.onload	= OnLoadReposition;
		window.onresize = OnResizeReposition;
		
		LinkLayerToPic( "MyLayer", "PosPic" );
		LinkLayerToPic( "SecondFloater", "BluePosPic" );
		
		SetNS4PageReload( false );
	//-->
	</script>
*/	

// Class 
function CLayerPosition( sLayerName_, sPosPicName_, nOffsetX_, nOffsetY_ )
{
	this.sLayerName 	= sLayerName_;
	this.sPosPicName	= sPosPicName_;
	this.nOffsetX		= (typeof nOffsetX_ != "undefined")?nOffsetX_:0;
	this.nOffsetY		= (typeof nOffsetY_ != "undefined")?nOffsetY_:0;
	this.toString		= function()	
	{ 
		return "Layer\t= \t"+this.sLayerName
				+"\nPosPic\t= \t"+this.sPosPicName
				+"\nOffsetX\t= \t"+this.nOffsetX
				+"\nOffsetY\t= \t"+this.nOffsetY;
	}
}

function OnLoadReposition()				{	RepositionLayers(g_bDocLoaded);g_bDocLoaded = true;	}
function OnResizeReposition()			{	RepositionLayers(g_bDocLoaded);	}
function SetNS4PageReload( bReload_ )	{	g_bReloadNS4 = bReload_;	}

function LinkLayerToPic( sLayerName_, sPosPicName_, nOffsetX_, nOffsetY_ )
{
	nOffsetX_ = (typeof nOffsetX_ != "undefined")?nOffsetX_:0;
	nOffsetY_ = (typeof nOffsetY_ != "undefined")?nOffsetY_:0;
	aLayerPosition[aLayerPosition.length] = new CLayerPosition( sLayerName_, sPosPicName_, nOffsetX_, nOffsetY_ );
}

function RepositionLayers( bDocLoaded_ )
{
	var bMsgShown = false;
	for( var i = 0; i < aLayerPosition.length; i++ )
	{
		var sLayerName 	= aLayerPosition[i].sLayerName;
		var sPosPicName	= aLayerPosition[i].sPosPicName;
		var nOffsetX	= aLayerPosition[i].nOffsetX;
		var nOffsetY	= aLayerPosition[i].nOffsetY;
		
		if( typeof document.getElementById != "undefined" )
		{
			// set the PosPic id to the name (needed by NS6)
			var aImg = document.getElementsByTagName("img");
			if( typeof aImg[sPosPicName] != "undefined" ){aImg[sPosPicName].id=sPosPicName;}

			var oLayer 	= document.getElementById(sLayerName);	VERIFY( oLayer, "GetElementById::Layer" );
			var oPosPic = document.getElementById(sPosPicName);	VERIFY( oPosPic, "GetElementById::PosPic" );
		
			// Calculate full offset
			var sFullOffsetLeft 	= oPosPic.offsetLeft;
			var sFullOffsetTop 		= oPosPic.offsetTop;
			var oPosParent 			= oPosPic.offsetParent;
			while( typeof oPosParent != "undefined" && oPosParent != null )
			{
				sFullOffsetLeft 	+= oPosParent.offsetLeft;
				sFullOffsetTop		+= oPosParent.offsetTop;
				oPosParent 			= oPosParent.offsetParent;
			}
			
			// Position the layer
			oLayer.style.left 	= sFullOffsetLeft + nOffsetX;
			oLayer.style.top 	= sFullOffsetTop + nOffsetY;
			
			// For Opera: no onresize event available, so keep resizing stuff
			if( g_bIsOpera )	{	setTimeout( "RepositionLayers(true)", g_nOperaTimer );	}
		}
		else 											// browser does not support getElementById, ...
		if( typeof document.layers != "undefined" ) 	// ... so maybe it supports document.layers...
		{
			if( g_bReloadNS4 && bDocLoaded_ )
			{
				// cheap, but working:
				this.document.location.reload();
				return;
			}
			else
			{
				// this code partially works, but Netscape 4.x tends to resize the layer or 
				// change the content layout when respositioning the layer
				var oLayer 	= document.layers[sLayerName];	VERIFY( oLayer, "doc.layers::Layer" );
				var oPosPic = document.images[sPosPicName];	VERIFY( oPosPic, "doc.layers::PosPic" );
	
				// Position the layer
				oLayer.left = oPosPic.x + nOffsetX;
				oLayer.top 	= oPosPic.y + nOffsetY;
			}
		}
		else 											// browser seems not to support repositioning at all!
		{
			// nothing to do
			if( g_bDebugMsgs && !bMsgShown )
			{	
				alert( "This browser seems not to support repositioning at all!" );
				bMsgShown = true;
			}
		}
	}
}

function VERIFY( oObj_, sCallId_ )
{
	if( typeof oObj_ != "undefined" && oObj_ != null )
	{	return true;	}
	else if( g_bDebugMsgs )
	{	alert( "VERIFY failed!" + ((typeof sCallId_ != "undefined")?"\n\n"+sCallId_:""));	}
	return false;
}

// eof
