//version 1.3
app.addSubMenu({
	cName: "ADBE_JFG_PortfolioUtilities",
	cUser: "Joel's Portfolio Utilities",
	cParent: "Document"
	});

app.addMenuItem({
	cName: "Help",
	cUser: "Combine All Open PDF Portfolios",
	cParent: "ADBE_JFG_PortfolioUtilities",
	cExec:"ADBE_JFG_CombineCollections()"
	});

app.addMenuItem({
	cName: "Help",
	cUser: "Copy PDF Document Properties to Portfolio Fields",
	cParent: "ADBE_JFG_PortfolioUtilities",
	cExec:"ADBE_JFG_copyDocInfoToPackage()"
	});

app.addMenuItem({
	cName: "Help",
	cUser: "Export Portfolio Metadata to Console",
	cParent: "ADBE_JFG_PortfolioUtilities",
	cExec:"ExportPortfolioMetaData()",
	cEnable: "event.rc = (this.collection);"
	});
		
app.addSubMenu({
	cName: "ADBE_JFG_PortfolioInitialView",
	cUser: "Set Portfolio Initial View",
	cParent: "ADBE_JFG_PortfolioUtilities"
	});
	
app.addMenuItem({
	cName: "Help",
	cUser: "Tile",
	cParent: "ADBE_JFG_PortfolioInitialView",
	cExec:"this.collection.initialView = 'Tile';",
	cEnable: "event.rc = (this.collection);"
	});

app.addMenuItem({
	cName: "Help",
	cUser: "Details",
	cParent: "ADBE_JFG_PortfolioInitialView",
	cExec:"this.collection.initialView = 'Details';",
	cEnable: "event.rc = (this.collection);"
	});
	
app.addMenuItem({
	cName: "Help",
	cUser: "Hidden",
	cParent: "ADBE_JFG_PortfolioInitialView",
	cExec:"this.collection.initialView = 'Hidden';",
	cEnable: "event.rc = (this.collection);"
	});

function ADBE_JFG_copyDocInfoToPackage()
{
	if (this.collection)
		{
		var attachments = this.dataObjects;
		t = app.thermometer;
		t.duration = attachments.length;
		t.begin();
		for (var a = 0; a < attachments.length; a++)
			{
			t.value = a
			t.text = "Processing attachment named "+attachments[a].name
			dataObject = this.getDataObject(attachments[a].name);
			fileType=dataObject.name.substring(dataObject.name.lastIndexOf(".")).toLowerCase();
			console.println(dataObject.name+" = "+fileType);
			if (fileType == ".pdf")
				{
				dataObjectDoc = this.openDataObject(attachments[a].name);
				docInfo = eval(dataObjectDoc.info.toSource());
				for (i in docInfo)
					{
					try
						{
						this.collection.addField({name:i, text:i, type:"S"});
						}
					catch (err)
						{
						console.println("Field named "+i+" could not be created, it may be already present.");	
						}
					this.dataObjects[a].setFieldValue(i, docInfo[i]);
					}
				}
			}
		t.end();
		}
}
	
function ExportPortfolioMetaData()
{

	if (this.collection && this.collection.fields.length > 0)
	{
		console.show();
		console.clear();	
		var attachments = this.dataObjects;
		t = app.thermometer;
		t.duration = attachments.length;
		t.begin();
		//Create out the heading row
		headingRow = '\"'+this.collection.fields[0].name+'\"'
		fields = [this.collection.fields[0].name]
		for (i=1; i<this.collection.fields.length; i++)
		{
			headingRow = headingRow + ',\"'+ this.collection.fields[i].name + '\"'
			fields.push(this.collection.fields[i].name)
		}
		//Write heading Row
		console.println(headingRow)
		//get metadata from each file
		for (var a = 0; a < attachments.length; a++)
			{
			t.value = a
			t.text = "Processing attachment named "+attachments[a].name
			//dataObject = this.getDataObject(attachments[a].name);
			//dataObjectDoc = this.openDataObject(attachments[a].name);
			fieldName = fields[0]
			dataRow = '\"'+this.dataObjects[a].getFieldValue(fieldName)+'\"'
			for (i=1; i<this.collection.fields.length; i++)
				{
					fieldName = fields[i]
					dataRow = dataRow + ',\"' + this.dataObjects[a].getFieldValue(fieldName) + '\"'
				}
			console.println(dataRow)	
			}
		t.end();
	}
	else
	{
		app.alert("There must be at least one user defined field to run this script.")
	}
}

trustedActiveDocs = app.trustedFunction ( 
		function()
		{
			app.beginPriv(); // Explicitly raise the privilege
			var d = app.activeDocs;
			app.endPriv();
			return d;
		}
)

trustedNewCollection = app.trustedFunction ( 
		function()
		{
			app.beginPriv(); // Explicitly raise the privilege
			var d = app.newCollection();
			app.endPriv();
			return d;
		}
)



function ADBE_JFG_CombineCollections()
{
	//get an array of all of the open documents
	var docs = trustedActiveDocs();
	if (docs.length > 0)
	{
	  //Create a new empty portfolio
	  newPortfolio = trustedNewCollection();
	  //create a thermometer
	  t = app.thermometer;
	  t.duration = docs.length;
	  t.begin();
	  i=0
	  //loop through each open document
	  for each (doc in docs)
	  {
		  t.value = i
		  //if the document is a Portfolio... (we won't add single PDFs)
		  if (doc.collection)
			  {
				//first add all the fields that we need
				for each (f in doc.collection.fields)
				{
					try
						{
						newPortfolio.collection.addField({name:f.name, text:f.text, type:f.type});
						console.println("Field named "+f.name+" added");
						}
					catch (err)
						{
						console.println("Field named "+f.name+" already present.");	
						}		
				}
				for each (item in doc.dataObjects)
				{
					t.text = "Inserting file "+item.name;
					newPortfolio.createDataObject(item.name, "");
					newPortfolio.setDataObjectContents(item.name, doc.getDataObjectContents(item.name));
					for each (f in doc.collection.fields)
					{
						newPortfolio.getDataObject(item.name).setFieldValue(f.name, item.getFieldValue(f.name));
					}
					
				}
		}
	  }
	}
t.end();	
}