//------------------------------------------------------------------------------
//
// ADOBE SYSTEMS INCORPORATED
// Copyright 2003 Adobe Systems Incorporated 
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute 
// this file in accordance with the terms of the Adobe license 
// agreement accompanying it. If you have received this file 
// from a source other than Adobe, then your use, modification, 
// or distribution of it requires the prior written permission 
// of Adobe.
//
//------------------------------------------------------------------------------

function RTFGroup( parent )
{
	this.parentGroup	= parent;
	this.entries		= new Array;
	this.state			= 'open';
	this.type			= '';
	
	// debug
	this.uniqueID		= null;
	
	if( module.debug )
		this.uniqueID	= new Date;
}

RTFGroup.prototype.addGroup = function( subGroup )
{
	this.entries.push( subGroup );
}

RTFGroup.prototype.addCtrlSymbol = function( character )
{
	if( character == '*' )
	{
		this.type = 'destination';
	}
	else
	{
		var entry = new RTFEntry( 'ctrlSymbol', character, this );
		this.entries.push( entry );
	}
}

RTFGroup.prototype.addCtrlWord = function( word )
{
	var entry = new RTFEntry( 'ctrlWord', word, this );
	var count = this.entries.push( entry );
	
	if( count == 1 && this.type == '' )
		this.setGroupType( entry.name );
}

RTFGroup.prototype.addCharacter = function( character )
{
	var lastEntry = this.entries[ this.entries.length - 1 ];
	
	if( lastEntry != undefined && lastEntry.constructor.name == 'RTFEntry' && lastEntry.type == 'character' )
	{
		lastEntry.addCharacter( character );
	}
	else
	{
		var entry = new RTFEntry( 'character', character, this );
		this.entries.push( entry );
	}
}

RTFGroup.prototype.close = function()
{
	var ret = false;
	
	if( this.state == 'open' )
	{
		this.state = 'closed';
		
		switch( this.type )
		{
			case 'font':
			case 'fonttable':
			{
				for( var i=0; i<this.entries.length; i++ )
				{
					var entry = this.entries[i];
					
					if( entry.constructor.name == 'RTFEntry' && entry.type == 'character' )
					{
						entry.type = 'name';
						
						if( entry.value[ entry.value.length-1 ] == ';' )
							entry.value = entry.value.substr(0, entry.value.length-1);
							
						entry.name = entry.value;
						entry.value = '';
					}
				}
			}
			break;
			
			case 'colortable':
			{
				for( var i=0; i<this.entries.length; i++ )
				{
					if( this.entries[i].type == 'character' && this.entries[i].name == ';' )
						this.entries.splice( i, 1 );
				}
			}
			break;
		}
	}
	
	return ret;
}

RTFGroup.prototype.setGroupType = function( chars )
{
	for( var i=0; i<rtfGroups.length && this.type == ''; i++ )
	{
		if( chars == rtfGroups[i][0] )
		{
			this.type = rtfGroups[i][1];
			
			switch( this.type )
			{
				case 'font':
				{
					if( this.parentGroup.type != 'fonttable' )
						this.type = '';
				}
				break;
			}
		}
	}
}