/* @Packager.Header
<FileDescription>
	Tool object to manage HTML lists items (UL, OL and LI).
</FileDescription>
<Author name="Frederico Caldeira Knabben" email="www.fckeditor.net" />
*/

var FCKListHandler = 
{
	OutdentListItem : function( listItem )
	{
		var eParent = listItem.parentNode ;
		
		// It may happen that a LI is not in a UL or OL (Orphan).
		if ( eParent.tagName.toUpperCase().Equals( 'UL','OL' ) )
		{
			var oDocument = FCKTools.GetElementDocument( listItem ) ;
			
			// All children and successive siblings will be moved to a a DocFrag.
			var eNextSiblings = oDocument.createDocumentFragment() ;
			var eHasLiSibling = false ;

			// If we have nested lists inside it, let's move it to the list of siblings.
			var eChildList = FCKDomTools.GetFirstChild( listItem, ['UL','OL'] ) ;				
			if ( eChildList )
			{
				eHasLiSibling = true ;

				var eChild ;
				while ( eChild = eChildList.firstChild )
					eNextSiblings.appendChild( eChildList.removeChild( eChild ) ) ;
				
				FCKDomTools.RemoveNode( eChildList ) ;
			}
			
			// Move all successive siblings.
			var eSibling ;
			var eHasSuccessiveLiSibling = false ;
			while ( eSibling = listItem.nextSibling )
			{
				if ( !eHasLiSibling && eSibling.nodeType == 1 && eSibling.nodeName.toUpperCase() == 'LI' )
					eHasSuccessiveLiSibling = eHasLiSibling = true ;
				
				eNextSiblings.appendChild( eSibling.parentNode.removeChild( eSibling ) ) ;
				
				// If a sibling is a incorrectly nested UL or OL, consider only its children.
				if ( !eHasSuccessiveLiSibling && eSibling.nodeType == 1 && eSibling.nodeName.toUpperCase().Equals( 'UL','OL' ) )
					FCKDomTools.RemoveNode( eSibling, true ) ;
			}
			
			// If we are in a list chain.
			var sParentParentTag = eParent.parentNode.tagName.toUpperCase() ;
			var bWellNested = ( sParentParentTag == 'LI' ) ;
			if ( bWellNested || sParentParentTag.Equals( 'UL','OL' ) )
			{
				if ( eHasLiSibling )
				{
					var eChildList = eParent.cloneNode( false ) ;
					eChildList.appendChild( eNextSiblings ) ;
					listItem.appendChild( eChildList ) ;
				}
				else if ( bWellNested )
					FCKDomTools.InsertAfterNode( eParent.parentNode, eNextSiblings ) ;
				else
					FCKDomTools.InsertAfterNode( eParent, eNextSiblings ) ;
				
				// Move the LI after its parent.parentNode (the upper LI in the hierarchi).
				if ( bWellNested )
					FCKDomTools.InsertAfterNode( eParent.parentNode, eParent.removeChild( listItem ) ) ;	
				else
					FCKDomTools.InsertAfterNode( eParent, eParent.removeChild( listItem ) ) ;	
			}
			else
			{
				if ( eHasLiSibling )
				{
					var eNextList = eParent.cloneNode( false ) ;
					eNextList.appendChild( eNextSiblings ) ;
					FCKDomTools.InsertAfterNode( eParent, eNextList ) ;
				}
				
				var eBlock = oDocument.createElement( 'p' ) ;	// TODO: Get from configuration.
				eBlock.appendChild( eParent.removeChild( listItem ) ) ;
				FCKDomTools.RemoveNode( listItem, true ) ;

				FCKDomTools.InsertAfterNode( eParent, eBlock ) ;
				
				listItem = eBlock ;
			}

			if ( this.CheckEmptyList( eParent ) )
				FCKDomTools.RemoveNode( eParent, true ) ;
		}
		
		return listItem ;
	},
	
	CheckEmptyList : function( listElement )
	{
		return ( FCKDomTools.GetFirstChild( listElement, 'LI' ) == null ) ;
	},
	
	// Check if the list has contents (excluding nested lists).
	CheckListHasContents : function( listElement )
	{
		var eChildNode = listElement.firstChild ;

		while ( eChildNode )
		{
			switch ( eChildNode.nodeType ) 
			{
				case 1 :
					if ( !eChildNode.nodeName.IEquals( 'UL','LI' ) )
						return true ;
					break ;
		
				case 3 :
					if ( eChildNode.nodeValue.Trim().length > 0 )
						return true ;
			}
			
			eChildNode = eChildNode.nextSibling ;
		}

		return false ;
	}
} ;