// Copyright (C) 1997-2006 Autodesk, Inc., and/or its licensors.
// All rights reserved.
//
// The coded instructions, statements, computer programs, and/or related
// material (collectively the "Data") in these files contain unpublished
// information proprietary to Autodesk, Inc. ("Autodesk") and/or its licensors,
// which is protected by U.S. and Canadian federal copyright law and by
// international treaties.
//
// The Data is provided for use exclusively by You. You have the right to use,
// modify, and incorporate this Data into other products for purposes authorized 
// by the Autodesk software license agreement, without fee.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. AUTODESK
// DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTIES
// INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF NON-INFRINGEMENT,
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR ARISING FROM A COURSE 
// OF DEALING, USAGE, OR TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS
// LICENSORS BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
// DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK AND/OR ITS
// LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY OR PROBABILITY OF SUCH DAMAGES.

//merge visible/selected layers

// reorderLayers.mel
// this script reorders all layers in the layerEditor
// alphabetically or chronologically
//
// int $alphaOrder == 1 sort alphabetically
// int $alphaOrder == 0 sort chronologically
//
global proc reorderLayers( int $alphaOrder ){
	
	// set up the variables
	//
	string $type = "Render";
	if (`radioButtonGrp -query -select LayerEditorTypeRadio` == 1)
		$type = "Display";
	$gridLayout = ("LayerEditor" + $type + "LayerLayout");
	string $orderArray[] = `gridLayout -query -gridOrder $gridLayout`;

	// order alphabetically
	//
	if ($alphaOrder){
		// reorder the layers alphabetically
		$orderArray = `sort $orderArray`;

		// loop through the layers
		int $index = 1;
		for ($layer in $orderArray){
			// there may be null layers if a layer was deleted as the
			// other layers' connections to the layerManager are not updated
			if ($layer != "(null)"){
				// change the position of the layer in the layerEditor
				gridLayout -edit -position $layer $index $gridLayout;
				$index++;
			}
		}
	// order chronologically
	} else {
		// Render layer editor has default (master layer) displayed in the list,
		// so indeces start at 0. We have to compensate for Display layer editor,
		// because its indeces start at 1. 
		int $count = size($orderArray) + ("Display" == $type);
		
		for ($layer in $orderArray){
			// there may be null layers if a layer was deleted as the
			// other layers' connections to the layerManager are not updated
			if ($layer != "(null)"){
				// the displayOrder attr states the order layers were created in
				int $index = $count - `getAttr ($layer + ".displayOrder")`;
				// change the position of the layer in the layerEditor
				gridLayout -edit -position $layer $index $gridLayout;
			}
		}
	}
}
