/**
 * Copyright (c) 2006-2008 John Ford, released under the MIT license
 * pdf_bookmark.js - http://www.aldenta.com/
 * http://www.aldenta.com/2006/09/15/plugin-bookmark-a-page-in-your-pdf/
 * Inspired by http://www.pdfhacks.com/bookmark_page/
 *
 * Similar to a physical book and bookmark, this plugin lets you bookmark 
 * the page you're on and return to that single page later.
 *
 * 1.2 - Nov 30, 2008 - Use documentFileName on PDFs that don't have a docID
 *                      Add French translation (TitCouille)
 *                      Add installation paths for Linux (TitCouille)
 * 1.1 - Dec 20, 2007 - Update to work with Adobe Reader 8 due to security changes (app.trustedFunction)
 * 1.0 - Sep 15, 2006 - Initial release
 *
 * Installation:
 * 
 * Adobe Reader 9 - copy this file to:
 * Mac:     ~/Library/Application Support/Adobe/Acrobat/9.0_x86/JavaScripts (or similar)
 * Linux:   /home/[User Name]/.adobe/Acrobat/9.0/JavaScripts/ 
 * Windows: C:\Documents and Settings\[User Name]\Application Data\Adobe\Acrobat\9.0\JavaScripts (or similar)
 * 
 * Adobe Reader 8 - copy this file to:
 * Mac:     ~/Library/Acrobat User Data/8.0_x86/JavaScripts (or similar)
 * Linux:   /home/[User Name]/.adobe/Acrobat/8.0/JavaScripts/
 * Windows: C:\Documents and Settings\[User Name]\Application Data\Adobe\Acrobat\8.0\JavaScripts (or similar)
 *
 * Adobe Reader 7 - copy this file to:
 * Mac:     ~/Library/Acrobat User Data/7.0/JavaScripts (or similar)
 * Linux:   /home/[User Name]/.adobe/Acrobat/7.0/JavaScripts/
 * Windows: C:\Documents and Settings\[User Name]\Application Data\Adobe\Acrobat\7.0\JavaScripts (or similar)
 *
 * Note: you may have to create the JavaScripts folder
 *
 * Open Adobe Reader and access the menu by going to Tools -> Bookmarks.
 * Sometimes it may take a minute to load the menu so just give it a second and then click on Tools again.
 *
 * Tested with Adobe Reader 7 and 8 on Mac and Windows
 */

var DELIM = '%#%#';
var DEBUG = false;

/**
 * Add a bookmark for this page. Only one bookmark will be kept per document
 */
function addBookmark() {
	var id = (this.docID && this.docID[0] && this.docID[0] != '') ? this.docID[0] : this.documentFileName;
	
	if (id != null) {
		data = getData();
		data[id] = this.pageNum;
		saveData(data);
	}
}

/**
 * Go to the bookmarked page for this document if one exists
 */
function gotoBookmark() {
	var data = getData();	
	var id = (this.docID && this.docID[0] && this.docID[0] != '') ? this.docID[0] : this.documentFileName;
	
	for (var n in data) {
		if (id == n) {
			this.pageNum = data[n];
			return;
		}
	}
}

/**
 * Loads the bookmarks from the glob.js
 */
function getData() {
	if ( getPDFBookmark() == null ) {
		return {};
	}
	
	var flat = getPDFBookmark().split( DELIM );
	var data = {};

	for (i=0; i<flat.length; i+=2) {
		if (flat[i] != '') {
			data[flat[i]] = flat[i+1];
		}
	}
	
	debug("getData", data);
	
	return data;
}

/**
 * Saves the bookmarks to the glob.js
 */
function saveData(data) {
	var ds = '';
	for (var n in data) {
		ds += n + DELIM + data[n] + DELIM; 
	}

	debug("saveData", ds);

	setPDFBookmark(ds);
	global.setPersistent( "pdf_bookmark", true );
}

/**
 * Extra security in Adobe Reader 8 forces access of global variables
 * by multiple documents to be setup as a 'trustedFunction'
 */
getPDFBookmark = app.trustedFunction ( function() { 
	app.beginPriv(); 
    return global.pdf_bookmark;
    app.endPriv(); 
});

/**
 * Extra security in Adobe Reader 8 forces access of global variables
 * by multiple documents to be setup as a 'trustedFunction'
 */
setPDFBookmark = app.trustedFunction ( function(value) { 
	app.beginPriv(); 
    global.pdf_bookmark = value;
    app.endPriv(); 
});

/**
 * Prints the contents of data in an alert dialog box to help debug
 */
function debug(title, data) {
	if (DEBUG) {
		if (typeof data == "object") {
			var message = '';
			for (var n in data) {
				message += n + "=" + data[n] + "\n";
			}
			app.alert({ cTitle: title, cMsg: message });
		} else {
			app.alert({ cTitle: title, cMsg: data });
		}
	}
}

// Create translations for bookmark menu
var translations = {
	'ENU': {
		bookmark: 'Bookmark',
		bookmarkThisPage: 'Bookmark This Page',
		gotoBookmark: 'Go To Bookmark'
	},
	'FRA': {
		bookmark: 'Signet',
		bookmarkThisPage: 'Marquer la page',
		gotoBookmark: 'Retrouver la page'
	}
}

var translation = (translations[app.language]) ? translations[app.language] : translations['ENU'];

// Add bookmark menu
app.addSubMenu({
	cName: translation.bookmark,
	cParent: "Tools",
	cExec: "void(0);",
	cEnable: "event.rc = (event.target != null);" 
});

app.addMenuItem({
	cName: translation.bookmarkThisPage,
	cParent: translation.bookmark,
	cExec: "addBookmark();",
	cEnable: "event.rc = (event.target != null);"
});

app.addMenuItem({
	cName: translation.gotoBookmark,
	cParent: translation.bookmark,
	cExec: "gotoBookmark();",
	cEnable: "event.rc = (event.target != null);"
});
