function MenuManager( id )
{
	this.id = id;
	this.layers = new Array();
	this.mapping = null;
	this.homeLayer = null;
	this.lastLink = null;
	this.lastSelectedLink = null;
	this.isReadyLoaded = false;
	this.counter = 0;
	
	this.resetMenuLink = XresetMenuLink;
	this.isReady = XisReady;
	this.setMapping = XsetMapping;
	this.getMapping = XgetMapping;
	this.setLastLink = XsetLastLink;
	this.getLastLink = XgetLastLink;
	this.setLastSelectedLink = XsetLastSelectedLink;
	this.getLastSelectedLink = XgetLastSelectedLink;
	this.addMainLayer = XaddMainLayer;
	this.getMainLayer = XgetMainLayer;
	this.getMainLayerPos = XgetMainLayerPos;
	this.buildManager = XbuildManager;
	this.showLayer = XshowLayer;
	this.hideLayer = XhideLayer;
	this.goToLink = XgoToLink;
	this.getLinkLayer = XgetLinkLayer;
	this.setRolloverColor = XsetRolloverColor;
	return;
}

function XresetMenuLink( )
{		
	if ( this.getLastLink() != null )
	{
		if ( this.getLastLink() != this.getLastSelectedLink() )
		{
			this.goToLink( this.getLastLink() );
			return;
		}
	}
	return;
}

function XgetLastSelectedLink( )
{
	return this.lastSelectedLink
}

function XsetLastSelectedLink( lastSelectedLink )
{
	this.lastSelectedLink = lastSelectedLink;
	return;
}

function XsetRolloverColor( mainLayerId, color, group )
{
	if ( mainLayerId != null)
	{
		if ( typeof( mainLayerId ) != "string" )
		{
			mainLayerId = mainLayerId.id;
		}
		var mainLayer = findLayer( "linkLayer_" + mainLayerId );	
		if ( mainLayer != null )
		{	
			setLayerBackgroundColor( mainLayer, color );			
			var linkLayer = this.getLinkLayer( group );
			if ( linkLayer != null )
			{
				if ( linkLayer.lastSelectedItem != null && linkLayer.lastSelectedItem != mainLayerId )
				{
					this.setRolloverColor( linkLayer.lastSelectedItem, linkLayer.parent.bgColor );
				}
				linkLayer.lastSelectedItem = mainLayerId;
			}
		}
	}
	return;
}

function XisReady()
{
	return this.isReadyLoaded;
}

function XsetLastLink( lastLink )
{
	this.lastLink = lastLink;
	return;
}

function XgetLastLink( )
{
	return this.lastLink
}

function XgetLinkLayer( layerId )
{
	if ( this.layers != null && layerId != null )
	{
		for( i = 0; i < this.layers.length; i++ )
		{
			for( x = 0; x < this.layers[i].items.length; x++ )
			{
				if ( this.layers[i].items[x].id == layerId )
				{
					return this.layers[i].items[x];
				}
			}			
		}
	}
	return null;
} 

function XgoToLink( mappingId )
{
	if ( mappingId != null && mappingId.length > 0 )
	{
		var showLayers = new Array();
		var showLayerLinks = new Array();
		var mappingOriginal = mappingId;
		var pos = 0;
		while( pos > -1 )
		{
			pos = mappingId.lastIndexOf( "_" );
			if ( pos >-1 )
			{
				showLayers[ showLayers.length ] = mappingId.substring(0, pos );
				showLayerLinks[ showLayerLinks.length ] = mappingId.substring( pos+1, mappingId.length );
				mappingId = showLayers[ showLayers.length-1 ];
			}
		}		
		if ( showLayers.length > 0 )
		{
			showLayers.reverse();
			showLayerLinks.reverse();
			this.hideLayer( this.layers[0].id );	
			for( var i=0; i < showLayers.length; i++)
			{	
				var linkLayer = this.getLinkLayer( showLayers[i] );
				if ( linkLayer != null)
				{
					var imageLink = linkLayer.getLink( showLayerLinks[i] );
					if ( imageLink != null )
					{	
						this.setLastLink( mappingOriginal );
						this.setLastSelectedLink( mappingOriginal );
						this.showLayer( showLayers[i] );
						this.setRolloverColor( this.layers[i].id + "_" + showLayers[i] + "_" + showLayerLinks[i], this.layers[i].highlighColor, showLayers[i] );
					}							
				}
			}
		}			
	}
	else
	{
		this.setLastLink( null );
		this.showLayer( this.layers[0].items[0].id );
	}
	return;
}

function XsetMapping( mapping )
{
	this.mapping = mapping;
	return;
}

function XgetMapping( )
{
	return this.mapping;
}

function XaddMainLayer( mainLayer )
	{
 	if ( mainLayer != null )
 	{
		mainLayer.parent = this;
		mainLayer.pos = this.layers.length;
 		this.layers[ this.layers.length ] = mainLayer;		
	}
	return;
}

function XgetMainLayer( id )
{
	var pos = this.getMainLayerPos( id );
	if ( pos > -1 )
	{
		return this.layers[pos];
	}
 	return null;
}

function XgetMainLayerPos( id )
{
	if ( id && id != null )
	{
		for( var i=0; i < this.layers.length; i++ )
		{
 			if ( this.layers[i].id == id )
 			{
				return i;
  			}
 		}
	}
 	return -1;
}

function XbuildManager( editMode )
{
	var displayLayer = null;
	var counter = 0;
	for( var i=0; i < this.layers.length; i++ )
	{
 		displayLayer = findLayer( this.layers[i].id );
		if ( displayLayer != null )
		{
			var items = this.layers[i].items;
			var layerSource = '';
			if ( items != null )
			{
				for( var x=0; x < items.length ; x++ )
				{
					var links = items[x].links;
					var itemSource = "";
					if ( links != null )
					{
						var itemPos = 0;
						for( var y=0; y < links.length ; y++ )
						{
							links[y].mainLayerPos = i;
							links[y].itemLayerPos = x;
							links[y].itemPos = y;
							if ( typeof( links[y] ) == "string" )
							{
								itemSource += links[y];
								counter++;
								continue;
							}
							itemPos  = items[x].itemSize * y;
							var linkSource = "<div id=\"linkLayer_" + this.layers[i].id + "_" + items[x].id + "_" + links[y].id + "\" class=\"menuLayerItemStyle\"><table cellpadding='0' cellspacing='0' border='0'><tr><td nowrap>";		
							var filler = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
							if ( links[y].childMenuId != false )
							{
								var action = "javascript:" + this.id +".setLastLink( '" + items[x].id + "_" + links[y].id  + "' ); ";
								var mappedLink = this.getMapping( ).getMappedLink( items[x].id, links[y].id );								
								if ( mappedLink != null && mappedLink.action != null  )
								{
									action += "putHtmlIntoFrame('" + mappedLink.action + "'";
									if ( mappedLink.target )
									{
										action += ", '" + mappedLink.target + "'";
									}	
									action += "); ";
									mappedLink.mainLayerPos = i;
									mappedLink.itemLayerPos = x;
									mappedLink.itemPos = y;
								}							
								linkSource += "<a href=\"" + action ;
								if ( this.layers[i].mainStyle )
								{
									linkSource += "\" class=\"" + this.layers[i].mainStyle;
								}
								linkSource += "\" onmouseover=\"" + this.id + ".setRolloverColor( '"+ this.layers[i].id + "_" + items[x].id + "_" + links[y].id + "', '" + this.layers[i].highlighColor + "', '" + items[x].id + "' ); ";
								linkSource += " " + this.id +".setLastSelectedLink( '" + items[x].id + "_" + links[y].id  + "' ); ";
								linkSource += this.id +".hideLayer( '" + this.layers[i].id + "' ); ";
								if ( links[y].childMenuId != null )
								{
									if ( typeof( links[y].childMenuId ) == 'string' )
									{
										linkSource += this.id +".showLayer('" + links[y].childMenuId + "' ); ";
									}
									else
									{
										linkSource += this.id +".showLayer( '" + links[y].childMenuId.id + "' ); ";
									}
								}
								linkSource += "\">";
								linkSource += links[y].text + filler;
								linkSource += "</a>";
							}
							else
							{
								if ( this.layers[i].mainStyle )
								{
									linkSource += "<font class=\"" + this.layers[i].mainStyle + "\">";
									linkSource += links[y].text + filler ;
									linkSource += "</font>";
								}
								else
								{
									linkSource += links[y].text + filler;
								}
							}	
							linkSource += "</td></tr></table></div>";
							itemSource += linkSource;
							counter++;
							continue;
						}
					}
					layerSource += "<div id='itemLayer_" + items[x].id + "' class='menuLayerStyle'>" + itemSource + "</div>";
				}
			}
			if ( layerSource.length > 0 )
			{
				writeLayer( displayLayer, layerSource );
			}
		}
 	}
	this.isReadyLoaded = true;
	if ( this.homeLayer )
	{
		this.showLayer( this.homeLayer.id );
	}
	else
	{
		this.showLayer( this.layers[0].items[0].id );
	}
	return;
}

function XshowLayer( id )
{
	if ( id != null )
	{	
		var layer = findLayer( 'itemLayer_' + id );	
		if ( layer != null )
		{
			var linkLayer = this.getLinkLayer( id );
			if ( linkLayer != null && linkLayer.parent != null )
			{
				if ( linkLayer.parent.lastVisibleLayer != null )
				{
					setLayerVisible( linkLayer.parent.lastVisibleLayer, false);
				}
				linkLayer.parent.lastVisibleLayer = layer;
			}
			setLayerVisible( layer, true);
		}
	}
	return;
}

function XhideLayer( id )
{
	if ( id != null )
	{
		var pos = this.getMainLayerPos( id );
		if ( pos != null && pos > -1 && ++pos < this.layers.length )
		{
			for( var i = pos; i < this.layers.length; i++ )
			{				
				if ( this.layers[i].lastVisibleLayer != null )
				{				
					setLayerVisible( this.layers[i].lastVisibleLayer, false);
					this.layers[i].lastVisibleLayer = null;
				}
			}
		}
	}
	return;
}

function MainLayer( id, mainStyle, highlighColor, bgColor )
{
	this.id = id;
	this.parent = null;
	this.mainStyle = mainStyle;
	this.highlighColor = highlighColor;
	this.lastVisibleLayer = null;
	this.pos = -1;
	this.items = new Array();
	this.counter = 0;
	this.bgColor = bgColor;
	
	this.getId = XgetId;
	this.addItemLayer = XaddItemLayer;
	this.getItemLayer = XgetItemLayer;
	return;
}

function XgetId()
{
	return this.id;
}

function XaddItemLayer( itemLayer )
{
 	if ( itemLayer && itemLayer != null )
 	{
		if ( this.parent.homeLayer  == null )
		{
			this.parent.homeLayer = itemLayer;
		}
		itemLayer.parent = this;
		itemLayer.pos = this.items.length;
		this.items[this.items.length] = itemLayer;
		this.counter += itemLayer.counter;
		this.parent.counter += itemLayer.counter;
	}
	return;
}

function XgetItemLayer( id )
{
	if ( id && id != null )
	{
		for( var i=0; i < this.items.length; i++ )
		{
 			if ( this.items[i].id == id )
 			{
				return this.items[i];
  			}
 		}
	}
 	return null;
}

function ItemLayer( id, itemSize )
{
	this.id = id;
	this.itemSize = itemSize;
	this.parent = null;
	this.links = new Array();
	this.pos = -1;
	this.counter = 0;
	this.lastSelectedItem = null;
	
	this.addTextLink = XaddTextLink;
	this.addNewTextLink = XaddNewTextLink;
	this.addSource = XaddSource;	
	this.getLink = XgetLink;
	return;
}

function XaddSource( source )
{
	if ( source != null )
 	{
		this.addTextLink( source );
	}
	return;
}

function XgetLink( id )
{
	if ( id && id != null )
	{
		for( var i=0; i < this.links.length; i++ )
		{
 			if ( this.links[i].id == id )
 			{
				return this.links[i];
  			}
 		}
	}
 	return null;
}

function XaddTextLink( link )
{
 	if ( link && link != null )
 	{
		link.parent = this;
		this.links[this.links.length] = link;
		this.counter++;
	}
	return;
}

function XaddNewTextLink( id, text, childMenuId )
{
 	if ( id != null )
 	{
		this.addTextLink( new MenuTextLink( id, text, childMenuId ) );
	}
	return;
}

function MenuTextLink( id, text, childMenuId )
{
	this.id = id;
	this.parent = null;
	this.mainLayerPos = -1;
	this.itemLayerPos = -1;
	this.itemPos = -1;
	this.text = text;
	this.childMenuId = childMenuId;
	return;
}

function Mapping( action, target )
{
	this.action = action;
	this.mainLayerPos = -1;
	this.itemLayerPos = -1;
	this.itemPos = -1;
	this.target = target;
	return;
}

function XgetMappedLink( mappingPrefix, mappingSuffix )
{
	if ( mappingPrefix != null )
	{
		var retValue = null;
		if ( mappingSuffix != null && mappingSuffix.length > 0 )
		{
			eval( "retValue = this." + mappingPrefix + "_" + mappingSuffix + ";" );	
		}
		else
		{
			eval( "retValue = this." + mappingPrefix + ";" );	
		}
		return retValue;
	}
	return null;
}
