/*
 *  Tree menu engine
 *
 */

// User determinable variables (copy these into your own script, do not edit here!)
var nTopOffset      = 0;
var nLeftOffset     = 0;
var nTopMargin      = 0;
var nLeftMargin     = 0;
var nNodeTopMargin  = 0;
var nNodeLeftMargin = 0;
var bCloseChild     = true;
var oSelected       = null;

// Do not edit below this line!
// -------------------------------------------------------------------------

// Functions
function treeNode( sEmbed, sFirstChildAttach, sNextChildAttach, sHighlight, sLowlight, sImage )
{
	this.myChild                = new Array();
	this.myContainer            = sEmbed;
	this.myName                 = sEmbed.replace( / /g, "_" );
	this.myStatus               = true;
	this.isMaster               = true;
	this.isParent               = true;
	this.myRoot                 = this;
	this.myActive               = null;
	this.pxlLeft                = nLeftOffset;
	this.pxlTop                 = nTopOffset;
	this.pxlHeight              = 0;
	this.pxlWidth               = 0;
	this.myChildTop             = 0;
	this.myChildLeft            = 0;
	this.myTimer                = null;
	this.myHighlight            = false;
	this.myLowlight             = false;
	this.myClickHandler         = false;

	this.getRoot = function()
	{
		return this.isMaster ? this : this.myParent.isMaster ? this.myParent : this.myParent.getRoot();
	}
	this.setLinkHandler = function( sFunction )
	{
		this.myClickHandler = sFunction;
	}
	this.setHighlightClass = function( sClass )
	{
		this.myHighlight = sClass;
	}
	this.setLowlightClass = function( sClass )
	{
		this.myLowlight = sClass;
	}
	this.childPositioning = function( sFirstChild, sNextChild )
	{
		this.myFirstChildAttach    = this.analyseAttachment( sFirstChild );
		this.myOtherChildrenAttach = this.analyseAttachment( sNextChild );
	}
	this.inheritPositioning = function( sFirstChild, sNextChild )
	{
		this.myFirstChildInherit    = this.analyseAttachment( sFirstChild );
		this.myOtherChildrenInherit = this.analyseAttachment( sNextChild );
	}
	this.getFirstChildAttachment = function()
	{
		return !empty( this.myFirstChildInherit ) ? this.myFirstChildInherit : this.myFirstChildAttach;
	}
	this.getOtherChildrenAttachment = function()
	{
		return !empty( this.myOtherChildrenInherit ) ? this.myOtherChildrenInherit : this.myOtherChildrenAttach;
	}
	this.setLink = function( sHref )
	{
		if ( !empty( sHref ) )
			this.myLink = sHref;
		else
			this.myLink = false;
	}
	this.setLowlightImage = function( sImage )
	{
		if ( !empty( sImage ) )
		{
			this.myLowlight     = new Image();
			this.myLowlight.src = sImage;
			return true;
		}
		else
		{
			this.myLowlight = null;
		}
	}
	this.setHighlightImage = function( sImage )
	{
		if ( !empty( sImage ) )
		{
			this.myHighlight     = new Image();
			this.myHighlight.src = sImage;
			return true;
		}
		else
		{
			this.myHighlight = null;
		}
	}
	this.setLowlightClass = function( sClass )
	{
		if ( !empty( sClass ) )
			this.myLowlight = sClass;
		else
			this.myLowlight = null;
	}
	this.setHighlightClass = function( sClass )
	{
		if ( !empty( sClass ) )
			this.myHighlight = sClass;
		else
			this.myHighlight = null;
	}
	this.setLightingModes = function( sLowlight, sHighlight, sImage )
	{
		if ( !empty( sImage ) )
			this.myImage = this.object.document ? this.object.document.images[ sImage ] : document[ sImage ];
		else
			this.myImage = false;

		if ( this.myImage )
		{
			if ( !empty( sLowlight ) )
				this.setLowlightImage( sLowlight );
			else if ( empty( sLowlight ) && !empty( sHighlight ) )
				this.setLowlightImage( this.myImage.src );
			else
				this.myLowlight = null;
		
			if ( !empty( sHighlight ) )
				this.setHighlightImage( sHighlight );
			else
				this.myHighlight = null;
		}
		else
		{
			if ( !empty( sLowlight ) )
				this.setLowlightClass( sLowlight );
			else if ( !empty( sHighlight ) )
				this.setLowlightClass( this.object.className || null );
			else
				this.setLowlightClass( !this.isMaster && this.myParent.myImage ? this.object.className || null : this.isMaster ? null : this.myParent.myLowlight );
		
			if ( !empty( sHighlight ) )
				this.setHighlightClass( sHighlight );
			else
				this.setHighlightClass( !this.isMaster && this.myParent.myImage ? this.object.className || null : this.isMaster ? null : this.myParent.myHighlight );
		}
	}
	this.getParentStatus = function()
	{
		return this.myStatus ? this.isMaster || this.myParent.isMaster ? true : this.myParent.getParentStatus() : false;
	}
	this.analyseAttachment = function( vAttach )
	{
		switch( vAttach.toLowerCase().substring( 0, 1 ) )
		{
			case "t":case "u":case "o":
				return 0;
			case "r":
				return 1;
			case "b":case "d":case "n":
				return 2;
			case "l":
				return 3;
			default:
				alert( "Incorrect attachment argument provided!!!!" );
				break;
		}
	}
	this.repositioning = function( nIndex )
	{
		if ( nIndex < 1 )
		{
			switch ( this.myFirstChildAttach )
			{
				case 0:
					this.myChildLeft = ( this.pxlLeft + nLeftMargin );
					this.myChildTop  = ( this.pxlTop - this.myChild[ nIndex ].pxlHeight ) - nTopMargin + nNodeTopMargin;
					break;
				case 1:
					this.myChildLeft = ( this.pxlLeft + this.pxlWidth ) + nLeftMargin + nNodeLeftMargin;
					this.myChildTop  = this.pxlTop + nTopMargin;
					break;
				case 2:
					this.myChildLeft = this.pxlLeft + nLeftMargin;
					this.myChildTop  = ( this.pxlTop + this.pxlHeight ) + nTopMargin + nNodeTopMargin;
					break;
				case 3:
					this.myChildLeft = ( this.pxlLeft - this.myChild[ nIndex ].pxlWidth ) - nLeftMargin + nNodeLeftMargin;
					this.myChildTop  = this.pxlTop + nTopMargin;
					break;
			}
		}
		else
		{
			switch ( this.myOtherChildrenAttach )
			{
				case 0:
					this.myChildTop -= this.myChild[ nIndex - 1 ].pxlHeight + nNodeTopMargin;
					break;
				case 1:
					this.myChildLeft += this.myChild[ nIndex - 1 ].pxlWidth + nNodeLeftMargin;
					break;
				case 2:
					this.myChildTop += this.myChild[ nIndex - 1 ].pxlHeight + nNodeTopMargin;
					break;
				case 3:
					this.myChildLeft -= this.myChild[ nIndex - 1 ].pxlWidth + nNodeLeftMargin;
					break;
			}
		}
	}
	this.goLink = function()
	{
		if ( this.myLink )
		{
			if ( this.myLinkHandler )
				this.myLinkHandler();
			else
				document.location = this.myLink;
		}
			
	}
	this.toggleChild = function( oChild )
	{
		if ( this.myActive != oChild )
		{
			if ( !empty( this.myActive ) )
				this.myActive.myStatus = false;
			this.myActive = oChild;
		}
	}
	this.closeAll = function()
	{
		for ( var i = 0; i < this.myChild.length; ++i )
			if ( this.myChild[ i ] )
				this.myChild[ i ].myStatus = false;
		toggle( this );
	}
	this.addChild = function( oChild )
	{
		this.myChild[ this.myChild.length ] = this.myChild[ oChild.myName ] = oChild;
		return oChild;
	}
	this.addBranch = function( sLayer, sHref, sFirstChildAttach, sNextChildAttach, sHighlight, sLowlight, sImage )
	{
		return this.addChild( new branchNode( sLayer, this, sHref, sFirstChildAttach, sNextChildAttach, sHighlight, sLowlight, sImage ) );
	}
	this.addLeaf = function( sLayer, sHref, sHighlight, sLowlight, sImage )
	{
		return this.addChild( new leafNode( sLayer, this, sHref, sHighlight, sLowlight, sImage ) );
	}

	this.childPositioning( sFirstChildAttach, sNextChildAttach );
	this.setLightingModes( sLowlight, sHighlight, sImage );

	return this;
}

function branchNode( sLayer, oParent, sHref, sFirstChildAttach, sNextChildAttach, sHighlight, sLowlight, sImage )
{
	this.mom = dhtmlObjectPlus;
	this.mom( sLayer, oParent.myRoot.myContainer );

	// Default properties
	this.myChild                = new Array();
	this.myContainer            = oParent.myContainer;
	this.myName                 = sLayer.replace( / /g, "_" );
	this.myParent               = oParent;
	this.myRoot                 = oParent.myRoot;
	this.myStatus               = false;
	this.myActive               = null;
	this.isMaster               = false;
	this.isParent               = true;
	this.myHighlight            = false;
	this.myLowlight             = false;
	this.myClickHandler         = this.myRoot.myClickHandler;

	// Methods by reference
	this.analyseAttachment          = this.myParent.analyseAttachment;
	this.repositioning              = this.myParent.repositioning;
	this.addChild                   = this.myParent.addChild;
	this.addBranch                  = this.myParent.addBranch;
	this.addLeaf                    = this.myParent.addLeaf;
	this.childPositioning           = this.myParent.childPositioning;
	this.inheritPositioning         = this.myParent.inheritPositioning;
	this.setLowlightImage           = this.myParent.setLowlightImage;
	this.setHighlightImage          = this.myParent.setHighlightImage;
	this.setLowlightClass           = this.myParent.setLowlightClass;
	this.setHighlightClass          = this.myParent.setHighlightClass;
	this.setLightingModes           = this.myParent.setLightingModes;
	this.setLink                    = this.myParent.setLink;
	this.goLink                     = this.myParent.goLink;
	this.getRoot                    = this.myParent.getRoot;
	this.toggleChild                = this.myParent.toggleChild;
	this.getFirstChildAttachment    = this.myParent.getFirstChildAttachment;
	this.getOtherChildrenAttachment = this.myParent.getOtherChildrenAttachment;
	this.getParentStatus            = this.myParent.getParentStatus;

	// Dynamic properties, check provided arguments and set them accordingly
	
	this.setLink( sHref );
	this.setLightingModes( sLowlight, sHighlight, sImage );

	// Inherited arguments
	this.myFirstChildAttach    = !empty( sFirstChildAttach ) ? this.analyseAttachment( sFirstChildAttach ) : this.myParent.getFirstChildAttachment();
	this.myOtherChildrenAttach = !empty( sNextChildAttach ) ? this.analyseAttachment( sNextChildAttach ) : this.myParent.getOtherChildrenAttachment();

	// Methods
	this.doToggle = function()
	{
		if ( !this.myStatus )
			this.myStatus = true;
		this.myParent.toggleChild( this );
		toggle( this.getRoot() );
	}
	this.clearCloseTimer = function()
	{
		clearTimeout( this.myRoot.myTimer );
	}
	this.setCloseTimer = function()
	{
		this.myRoot.myTimer = setTimeout( this.self + ".myRoot.closeAll()", 500 );
	}
	this.hoverOn = function()
	{
		this.clearCloseTimer();
		this.doToggle();
	}
	this.hoverOff = function()
	{
		for ( var i = 0; i < this.myChild.length; ++i )
			this.myChild[ i ].myStatus = false;
		this.setCloseTimer();
	}

	this.addEventHandler( "mousedown", "goLink" );
	this.addEventHandler( "mouseover", "hoverOn" );
	this.addEventHandler( "mouseout", "hoverOff" );

	return this;
}

function leafNode( sLayer, oParent, sHref, sHighlight, sLowlight, sImage )
{
	this.mom = dhtmlObjectPlus;
	this.mom( sLayer, oParent.myRoot.myContainer );

	this.myContainer    = oParent.myContainer;
	this.myName         = sLayer.replace( / /g, "_" );
	this.myParent       = oParent;
	this.myRoot         = oParent.myRoot;
	this.myChild        = false;
	this.isParent       = false;
	this.myHighlight    = false;
	this.myLowlight     = false;
	this.myClickHandler = this.myRoot.myClickHandler;

	// Functions by reference
	this.setLowlightImage    = this.myParent.setLowlightImage;
	this.setHighlightImage   = this.myParent.setHighlightImage;
	this.setLowlightClass    = this.myParent.setLowlightClass;
	this.setHighlightClass   = this.myParent.setHighlightClass;
	this.setLightingModes    = this.myParent.setLightingModes;
	this.setLink             = this.myParent.setLink;
	this.getRoot             = this.myParent.getRoot;
	this.goLink              = this.myParent.goLink;

	this.setLink( sHref );
	this.setLightingModes( sLowlight, sHighlight, sImage )

	this.clearCloseTimer = function()
	{
		clearTimeout( this.myRoot.myTimer );
	}
	this.setCloseTimer = function()
	{
		if ( !empty( this.myLowlight ) )
			this.object.className = this.myLowlight;
		this.myRoot.myTimer = setTimeout( this.self + ".myRoot.closeAll()", 500 );
	}
	this.hoverOn = function()
	{
		// Make sure previous sibbling items with children are being closed.
//		if ( !this.myStatus )
//			this.myStatus = true;
		this.myParent.toggleChild( this );
		toggle( this.getRoot() );

		if ( !empty( this.myHighlight ) )
			this.object.className = this.myHighlight;
		this.clearCloseTimer();
	}
	this.hoverOff = function()
	{
		this.setCloseTimer();
	}

	this.addEventHandler( "mousedown", "goLink" );
	this.addEventHandler( "mouseover", "hoverOn" );
	this.addEventHandler( "mouseout", "hoverOff" );

	return this;
}

function toggle( oToggle )
{
	for ( var i = 0; i < oToggle.myChild.length; ++i )
	{
		mySelf = oToggle.myChild[ i ];
		if ( mySelf.myImage )
			mySelf.myImage.src = mySelf.myStatus ? mySelf.myHighlight.src : mySelf.myLowlight.src;
		if ( bCloseChild && !oToggle.isMaster )
			oToggle.myStatus = oToggle.getParentStatus();

		if ( oToggle.myStatus && ( oToggle.isMaster || oToggle.getParentStatus() ) )
		{
			if ( !oToggle.isMaster && oToggle.myHighlight && !empty( oToggle.myHighlight ) )
				oToggle.object.className = oToggle.myHighlight;

			oToggle.repositioning( i );
			mySelf.move( oToggle.myChildLeft, oToggle.myChildTop );
			mySelf.show();
		}
		else
		{
			if ( !oToggle.isMaster && oToggle.myLowlight && !empty( oToggle.myLowlight ) )
				oToggle.object.className = oToggle.myLowlight;

			mySelf.move( -1000, -1000 );
			mySelf.hide();
		}
		if ( mySelf.myChild );
			arguments.callee( mySelf );
	}
}

function empty( vCheck )
{
	return ( vCheck == "" || vCheck == null );
}