/***********************************************************************/
/*                                                                     */
/*      ReorderPages :: Reorder the pages of an InDesign document      */
/*                                                                     */
/*      [Ver: 1.01]    [Author: Marc Autret]    [Modif: 12/01/09]      */
/*      [Lang: EN]     [Req: InDesign CS4]      [Creat: 11/07/09]      */
/*                                                                     */
/*      Installation:                                                  */
/*                                                                     */
/*      1) Place the current file into Scripts/Scripts Panel/          */
/*                                                                     */
/*      2) Run InDesign, open a document                               */
/*                                                                     */
/*      3) Exec script from Window > Automatisation > Scripts          */
/*         (double-click on the script name)                           */
/*                                                                     */
/*      Bugs & Feedback : marc{at}indiscripts{dot}com                  */
/*                        www.indiscripts.com                          */
/*                                                                     */
/***********************************************************************/

function reorderPages(/*str*/newOrder)
//--------------------------------------
{
var pages = app.activeDocument.pages;
 
var pgMap = (function()
	{
	var items = newOrder.replace(/[^\d,-]/g,'').split(',');
	var r = [], bkp={}, i, i0, i1, sz, p, inc;
     
	while(i=items.shift())
		{
		i = i.split('-');
 
		// don't allow "x-y-z"
		if ( (sz=i.length) > 2 ) return "Invalid range: '"+i.join('-')+"'";
 
		i0 = Number(i[0]);
		i1 = Number(i[sz-1]);
		inc = ( i0 > i1 ) ? -1 : 1;
 
		for( p=i0 ; p*inc <= i1*inc ; p+=inc )
			{
			if (!(pages.item(''+p).isValid))
				return "The page "+p+" doesn't exist!";
			if ((''+p) in bkp)
				return "The page "+p+" is specified twice!";
			bkp[''+p]=1;
			r.push(pages.item(''+p).id);
			}
		}
	return r;
	})();

if ( typeof pgMap == 'string') return pgMap;
 
var sz = pgMap.length;
if (sz != pages.length)
	return "Page number mismatch -- "+pgMap.length+" given, "+pages.length+" in document";
 
// at this point, {pgMap} contains a valid permutation
 
app.scriptPreferences.enableRedraw = false;
for(var i=0 ; i < sz ; i++ )
	{
	pages.itemByID(pgMap[i]).move(LocationOptions.AT_END);
	}
app.scriptPreferences.enableRedraw = true;

return true;
}

function main()
//--------------------------------------
{
if (!app.documents.length)
	{alert("Please open a document");return;}
	
if (app.activeDocument.pages.length < 2)
	{alert("The document has a single page!");return;}

var order = (function()
	{
	return this.lastItem().name + '-' + this.firstItem().name;
	}).call(app.activeDocument.pages);

var ret;
while(true)
	{
	order = prompt("Enter the new order of the pages using page numbers and/or page ranges separated by commas (e.g. 4, 7-5, 8, 1-3):",
		order, " Reorder Pages  |  \u00A9Indiscripts.com");
	if (order === null) return;
	ret = reorderPages(order);
	if( ret === true ) return;
	alert(ret);
	}
}

main();