// JavaScript Document

function LinkSet( level )
{
	this.level = level;
	this.parent = null;
	this.defaultLink = null;
	this.list = new Array();
}

LinkSet.prototype.add = function( key, value, image )
{
	var listItem = new Link( key, value, image, this );
	if ( typeof value == "object" )
	{
		value.setParent( this );
	}
	this.list[this.list.length] = listItem; 
}

LinkSet.prototype.addShim = function( image )
{
	var listItem = new Shim( image );
	this.list[this.list.length] = listItem;
}

LinkSet.prototype.mark = function()
{
	result = this._mark( window.document.location.href );
	if ( result ) { return; }
	
	//alert( "Could not find link matching href: " + window.document.location.href );
}

LinkSet.prototype.markTab = function( set )
{
	for ( var i = 0; i < set.list.length; i++ )
	{
		var listItem = set.list[i];
		var href = listItem["value"];
		
		var value;
		if ( typeof href == "object" )
		{
			value = href.getDefaultValue();
		}
		else
		{
			value = href;
		}
		
		result = this._mark( value );
		if ( result ) { return; }
	}
	
	alert( "Could not find tab containing href: " + window.document.location.href );
}

LinkSet.prototype._mark = function( href )
{
	for ( var i = 0; i < this.list.length; i++ )
	{
		var listItem = this.list[i];
		var value = listItem["value"];
		
		if ( value == null ) { continue; }
		if ( typeof value == "object" )
		{
			var result = value._mark(href);
			if ( result )
			{
				this.marker = listItem;
				return true;
			}
		}
		else
		{
			var re = new RegExp( value + "$" );
			if ( href.search( re ) >= 0 )
			{
				this.marker = listItem;
				return true;
			}
		}
	}
	
	if ( this.defaultLink != null )
	{
		var re = new RegExp( this.defaultLink["value"] + "$" );
		if ( href.search( re ) >= 0 )
		{
			this.marker = this.defaultLink;
			return true;
		}
	}
	
	return false;
}

LinkSet.prototype.setDefaultValue = function( defaultValue )
{
	this.defaultLink = new Link( null, defaultValue, null, this );
}

LinkSet.prototype.getDefaultValue = function()
{
	if ( this.defaultLink == null )
	{
		return this.list[0]["value"];
	}
	else
	{
		return this.defaultLink["value"];
	}
}

LinkSet.prototype.setParent = function( parent )
{
	this.parent = parent;
}

LinkSet.prototype.getParent = function()
{
	return this.parent;
}

LinkSet.prototype.showItem = function( index )
{
	this._showItem( this.list[index] );
}

LinkSet.prototype._showItem = function( listItem )
{						   
	var key = listItem["key"];
	var value = listItem["value"];
	var button = listItem["button"];
	var mark = ( ( typeof this.marker != "undefined" ) && 
		( key == this.marker["key"] ) ) ? true : false;
	
	if ( value == null )
	{
		var image = listItem["image"];
		document.writeln( "<img border='0' alt='shim' src='" + image + "'>" );
		return;
	}
	
	if ( typeof value == "object" )
	{
		if ( mark )
		{
			if ( button == null )
			{
				// selected text - has child menu
				document.writeln( 
					"<span class='menuLabel_" + this.level + "'>" + key + "</span>" );
			}
			else
			{
				// selected button - has child menu
				document.writeln( button.toDown() );
			}
			value.showMenu();
		}
		else
		{
			if ( button == null )
			{
				// not selected text - has child
				document.writeln(
					"<a href='" + value.getDefaultValue() + "' class='menuLink_" + 
					this.level + "'>" + key + "</a>" );
			}
			else
			{
				// not selected button - has child
				if ( value != null )
				{
					button.setValue( value.getDefaultValue() );
				}
				document.writeln( button.toUp() );
			}
		}
	}
	else
	{
		if ( mark )
		{
			if ( button == null )
			{
				// selected text - no child
				document.writeln( 
					"<span class='menuLabel_" + this.level + "'>" + 
					key + 
					"</span>" );
			}
			else
			{
				// selected button - no child
				document.writeln( button.toDown() );
			}
		}
		else
		{
			if ( button == null )
			{
				// not selected text - no child
				document.writeln(
					"<a href='" + value + "' class='menuLink_" + this.level + "'>" + 
					 key + 
					 "</a>" );
			}
			else
			{
				// not selected button - no child
				document.writeln( button.toUp() );
			}
		}
	}
}

LinkSet.prototype.showMenu = function()
{
	document.write( "<table width='100%' cellpadding='0' cellspacing='0' border='0'>" );
	for( var i = 0; i < this.list.length; i++ )
	{
		document.write( "<tr><td>" );
		var listItem = this.list[i];
		this._showItem( listItem );
		document.write( "</td></tr>" );
	}
	
	document.write( "</table>" );
}

LinkSet.prototype.showNavBar = function()
{
	document.write( "<table width='100%' cellpadding='0' cellspacing='0' border='0'>" );
	document.write( "<tr>" );
	for( var i = 0; i < this.list.length; i++ )
	{
		document.write( "<td>" );
		var listItem = this.list[i];
		this._showItem( listItem );
		document.write( "</td>" );
	}
	
	document.write( "</tr>" );
	document.write( "</table>" );
}

LinkSet.prototype.showSceneList = function()
{
	var d = window.document;
	for ( var i = 0; i < this.list.length; i++ )
	{
		var listItem = this.list[i];
		var key = listItem["key"];
		var mark = ( ( typeof this.marker != "undefined" ) && 
			( key == this.marker["key"] ) ) ? true : false;
		
		d.write( listItem.toMarkup( mark, true ) );
	}
}

LinkSet.prototype.getAll = function()
{
	var result = new Array();
	for ( var i = 0; i < this.list.length; i++ )
	{
		var listItem = this.list[i];
		var key = listItem["key"];
		var value = listItem["value"];
		if ( typeof value == "object" )
		{
			result = result.concat( value.getAll() );
		}
		else
		{
			result = result.concat( value );
		}
	}
	
	if ( this.defaultLink != null )
	{
		result = this.getDefaultValue();
	}
	
	return result;
}

LinkSet.prototype.getUp = function()
{
	if ( this.marker == null )
	{
		return null;
	}
	
	var value = this.marker["value"];
	var result = value._getUp();
	
	return result;
}

LinkSet.prototype._getUp = function()
{
	var value = this.marker["value"];
	if ( typeof value == "object" )
	{
		return value._getUp();
	}
	else
	{
		return this.marker.owner.getParent().getDefaultValue();
	}
}

LinkSet.prototype.hasUp = function()
{
	return ( this.getUp() != null );
}

LinkSet.prototype.goUp = function()
{
	var u = this.getUp();
	if ( u == null )
	{
		alert( "You are at the top page" );
		return false;
	}
	
	window.document.location.href = u;
	return false;	
}

LinkSet.prototype.getPrevious = function()
{
	var index = new LinkSetIndex( this.getAll() );
	return index.getPrevious( window.document.location.href );
}

LinkSet.prototype.hasPrevious = function()
{
	return ( this.getPrevious() != null );
}

LinkSet.prototype.goPrevious = function()
{
	var p = this.getPrevious();
	if ( p == null )
	{
		alert( "You are on the first page." );
		return false;
	}
	
	window.document.location.href = p;
	return false;
}

LinkSet.prototype.getNext = function()
{
	var index = new LinkSetIndex( this.getAll() );
	return index.getNext( window.document.location.href );
}

LinkSet.prototype.hasNext = function()
{
	return ( this.getNext() != null );
}

LinkSet.prototype.goNext = function()
{
	var n = this.getNext();
	if ( n == null )
	{
		alert( "You are on the last page." );
		return false;
	}
	
	window.document.location.href = n;
	return false;
}

LinkSet.prototype.doClose = function()
{
	self.close();
	return true;
}

LinkSet.prototype.dump = function()
{
	var s = "";
	if ( this.level == 0 )
	{
		s += "[level] key <value>\n";
		s += "=====================================\n";
	}
	
	for ( var i = 0; i < this.list.length; i++ )
	{
		var listItem = this.list[i];
		var key = listItem["key"];
		var value = listItem["value"];
		var image = listItem["image"];
		
		if ( value == null )
		{
			s += "[shim]: " + image + "\n";
		}
		else if ( typeof value == "object" )
		{
			s += "[" + this.level + "] " + key + "<" + value.getDefaultValue() + ">" + "\n";
			s += value.dump();
		}
		else
		{
			s += "[" + this.level + "] " + key + "<" + value + ">" + "\n";
		}
	}
	
	return s;
}

LinkSet.prototype.toString = function()
{
	return "[LinkSet object]: level: " + this.level + "; length:" + this.list.length + ";";
}

function LinkSetIndex( list )
{
	this.list = list;
}

LinkSetIndex.prototype.getIndex = function( href )
{
	for ( var i = 0; i < this.list.length; i++ )
	{
		var re = new RegExp( this.list[i] + "$" );
		if ( href.search( re ) > 0 )
		{
			return i;
		}
	}
	
	return -1;
}

LinkSetIndex.prototype.getNext = function( href )
{
	var i = this.getIndex( href );
	return ( i == ( this.list.length - 1 ) ) ? null : this.list[i+1];
}

LinkSetIndex.prototype.getPrevious = function( href )
{
	var i = this.getIndex( href );			
	return ( i == 0 ) ? null : this.list[i-1];
}

function Shim( image )
{
	this.image = image;
}


