Index: /FCKeditor/trunk/_whatsnew.html
===================================================================
--- /FCKeditor/trunk/_whatsnew.html	(revision 3896)
+++ /FCKeditor/trunk/_whatsnew.html	(revision 3897)
@@ -76,4 +76,6 @@
 		<li>[<a target="_blank" href="http://dev.fckeditor.net/ticket/1537">#1537</a>] Fixed extra
 			&lt;p&gt; tag added before pasted contents from Paste From Word dialog.</li>
+		<li>[<a target="_blank" href="http://dev.fckeditor.net/ticket/2874">#2874</a>] Fixed wrong position
+			of caption tag in tables with table headers.</li>
 	</ul>
 	<p>
Index: /FCKeditor/trunk/editor/dialog/fck_table.html
===================================================================
--- /FCKeditor/trunk/editor/dialog/fck_table.html	(revision 3896)
+++ /FCKeditor/trunk/editor/dialog/fck_table.html	(revision 3897)
@@ -156,7 +156,137 @@
 	SetAttribute( table, 'summary'		, GetE('txtSummary').value ) ;
 
+	var headers = GetE('selHeaders').value ;
+	if ( bExists )
+	{
+		// Should we make a <thead>?
+		if ( table.tHead==null && (headers=='row' || headers=='both') )
+		{
+			var oThead = table.createTHead() ;
+			var tbody = FCKDomTools.GetFirstChild( table, 'TBODY' ) ;
+			var theRow= FCKDomTools.GetFirstChild( tbody, 'TR' ) ;
+
+			//now change TD to TH:
+			for (var i = 0; i<theRow.childNodes.length ; i++)
+			{
+				var th = RenameNode(theRow.childNodes[i], 'TH') ;
+				if (th != null)
+					th.scope='col' ;
+			}
+			oThead.appendChild( theRow ) ;
+		}
+
+		if ( table.tHead!==null && !(headers=='row' || headers=='both') )
+		{
+			// Move the row out of the THead and put it in the TBody:
+			var tHead = table.tHead ;
+			var tbody = FCKDomTools.GetFirstChild( table, 'TBODY' ) ;
+
+			var previousFirstRow = tbody.firstChild ;
+			while ( tHead.firstChild )
+			{
+				var theRow = tHead.firstChild ;
+				for (var i = 0; i < theRow.childNodes.length ; i++ )
+				{
+					var newCell = RenameNode( theRow.childNodes[i], 'TD' ) ;
+					if ( newCell != null )
+						newCell.removeAttribute( 'scope' ) ;
+				}
+				tbody.insertBefore( theRow, previousFirstRow ) ;
+			}
+			table.removeChild( tHead ) ;
+		}
+
+		// Should we make all first cells in a row TH?
+		if ( (!hasColumnHeaders)  && (headers=='col' || headers=='both') )
+		{
+			for( var row=0 ; row < table.rows.length ; row++ )
+			{
+				var newCell = RenameNode(table.rows[row].cells[0], 'TH') ;
+				if ( newCell != null )
+					newCell.scope = 'row' ;
+			}
+		}
+
+		// Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
+		if ( (hasColumnHeaders)  && !(headers=='col' || headers=='both') )
+		{
+			for( var row=0 ; row < table.rows.length ; row++ )
+			{
+				var oRow = table.rows[row] ;
+				if ( oRow.parentNode.nodeName == 'TBODY' )
+				{
+					var newCell = RenameNode(oRow.cells[0], 'TD') ;
+					if (newCell != null)
+						newCell.removeAttribute( 'scope' ) ;
+				}
+			}
+		}
+	}
+
+	if (! bExists)
+	{
+		var iRows = GetE('txtRows').value ;
+		var iCols = GetE('txtColumns').value ;
+
+		var startRow = 0 ;
+		// Should we make a <thead> ?
+		if (headers=='row' || headers=='both')
+		{
+			startRow++ ;
+			var oThead = table.createTHead() ;
+			var oRow = table.insertRow(-1) ;
+			oThead.appendChild(oRow);
+
+			for ( var c = 0 ; c < iCols ; c++ )
+			{
+				var oThcell = oDoc.createElement( 'TH' ) ;
+				oThcell.scope = 'col' ;
+				oRow.appendChild( oThcell ) ;
+				if ( oEditor.FCKBrowserInfo.IsGeckoLike )
+					oEditor.FCKTools.AppendBogusBr( oThcell ) ;
+			}
+		}
+
+		// Opera automatically creates a tbody when a thead has been added
+		var oTbody = FCKDomTools.GetFirstChild( table, 'TBODY' ) ;
+		if ( !oTbody )
+		{
+			// make TBODY if it doesn't exist
+			oTbody = oDoc.createElement( 'TBODY' ) ;
+			table.appendChild( oTbody ) ;
+		}
+		for ( var r = startRow ; r < iRows; r++ )
+		{
+			var oRow = oDoc.createElement( 'TR' ) ;
+			oTbody.appendChild(oRow) ;
+
+			var startCol = 0 ;
+			// Is the first column a header?
+			if (headers=='col' || headers=='both')
+			{
+				var oThcell = oDoc.createElement( 'TH' ) ;
+				oThcell.scope = 'row' ;
+				oRow.appendChild( oThcell ) ;
+				if ( oEditor.FCKBrowserInfo.IsGeckoLike )
+					oEditor.FCKTools.AppendBogusBr( oThcell ) ;
+
+				startCol++ ;
+			}
+			for ( var c = startCol ; c < iCols ; c++ )
+			{
+				// IE will leave the TH at the end of the row if we use now oRow.insertCell(-1)
+				var oCell = oDoc.createElement( 'TD' ) ;
+				oRow.appendChild( oCell ) ;
+				if ( oEditor.FCKBrowserInfo.IsGeckoLike )
+					oEditor.FCKTools.AppendBogusBr( oCell ) ;
+			}
+		}
+
+		oEditor.FCK.InsertElement( table ) ;
+	}
+
 	var eCaption = oEditor.FCKDomTools.GetFirstChild( table, 'CAPTION' ) ;
 
-	if ( document.getElementById('txtCaption').value != '')
+	if ( document.getElementById('txtCaption').value != '' )
 	{
 		if ( !eCaption )
@@ -176,134 +306,4 @@
 		else
 			eCaption.parentNode.removeChild( eCaption ) ;
-	}
-
-	var headers = GetE('selHeaders').value ;
-	if ( bExists )
-	{
-		// Should we make a <thead>?
-		if ( table.tHead==null && (headers=='row' || headers=='both') )
-		{
-			var oThead = table.createTHead() ;
-			var tbody = FCKDomTools.GetFirstChild( table, 'TBODY' ) ;
-			var theRow= FCKDomTools.GetFirstChild( tbody, 'TR' ) ;
-
-			//now change TD to TH:
-			for (var i = 0; i<theRow.childNodes.length ; i++)
-			{
-				var th = RenameNode(theRow.childNodes[i], 'TH') ;
-				if (th != null)
-					th.scope='col' ;
-			}
-			oThead.appendChild( theRow ) ;
-		}
-
-		if ( table.tHead!==null && !(headers=='row' || headers=='both') )
-		{
-			// Move the row out of the THead and put it in the TBody:
-			var tHead = table.tHead ;
-			var tbody = FCKDomTools.GetFirstChild( table, 'TBODY' ) ;
-
-			var previousFirstRow = tbody.firstChild ;
-			while ( tHead.firstChild )
-			{
-				var theRow = tHead.firstChild ;
-				for (var i = 0; i < theRow.childNodes.length ; i++ )
-				{
-					var newCell = RenameNode( theRow.childNodes[i], 'TD' ) ;
-					if ( newCell != null )
-						newCell.removeAttribute( 'scope' ) ;
-				}
-				tbody.insertBefore( theRow, previousFirstRow ) ;
-			}
-			table.removeChild( tHead ) ;
-		}
-
-		// Should we make all first cells in a row TH?
-		if ( (!hasColumnHeaders)  && (headers=='col' || headers=='both') )
-		{
-			for( var row=0 ; row < table.rows.length ; row++ )
-			{
-				var newCell = RenameNode(table.rows[row].cells[0], 'TH') ;
-				if ( newCell != null )
-					newCell.scope = 'row' ;
-			}
-		}
-
-		// Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
-		if ( (hasColumnHeaders)  && !(headers=='col' || headers=='both') )
-		{
-			for( var row=0 ; row < table.rows.length ; row++ )
-			{
-				var oRow = table.rows[row] ;
-				if ( oRow.parentNode.nodeName == 'TBODY' )
-				{
-					var newCell = RenameNode(oRow.cells[0], 'TD') ;
-					if (newCell != null)
-						newCell.removeAttribute( 'scope' ) ;
-				}
-			}
-		}
-	}
-
-	if (! bExists)
-	{
-		var iRows = GetE('txtRows').value ;
-		var iCols = GetE('txtColumns').value ;
-
-		var startRow = 0 ;
-		// Should we make a <thead> ?
-		if (headers=='row' || headers=='both')
-		{
-			startRow++ ;
-			var oThead = table.createTHead() ;
-			var oRow = table.insertRow(-1) ;
-			oThead.appendChild(oRow);
-
-			for ( var c = 0 ; c < iCols ; c++ )
-			{
-				var oThcell = oDoc.createElement( 'TH' ) ;
-				oThcell.scope = 'col' ;
-				oRow.appendChild( oThcell ) ;
-				if ( oEditor.FCKBrowserInfo.IsGeckoLike )
-					oEditor.FCKTools.AppendBogusBr( oThcell ) ;
-			}
-		}
-
-		// Opera automatically creates a tbody when a thead has been added
-		var oTbody = FCKDomTools.GetFirstChild( table, 'TBODY' ) ;
-		if ( !oTbody )
-		{
-			// make TBODY if it doesn't exist
-			oTbody = oDoc.createElement( 'TBODY' ) ;
-			table.appendChild( oTbody ) ;
-		}
-		for ( var r = startRow ; r < iRows; r++ )
-		{
-			var oRow = oDoc.createElement( 'TR' ) ;
-			oTbody.appendChild(oRow) ;
-
-			var startCol = 0 ;
-			// Is the first column a header?
-			if (headers=='col' || headers=='both')
-			{
-				var oThcell = oDoc.createElement( 'TH' ) ;
-				oThcell.scope = 'row' ;
-				oRow.appendChild( oThcell ) ;
-				if ( oEditor.FCKBrowserInfo.IsGeckoLike )
-					oEditor.FCKTools.AppendBogusBr( oThcell ) ;
-
-				startCol++ ;
-			}
-			for ( var c = startCol ; c < iCols ; c++ )
-			{
-				// IE will leave the TH at the end of the row if we use now oRow.insertCell(-1)
-				var oCell = oDoc.createElement( 'TD' ) ;
-				oRow.appendChild( oCell ) ;
-				if ( oEditor.FCKBrowserInfo.IsGeckoLike )
-					oEditor.FCKTools.AppendBogusBr( oCell ) ;
-			}
-		}
-
-		oEditor.FCK.InsertElement( table ) ;
 	}
 
