// menu.js - Mikael Simonsson 010703
//
// DHtml Menu
// Tested in: Opera 5.11, IE 4 & 5 & 6, Netscape 4 & 6

var g_iObjCount;
var g_aLayers;

var g_bOldNs;
var g_bOldIe;
var g_bNewerBrowser;

var g_imgPlus, g_imgMinus;

// initMenu()
// Initializes the menu
function initMenu()
{
	g_iObjCount = 0

	if ( document.images )
	{
		g_imgPlus = new Image();
		g_imgPlus.src = '/_/i/ico/plus.gif';
		g_imgMinus = new Image();
		g_imgMinus.src = '/_/i/ico/minus.gif';
	}
	
	g_bNewerBrowser = (document.getElementById) ? true : false;
	g_bOldNs = (document.layers) ? true : false;
	g_bOldIe = (document.all) ? true : false;
}

// layerObj(iTop, iHeight)
// Creates layer object
// NO BROWSER SPECIFIC CODE HERE!
function layerObj(iTop, iHeight, bVisible)
{
	this.iID_ = g_iObjCount;
	this.bVisible_ = bVisible;
	this.iTop_ = iTop;
	this.iHeight_ = iHeight;
	this.switchState = switchState;
	this.updateCss = updateCss;

	g_iObjCount++; // Increase global object counter
}

// switchState()
// INTERNAL: Turns visibility on/off and updates menu positions.
// NO BROWSER SPECIFIC CODE HERE!
function switchState()
{
	if ( this.bVisible_ == true )
	{
		this.bVisible_ = false;
		this.updateCss();
		
		for ( var i = this.iID_ + 1 ; i < g_iObjCount ; ++i )
		{
			g_aLayers[i].iTop_ -= this.iHeight_;
			g_aLayers[i].updateCss();
		}
		
		if ( document.images )
		{
			setPlus( this.iID_ );
		}
	}
	else
	{
		this.bVisible_ = true;
		this.updateCss();
		
		for ( var i = this.iID_ + 1 ; i < g_iObjCount ; ++i )
		{
			g_aLayers[i].iTop_ += this.iHeight_;
			g_aLayers[i].updateCss();
		}
		
		if ( document.images )
		{
			setMinus( this.iID_ );
		}
	}
}

// updateCss()
// INTERNAL: Sets needed css properties.
function updateCss()
{
	if ( g_bNewerBrowser )
	{
		// Set visibility
		if ( this.bVisible_ )
			document.getElementById('mBox' + this.iID_).style.visibility = 'visible';
		else
			document.getElementById('mBox' + this.iID_).style.visibility = 'hidden';
	
		// Set top
		document.getElementById('mBox' + this.iID_).style.top = this.iTop_;
	}
	else if ( g_bOldNs )
	{
		// Set visibility
		if ( this.bVisible_ )
			document.layers['mBox' + this.iID_].visibility = 'show';
		else
			document.layers['mBox' + this.iID_].visibility = 'hide';
	
		// Set top
		document.layers['mBox' + this.iID_].top = this.iTop_;
	}
	else if ( g_bOldIe )
	{
		// Set visibility
		if ( this.bVisible_ )
			document.all['mBox' + this.iID_].style.visibility = 'visible';
		else
			document.all['mBox' + this.iID_].style.visibility = 'hidden';
	
		// Set top
		document.all['mBox' + this.iID_].style.top = this.iTop_;
	}
}

// setPlus(iID)
function setPlus(iID)
{
	if ( g_bNewerBrowser )
	{
		document.images['mImg' + iID].src = g_imgPlus.src;
	}
	else if ( g_bOldNs )
	{
		document.layers['mBox' + (iID - 1)].document.images['mImg' + iID].src = g_imgPlus.src;
	}
	else if ( g_bOldIe )
	{
		document.images['mImg' + iID].src = g_imgPlus.src;
	}
}

// setMinus(iID)
function setMinus(iID)
{
	if ( g_bNewerBrowser )
	{
		document.images['mImg' + iID].src = g_imgMinus.src;
	}
	else if ( g_bOldNs )
	{
		document.layers['mBox' + (iID - 1)].document.images['mImg' + iID].src = g_imgMinus.src;
	}
	else if ( g_bOldIe )
	{
		document.images['mImg' + iID].src = g_imgMinus.src;
	}
}

// goLayer(iID)
// Calls switchState on object if valid id.
function goLayer(iID)
{
	if ( iID > -1 && iID < g_iObjCount && g_iObjCount > 0 )
	{
		g_aLayers[iID].switchState();
	}
}
